Search in sources :

Example 1 with ClientWorld

use of com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld in project ViaBackwards by ViaVersion.

the class EntityPackets1_16 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_16.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.VAR_INT);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - Data
            map(Type.INT);
            handler(wrapper -> {
                EntityType entityType = typeFromId(wrapper.get(Type.VAR_INT, 1));
                if (entityType == Entity1_16Types.LIGHTNING_BOLT) {
                    // Map to old weather entity packet
                    wrapper.cancel();
                    PacketWrapper spawnLightningPacket = wrapper.create(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY);
                    // Entity id
                    spawnLightningPacket.write(Type.VAR_INT, wrapper.get(Type.VAR_INT, 0));
                    // Lightning type
                    spawnLightningPacket.write(Type.BYTE, (byte) 1);
                    // X
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 0));
                    // Y
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 1));
                    // Z
                    spawnLightningPacket.write(Type.DOUBLE, wrapper.get(Type.DOUBLE, 2));
                    spawnLightningPacket.send(Protocol1_15_2To1_16.class);
                }
            });
            handler(getSpawnTrackerWithDataHandler(Entity1_16Types.FALLING_BLOCK));
        }
    });
    registerSpawnTracker(ClientboundPackets1_16.SPAWN_MOB);
    protocol.registerClientbound(ClientboundPackets1_16.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                // Grab the tracker for world names
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // World Name
                String nextWorldName = wrapper.read(Type.STRING);
                // Seed
                wrapper.passthrough(Type.LONG);
                // Gamemode
                wrapper.passthrough(Type.UNSIGNED_BYTE);
                // Previous gamemode
                wrapper.read(Type.BYTE);
                // Grab client world
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimension = wrapper.get(Type.INT, 0);
                // Send a dummy respawn with a different dimension if the world name was different and the same dimension was used
                if (clientWorld.getEnvironment() != null && dimension == clientWorld.getEnvironment().getId() && (wrapper.user().isClientSide() || Via.getPlatform().isProxy() || // Hotfix for https://github.com/ViaVersion/ViaBackwards/issues/381
                wrapper.user().getProtocolInfo().getProtocolVersion() <= ProtocolVersion.v1_12_2.getVersion() || !nextWorldName.equals(worldNameTracker.getWorldName()))) {
                    PacketWrapper packet = wrapper.create(ClientboundPackets1_15.RESPAWN);
                    packet.write(Type.INT, dimension == 0 ? -1 : 0);
                    packet.write(Type.LONG, 0L);
                    packet.write(Type.UNSIGNED_BYTE, (short) 0);
                    packet.write(Type.STRING, "default");
                    packet.send(Protocol1_15_2To1_16.class);
                }
                clientWorld.setEnvironment(dimension);
                // Level type
                wrapper.write(Type.STRING, "default");
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
                // Keep all playerdata
                wrapper.read(Type.BOOLEAN);
                // Finally update the world name
                worldNameTracker.setWorldName(nextWorldName);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous gamemode
            map(Type.BYTE, Type.NOTHING);
            // World list
            map(Type.STRING_ARRAY, Type.NOTHING);
            // whatever this is
            map(Type.NBT, Type.NOTHING);
            // Dimension Type
            map(dimensionTransformer);
            handler(wrapper -> {
                WorldNameTracker worldNameTracker = wrapper.user().get(WorldNameTracker.class);
                // Save the world name
                worldNameTracker.setWorldName(wrapper.read(Type.STRING));
            });
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                clientChunks.setEnvironment(wrapper.get(Type.INT, 1));
                tracker(wrapper.user()).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER);
                // Level type
                wrapper.write(Type.STRING, "default");
                // View distance
                wrapper.passthrough(Type.VAR_INT);
                // Reduced debug info
                wrapper.passthrough(Type.BOOLEAN);
                // Show death screen
                wrapper.passthrough(Type.BOOLEAN);
                // Debug
                wrapper.read(Type.BOOLEAN);
                if (wrapper.read(Type.BOOLEAN)) {
                    wrapper.set(Type.STRING, 0, "flat");
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_16.SPAWN_EXPERIENCE_ORB, Entity1_16Types.EXPERIENCE_ORB);
    // F Spawn Global Object, it is no longer with us :(
    registerTracker(ClientboundPackets1_16.SPAWN_PAINTING, Entity1_16Types.PAINTING);
    registerTracker(ClientboundPackets1_16.SPAWN_PLAYER, Entity1_16Types.PLAYER);
    registerRemoveEntities(ClientboundPackets1_16.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_16.ENTITY_METADATA, Types1_16.METADATA_LIST, Types1_14.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_16.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                int size = wrapper.passthrough(Type.INT);
                for (int i = 0; i < size; i++) {
                    String attributeIdentifier = wrapper.read(Type.STRING);
                    String oldKey = protocol.getMappingData().getAttributeMappings().get(attributeIdentifier);
                    wrapper.write(Type.STRING, oldKey != null ? oldKey : attributeIdentifier.replace("minecraft:", ""));
                    wrapper.passthrough(Type.DOUBLE);
                    int modifierSize = wrapper.passthrough(Type.VAR_INT);
                    for (int j = 0; j < modifierSize; j++) {
                        wrapper.passthrough(Type.UUID);
                        wrapper.passthrough(Type.DOUBLE);
                        wrapper.passthrough(Type.BYTE);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16.PLAYER_INFO, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(packetWrapper -> {
                int action = packetWrapper.passthrough(Type.VAR_INT);
                int playerCount = packetWrapper.passthrough(Type.VAR_INT);
                for (int i = 0; i < playerCount; i++) {
                    packetWrapper.passthrough(Type.UUID);
                    if (action == 0) {
                        // Add
                        packetWrapper.passthrough(Type.STRING);
                        int properties = packetWrapper.passthrough(Type.VAR_INT);
                        for (int j = 0; j < properties; j++) {
                            packetWrapper.passthrough(Type.STRING);
                            packetWrapper.passthrough(Type.STRING);
                            if (packetWrapper.passthrough(Type.BOOLEAN)) {
                                packetWrapper.passthrough(Type.STRING);
                            }
                        }
                        packetWrapper.passthrough(Type.VAR_INT);
                        packetWrapper.passthrough(Type.VAR_INT);
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display Name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    } else if (action == 1) {
                        // Update Game Mode
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 2) {
                        // Update Ping
                        packetWrapper.passthrough(Type.VAR_INT);
                    } else if (action == 3) {
                        // Update Display Name
                        if (packetWrapper.passthrough(Type.BOOLEAN)) {
                            // Display name
                            protocol.getTranslatableRewriter().processText(packetWrapper.passthrough(Type.COMPONENT));
                        }
                    }
                // 4 = Remove Player
                }
            });
        }
    });
}
Also used : Particle(com.viaversion.viaversion.api.type.types.Particle) Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) WolfDataMaskStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Via(com.viaversion.viaversion.api.Via) Item(com.viaversion.viaversion.api.minecraft.item.Item) ProtocolVersion(com.viaversion.viaversion.api.protocol.version.ProtocolVersion) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) Type(com.viaversion.viaversion.api.type.Type) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) WorldNameTracker(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.data.WorldNameTracker) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_15_2To1_16(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)

