Search in sources :

Example 1 with ServerboundPackets1_16

use of com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16 in project ViaBackwards by ViaVersion.

the class Protocol1_16_1To1_16_2 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_16_2To1_16_1.class, MAPPINGS::load);
    translatableRewriter.registerBossBar(ClientboundPackets1_16_2.BOSSBAR);
    translatableRewriter.registerCombatEvent(ClientboundPackets1_16_2.COMBAT_EVENT);
    translatableRewriter.registerDisconnect(ClientboundPackets1_16_2.DISCONNECT);
    translatableRewriter.registerTabList(ClientboundPackets1_16_2.TAB_LIST);
    translatableRewriter.registerTitle(ClientboundPackets1_16_2.TITLE);
    translatableRewriter.registerOpenWindow(ClientboundPackets1_16_2.OPEN_WINDOW);
    translatableRewriter.registerPing();
    new CommandRewriter1_16_2(this).registerDeclareCommands(ClientboundPackets1_16_2.DECLARE_COMMANDS);
    (blockItemPackets = new BlockItemPackets1_16_2(this)).register();
    entityRewriter.register();
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_16_2.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_16_2.ENTITY_SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_16_2.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_16_2.STOP_SOUND);
    registerClientbound(ClientboundPackets1_16_2.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                JsonElement message = wrapper.passthrough(Type.COMPONENT);
                translatableRewriter.processText(message);
                byte position = wrapper.passthrough(Type.BYTE);
                if (position == 2) {
                    // https://bugs.mojang.com/browse/MC-119145
                    wrapper.clearPacket();
                    wrapper.setId(ClientboundPackets1_16.TITLE.ordinal());
                    wrapper.write(Type.VAR_INT, 2);
                    wrapper.write(Type.COMPONENT, message);
                }
            });
        }
    });
    // Recipe book data has been split into 2 separate packets
    registerServerbound(ServerboundPackets1_16.RECIPE_BOOK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.read(Type.VAR_INT);
                    if (type == 0) {
                        // Shown, change to its own packet
                        // Recipe
                        wrapper.passthrough(Type.STRING);
                        wrapper.setId(ServerboundPackets1_16_2.SEEN_RECIPE.ordinal());
                    } else {
                        wrapper.cancel();
                        // Settings
                        for (int i = 0; i < 3; i++) {
                            sendSeenRecipePacket(i, wrapper);
                        }
                    }
                }

                private void sendSeenRecipePacket(int recipeType, PacketWrapper wrapper) throws Exception {
                    boolean open = wrapper.read(Type.BOOLEAN);
                    boolean filter = wrapper.read(Type.BOOLEAN);
                    PacketWrapper newPacket = wrapper.create(ServerboundPackets1_16_2.RECIPE_BOOK_DATA);
                    newPacket.write(Type.VAR_INT, recipeType);
                    newPacket.write(Type.BOOLEAN, open);
                    newPacket.write(Type.BOOLEAN, filter);
                    newPacket.sendToServer(Protocol1_16_1To1_16_2.class);
                }
            });
        }
    });
    new TagRewriter(this).register(ClientboundPackets1_16_2.TAGS, RegistryType.ENTITY);
    new StatisticsRewriter(this).register(ClientboundPackets1_16_2.STATISTICS);
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) Protocol1_16_2To1_16_1(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.Protocol1_16_2To1_16_1) TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) 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) ServerboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16) ServerboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) BlockItemPackets1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.packets.BlockItemPackets1_16_2) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) CommandRewriter1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.data.CommandRewriter1_16_2) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) EntityPackets1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.packets.EntityPackets1_16_2) CommandRewriter1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.data.CommandRewriter1_16_2) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) BlockItemPackets1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.packets.BlockItemPackets1_16_2) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter)

Example 2 with ServerboundPackets1_16

use of com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16 in project ViaVersion by ViaVersion.

the class Protocol1_16_2To1_16_1 method registerPackets.

