Search in sources :

Example 1 with MetaHandler

use of com.viaversion.viaversion.rewriter.meta.MetaHandler in project ViaBackwards by ViaVersion.

the class EntityPackets1_14 method registerRewrites.

@Override
protected void registerRewrites() {
    mapTypes(Entity1_14Types.values(), Entity1_13Types.EntityType.class);
    mapEntityTypeWithData(Entity1_14Types.CAT, Entity1_14Types.OCELOT).jsonName();
    mapEntityTypeWithData(Entity1_14Types.TRADER_LLAMA, Entity1_14Types.LLAMA).jsonName();
    mapEntityTypeWithData(Entity1_14Types.FOX, Entity1_14Types.WOLF).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PANDA, Entity1_14Types.POLAR_BEAR).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PILLAGER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.WANDERING_TRADER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.RAVAGER, Entity1_14Types.COW).jsonName();
    filter().handler((event, meta) -> {
        int typeId = meta.metaType().typeId();
        if (typeId <= 15) {
            meta.setMetaType(Types1_13_2.META_TYPES.byId(typeId));
        }
        MetaType type = meta.metaType();
        if (type == Types1_13_2.META_TYPES.itemType) {
            Item item = (Item) meta.getValue();
            meta.setValue(protocol.getItemRewriter().handleItemToClient(item));
        } else if (type == Types1_13_2.META_TYPES.blockStateType) {
            int blockstate = meta.value();
            meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate));
        }
    });
    filter().type(Entity1_14Types.PILLAGER).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(16);
    filter().type(Entity1_14Types.FOX).cancel(17);
    filter().type(Entity1_14Types.FOX).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(15);
    filter().type(Entity1_14Types.PANDA).cancel(16);
    filter().type(Entity1_14Types.PANDA).cancel(17);
    filter().type(Entity1_14Types.PANDA).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(19);
    filter().type(Entity1_14Types.PANDA).cancel(20);
    filter().type(Entity1_14Types.CAT).cancel(18);
    filter().type(Entity1_14Types.CAT).cancel(19);
    filter().type(Entity1_14Types.CAT).cancel(20);
    filter().handler((event, meta) -> {
        EntityType type = event.entityType();
        if (type == null)
            return;
        if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE) || type == Entity1_14Types.RAVAGER || type == Entity1_14Types.WITCH) {
            int index = event.index();
            if (index == 14) {
                event.cancel();
            } else if (index > 14) {
                event.setIndex(index - 1);
            }
        }
    });
    filter().type(Entity1_14Types.AREA_EFFECT_CLOUD).index(10).handler((event, meta) -> {
        rewriteParticle((Particle) meta.getValue());
    });
    filter().type(Entity1_14Types.FIREWORK_ROCKET).index(8).handler((event, meta) -> {
        meta.setMetaType(Types1_13_2.META_TYPES.varIntType);
        Integer value = (Integer) meta.getValue();
        if (value == null) {
            meta.setValue(0);
        }
    });
    filter().filterFamily(Entity1_14Types.ABSTRACT_ARROW).removeIndex(9);
    // Head shake timer
    filter().type(Entity1_14Types.VILLAGER).cancel(15);
    MetaHandler villagerDataHandler = (event, meta) -> {
        VillagerData villagerData = (VillagerData) meta.getValue();
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, villagerDataToProfession(villagerData));
        if (meta.id() == 16) {
            // decreased by 2 again in one of the following handlers
            event.setIndex(15);
        }
    };
    filter().type(Entity1_14Types.ZOMBIE_VILLAGER).index(18).handler(villagerDataHandler);
    filter().type(Entity1_14Types.VILLAGER).index(16).handler(villagerDataHandler);
    // Holding arms up - from bitfield into own boolean
    filter().filterFamily(Entity1_14Types.ABSTRACT_SKELETON).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(14, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(16, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).addIndex(16);
    // Remove bed location
    filter().filterFamily(Entity1_14Types.LIVINGENTITY).handler((event, meta) -> {
        int index = event.index();
        if (index == 12) {
            Position position = (Position) meta.getValue();
            if (position != null) {
                // Use bed
                PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_13.USE_BED, null, event.user());
                wrapper.write(Type.VAR_INT, event.entityId());
                wrapper.write(Type.POSITION, position);
                try {
                    wrapper.scheduleSend(Protocol1_13_2To1_14.class);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            event.cancel();
        } else if (index > 12) {
            event.setIndex(index - 1);
        }
    });
    // Pose
    filter().removeIndex(6);
    filter().type(Entity1_14Types.OCELOT).index(13).handler((event, meta) -> {
        event.setIndex(15);
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, 0);
    });
    filter().type(Entity1_14Types.CAT).handler((event, meta) -> {
        if (event.index() == 15) {
            meta.setValue(1);
        } else if (event.index() == 13) {
            meta.setValue((byte) ((byte) meta.getValue() & 0x4));
        }
    });
    filter().handler((event, meta) -> {
        if (meta.metaType().typeId() > 15) {
            throw new IllegalArgumentException("Unhandled metadata: " + meta);
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) Item(com.viaversion.viaversion.api.minecraft.item.Item) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) Particle(com.viaversion.viaversion.api.type.types.Particle) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) Position(com.viaversion.viaversion.api.minecraft.Position) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) DifficultyStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) Position(com.viaversion.viaversion.api.minecraft.Position) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Aggregations

ViaBackwards (com.viaversion.viabackwards.ViaBackwards)1 EntityData (com.viaversion.viabackwards.api.entities.storage.EntityData)1 EntityPositionHandler (com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler)1 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)1 Protocol1_13_2To1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)1 ChunkLightStorage (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage)1 DifficultyStorage (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage)1 EntityPositionStorage1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1 Position (com.viaversion.viaversion.api.minecraft.Position)1 VillagerData (com.viaversion.viaversion.api.minecraft.VillagerData)1 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)1 Entity1_14Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types)1 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)1 Item (com.viaversion.viaversion.api.minecraft.item.Item)1 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)1 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)1 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)1 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1