Example 2 with ClientWorld

use of com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_10 method registerPackets.

@Override
protected void registerPackets() {
    registerSetSlot(ClientboundPackets1_9_3.SET_SLOT, Type.ITEM);
    registerWindowItems(ClientboundPackets1_9_3.WINDOW_ITEMS, Type.ITEM_ARRAY);
    // Entity Equipment Packet
    registerEntityEquipment(ClientboundPackets1_9_3.ENTITY_EQUIPMENT, Type.ITEM);
    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);
                        }
                    }
                }
            });
        }
    });
    registerClickWindow(ServerboundPackets1_9_3.CLICK_WINDOW, Type.ITEM);
    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);
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk chunk = wrapper.passthrough(type);
                    handleChunk(chunk);
                }
            });
        }
    });
    // Block Change Packet
    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));
                }
            });
        }
    });
    // Multi Block Change Packet
    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()));
                    }
                }
            });
        }
    });
    // Rewrite metadata items
    protocol.getEntityRewriter().filter().handler((event, meta) -> {
        if (// Is Item
        meta.metaType().type().equals(Type.ITEM))
            meta.setValue(handleItemToClient((Item) meta.getValue()));
    });
    // Particle
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            map(Type.BOOLEAN);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 46) {
                        // new falling_dust
                        // -> block_dust
                        wrapper.set(Type.INT, 0, 38);
                    }
                }
            });
        }
    });
}
Also used : BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type)