@Override
protected void registerPackets() {
    metadataRewriter.register();
    itemRewriter.register();
    EntityPackets.register(this);
    WorldPackets.register(this);
    tagRewriter = new TagRewriter(this);
    tagRewriter.register(ClientboundPackets1_16.TAGS, RegistryType.ENTITY);
    new StatisticsRewriter(this).register(ClientboundPackets1_16.STATISTICS);
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_16.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_16.ENTITY_SOUND);
    // Recipe book data has been split into 2 separate packets
    registerServerbound(ServerboundPackets1_16_2.RECIPE_BOOK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int recipeType = wrapper.read(Type.VAR_INT);
                boolean open = wrapper.read(Type.BOOLEAN);
                boolean filter = wrapper.read(Type.BOOLEAN);
                // Settings
                wrapper.write(Type.VAR_INT, 1);
                // Crafting
                wrapper.write(Type.BOOLEAN, recipeType == 0 && open);
                wrapper.write(Type.BOOLEAN, filter);
                // Furnace
                wrapper.write(Type.BOOLEAN, recipeType == 1 && open);
                wrapper.write(Type.BOOLEAN, filter);
                // Blast Furnace
                wrapper.write(Type.BOOLEAN, recipeType == 2 && open);
                wrapper.write(Type.BOOLEAN, filter);
                // Smoker
                wrapper.write(Type.BOOLEAN, recipeType == 3 && open);
                wrapper.write(Type.BOOLEAN, filter);
            });
        }
    });
    registerServerbound(ServerboundPackets1_16_2.SEEN_RECIPE, ServerboundPackets1_16.RECIPE_BOOK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                String recipe = wrapper.read(Type.STRING);
                // Shown
                wrapper.write(Type.VAR_INT, 0);
                wrapper.write(Type.STRING, recipe);
            });
        }
    });
}
Also used : EntityPackets(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.EntityPackets) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) MetadataRewriter1_16_2To1_16_1(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.metadata.MetadataRewriter1_16_2To1_16_1) Entity1_16_2Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types) WorldPackets(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.WorldPackets) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16) MappingData(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.data.MappingData) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.packets.InventoryPackets) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter)

Example 3 with ServerboundPackets1_16

use of com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16 in project ViaVersion by ViaVersion.

the class Protocol1_16To1_15_2 method registerPackets.

