Search in sources :

Example 1 with ChestedHorseStorage

use of com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_11 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_9_3.SET_SLOT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Slot ID
            map(Type.SHORT);
            // 2 - Slot Value
            map(Type.ITEM);
            handler(itemToClientHandler(Type.ITEM));
            // Handle Llama
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (isLlama(wrapper.user())) {
                        Optional<ChestedHorseStorage> horse = getChestedHorse(wrapper.user());
                        if (!horse.isPresent())
                            return;
                        ChestedHorseStorage storage = horse.get();
                        int currentSlot = wrapper.get(Type.SHORT, 0);
                        wrapper.set(Type.SHORT, 0, ((Integer) (currentSlot = getNewSlotId(storage, currentSlot))).shortValue());
                        wrapper.set(Type.ITEM, 0, getNewItem(storage, currentSlot, wrapper.get(Type.ITEM, 0)));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.WINDOW_ITEMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Window Values
            map(Type.ITEM_ARRAY);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item[] stacks = wrapper.get(Type.ITEM_ARRAY, 0);
                    for (int i = 0; i < stacks.length; i++) stacks[i] = handleItemToClient(stacks[i]);
                    if (isLlama(wrapper.user())) {
                        Optional<ChestedHorseStorage> horse = getChestedHorse(wrapper.user());
                        if (!horse.isPresent())
                            return;
                        ChestedHorseStorage storage = horse.get();
                        stacks = Arrays.copyOf(stacks, !storage.isChested() ? 38 : 53);
                        for (int i = stacks.length - 1; i >= 0; i--) {
                            stacks[getNewSlotId(storage, i)] = stacks[i];
                            stacks[i] = getNewItem(storage, i, stacks[i]);
                        }
                        wrapper.set(Type.ITEM_ARRAY, 0, stacks);
                    }
                }
            });
        }
    });
    registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
    // Plugin message Packet -> Trading
    protocol.registerClientbound(ClientboundPackets1_9_3.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (wrapper.get(Type.STRING, 0).equalsIgnoreCase("MC|TrList")) {
                        // Passthrough Window ID
                        wrapper.passthrough(Type.INT);
                        int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                        for (int i = 0; i < size; i++) {
                            // Input Item
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            // Output Item
                            wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            // Has second item
                            boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                            if (secondItem) {
                                // Second Item
                                wrapper.write(Type.ITEM, handleItemToClient(wrapper.read(Type.ITEM)));
                            }
                            // Trade disabled
                            wrapper.passthrough(Type.BOOLEAN);
                            // Number of tools uses
                            wrapper.passthrough(Type.INT);
                            // Maximum number of trade uses
                            wrapper.passthrough(Type.INT);
                        }
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9_3.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Slot
            map(Type.SHORT);
            // 2 - Button
            map(Type.BYTE);
            // 3 - Action number
            map(Type.SHORT);
            // 4 - Mode
            map(Type.VAR_INT);
            // 5 - Clicked Item
            map(Type.ITEM);
            handler(itemToServerHandler(Type.ITEM));
            // Llama slot
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (isLlama(wrapper.user())) {
                        Optional<ChestedHorseStorage> horse = getChestedHorse(wrapper.user());
                        if (!horse.isPresent())
                            return;
                        ChestedHorseStorage storage = horse.get();
                        int clickSlot = wrapper.get(Type.SHORT, 0);
                        int correctSlot = getOldSlotId(storage, clickSlot);
                        wrapper.set(Type.SHORT, 0, ((Integer) correctSlot).shortValue());
                    }
                }
            });
        }
    });
    registerCreativeInvAction(ServerboundPackets1_9_3.CREATIVE_INVENTORY_ACTION, Type.ITEM);
    protocol.registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    // Use the 1.10 Chunk type since nothing changed.
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk chunk = wrapper.passthrough(type);
                    handleChunk(chunk);
                    // only patch it for signs for now
                    for (CompoundTag tag : chunk.getBlockEntities()) {
                        Tag idTag = tag.get("id");
                        if (!(idTag instanceof StringTag))
                            continue;
                        String id = (String) idTag.getValue();
                        if (id.equals("minecraft:sign")) {
                            ((StringTag) idTag).setValue("Sign");
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Block Position
            map(Type.POSITION);
            // 1 - Block
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int idx = wrapper.get(Type.VAR_INT, 0);
                    wrapper.set(Type.VAR_INT, 0, handleBlockID(idx));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Chunk X
            map(Type.INT);
            // 1 - Chunk Z
            map(Type.INT);
            map(Type.BLOCK_CHANGE_RECORD_ARRAY);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (BlockChangeRecord record : wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0)) {
                        record.setBlockId(handleBlockID(record.getBlockId()));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action
            map(Type.UNSIGNED_BYTE);
            // 2 - NBT
            map(Type.NBT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Remove on shulkerbox decleration
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 10) {
                        wrapper.cancel();
                    }
                    // Handler Spawners
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1) {
                        CompoundTag tag = wrapper.get(Type.NBT, 0);
                        EntityIdRewriter.toClientSpawner(tag, true);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.OPEN_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Window ID
            map(Type.UNSIGNED_BYTE);
            // 1 - Window Type
            map(Type.STRING);
            // 2 - Title
            map(Type.COMPONENT);
            // 3 - Slots
            map(Type.UNSIGNED_BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = -1;
                    // Passthrough Entity ID
                    if (wrapper.get(Type.STRING, 0).equals("EntityHorse")) {
                        entityId = wrapper.passthrough(Type.INT);
                    }
                    // Track Inventory
                    String inventory = wrapper.get(Type.STRING, 0);
                    WindowTracker windowTracker = wrapper.user().get(WindowTracker.class);
                    windowTracker.setInventory(inventory);
                    windowTracker.setEntityId(entityId);
                    // Change llama slotcount to the donkey one
                    if (isLlama(wrapper.user())) {
                        wrapper.set(Type.UNSIGNED_BYTE, 1, (short) 17);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.CLOSE_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Inventory tracking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    WindowTracker windowTracker = wrapper.user().get(WindowTracker.class);
                    windowTracker.setInventory(null);
                    windowTracker.setEntityId(-1);
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9_3.CLOSE_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Inventory tracking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    WindowTracker windowTracker = wrapper.user().get(WindowTracker.class);
                    windowTracker.setInventory(null);
                    windowTracker.setEntityId(-1);
                }
            });
        }
    });
    protocol.getEntityRewriter().filter().handler((event, meta) -> {
        if (// Is Item
        meta.metaType().type().equals(Type.ITEM))
            meta.setValue(handleItemToClient((Item) meta.getValue()));
    });
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) WindowTracker(com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.WindowTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChestedHorseStorage(com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) Item(com.viaversion.viaversion.api.minecraft.item.Item) MappedLegacyBlockItem(com.viaversion.viabackwards.api.data.MappedLegacyBlockItem) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 2 with ChestedHorseStorage

use of com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage in project ViaBackwards by ViaVersion.

the class EntityPackets1_11 method registerRewrites.

@Override
protected void registerRewrites() {
    // Guardian
    mapEntityTypeWithData(Entity1_11Types.EntityType.ELDER_GUARDIAN, Entity1_11Types.EntityType.GUARDIAN);
    // Skeletons
    mapEntityTypeWithData(Entity1_11Types.EntityType.WITHER_SKELETON, Entity1_11Types.EntityType.SKELETON).spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(1)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.STRAY, Entity1_11Types.EntityType.SKELETON).plainName().spawnMetadata(storage -> storage.add(getSkeletonTypeMeta(2)));
    // Zombies
    mapEntityTypeWithData(Entity1_11Types.EntityType.HUSK, Entity1_11Types.EntityType.ZOMBIE).plainName().spawnMetadata(storage -> handleZombieType(storage, 6));
    mapEntityTypeWithData(Entity1_11Types.EntityType.ZOMBIE_VILLAGER, Entity1_11Types.EntityType.ZOMBIE).spawnMetadata(storage -> handleZombieType(storage, 1));
    // Horses
    // Nob able to ride the horse without having the MetaType sent.
    mapEntityTypeWithData(Entity1_11Types.EntityType.HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(0)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.DONKEY, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(1)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.MULE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(2)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.SKELETON_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(4)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.ZOMBIE_HORSE, Entity1_11Types.EntityType.HORSE).spawnMetadata(storage -> storage.add(getHorseMetaType(3)));
    // New mobs
    mapEntityTypeWithData(Entity1_11Types.EntityType.EVOCATION_FANGS, Entity1_11Types.EntityType.SHULKER);
    mapEntityTypeWithData(Entity1_11Types.EntityType.EVOCATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).plainName();
    mapEntityTypeWithData(Entity1_11Types.EntityType.VEX, Entity1_11Types.EntityType.BAT).plainName();
    // Base Profession
    mapEntityTypeWithData(Entity1_11Types.EntityType.VINDICATION_ILLAGER, Entity1_11Types.EntityType.VILLAGER).plainName().spawnMetadata(storage -> storage.add(new Metadata(13, MetaType1_9.VarInt, 4)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.LIAMA, Entity1_11Types.EntityType.HORSE).plainName().spawnMetadata(storage -> storage.add(getHorseMetaType(1)));
    mapEntityTypeWithData(Entity1_11Types.EntityType.LIAMA_SPIT, Entity1_11Types.EntityType.SNOWBALL);
    mapObjectType(Entity1_11Types.ObjectType.LIAMA_SPIT, Entity1_11Types.ObjectType.SNOWBALL, -1);
    // Replace with endertorchthingies
    mapObjectType(Entity1_11Types.ObjectType.EVOCATION_FANGS, Entity1_11Types.ObjectType.FALLING_BLOCK, 198 | 1 << 12);
    // Handle ElderGuardian & target metadata
    filter().filterFamily(Entity1_11Types.EntityType.GUARDIAN).index(12).handler((event, meta) -> {
        boolean b = (boolean) meta.getValue();
        int bitmask = b ? 0x02 : 0;
        if (event.entityType() == Entity1_11Types.EntityType.ELDER_GUARDIAN) {
            bitmask |= 0x04;
        }
        meta.setTypeAndValue(MetaType1_9.Byte, (byte) bitmask);
    });
    // Handle skeleton swing
    filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_SKELETON).index(12).toIndex(13);
    /*
            ZOMBIE CHANGES
         */
    filter().filterFamily(Entity1_11Types.EntityType.ZOMBIE).handler((event, meta) -> {
        switch(meta.id()) {
            case 13:
                event.cancel();
                return;
            case 14:
                event.setIndex(15);
                break;
            case 15:
                event.setIndex(14);
                break;
            // Profession
            case 16:
                event.setIndex(13);
                meta.setValue(1 + (int) meta.getValue());
                break;
        }
    });
    // Handle Evocation Illager
    filter().type(Entity1_11Types.EntityType.EVOCATION_ILLAGER).index(12).handler((event, meta) -> {
        event.setIndex(13);
        // Change the profession for the states
        meta.setTypeAndValue(MetaType1_9.VarInt, ((Byte) meta.getValue()).intValue());
    });
    // Handle Vex (Remove this field completely since the position is not updated correctly when idling for bats. Sad ):
    filter().type(Entity1_11Types.EntityType.VEX).index(12).handler((event, meta) -> {
        meta.setValue((byte) 0x00);
    });
    // Handle VindicationIllager
    filter().type(Entity1_11Types.EntityType.VINDICATION_ILLAGER).index(12).handler((event, meta) -> {
        event.setIndex(13);
        meta.setTypeAndValue(MetaType1_9.VarInt, ((Number) meta.getValue()).intValue() == 1 ? 2 : 4);
    });
    /*
            HORSES
         */
    // Handle horse flags
    filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_HORSE).index(13).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        byte b = (byte) meta.getValue();
        if (data.has(ChestedHorseStorage.class) && data.get(ChestedHorseStorage.class).isChested()) {
            // Chested
            b |= 0x08;
            meta.setValue(b);
        }
    });
    // Create chested horse storage
    filter().filterFamily(Entity1_11Types.EntityType.CHESTED_HORSE).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        if (!data.has(ChestedHorseStorage.class)) {
            data.put(new ChestedHorseStorage());
        }
    });
    // Handle horse armor
    filter().type(Entity1_11Types.EntityType.HORSE).index(16).toIndex(17);
    // Handle chested horse
    filter().filterFamily(Entity1_11Types.EntityType.CHESTED_HORSE).index(15).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        ChestedHorseStorage storage = data.get(ChestedHorseStorage.class);
        boolean b = (boolean) meta.getValue();
        storage.setChested(b);
        event.cancel();
    });
    // Get rid of Liama metadata
    filter().type(Entity1_11Types.EntityType.LIAMA).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        ChestedHorseStorage storage = data.get(ChestedHorseStorage.class);
        int index = event.index();
        // Store them for later (:
        switch(index) {
            case 16:
                storage.setLiamaStrength((int) meta.getValue());
                event.cancel();
                break;
            case 17:
                storage.setLiamaCarpetColor((int) meta.getValue());
                event.cancel();
                break;
            case 18:
                storage.setLiamaVariant((int) meta.getValue());
                event.cancel();
                break;
        }
    });
    // Handle Horse (Correct owner)
    filter().filterFamily(Entity1_11Types.EntityType.ABSTRACT_HORSE).index(14).toIndex(16);
    // Handle villager - Change non-existing profession
    filter().type(Entity1_11Types.EntityType.VILLAGER).index(13).handler((event, meta) -> {
        if ((int) meta.getValue() == 5) {
            meta.setValue(0);
        }
    });
    // handle new Shulker color meta
    filter().type(Entity1_11Types.EntityType.SHULKER).cancel(15);
}
Also used : WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) ChestedHorseStorage(com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage)

Example 3 with ChestedHorseStorage

use of com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_11 method getChestedHorse.

private Optional<ChestedHorseStorage> getChestedHorse(UserConnection user) {
    WindowTracker tracker = user.get(WindowTracker.class);
    if (tracker.getInventory() != null && tracker.getInventory().equals("EntityHorse")) {
        EntityTracker entTracker = user.getEntityTracker(Protocol1_10To1_11.class);
        StoredEntityData entityData = entTracker.entityData(tracker.getEntityId());
        if (entityData != null)
            return Optional.of(entityData.get(ChestedHorseStorage.class));
    }
    return Optional.empty();
}
Also used : WindowTracker(com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.WindowTracker) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData)

Aggregations

ChestedHorseStorage (com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage)2 WindowTracker (com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.WindowTracker)2 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)2 MappedLegacyBlockItem (com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)1 WrappedMetadata (com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)1 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)1 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)1 Item (com.viaversion.viaversion.api.minecraft.item.Item)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 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)1 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)1 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)1 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)1 Chunk1_9_3_4Type (com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type)1 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)1