Search in sources :

Example 1 with RegistryType

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

the class Protocol1_16_4To1_17 method registerPackets.

@Override
protected void registerPackets() {
    executeAsyncAfterLoaded(Protocol1_17To1_16_4.class, MAPPINGS::load);
    translatableRewriter.registerChatMessage(ClientboundPackets1_17.CHAT_MESSAGE);
    translatableRewriter.registerBossBar(ClientboundPackets1_17.BOSSBAR);
    translatableRewriter.registerDisconnect(ClientboundPackets1_17.DISCONNECT);
    translatableRewriter.registerTabList(ClientboundPackets1_17.TAB_LIST);
    translatableRewriter.registerOpenWindow(ClientboundPackets1_17.OPEN_WINDOW);
    translatableRewriter.registerPing();
    blockItemPackets = new BlockItemPackets1_17(this);
    blockItemPackets.register();
    entityRewriter.register();
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_17.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_17.ENTITY_SOUND);
    soundRewriter.registerNamedSound(ClientboundPackets1_17.NAMED_SOUND);
    soundRewriter.registerStopSound(ClientboundPackets1_17.STOP_SOUND);
    TagRewriter tagRewriter = new TagRewriter(this);
    registerClientbound(ClientboundPackets1_17.TAGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Map<String, List<TagData>> tags = new HashMap<>();
                int length = wrapper.read(Type.VAR_INT);
                for (int i = 0; i < length; i++) {
                    String resourceKey = wrapper.read(Type.STRING);
                    if (resourceKey.startsWith("minecraft:")) {
                        resourceKey = resourceKey.substring(10);
                    }
                    List<TagData> tagList = new ArrayList<>();
                    tags.put(resourceKey, tagList);
                    int tagLength = wrapper.read(Type.VAR_INT);
                    for (int j = 0; j < tagLength; j++) {
                        String identifier = wrapper.read(Type.STRING);
                        int[] entries = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
                        tagList.add(new TagData(identifier, entries));
                    }
                }
                // Put them into the hardcoded order of Vanilla tags (and only those), rewrite ids
                for (RegistryType type : RegistryType.getValues()) {
                    List<TagData> tagList = tags.get(type.getResourceLocation());
                    IdRewriteFunction rewriter = tagRewriter.getRewriter(type);
                    wrapper.write(Type.VAR_INT, tagList.size());
                    for (TagData tagData : tagList) {
                        int[] entries = tagData.entries();
                        if (rewriter != null) {
                            // Handle id rewriting now
                            IntList idList = new IntArrayList(entries.length);
                            for (int id : entries) {
                                int mappedId = rewriter.rewrite(id);
                                if (mappedId != -1) {
                                    idList.add(mappedId);
                                }
                            }
                            entries = idList.toArray(EMPTY_ARRAY);
                        }
                        wrapper.write(Type.STRING, tagData.identifier());
                        wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, entries);
                    }
                    // Stop after the entity types
                    if (type == RegistryType.ENTITY) {
                        break;
                    }
                }
            });
        }
    });
    new StatisticsRewriter(this).register(ClientboundPackets1_17.STATISTICS);
    registerClientbound(ClientboundPackets1_17.RESOURCE_PACK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.STRING);
                wrapper.passthrough(Type.STRING);
                // Required
                wrapper.read(Type.BOOLEAN);
                // Prompt message
                wrapper.read(Type.OPTIONAL_COMPONENT);
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Strength
            map(Type.FLOAT);
            handler(wrapper -> {
                // Collection length
                wrapper.write(Type.INT, wrapper.read(Type.VAR_INT));
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.SPAWN_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                // Angle (which Mojang just forgot to write to the buffer, lol)
                wrapper.read(Type.FLOAT);
            });
        }
    });
    registerClientbound(ClientboundPackets1_17.PING, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                int id = wrapper.read(Type.INT);
                short shortId = (short) id;
                if (id == shortId && ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) {
                    wrapper.user().get(PingRequests.class).addId(shortId);
                    // Send inventory acknowledgement to replace ping packet functionality in the unsigned byte range
                    PacketWrapper acknowledgementPacket = wrapper.create(ClientboundPackets1_16_2.WINDOW_CONFIRMATION);
                    // Inventory id
                    acknowledgementPacket.write(Type.UNSIGNED_BYTE, (short) 0);
                    // Confirmation id
                    acknowledgementPacket.write(Type.SHORT, shortId);
                    // Accepted
                    acknowledgementPacket.write(Type.BOOLEAN, false);
                    acknowledgementPacket.send(Protocol1_16_4To1_17.class);
                    return;
                }
                // Plugins expecting a real response will have to handle this accordingly themselves
                PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG);
                pongPacket.write(Type.INT, id);
                pongPacket.sendToServer(Protocol1_16_4To1_17.class);
            });
        }
    });
    registerServerbound(ServerboundPackets1_16_2.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Locale
            map(Type.STRING);
            // View distance
            map(Type.BYTE);
            // Chat mode
            map(Type.VAR_INT);
            // Chat colors
            map(Type.BOOLEAN);
            // Chat flags
            map(Type.UNSIGNED_BYTE);
            // Main hand
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Text filtering
                wrapper.write(Type.BOOLEAN, false);
            });
        }
    });
    // TODO translatables
    mergePacket(ClientboundPackets1_17.TITLE_TEXT, ClientboundPackets1_16_2.TITLE, 0);
    mergePacket(ClientboundPackets1_17.TITLE_SUBTITLE, ClientboundPackets1_16_2.TITLE, 1);
    mergePacket(ClientboundPackets1_17.ACTIONBAR, ClientboundPackets1_16_2.TITLE, 2);
    mergePacket(ClientboundPackets1_17.TITLE_TIMES, ClientboundPackets1_16_2.TITLE, 3);
    registerClientbound(ClientboundPackets1_17.CLEAR_TITLES, ClientboundPackets1_16_2.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                if (wrapper.read(Type.BOOLEAN)) {
                    // Reset times
                    wrapper.write(Type.VAR_INT, 5);
                } else {
                    // Simple clear
                    wrapper.write(Type.VAR_INT, 4);
                }
            });
        }
    });
    cancelClientbound(ClientboundPackets1_17.ADD_VIBRATION_SIGNAL);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) TranslatableRewriter(com.viaversion.viabackwards.api.rewriters.TranslatableRewriter) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) PingRequests(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.storage.PingRequests) IdRewriteFunction(com.viaversion.viaversion.rewriter.IdRewriteFunction) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) HashMap(java.util.HashMap) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) ArrayList(java.util.ArrayList) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Map(java.util.Map) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) TagData(com.viaversion.viaversion.api.minecraft.TagData) EntityPackets1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.EntityPackets1_17) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) BlockItemPackets1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.BlockItemPackets1_17) List(java.util.List) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) IntArrayList(com.viaversion.viaversion.libs.fastutil.ints.IntArrayList) ServerboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) BackwardsMappings(com.viaversion.viabackwards.api.data.BackwardsMappings) IntList(com.viaversion.viaversion.libs.fastutil.ints.IntList) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) BlockItemPackets1_17(com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.BlockItemPackets1_17) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) IntList(com.viaversion.viaversion.libs.fastutil.ints.IntList) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) TagData(com.viaversion.viaversion.api.minecraft.TagData) SoundRewriter(com.viaversion.viabackwards.api.rewriters.SoundRewriter) ArrayList(java.util.ArrayList) List(java.util.List) IntArrayList(com.viaversion.viaversion.libs.fastutil.ints.IntArrayList) IntList(com.viaversion.viaversion.libs.fastutil.ints.IntList) IdRewriteFunction(com.viaversion.viaversion.rewriter.IdRewriteFunction) IntArrayList(com.viaversion.viaversion.libs.fastutil.ints.IntArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with RegistryType

use of com.viaversion.viaversion.api.minecraft.RegistryType in project ViaVersion by ViaVersion.

the class Protocol1_17To1_16_4 method registerPackets.

@Override
protected void registerPackets() {
    entityRewriter.register();
    itemRewriter.register();
    WorldPackets.register(this);
    registerClientbound(ClientboundPackets1_16_2.TAGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Tags are now generically written with resource location - 5 different Vanilla types
                wrapper.write(Type.VAR_INT, 5);
                for (RegistryType type : RegistryType.getValues()) {
                    // Prefix with resource location
                    wrapper.write(Type.STRING, type.resourceLocation());
                    // Id conversion
                    tagRewriter.handle(wrapper, tagRewriter.getRewriter(type), tagRewriter.getNewTags(type));
                    // Stop iterating after entity types
                    if (type == RegistryType.ENTITY) {
                        break;
                    }
                }
                // New Game Event tags type
                wrapper.write(Type.STRING, RegistryType.GAME_EVENT.resourceLocation());
                wrapper.write(Type.VAR_INT, NEW_GAME_EVENT_TAGS.length);
                for (String tag : NEW_GAME_EVENT_TAGS) {
                    wrapper.write(Type.STRING, tag);
                    wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[0]);
                }
            });
        }
    });
    new StatisticsRewriter(this).register(ClientboundPackets1_16_2.STATISTICS);
    SoundRewriter soundRewriter = new SoundRewriter(this);
    soundRewriter.registerSound(ClientboundPackets1_16_2.SOUND);
    soundRewriter.registerSound(ClientboundPackets1_16_2.ENTITY_SOUND);
    registerClientbound(ClientboundPackets1_16_2.RESOURCE_PACK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.STRING);
                wrapper.passthrough(Type.STRING);
                // Required
                wrapper.write(Type.BOOLEAN, Via.getConfig().isForcedUse1_17ResourcePack());
                // Prompt message
                wrapper.write(Type.OPTIONAL_COMPONENT, Via.getConfig().get1_17ResourcePackPrompt());
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.MAP_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.passthrough(Type.VAR_INT);
                wrapper.passthrough(Type.BYTE);
                // Tracking position removed
                wrapper.read(Type.BOOLEAN);
                wrapper.passthrough(Type.BOOLEAN);
                int size = wrapper.read(Type.VAR_INT);
                // Write whether markers exists or not
                if (size != 0) {
                    wrapper.write(Type.BOOLEAN, true);
                    wrapper.write(Type.VAR_INT, size);
                } else {
                    wrapper.write(Type.BOOLEAN, false);
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.TITLE, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Title packet actions have been split into individual packets (the content hasn't changed)
                int type = wrapper.read(Type.VAR_INT);
                ClientboundPacketType packetType;
                switch(type) {
                    case 0:
                        packetType = ClientboundPackets1_17.TITLE_TEXT;
                        break;
                    case 1:
                        packetType = ClientboundPackets1_17.TITLE_SUBTITLE;
                        break;
                    case 2:
                        packetType = ClientboundPackets1_17.ACTIONBAR;
                        break;
                    case 3:
                        packetType = ClientboundPackets1_17.TITLE_TIMES;
                        break;
                    case 4:
                        packetType = ClientboundPackets1_17.CLEAR_TITLES;
                        // Reset times
                        wrapper.write(Type.BOOLEAN, false);
                        break;
                    case 5:
                        packetType = ClientboundPackets1_17.CLEAR_TITLES;
                        // Reset times
                        wrapper.write(Type.BOOLEAN, true);
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid title type received: " + type);
                }
                wrapper.setId(packetType.getId());
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Strength
            map(Type.FLOAT);
            handler(wrapper -> {
                // Collection length is now a var int
                wrapper.write(Type.VAR_INT, wrapper.read(Type.INT));
            });
        }
    });
    registerClientbound(ClientboundPackets1_16_2.SPAWN_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION1_14);
            handler(wrapper -> {
                // Angle (which Mojang just forgot to write to the buffer, lol)
                wrapper.write(Type.FLOAT, 0f);
            });
        }
    });
    registerServerbound(ServerboundPackets1_17.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Locale
            map(Type.STRING);
            // View distance
            map(Type.BYTE);
            // Chat mode
            map(Type.VAR_INT);
            // Chat colors
            map(Type.BOOLEAN);
            // Chat flags
            map(Type.UNSIGNED_BYTE);
            // Main hand
            map(Type.VAR_INT);
            handler(wrapper -> {
                // Text filtering
                wrapper.read(Type.BOOLEAN);
            });
        }
    });
}
Also used : MappingData(com.viaversion.viaversion.api.data.MappingData) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.InventoryPackets) ParticleType(com.viaversion.viaversion.api.type.types.minecraft.ParticleType) InventoryAcknowledgements(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.InventoryAcknowledgements) WorldPackets(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.WorldPackets) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) Types1_17(com.viaversion.viaversion.api.type.types.version.Types1_17) MappingDataBase(com.viaversion.viaversion.api.data.MappingDataBase) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) ServerboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2) 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) EntityRewriter(com.viaversion.viaversion.api.rewriter.EntityRewriter) EntityPackets(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.EntityPackets) StatisticsRewriter(com.viaversion.viaversion.rewriter.StatisticsRewriter) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType)