Example 3 with ClientWorld

use of com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld in project ViaBackwards by ViaVersion.

the class Protocol1_13_2To1_14 method init.

@Override
public void init(UserConnection user) {
    // Register ClientWorld
    if (!user.has(ClientWorld.class)) {
        user.put(new ClientWorld(user));
    }
    user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, Entity1_14Types.PLAYER, true));
    if (!user.has(ChunkLightStorage.class)) {
        user.put(new ChunkLightStorage(user));
    }
    user.put(new DifficultyStorage(user));
}
Also used : ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) DifficultyStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase)

Example 4 with ClientWorld

use of com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_14 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerServerbound(ServerboundPackets1_13.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.OPEN_WINDOW, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int windowId = wrapper.read(Type.VAR_INT);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) windowId);
                    int type = wrapper.read(Type.VAR_INT);
                    String stringType = null;
                    String containerTitle = null;
                    int slotSize = 0;
                    if (type < 6) {
                        if (type == 2)
                            containerTitle = "Barrel";
                        stringType = "minecraft:container";
                        slotSize = (type + 1) * 9;
                    } else {
                        switch(type) {
                            case 11:
                                stringType = "minecraft:crafting_table";
                                break;
                            // blast furnace
                            case 9:
                            // smoker
                            case 20:
                            // furnace
                            case 13:
                            case // grindstone
                            14:
                                if (type == 9)
                                    containerTitle = "Blast Furnace";
                                else if (type == 20)
                                    containerTitle = "Smoker";
                                else if (type == 14)
                                    containerTitle = "Grindstone";
                                stringType = "minecraft:furnace";
                                slotSize = 3;
                                break;
                            case 6:
                                stringType = "minecraft:dropper";
                                slotSize = 9;
                                break;
                            case 12:
                                stringType = "minecraft:enchanting_table";
                                break;
                            case 10:
                                stringType = "minecraft:brewing_stand";
                                slotSize = 5;
                                break;
                            case 18:
                                stringType = "minecraft:villager";
                                break;
                            case 8:
                                stringType = "minecraft:beacon";
                                slotSize = 1;
                                break;
                            // cartography_table
                            case 21:
                            case 7:
                                if (type == 21)
                                    containerTitle = "Cartography Table";
                                stringType = "minecraft:anvil";
                                break;
                            case 15:
                                stringType = "minecraft:hopper";
                                slotSize = 5;
                                break;
                            case 19:
                                stringType = "minecraft:shulker_box";
                                slotSize = 27;
                                break;
                        }
                    }
                    if (stringType == null) {
                        ViaBackwards.getPlatform().getLogger().warning("Can't open inventory for 1.13 player! Type: " + type);
                        wrapper.cancel();
                        return;
                    }
                    wrapper.write(Type.STRING, stringType);
                    JsonElement title = wrapper.read(Type.COMPONENT);
                    if (containerTitle != null) {
                        // Don't rewrite renamed, only translatable titles
                        JsonObject object;
                        if (title.isJsonObject() && (object = title.getAsJsonObject()).has("translate")) {
                            // Don't rewrite other 9x3 translatable containers
                            if (type != 2 || object.getAsJsonPrimitive("translate").getAsString().equals("container.barrel")) {
                                title = ChatRewriter.legacyTextToJson(containerTitle);
                            }
                        }
                    }
                    wrapper.write(Type.COMPONENT, title);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) slotSize);
                }
            });
        }
    });
    // Horse window -> Open Window
    protocol.registerClientbound(ClientboundPackets1_14.OPEN_HORSE_WINDOW, ClientboundPackets1_13.OPEN_WINDOW, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Window id
                    wrapper.passthrough(Type.UNSIGNED_BYTE);
                    // Type
                    wrapper.write(Type.STRING, "EntityHorse");
                    JsonObject object = new JsonObject();
                    object.addProperty("translate", "minecraft.horse");
                    // Title
                    wrapper.write(Type.COMPONENT, object);
                    // Number of slots
                    wrapper.write(Type.UNSIGNED_BYTE, wrapper.read(Type.VAR_INT).shortValue());
                    // Entity id
                    wrapper.passthrough(Type.INT);
                }
            });
        }
    });
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION);
    registerSetCooldown(ClientboundPackets1_14.COOLDOWN);
    registerWindowItems(ClientboundPackets1_14.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_14.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_14.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    // Trade List -> Plugin Message
    protocol.registerClientbound(ClientboundPackets1_14.TRADE_LIST, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.write(Type.STRING, "minecraft:trader_list");
                    int windowId = wrapper.read(Type.VAR_INT);
                    wrapper.write(Type.INT, windowId);
                    int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; i++) {
                        // Input Item
                        Item input = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                        input = handleItemToClient(input);
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, input);
                        // Output Item
                        Item output = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                        output = handleItemToClient(output);
                        wrapper.write(Type.FLAT_VAR_INT_ITEM, output);
                        // Has second item
                        boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
                        if (secondItem) {
                            // Second Item
                            Item second = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                            second = handleItemToClient(second);
                            wrapper.write(Type.FLAT_VAR_INT_ITEM, second);
                        }
                        // Trade disabled
                        wrapper.passthrough(Type.BOOLEAN);
                        // Number of tools uses
                        wrapper.passthrough(Type.INT);
                        // Maximum number of trade uses
                        wrapper.passthrough(Type.INT);
                        wrapper.read(Type.INT);
                        wrapper.read(Type.INT);
                        wrapper.read(Type.FLOAT);
                    }
                    wrapper.read(Type.VAR_INT);
                    wrapper.read(Type.VAR_INT);
                    wrapper.read(Type.BOOLEAN);
                }
            });
        }
    });
    // Open Book -> Plugin Message
    protocol.registerClientbound(ClientboundPackets1_14.OPEN_BOOK, ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.write(Type.STRING, "minecraft:book_open");
                    wrapper.passthrough(Type.VAR_INT);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_EQUIPMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Slot ID
            map(Type.VAR_INT);
            // 2 - Item
            map(Type.FLAT_VAR_INT_ITEM);
            handler(itemToClientHandler(Type.FLAT_VAR_INT_ITEM));
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityType entityType = wrapper.user().getEntityTracker(Protocol1_13_2To1_14.class).entityType(entityId);
                    if (entityType == null)
                        return;
                    if (entityType.isOrHasParent(Entity1_14Types.ABSTRACT_HORSE)) {
                        wrapper.setId(0x3F);
                        wrapper.resetReader();
                        wrapper.passthrough(Type.VAR_INT);
                        wrapper.read(Type.VAR_INT);
                        Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                        int armorType = item == null || item.identifier() == 0 ? 0 : item.identifier() - 726;
                        if (armorType < 0 || armorType > 3) {
                            ViaBackwards.getPlatform().getLogger().warning("Received invalid horse armor: " + item);
                            wrapper.cancel();
                            return;
                        }
                        List<Metadata> metadataList = new ArrayList<>();
                        metadataList.add(new Metadata(16, Types1_13_2.META_TYPES.varIntType, armorType));
                        wrapper.write(Types1_13.METADATA_LIST, metadataList);
                    }
                }
            });
        }
    });
    RecipeRewriter recipeHandler = new RecipeRewriter1_13_2(protocol);
    protocol.registerClientbound(ClientboundPackets1_14.DECLARE_RECIPES, new PacketRemapper() {

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

                private final Set<String> removedTypes = ImmutableSet.of("crafting_special_suspiciousstew", "blasting", "smoking", "campfire_cooking", "stonecutting");

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int size = wrapper.passthrough(Type.VAR_INT);
                    int deleted = 0;
                    for (int i = 0; i < size; i++) {
                        String type = wrapper.read(Type.STRING);
                        // Recipe Identifier
                        String id = wrapper.read(Type.STRING);
                        type = type.replace("minecraft:", "");
                        if (removedTypes.contains(type)) {
                            switch(type) {
                                case "blasting":
                                case "smoking":
                                case "campfire_cooking":
                                    // Group
                                    wrapper.read(Type.STRING);
                                    // Ingredients
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM);
                                    // EXP
                                    wrapper.read(Type.FLOAT);
                                    // Cooking time
                                    wrapper.read(Type.VAR_INT);
                                    break;
                                case "stonecutting":
                                    // Group?
                                    wrapper.read(Type.STRING);
                                    // Ingredients
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT);
                                    // Result
                                    wrapper.read(Type.FLAT_VAR_INT_ITEM);
                                    break;
                            }
                            deleted++;
                            continue;
                        }
                        wrapper.write(Type.STRING, id);
                        wrapper.write(Type.STRING, type);
                        // Handle the rest of the types
                        recipeHandler.handle(wrapper, type);
                    }
                    wrapper.set(Type.VAR_INT, 0, size - deleted);
                }
            });
        }
    });
    registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_BREAK_ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.POSITION1_14, Type.POSITION);
            map(Type.BYTE);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14, Type.POSITION);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Location
            map(Type.POSITION1_14, Type.POSITION);
            // Action id
            map(Type.UNSIGNED_BYTE);
            // Action param
            map(Type.UNSIGNED_BYTE);
            // Block id - /!\ NOT BLOCK STATE
            map(Type.VAR_INT);
            handler(wrapper -> {
                int mappedId = protocol.getMappingData().getNewBlockId(wrapper.get(Type.VAR_INT, 0));
                if (mappedId == -1) {
                    wrapper.cancel();
                    return;
                }
                wrapper.set(Type.VAR_INT, 0, mappedId);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14, Type.POSITION);
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.VAR_INT, 0);
                    wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(id));
                }
            });
        }
    });
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_14.MULTI_BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_14.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Radius
            map(Type.FLOAT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (int i = 0; i < 3; i++) {
                        float coord = wrapper.get(Type.FLOAT, i);
                        if (coord < 0f) {
                            coord = (float) Math.floor(coord);
                            wrapper.set(Type.FLOAT, i, coord);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.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);
                    Chunk chunk = wrapper.read(new Chunk1_14Type());
                    wrapper.write(new Chunk1_13Type(clientWorld), chunk);
                    ChunkLightStorage.ChunkLight chunkLight = wrapper.user().get(ChunkLightStorage.class).getStoredLight(chunk.getX(), chunk.getZ());
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection section = chunk.getSections()[i];
                        if (section == null)
                            continue;
                        ChunkSectionLight sectionLight = new ChunkSectionLightImpl();
                        section.setLight(sectionLight);
                        if (chunkLight == null) {
                            sectionLight.setBlockLight(ChunkLightStorage.FULL_LIGHT);
                            if (clientWorld.getEnvironment() == Environment.NORMAL) {
                                sectionLight.setSkyLight(ChunkLightStorage.FULL_LIGHT);
                            }
                        } else {
                            byte[] blockLight = chunkLight.getBlockLight()[i];
                            sectionLight.setBlockLight(blockLight != null ? blockLight : ChunkLightStorage.FULL_LIGHT);
                            if (clientWorld.getEnvironment() == Environment.NORMAL) {
                                byte[] skyLight = chunkLight.getSkyLight()[i];
                                sectionLight.setSkyLight(skyLight != null ? skyLight : ChunkLightStorage.FULL_LIGHT);
                            }
                        }
                        if (Via.getConfig().isNonFullBlockLightFix() && section.getNonAirBlocksCount() != 0 && sectionLight.hasBlockLight()) {
                            for (int x = 0; x < 16; x++) {
                                for (int y = 0; y < 16; y++) {
                                    for (int z = 0; z < 16; z++) {
                                        int id = section.getFlatBlock(x, y, z);
                                        if (Protocol1_14To1_13_2.MAPPINGS.getNonFullBlocks().contains(id)) {
                                            sectionLight.getBlockLightNibbleArray().set(x, y, z, 0);
                                        }
                                    }
                                }
                            }
                        }
                        for (int j = 0; j < section.getPaletteSize(); j++) {
                            int old = section.getPaletteEntry(j);
                            int newId = protocol.getMappingData().getNewBlockStateId(old);
                            section.setPaletteEntry(j, newId);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.UNLOAD_CHUNK, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int x = wrapper.passthrough(Type.INT);
                    int z = wrapper.passthrough(Type.INT);
                    wrapper.user().get(ChunkLightStorage.class).unloadChunk(x, z);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Effect Id
            map(Type.INT);
            // Location
            map(Type.POSITION1_14, Type.POSITION);
            // Data
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    int data = wrapper.get(Type.INT, 1);
                    if (id == 1010) {
                        // Play record
                        wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(data));
                    } else if (id == 2001) {
                        // Block break + block break sound
                        wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(data));
                    }
                }
            });
        }
    });
    registerSpawnParticle(ClientboundPackets1_14.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
    protocol.registerClientbound(ClientboundPackets1_14.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.BYTE);
            map(Type.BOOLEAN);
            // Locked
            map(Type.BOOLEAN, Type.NOTHING);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14, Type.POSITION);
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) Environment(com.viaversion.viaversion.api.minecraft.Environment) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) Types1_13(com.viaversion.viaversion.api.type.types.version.Types1_13) Item(com.viaversion.viaversion.api.minecraft.item.Item) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ArrayList(java.util.ArrayList) 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) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ServerboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) ChunkSectionLightImpl(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLightImpl) ImmutableSet(com.google.common.collect.ImmutableSet) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) EnchantmentRewriter(com.viaversion.viabackwards.api.rewriters.EnchantmentRewriter) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Set(java.util.Set) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) Via(com.viaversion.viaversion.api.Via) ChunkSectionLight(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLight) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) List(java.util.List) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ArrayList(java.util.ArrayList) Chunk1_14Type(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type) JsonObject(com.viaversion.viaversion.libs.gson.JsonObject) RecipeRewriter1_13_2(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Item(com.viaversion.viaversion.api.minecraft.item.Item) ChunkSectionLight(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLight) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) RecipeRewriter(com.viaversion.viaversion.rewriter.RecipeRewriter) ChunkSectionLightImpl(com.viaversion.viaversion.api.minecraft.chunks.ChunkSectionLightImpl) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)

Example 5 with ClientWorld

use of com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld 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)

Aggregations

ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)25 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)19 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)19 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)18 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)15 Type (com.viaversion.viaversion.api.type.Type)11 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)10 Chunk1_9_3_4Type (com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type)10 Via (com.viaversion.viaversion.api.Via)9 Item (com.viaversion.viaversion.api.minecraft.item.Item)9 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)7 Position (com.viaversion.viaversion.api.minecraft.Position)7 EntityTrackerBase (com.viaversion.viaversion.data.entity.EntityTrackerBase)7 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)6 Chunk1_13Type (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type)6 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)5 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)5 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)5 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)3 ChunkLightStorage (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage)3