Search in sources :

Example 1 with IntArrayList

use of com.viaversion.viaversion.libs.fastutil.ints.IntArrayList 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)

Aggregations

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 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 RegistryType (com.viaversion.viaversion.api.minecraft.RegistryType)1 TagData (com.viaversion.viaversion.api.minecraft.TagData)1 Entity1_17Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types)1 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 EntityRewriter (com.viaversion.viaversion.api.rewriter.EntityRewriter)1 ItemRewriter (com.viaversion.viaversion.api.rewriter.ItemRewriter)1 Type (com.viaversion.viaversion.api.type.Type)1 EntityTrackerBase (com.viaversion.viaversion.data.entity.EntityTrackerBase)1 IntArrayList (com.viaversion.viaversion.libs.fastutil.ints.IntArrayList)1 IntList (com.viaversion.viaversion.libs.fastutil.ints.IntList)1