Aggregations

UserConnection (com.viaversion.viaversion.api.connection.UserConnection)2 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)2 Entity1_17Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types)2 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)2 EntityRewriter (com.viaversion.viaversion.api.rewriter.EntityRewriter)2 ItemRewriter (com.viaversion.viaversion.api.rewriter.ItemRewriter)2 Type (com.viaversion.viaversion.api.type.Type)2 EntityTrackerBase (com.viaversion.viaversion.data.entity.EntityTrackerBase)2 ClientboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2)2 ServerboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2)2 StatisticsRewriter (com.viaversion.viaversion.rewriter.StatisticsRewriter)2 TagRewriter (com.viaversion.viaversion.rewriter.TagRewriter)2 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)1 BackwardsProtocol (com.viaversion.viabackwards.api.BackwardsProtocol)1 BackwardsMappings (com.viaversion.viabackwards.api.data.BackwardsMappings)1 SoundRewriter (com.viaversion.viabackwards.api.rewriters.SoundRewriter)1 TranslatableRewriter (com.viaversion.viabackwards.api.rewriters.TranslatableRewriter)1 BlockItemPackets1_17 (com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.BlockItemPackets1_17)1 EntityPackets1_17 (com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.packets.EntityPackets1_17)1 PingRequests (com.viaversion.viabackwards.protocol.protocol1_16_4to1_17.storage.PingRequests)1