Search in sources :

Example 6 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaBackwards by ViaVersion.

the class EntityPackets1_13_2 method register.

public static void register(Protocol1_13_1To1_13_2 protocol) {
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, 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 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_13_2.METADATA_LIST, Types1_13.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) {
                        metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId()));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Metadata
            map(Types1_13_2.METADATA_LIST, Types1_13.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) {
                        metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId()));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.ENTITY_METADATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Metadata list
            map(Types1_13_2.METADATA_LIST, Types1_13.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    for (Metadata metadata : wrapper.get(Types1_13.METADATA_LIST, 0)) {
                        metadata.setMetaType(Types1_13.META_TYPES.byId(metadata.metaType().typeId()));
                    }
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

Example 7 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata 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 8 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaBackwards by ViaVersion.

the class EntityPackets1_11 method registerPackets.

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

        @Override
        public void registerMap() {
            map(Type.INT);
            map(Type.POSITION);
            map(Type.INT);
            handler(wrapper -> {
                int type = wrapper.get(Type.INT, 0);
                if (type == 2002 || type == 2007) {
                    // 2007 potion id doesn't exist in 1.10
                    if (type == 2007) {
                        wrapper.set(Type.INT, 0, 2002);
                    }
                    int mappedData = PotionSplashHandler.getOldData(wrapper.get(Type.INT, 1));
                    if (mappedData != -1) {
                        wrapper.set(Type.INT, 1, mappedData);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.BYTE);
            // 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);
            // Track Entity
            handler(getObjectTrackerHandler());
            handler(getObjectRewriter(id -> Entity1_11Types.ObjectType.findById(id).orElse(null)));
            // Handle FallingBlock blocks
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Optional<Entity1_12Types.ObjectType> type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0));
                    if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) {
                        int objectData = wrapper.get(Type.INT, 0);
                        int objType = objectData & 4095;
                        int data = objectData >> 12 & 15;
                        Block block = protocol.getItemRewriter().handleBlock(objType, data);
                        if (block == null)
                            return;
                        wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12);
                    }
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_9_3.SPAWN_EXPERIENCE_ORB, Entity1_11Types.EntityType.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_9_3.SPAWN_GLOBAL_ENTITY, Entity1_11Types.EntityType.WEATHER);
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.VAR_INT, Type.UNSIGNED_BYTE);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_9.METADATA_LIST);
            // Track entity
            handler(getTrackerHandler(Type.UNSIGNED_BYTE, 0));
            // Rewrite entity type / metadata
            handler(wrapper -> {
                int entityId = wrapper.get(Type.VAR_INT, 0);
                EntityType type = tracker(wrapper.user()).entityType(entityId);
                List<Metadata> list = wrapper.get(Types1_9.METADATA_LIST, 0);
                handleMetadata(wrapper.get(Type.VAR_INT, 0), list, wrapper.user());
                EntityData entityData = entityDataForType(type);
                if (entityData != null) {
                    wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId());
                    if (entityData.hasBaseMeta()) {
                        entityData.defaultMeta().createMeta(new WrappedMetadata(list));
                    }
                }
                // Sub 1.11 clients will error if the list is empty
                if (list.isEmpty()) {
                    list.add(new Metadata(0, MetaType1_9.Byte, (byte) 0));
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_9_3.SPAWN_PAINTING, Entity1_11Types.EntityType.PAINTING);
    registerJoinGame(ClientboundPackets1_9_3.JOIN_GAME, Entity1_11Types.EntityType.PLAYER);
    registerRespawn(ClientboundPackets1_9_3.RESPAWN);
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Metadata list
            map(Types1_9.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_9.METADATA_LIST, Entity1_11Types.EntityType.PLAYER));
            handler(wrapper -> {
                // Sub 1.11 clients will cry if the list is empty
                List<Metadata> metadata = wrapper.get(Types1_9.METADATA_LIST, 0);
                if (metadata.isEmpty()) {
                    metadata.add(new Metadata(0, MetaType1_9.Byte, (byte) 0));
                }
            });
        }
    });
    registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_9_3.ENTITY_STATUS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Entity Status
            map(Type.BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    byte b = wrapper.get(Type.BYTE, 0);
                    if (b == 35) {
                        wrapper.clearPacket();
                        // Change Game State
                        wrapper.setId(0x1E);
                        // Play Elder Guardian animation
                        wrapper.write(Type.UNSIGNED_BYTE, (short) 10);
                        wrapper.write(Type.FLOAT, 0F);
                    }
                }
            });
        }
    });
}
Also used : EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) Entity1_12Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) ChestedHorseStorage(com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage) PotionSplashHandler(com.viaversion.viabackwards.protocol.protocol1_10to1_11.PotionSplashHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) MetaType1_9(com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9) Block(com.viaversion.viabackwards.utils.Block) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) List(java.util.List) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Types1_9(com.viaversion.viaversion.api.type.types.version.Types1_9) Optional(java.util.Optional) Entity1_11Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) Protocol1_10To1_11(com.viaversion.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Block(com.viaversion.viabackwards.utils.Block) List(java.util.List)

Example 9 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata 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 10 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaBackwards by ViaVersion.

the class EntityPackets1_12 method registerPackets.

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

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.BYTE);
            // 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);
            // Track Entity
            handler(getObjectTrackerHandler());
            handler(getObjectRewriter(id -> Entity1_12Types.ObjectType.findById(id).orElse(null)));
            // Handle FallingBlock blocks
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Optional<Entity1_12Types.ObjectType> type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0));
                    if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) {
                        int objectData = wrapper.get(Type.INT, 0);
                        int objType = objectData & 4095;
                        int data = objectData >> 12 & 15;
                        Block block = protocol.getItemRewriter().handleBlock(objType, data);
                        if (block == null) {
                            return;
                        }
                        wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12);
                    }
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_12.SPAWN_EXPERIENCE_ORB, Entity1_12Types.EntityType.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_12.SPAWN_GLOBAL_ENTITY, Entity1_12Types.EntityType.WEATHER);
    protocol.registerClientbound(ClientboundPackets1_12.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - 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 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_12.METADATA_LIST);
            // Track entity
            handler(getTrackerHandler());
            // Rewrite entity type / metadata
            handler(getMobSpawnRewriter(Types1_12.METADATA_LIST));
        }
    });
    registerTracker(ClientboundPackets1_12.SPAWN_PAINTING, Entity1_12Types.EntityType.PAINTING);
    protocol.registerClientbound(ClientboundPackets1_12.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Metadata list
            map(Types1_12.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_12.METADATA_LIST, Entity1_12Types.EntityType.PLAYER));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Dimension
            map(Type.INT);
            handler(getTrackerHandler(Entity1_12Types.EntityType.PLAYER, Type.INT));
            handler(getDimensionHandler(1));
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ShoulderTracker tracker = wrapper.user().get(ShoulderTracker.class);
                    tracker.setEntityId(wrapper.get(Type.INT, 0));
                }
            });
            // Send fake inventory achievement
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper packetWrapper) throws Exception {
                    PacketWrapper wrapper = PacketWrapper.create(0x07, null, packetWrapper.user());
                    wrapper.write(Type.VAR_INT, 1);
                    wrapper.write(Type.STRING, "achievement.openInventory");
                    wrapper.write(Type.VAR_INT, 1);
                    wrapper.scheduleSend(Protocol1_11_1To1_12.class);
                }
            });
        }
    });
    registerRespawn(ClientboundPackets1_12.RESPAWN);
    registerRemoveEntities(ClientboundPackets1_12.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_12.ENTITY_METADATA, Types1_12.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_12.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.INT);
            handler(wrapper -> {
                int size = wrapper.get(Type.INT, 0);
                int newSize = size;
                for (int i = 0; i < size; i++) {
                    String key = wrapper.read(Type.STRING);
                    // Remove new attribute
                    if (key.equals("generic.flyingSpeed")) {
                        newSize--;
                        wrapper.read(Type.DOUBLE);
                        int modSize = wrapper.read(Type.VAR_INT);
                        for (int j = 0; j < modSize; j++) {
                            wrapper.read(Type.UUID);
                            wrapper.read(Type.DOUBLE);
                            wrapper.read(Type.BYTE);
                        }
                    } else {
                        wrapper.write(Type.STRING, key);
                        wrapper.passthrough(Type.DOUBLE);
                        int modSize = wrapper.passthrough(Type.VAR_INT);
                        for (int j = 0; j < modSize; j++) {
                            wrapper.passthrough(Type.UUID);
                            wrapper.passthrough(Type.DOUBLE);
                            wrapper.passthrough(Type.BYTE);
                        }
                    }
                }
                if (newSize != size) {
                    wrapper.set(Type.INT, 0, newSize);
                }
            });
        }
    });
}
Also used : ShoulderTracker(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker) Entity1_12Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) ClientboundPackets1_12(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.ClientboundPackets1_12) ParrotStorage(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ParrotStorage) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Block(com.viaversion.viabackwards.utils.Block) Types1_12(com.viaversion.viaversion.api.type.types.version.Types1_12) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) MetaType1_12(com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_12) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Optional(java.util.Optional) ShoulderTracker(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker) Protocol1_11_1To1_12(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.Protocol1_11_1To1_12) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Block(com.viaversion.viabackwards.utils.Block)

Aggregations

Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)37 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)21 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)15 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)14 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)11 Item (com.viaversion.viaversion.api.minecraft.item.Item)10 Type (com.viaversion.viaversion.api.type.Type)8 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)6 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)6 EntityData (com.viaversion.viabackwards.api.entities.storage.EntityData)5 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)5 Particle (com.viaversion.viaversion.api.type.types.Particle)5 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)5 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)4 WrappedMetadata (com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)4 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)4 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)4 ArrayList (java.util.ArrayList)4 EntityPositionHandler (com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler)3 Block (com.viaversion.viabackwards.utils.Block)3