@Override
protected void registerPackets() {
    metadataRewriter.register();
    itemRewriter.register();
    EntityPackets.register(this);
    WorldPackets.register(this);
    tagRewriter = new TagRewriter(this);
    tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
    new StatisticsRewriter(this).register(ClientboundPackets1_15.STATISTICS);
    // Login Success
    registerClientbound(State.LOGIN, 0x02, 0x02, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Transform string to int array
                UUID uuid = UUID.fromString(wrapper.read(Type.STRING));
                wrapper.write(Type.UUID_INT_ARRAY, uuid);
            });
        }
    });
    // Motd Status - line breaks are no longer allowed for player samples
    registerClientbound(State.STATUS, 0x00, 0x00, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                String original = wrapper.passthrough(Type.STRING);
                JsonObject object = GsonUtil.getGson().fromJson(original, JsonObject.class);
                JsonObject players = object.getAsJsonObject("players");
                if (players == null)
                    return;
                JsonArray sample = players.getAsJsonArray("sample");
                if (sample == null)
                    return;
                JsonArray splitSamples = new JsonArray();
                for (JsonElement element : sample) {
                    JsonObject playerInfo = element.getAsJsonObject();
                    String name = playerInfo.getAsJsonPrimitive("name").getAsString();
                    if (name.indexOf('\n') == -1) {
                        splitSamples.add(playerInfo);
                        continue;
                    }
                    String id = playerInfo.getAsJsonPrimitive("id").getAsString();
                    for (String s : name.split("\n")) {
                        JsonObject newSample = new JsonObject();
                        newSample.addProperty("name", s);
                        newSample.addProperty("id", id);
                        splitSamples.add(newSample);
                    }
                }
                // Replace data if changed
                if (splitSamples.size() != sample.size()) {
                    players.add("sample", splitSamples);
                    wrapper.set(Type.STRING, 0, object.toString());
                }
            });
        }
    });
    ComponentRewriter componentRewriter = new TranslationMappings(this);
    // Handle (relevant) component cases for translatable and score changes
    registerClientbound(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.COMPONENT);
            map(Type.BYTE);
            handler(wrapper -> {
                componentRewriter.processText(wrapper.get(Type.COMPONENT, 0));
                // Sender uuid - always send as 'system'
                wrapper.write(Type.UUID, ZERO_UUID);
            });
        }
    });
    componentRewriter.registerBossBar(ClientboundPackets1_15.BOSSBAR);
    componentRewriter.registerTitle(ClientboundPackets1_15.TITLE);
    componentRewriter.registerCombatEvent(ClientboundPackets1_15.COMBAT_EVENT);
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_15.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND);
    registerServerbound(ServerboundPackets1_16.INTERACT_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Entity Id
                wrapper.passthrough(Type.VAR_INT);
                int action = wrapper.passthrough(Type.VAR_INT);
                if (action == 0 || action == 2) {
                    if (action == 2) {
                        // Location
                        wrapper.passthrough(Type.FLOAT);
                        wrapper.passthrough(Type.FLOAT);
                        wrapper.passthrough(Type.FLOAT);
                    }
                    // Hand
                    wrapper.passthrough(Type.VAR_INT);
                }
                // New boolean: Whether the client is sneaking/pressing shift
                wrapper.read(Type.BOOLEAN);
            });
        }
    });
    if (Via.getConfig().isIgnoreLong1_16ChannelNames()) {
        registerServerbound(ServerboundPackets1_16.PLUGIN_MESSAGE, new PacketRemapper() {

            @Override
            public void registerMap() {
                handler(wrapper -> {
                    String channel = wrapper.passthrough(Type.STRING);
                    if (channel.length() > 32) {
                        if (!Via.getConfig().isSuppressConversionWarnings()) {
                            Via.getPlatform().getLogger().warning("Ignoring incoming plugin channel, as it is longer than 32 characters: " + channel);
                        }
                        wrapper.cancel();
                    } else if (channel.equals("minecraft:register") || channel.equals("minecraft:unregister")) {
                        String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0");
                        List<String> checkedChannels = new ArrayList<>(channels.length);
                        for (String registeredChannel : channels) {
                            if (registeredChannel.length() > 32) {
                                if (!Via.getConfig().isSuppressConversionWarnings()) {
                                    Via.getPlatform().getLogger().warning("Ignoring incoming plugin channel register of '" + registeredChannel + "', as it is longer than 32 characters");
                                }
                                continue;
                            }
                            checkedChannels.add(registeredChannel);
                        }
                        if (checkedChannels.isEmpty()) {
                            wrapper.cancel();
                            return;
                        }
                        wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(checkedChannels).getBytes(StandardCharsets.UTF_8));
                    }
                });
            }
        });
    }
    registerServerbound(ServerboundPackets1_16.PLAYER_ABILITIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.BYTE);
                // Flying and walking speed - not important anyways
                wrapper.write(Type.FLOAT, 0.05F);
                wrapper.write(Type.FLOAT, 0.1F);
            });
        }
    });
    cancelServerbound(ServerboundPackets1_16.GENERATE_JIGSAW);
    cancelServerbound(ServerboundPackets1_16.UPDATE_JIGSAW_BLOCK);
}
Also used : ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) JsonObject(com.google.gson.JsonObject) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ParticleType(com.viaversion.viaversion.api.type.types.minecraft.ParticleType) GsonUtil(com.viaversion.viaversion.util.GsonUtil) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) MetadataRewriter1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2) EntityPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.EntityPackets) MappingData(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.MappingData) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.InventoryPackets) State(com.viaversion.viaversion.api.protocol.packet.State) UUID(java.util.UUID) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) StandardCharsets(java.nio.charset.StandardCharsets) JsonArray(com.google.gson.JsonArray) InventoryTracker1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16) List(java.util.List) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) TranslationMappings(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.TranslationMappings) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) WorldPackets(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.packets.WorldPackets) Joiner(com.google.common.base.Joiner) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) ComponentRewriter(com.viaversion.viaversion.rewriter.ComponentRewriter) JsonElement(com.google.gson.JsonElement) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) TranslationMappings(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.TranslationMappings) UUID(java.util.UUID)

Example 4 with ServerboundPackets1_16

use of com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16 in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_16_2 method registerPackets.

@Override
protected void registerPackets() {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    new RecipeRewriter1_16(protocol).registerDefaultHandler(ClientboundPackets1_16_2.DECLARE_RECIPES);
    registerSetCooldown(ClientboundPackets1_16_2.COOLDOWN);
    registerWindowItems(ClientboundPackets1_16_2.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
    registerSetSlot(ClientboundPackets1_16_2.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
    registerEntityEquipmentArray(ClientboundPackets1_16_2.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
    registerTradeList(ClientboundPackets1_16_2.TRADE_LIST, Type.FLAT_VAR_INT_ITEM);
    registerAdvancements(ClientboundPackets1_16_2.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
    protocol.registerClientbound(ClientboundPackets1_16_2.UNLOCK_RECIPES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                // Open
                wrapper.passthrough(Type.BOOLEAN);
                // Filter
                wrapper.passthrough(Type.BOOLEAN);
                // Furnace Open
                wrapper.passthrough(Type.BOOLEAN);
                // Filter furnace
                wrapper.passthrough(Type.BOOLEAN);
                // Blast furnace / smoker
                wrapper.read(Type.BOOLEAN);
                wrapper.read(Type.BOOLEAN);
                wrapper.read(Type.BOOLEAN);
                wrapper.read(Type.BOOLEAN);
            });
        }
    });
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16_2.ACKNOWLEDGE_PLAYER_DIGGING);
    blockRewriter.registerBlockAction(ClientboundPackets1_16_2.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_16_2.BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Chunk chunk = wrapper.read(new Chunk1_16_2Type());
                wrapper.write(new Chunk1_16Type(), chunk);
                chunk.setIgnoreOldLightData(true);
                for (int i = 0; i < chunk.getSections().length; i++) {
                    ChunkSection section = chunk.getSections()[i];
                    if (section == null)
                        continue;
                    for (int j = 0; j < section.getPaletteSize(); j++) {
                        int old = section.getPaletteEntry(j);
                        section.setPaletteEntry(j, protocol.getMappingData().getNewBlockStateId(old));
                    }
                }
                for (CompoundTag blockEntity : chunk.getBlockEntities()) {
                    if (blockEntity != null) {
                        handleBlockEntity(blockEntity);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                handleBlockEntity(wrapper.passthrough(Type.NBT));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                long chunkPosition = wrapper.read(Type.LONG);
                // Ignore old light data
                wrapper.read(Type.BOOLEAN);
                int chunkX = (int) (chunkPosition >> 42);
                int chunkY = (int) (chunkPosition << 44 >> 44);
                int chunkZ = (int) (chunkPosition << 22 >> 42);
                wrapper.write(Type.INT, chunkX);
                wrapper.write(Type.INT, chunkZ);
                BlockChangeRecord[] blockChangeRecord = wrapper.read(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY);
                wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, blockChangeRecord);
                for (int i = 0; i < blockChangeRecord.length; i++) {
                    BlockChangeRecord record = blockChangeRecord[i];
                    int blockId = protocol.getMappingData().getNewBlockStateId(record.getBlockId());
                    // Relative y -> absolute y
                    blockChangeRecord[i] = new BlockChangeRecord1_8(record.getSectionX(), record.getY(chunkY), record.getSectionZ(), blockId);
                }
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001);
    registerSpawnParticle(ClientboundPackets1_16_2.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
    registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
    registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
    protocol.registerServerbound(ServerboundPackets1_16.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> handleItemToServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
        }
    });
}
Also used : BlockChangeRecord1_8(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_8) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Protocol1_16_1To1_16_2(com.viaversion.viabackwards.protocol.protocol1_16_1to1_16_2.Protocol1_16_1To1_16_2) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) IntArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) BlockChangeRecord1_8(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_8) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) RecipeRewriter1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data.RecipeRewriter1_16) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 5 with ServerboundPackets1_16

use of com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16 in project ViaVersion by ViaVersion.

the class EntityPackets method register.

public static void register(Protocol1_16To1_15_2 protocol) {
    MetadataRewriter1_16To1_15_2 metadataRewriter = protocol.get(MetadataRewriter1_16To1_15_2.class);
    // Spawn lightning -> Spawn entity
    protocol.registerClientbound(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY, ClientboundPackets1_16.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int entityId = wrapper.passthrough(Type.VAR_INT);
                byte type = wrapper.read(Type.BYTE);
                if (type != 1) {
                    // Cancel if not lightning/invalid id
                    wrapper.cancel();
                    return;
                }
                wrapper.user().getEntityTracker(Protocol1_16To1_15_2.class).addEntity(entityId, Entity1_16Types.LIGHTNING_BOLT);
                // uuid
                wrapper.write(Type.UUID, UUID.randomUUID());
                // entity type
                wrapper.write(Type.VAR_INT, Entity1_16Types.LIGHTNING_BOLT.getId());
                // x
                wrapper.passthrough(Type.DOUBLE);
                // y
                wrapper.passthrough(Type.DOUBLE);
                // z
                wrapper.passthrough(Type.DOUBLE);
                // yaw
                wrapper.write(Type.BYTE, (byte) 0);
                // pitch
                wrapper.write(Type.BYTE, (byte) 0);
                // data
                wrapper.write(Type.INT, 0);
                // velocity
                wrapper.write(Type.SHORT, (short) 0);
                // velocity
                wrapper.write(Type.SHORT, (short) 0);
                // velocity
                wrapper.write(Type.SHORT, (short) 0);
            });
        }
    });
    metadataRewriter.registerTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_16Types.FALLING_BLOCK);
    metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_MOB);
    metadataRewriter.registerTracker(ClientboundPackets1_15.SPAWN_PLAYER, Entity1_16Types.PLAYER);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_15.ENTITY_METADATA, Types1_14.METADATA_LIST, Types1_16.METADATA_LIST);
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_15.DESTROY_ENTITIES);
    protocol.registerClientbound(ClientboundPackets1_15.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(DIMENSION_HANDLER);
            // Seed
            map(Type.LONG);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                // Previous gamemode, set to none
                wrapper.write(Type.BYTE, (byte) -1);
                String levelType = wrapper.read(Type.STRING);
                // debug
                wrapper.write(Type.BOOLEAN, false);
                wrapper.write(Type.BOOLEAN, levelType.equals("flat"));
                // keep all playerdata
                wrapper.write(Type.BOOLEAN, true);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                // Previous gamemode, set to none
                wrapper.write(Type.BYTE, (byte) -1);
                // World list - only used for command completion
                wrapper.write(Type.STRING_ARRAY, Arrays.copyOf(WORLD_NAMES, WORLD_NAMES.length));
                // Dimension registry
                wrapper.write(Type.NBT, DIMENSIONS_TAG.clone());
            });
            // Dimension
            handler(DIMENSION_HANDLER);
            // Seed
            map(Type.LONG);
            // Max players
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                wrapper.user().getEntityTracker(Protocol1_16To1_15_2.class).addEntity(wrapper.get(Type.INT, 0), Entity1_16Types.PLAYER);
                // level type
                final String type = wrapper.read(Type.STRING);
                // View distance
                wrapper.passthrough(Type.VAR_INT);
                // Reduced debug info
                wrapper.passthrough(Type.BOOLEAN);
                // Show death screen
                wrapper.passthrough(Type.BOOLEAN);
                // Debug
                wrapper.write(Type.BOOLEAN, false);
                wrapper.write(Type.BOOLEAN, type.equals("flat"));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                int size = wrapper.passthrough(Type.INT);
                int actualSize = size;
                for (int i = 0; i < size; i++) {
                    // Attributes have been renamed and are now namespaced identifiers
                    String key = wrapper.read(Type.STRING);
                    String attributeIdentifier = protocol.getMappingData().getAttributeMappings().get(key);
                    if (attributeIdentifier == null) {
                        attributeIdentifier = "minecraft:" + key;
                        if (!com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.MappingData.isValid1_13Channel(attributeIdentifier)) {
                            if (!Via.getConfig().isSuppressConversionWarnings()) {
                                Via.getPlatform().getLogger().warning("Invalid attribute: " + key);
                            }
                            actualSize--;
                            wrapper.read(Type.DOUBLE);
                            int modifierSize = wrapper.read(Type.VAR_INT);
                            for (int j = 0; j < modifierSize; j++) {
                                wrapper.read(Type.UUID);
                                wrapper.read(Type.DOUBLE);
                                wrapper.read(Type.BYTE);
                            }
                            continue;
                        }
                    }
                    wrapper.write(Type.STRING, attributeIdentifier);
                    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);
                    }
                }
                if (size != actualSize) {
                    wrapper.set(Type.INT, 0, actualSize);
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_16.ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                InventoryTracker1_16 inventoryTracker = wrapper.user().get(InventoryTracker1_16.class);
                // Don't send an arm swing if the player has an inventory opened.
                if (inventoryTracker.getInventory() != -1) {
                    wrapper.cancel();
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) ByteTag(com.github.steveice10.opennbt.tag.builtin.ByteTag) Arrays(java.util.Arrays) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) UUID(java.util.UUID) Via(com.viaversion.viaversion.api.Via) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) Protocol1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2) Types1_16(com.viaversion.viaversion.api.type.types.version.Types1_16) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16) WorldIdentifiers(com.viaversion.viaversion.api.minecraft.WorldIdentifiers) Type(com.viaversion.viaversion.api.type.Type) ServerboundPackets1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16) InventoryTracker1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16) LongTag(com.github.steveice10.opennbt.tag.builtin.LongTag) Entity1_16Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) MetadataRewriter1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2) FloatTag(com.github.steveice10.opennbt.tag.builtin.FloatTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) InventoryTracker1_16(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.storage.InventoryTracker1_16) MetadataRewriter1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.metadata.MetadataRewriter1_16To1_15_2)

Aggregations

PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)6 Type (com.viaversion.viaversion.api.type.Type)6 ServerboundPackets1_16 (com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16)5 ClientboundPackets1_16 (com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16)4 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)3 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)3 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)3 EntityRewriter (com.viaversion.viaversion.api.rewriter.EntityRewriter)3 EntityTrackerBase (com.viaversion.viaversion.data.entity.EntityTrackerBase)3 ClientboundPackets1_15 (com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15)3 StatisticsRewriter (com.viaversion.viaversion.rewriter.StatisticsRewriter)3 TagRewriter (com.viaversion.viaversion.rewriter.TagRewriter)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)2 LongTag (com.github.steveice10.opennbt.tag.builtin.LongTag)2 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)2 Via (com.viaversion.viaversion.api.Via)2 Entity1_16Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_16Types)2 Entity1_16_2Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_16_2Types)2 AbstractProtocol (com.viaversion.viaversion.api.protocol.AbstractProtocol)2