Search in sources :

Example 11 with ClientWorld

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

the class Protocol1_11To1_10 method registerPackets.

@Override
protected void registerPackets() {
    entityRewriter.register();
    itemRewriter.register();
    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);
            // Track Entity
            handler(entityRewriter.objectTrackerHandler());
        }
    });
    registerClientbound(ClientboundPackets1_9_3.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.UNSIGNED_BYTE, 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_9.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    // Change Type :)
                    int type = wrapper.get(Type.VAR_INT, 1);
                    Entity1_11Types.EntityType entType = MetadataRewriter1_11To1_10.rewriteEntityType(type, wrapper.get(Types1_9.METADATA_LIST, 0));
                    if (entType != null) {
                        wrapper.set(Type.VAR_INT, 1, entType.getId());
                        // Register Type ID
                        wrapper.user().getEntityTracker(Protocol1_11To1_10.class).addEntity(entityId, entType);
                        entityRewriter.handleMetadata(entityId, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
                    }
                }
            });
        }
    });
    new SoundRewriter(this, this::getNewSoundId).registerSound(ClientboundPackets1_9_3.SOUND);
    registerClientbound(ClientboundPackets1_9_3.COLLECT_ITEM, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Collected entity id
            map(Type.VAR_INT);
            // 1 - Collector entity id
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 2 - Pickup Count
                    wrapper.write(Type.VAR_INT, 1);
                }
            });
        }
    });
    entityRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
    registerClientbound(ClientboundPackets1_9_3.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - x
            map(Type.DOUBLE);
            // 2 - y
            map(Type.DOUBLE);
            // 3 - z
            map(Type.DOUBLE);
            // 4 - yaw
            map(Type.BYTE);
            // 5 - pitch
            map(Type.BYTE);
            // 6 - onGround
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityID = wrapper.get(Type.VAR_INT, 0);
                    if (Via.getConfig().isHologramPatch()) {
                        EntityTracker1_11 tracker = wrapper.user().getEntityTracker(Protocol1_11To1_10.class);
                        if (tracker.isHologram(entityID)) {
                            Double newValue = wrapper.get(Type.DOUBLE, 1);
                            newValue -= (Via.getConfig().getHologramYOffset());
                            wrapper.set(Type.DOUBLE, 1, newValue);
                        }
                    }
                }
            });
        }
    });
    entityRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    registerClientbound(ClientboundPackets1_9_3.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.VAR_INT, 0);
                    // Handle the new ActionBar
                    if (action >= 2) {
                        wrapper.set(Type.VAR_INT, 0, action + 1);
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action ID
            map(Type.UNSIGNED_BYTE);
            // 2 - Action Param
            map(Type.UNSIGNED_BYTE);
            // 3 - Block Type
            map(Type.VAR_INT);
            // Cheap hack to ensure it's always right block
            handler(new PacketHandler() {

                @Override
                public void handle(final PacketWrapper actionWrapper) throws Exception {
                    if (Via.getConfig().isPistonAnimationPatch()) {
                        int id = actionWrapper.get(Type.VAR_INT, 0);
                        if (id == 33 || id == 29) {
                            actionWrapper.cancel();
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Action
            map(Type.UNSIGNED_BYTE);
            // 2 - NBT data
            map(Type.NBT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    CompoundTag tag = wrapper.get(Type.NBT, 0);
                    if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 1)
                        EntityIdRewriter.toClientSpawner(tag);
                    if (tag.contains("id"))
                        // Handle new identifier
                        ((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier((String) tag.get("id").getValue()));
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk chunk = wrapper.passthrough(new Chunk1_9_3_4Type(clientWorld));
                    if (chunk.getBlockEntities() == null)
                        return;
                    for (CompoundTag tag : chunk.getBlockEntities()) {
                        if (tag.contains("id")) {
                            String identifier = ((StringTag) tag.get("id")).getValue();
                            if (identifier.equals("MobSpawner")) {
                                EntityIdRewriter.toClientSpawner(tag);
                            }
                            // Handle new identifier
                            ((StringTag) tag.get("id")).setValue(BlockEntityRewriter.toNewIdentifier(identifier));
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.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(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 1);
                clientChunks.setEnvironment(dimensionId);
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 0);
                clientWorld.setEnvironment(dimensionId);
            });
        }
    });
    this.registerClientbound(ClientboundPackets1_9_3.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // effectID
            this.map(Type.INT);
            // pos
            this.map(Type.POSITION);
            // effectData
            this.map(Type.INT);
            // serverwide / global
            this.map(Type.BOOLEAN);
            handler(packetWrapper -> {
                int effectID = packetWrapper.get(Type.INT, 0);
                if (effectID == 2002) {
                    int data = packetWrapper.get(Type.INT, 1);
                    boolean isInstant = false;
                    Pair<Integer, Boolean> newData = PotionColorMapping.getNewData(data);
                    if (newData == null) {
                        Via.getPlatform().getLogger().warning("Received unknown 1.11 -> 1.10.2 potion data (" + data + ")");
                        data = 0;
                    } else {
                        data = newData.key();
                        isInstant = newData.value();
                    }
                    if (isInstant) {
                        packetWrapper.set(Type.INT, 0, 2007);
                    }
                    packetWrapper.set(Type.INT, 1, data);
                }
            });
        }
    });
    /*
            INCOMING PACKETS
        */
    registerServerbound(ServerboundPackets1_9_3.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Location
            map(Type.POSITION);
            // 1 - Face
            map(Type.VAR_INT);
            // 2 - Hand
            map(Type.VAR_INT);
            map(Type.FLOAT, toOldByte);
            map(Type.FLOAT, toOldByte);
            map(Type.FLOAT, toOldByte);
        }
    });
    registerServerbound(ServerboundPackets1_9_3.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Message
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 100 character limit on older servers
                    String msg = wrapper.get(Type.STRING, 0);
                    if (msg.length() > 100) {
                        wrapper.set(Type.STRING, 0, msg.substring(0, 100));
                    }
                }
            });
        }
    });
}
Also used : EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ValueTransformer(com.viaversion.viaversion.api.protocol.remapper.ValueTransformer) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) MetadataRewriter1_11To1_10(com.viaversion.viaversion.protocols.protocol1_11to1_10.metadata.MetadataRewriter1_11To1_10) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Types1_9(com.viaversion.viaversion.api.type.types.version.Types1_9) PotionColorMapping(com.viaversion.viaversion.protocols.protocol1_11to1_10.data.PotionColorMapping) Pair(com.viaversion.viaversion.util.Pair) EntityTracker1_11(com.viaversion.viaversion.protocols.protocol1_11to1_10.storage.EntityTracker1_11) 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) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_11to1_10.packets.InventoryPackets) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) Entity1_11Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) Pair(com.viaversion.viaversion.util.Pair)

Example 12 with ClientWorld

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

the class Protocol1_12To1_11_1 method registerPackets.

@Override
protected void registerPackets() {
    metadataRewriter.register();
    itemRewriter.register();
    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);
            // Track Entity
            handler(metadataRewriter.objectTrackerHandler());
        }
    });
    registerClientbound(ClientboundPackets1_9_3.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_12.METADATA_LIST);
            // Track mob and rewrite metadata
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_12.METADATA_LIST));
        }
    });
    registerClientbound(ClientboundPackets1_9_3.CHAT_MESSAGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (!Via.getConfig().is1_12NBTArrayFix())
                        return;
                    try {
                        JsonElement obj = Protocol1_9To1_8.FIX_JSON.transform(null, wrapper.passthrough(Type.COMPONENT).toString());
                        TranslateRewriter.toClient(obj, wrapper.user());
                        ChatItemRewriter.toClient(obj, wrapper.user());
                        wrapper.set(Type.COMPONENT, 0, obj);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk chunk = wrapper.passthrough(type);
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection section = chunk.getSections()[i];
                        if (section == null)
                            continue;
                        for (int y = 0; y < 16; y++) {
                            for (int z = 0; z < 16; z++) {
                                for (int x = 0; x < 16; x++) {
                                    int block = section.getBlockWithoutData(x, y, z);
                                    // Is this a bed?
                                    if (block == 26) {
                                        // NBT -> { color:14, x:132, y:64, z:222, id:"minecraft:bed" } (Debug output)
                                        CompoundTag tag = new CompoundTag();
                                        // Set color to red (Default in previous versions)
                                        tag.put("color", new IntTag(14));
                                        tag.put("x", new IntTag(x + (chunk.getX() << 4)));
                                        tag.put("y", new IntTag(y + (i << 4)));
                                        tag.put("z", new IntTag(z + (chunk.getZ() << 4)));
                                        tag.put("id", new StringTag("minecraft:bed"));
                                        // Add a fake block entity
                                        chunk.getBlockEntities().add(tag);
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    });
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_12.METADATA_LIST);
    registerClientbound(ClientboundPackets1_9_3.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            map(Type.UNSIGNED_BYTE);
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 1);
                clientChunks.setEnvironment(dimensionId);
            });
        }
    });
    registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 0);
                clientWorld.setEnvironment(dimensionId);
            });
        }
    });
    new SoundRewriter(this, this::getNewSoundId).registerSound(ClientboundPackets1_9_3.SOUND);
    // New packet at 0x01
    cancelServerbound(ServerboundPackets1_12.PREPARE_CRAFTING_GRID);
    // Client Settings (max length changed)
    registerServerbound(ServerboundPackets1_12.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Locale
            map(Type.STRING);
            // 1 - view distance
            map(Type.BYTE);
            // 2 - chat mode
            map(Type.VAR_INT);
            // 3 - chat colors
            map(Type.BOOLEAN);
            // 4 - chat flags
            map(Type.UNSIGNED_BYTE);
            // 5 - main hand
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // As part of the fix for MC-111054, the max length of
                    // the locale was raised to 16 (from 7), and the client
                    // now makes sure that resource packs have names in that
                    // length.  However, for older servers, it is still 7,
                    // and thus the server will reject it (and the client
                    // won't know that the pack's invalid).
                    // The fix is to just silently lower the length.  The
                    // server doesn't actually use the locale anywhere, so
                    // this is fine.
                    String locale = wrapper.get(Type.STRING, 0);
                    if (locale.length() > 7) {
                        wrapper.set(Type.STRING, 0, locale.substring(0, 7));
                    }
                }
            });
        }
    });
    // New packet at 0x17
    cancelServerbound(ServerboundPackets1_12.RECIPE_BOOK_DATA);
    // New packet 0x19
    cancelServerbound(ServerboundPackets1_12.ADVANCEMENT_TAB);
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Types1_12(com.viaversion.viaversion.api.type.types.version.Types1_12) JsonElement(com.google.gson.JsonElement) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) MetadataRewriter1_12To1_11_1(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.metadata.MetadataRewriter1_12To1_11_1) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ItemRewriter(com.viaversion.viaversion.api.rewriter.ItemRewriter) Entity1_12Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types) EntityRewriter(com.viaversion.viaversion.rewriter.EntityRewriter) ServerboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3) ViaProviders(com.viaversion.viaversion.api.platform.providers.ViaProviders) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) InventoryPackets(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.packets.InventoryPackets) Via(com.viaversion.viaversion.api.Via) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) InventoryQuickMoveProvider(com.viaversion.viaversion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) JsonElement(com.google.gson.JsonElement) SoundRewriter(com.viaversion.viaversion.rewriter.SoundRewriter) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Example 13 with ClientWorld

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

the class WorldPackets method register.

public static void register(Protocol protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION);
    protocol.registerClientbound(ClientboundPackets1_13.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.passthrough(new Chunk1_13Type(clientWorld));
                    for (ChunkSection section : chunk.getSections()) {
                        if (section == null)
                            continue;
                        for (int i = 0; i < section.getPaletteSize(); i++) {
                            section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(section.getPaletteEntry(i)));
                        }
                    }
                }
            });
        }
    });
    blockRewriter.registerBlockAction(ClientboundPackets1_13.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_13.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_13.MULTI_BLOCK_CHANGE);
    protocol.registerClientbound(ClientboundPackets1_13.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Effect Id
            map(Type.INT);
            // Location
            map(Type.POSITION);
            // Data
            map(Type.INT);
            handler(wrapper -> {
                int id = wrapper.get(Type.INT, 0);
                if (id == 2000) {
                    // Smoke
                    int data = wrapper.get(Type.INT, 1);
                    switch(data) {
                        case // North
                        1:
                            // North
                            wrapper.set(Type.INT, 1, 2);
                            break;
                        // North-West
                        case 0:
                        // West
                        case 3:
                        case // South-West
                        6:
                            // West
                            wrapper.set(Type.INT, 1, 4);
                            break;
                        // North-East
                        case 2:
                        // East
                        case 5:
                        case // South-East
                        8:
                            // East
                            wrapper.set(Type.INT, 1, 5);
                            break;
                        case // South
                        7:
                            // South
                            wrapper.set(Type.INT, 1, 3);
                            break;
                        default:
                            // Self and other directions
                            // Down
                            wrapper.set(Type.INT, 1, 0);
                            break;
                    }
                } else if (id == 1010) {
                    // Play record
                    wrapper.set(Type.INT, 1, protocol.getMappingData().getNewItemId(wrapper.get(Type.INT, 1)));
                } else if (id == 2001) {
                    // Block break + block break sound
                    wrapper.set(Type.INT, 1, protocol.getMappingData().getNewBlockStateId(wrapper.get(Type.INT, 1)));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.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(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // Store the player
                    ClientWorld clientChunks = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 1);
                    clientChunks.setEnvironment(dimensionId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.RESPAWN, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 0);
                    clientWorld.setEnvironment(dimensionId);
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Protocol(com.viaversion.viaversion.api.protocol.Protocol) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 14 with ClientWorld

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

the class Protocol1_10To1_9_3_4 method registerPackets.

@Override
protected void registerPackets() {
    itemRewriter.register();
    // Named sound effect
    registerClientbound(ClientboundPackets1_9_3.NAMED_SOUND, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sound name
            map(Type.STRING);
            // 1 - Sound Category
            map(Type.VAR_INT);
            // 2 - x
            map(Type.INT);
            // 3 - y
            map(Type.INT);
            // 4 - z
            map(Type.INT);
            // 5 - Volume
            map(Type.FLOAT);
            // 6 - Pitch
            map(Type.UNSIGNED_BYTE, TO_NEW_PITCH);
        }
    });
    // Sound effect
    registerClientbound(ClientboundPackets1_9_3.SOUND, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sound name
            map(Type.VAR_INT);
            // 1 - Sound Category
            map(Type.VAR_INT);
            // 2 - x
            map(Type.INT);
            // 3 - y
            map(Type.INT);
            // 4 - z
            map(Type.INT);
            // 5 - Volume
            map(Type.FLOAT);
            // 6 - Pitch
            map(Type.UNSIGNED_BYTE, TO_NEW_PITCH);
            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, getNewSoundId(id));
                }
            });
        }
    });
    // Metadata packet
    registerClientbound(ClientboundPackets1_9_3.ENTITY_METADATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Metadata list
            map(Types1_9.METADATA_LIST, TRANSFORM_METADATA);
        }
    });
    // Spawn Mob
    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.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, TRANSFORM_METADATA);
        }
    });
    // Spawn Player
    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, TRANSFORM_METADATA);
        }
    });
    // Join Game
    registerClientbound(ClientboundPackets1_9_3.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(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 1);
                    clientWorld.setEnvironment(dimensionId);
                }
            });
        }
    });
    // Respawn
    registerClientbound(ClientboundPackets1_9_3.RESPAWN, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 0);
                    clientWorld.setEnvironment(dimensionId);
                }
            });
        }
    });
    // Chunk Data
    registerClientbound(ClientboundPackets1_9_3.CHUNK_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    Chunk chunk = wrapper.passthrough(new Chunk1_9_3_4Type(clientWorld));
                    if (Via.getConfig().isReplacePistons()) {
                        int replacementId = Via.getConfig().getPistonReplacementId();
                        for (ChunkSection section : chunk.getSections()) {
                            if (section == null)
                                continue;
                            section.replacePaletteEntry(36, replacementId);
                        }
                    }
                }
            });
        }
    });
    // Packet Send ResourcePack
    registerClientbound(ClientboundPackets1_9_3.RESOURCE_PACK, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - URL
            map(Type.STRING);
            // 1 - Hash
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ResourcePackTracker tracker = wrapper.user().get(ResourcePackTracker.class);
                    // Store the hash for resourcepack status
                    tracker.setLastHash(wrapper.get(Type.STRING, 1));
                }
            });
        }
    });
    // Packet ResourcePack status
    registerServerbound(ServerboundPackets1_9_3.RESOURCE_PACK_STATUS, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ResourcePackTracker tracker = wrapper.user().get(ResourcePackTracker.class);
                    wrapper.write(Type.STRING, tracker.getLastHash());
                    wrapper.write(Type.VAR_INT, wrapper.read(Type.VAR_INT));
                }
            });
        }
    });
}
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) ResourcePackTracker(com.viaversion.viaversion.protocols.protocol1_10to1_9_3.storage.ResourcePackTracker) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 15 with ClientWorld

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

the class PlayerPackets method register.

public static void register(Protocol1_9To1_8 protocol) {
    protocol.registerClientbound(ClientboundPackets1_8.CHAT_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Chat Message (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 1 - Chat Positon
            map(Type.BYTE);
            handler(wrapper -> {
                try {
                    JsonObject obj = (JsonObject) wrapper.get(Type.COMPONENT, 0);
                    ChatRewriter.toClient(obj, wrapper.user());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TAB_LIST, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Header
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 1 - Footer
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.DISCONNECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Reason
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TITLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            // We only handle if the title or subtitle is set then just write through.
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 0 || action == 1) {
                    Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                }
            });
        // Everything else is handled.
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player X
            map(Type.DOUBLE);
            // 1 - Player Y
            map(Type.DOUBLE);
            // 2 - Player Z
            map(Type.DOUBLE);
            // 3 - Player Yaw
            map(Type.FLOAT);
            // 4 - Player Pitch
            map(Type.FLOAT);
            // 5 - Player Flags
            map(Type.BYTE);
            handler(wrapper -> {
                // 6 - Teleport ID was added
                wrapper.write(Type.VAR_INT, 0);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.TEAMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Team Name
            map(Type.STRING);
            // 1 - Mode
            map(Type.BYTE);
            handler(wrapper -> {
                // Mode
                byte mode = wrapper.get(Type.BYTE, 0);
                if (mode == 0 || mode == 2) {
                    // Display Name
                    wrapper.passthrough(Type.STRING);
                    // Prefix
                    wrapper.passthrough(Type.STRING);
                    // Suffix
                    wrapper.passthrough(Type.STRING);
                    // Friendly Fire
                    wrapper.passthrough(Type.BYTE);
                    // Name tag visibility
                    wrapper.passthrough(Type.STRING);
                    wrapper.write(Type.STRING, Via.getConfig().isPreventCollision() ? "never" : "");
                    // Colour
                    wrapper.passthrough(Type.BYTE);
                }
                if (mode == 0 || mode == 3 || mode == 4) {
                    // Players
                    String[] players = wrapper.passthrough(Type.STRING_ARRAY);
                    final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    String myName = wrapper.user().getProtocolInfo().getUsername();
                    String teamName = wrapper.get(Type.STRING, 0);
                    for (String player : players) {
                        if (entityTracker.isAutoTeam() && player.equalsIgnoreCase(myName)) {
                            if (mode == 4) {
                                // since removing add to auto team
                                // Workaround for packet order issue
                                wrapper.send(Protocol1_9To1_8.class);
                                wrapper.cancel();
                                entityTracker.sendTeamPacket(true, true);
                                entityTracker.setCurrentTeam("viaversion");
                            } else {
                                // since adding remove from auto team
                                entityTracker.sendTeamPacket(false, true);
                                entityTracker.setCurrentTeam(teamName);
                            }
                        }
                    }
                }
                if (mode == 1) {
                    // Remove team
                    final EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    String teamName = wrapper.get(Type.STRING, 0);
                    if (entityTracker.isAutoTeam() && teamName.equals(entityTracker.getCurrentTeam())) {
                        // team was removed
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_9To1_8.class);
                        wrapper.cancel();
                        entityTracker.sendTeamPacket(true, true);
                        entityTracker.setCurrentTeam("viaversion");
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Player ID
            map(Type.INT);
            // Parse this info
            handler(wrapper -> {
                int entityId = wrapper.get(Type.INT, 0);
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                tracker.addEntity(entityId, Entity1_10Types.EntityType.PLAYER);
                tracker.setClientEntityId(entityId);
            });
            // 1 - Player Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Player Dimension
            map(Type.BYTE);
            // 3 - World Difficulty
            map(Type.UNSIGNED_BYTE);
            // 4 - Max Players (Tab)
            map(Type.UNSIGNED_BYTE);
            // 5 - Level Type
            map(Type.STRING);
            // 6 - Reduced Debug info
            map(Type.BOOLEAN);
            handler(wrapper -> {
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                // Set player gamemode
                tracker.setGameMode(GameMode.getById(wrapper.get(Type.UNSIGNED_BYTE, 0)));
            });
            // Track player's dimension
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.BYTE, 0);
                clientWorld.setEnvironment(dimensionId);
            });
            // Gotta fake their op
            handler(wrapper -> {
                CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                provider.sendPermission(wrapper.user());
            });
            // Scoreboard will be cleared when join game is received
            handler(wrapper -> {
                EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                if (Via.getConfig().isAutoTeam()) {
                    entityTracker.setAutoTeam(true);
                    // Workaround for packet order issue
                    wrapper.send(Protocol1_9To1_8.class);
                    wrapper.cancel();
                    entityTracker.sendTeamPacket(true, true);
                    entityTracker.setCurrentTeam("viaversion");
                } else {
                    entityTracker.setAutoTeam(false);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLAYER_INFO, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action
            map(Type.VAR_INT);
            // 1 - Player Count
            map(Type.VAR_INT);
            // Due to this being a complex data structure we just use a handler.
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                int count = wrapper.get(Type.VAR_INT, 1);
                for (int i = 0; i < count; i++) {
                    // Player UUID
                    wrapper.passthrough(Type.UUID);
                    if (action == 0) {
                        // add player
                        // Player Name
                        wrapper.passthrough(Type.STRING);
                        int properties = wrapper.passthrough(Type.VAR_INT);
                        // loop through properties
                        for (int j = 0; j < properties; j++) {
                            // name
                            wrapper.passthrough(Type.STRING);
                            // value
                            wrapper.passthrough(Type.STRING);
                            boolean isSigned = wrapper.passthrough(Type.BOOLEAN);
                            if (isSigned) {
                                // signature
                                wrapper.passthrough(Type.STRING);
                            }
                        }
                        // gamemode
                        wrapper.passthrough(Type.VAR_INT);
                        // ping
                        wrapper.passthrough(Type.VAR_INT);
                        boolean hasDisplayName = wrapper.passthrough(Type.BOOLEAN);
                        if (hasDisplayName) {
                            // display name
                            Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                        }
                    } else if ((action == 1) || (action == 2)) {
                        // update gamemode || update latency
                        wrapper.passthrough(Type.VAR_INT);
                    } else if (action == 3) {
                        // update display name
                        boolean hasDisplayName = wrapper.passthrough(Type.BOOLEAN);
                        if (hasDisplayName) {
                            // display name
                            Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
                        }
                    } else if (action == 4) {
                    // remove player
                    // no fields
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel Name
            map(Type.STRING);
            handler(wrapper -> {
                String name = wrapper.get(Type.STRING, 0);
                if (name.equalsIgnoreCase("MC|BOpen")) {
                    // Not used anymore
                    wrapper.read(Type.REMAINING_BYTES);
                    wrapper.write(Type.VAR_INT, 0);
                }
                if (name.equalsIgnoreCase("MC|TrList")) {
                    // ID
                    wrapper.passthrough(Type.INT);
                    Short size = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    for (int i = 0; i < size; ++i) {
                        Item item1 = wrapper.passthrough(Type.ITEM);
                        ItemRewriter.toClient(item1);
                        Item item2 = wrapper.passthrough(Type.ITEM);
                        ItemRewriter.toClient(item2);
                        boolean present = wrapper.passthrough(Type.BOOLEAN);
                        if (present) {
                            Item item3 = wrapper.passthrough(Type.ITEM);
                            ItemRewriter.toClient(item3);
                        }
                        wrapper.passthrough(Type.BOOLEAN);
                        wrapper.passthrough(Type.INT);
                        wrapper.passthrough(Type.INT);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Dimension
            map(Type.INT);
            // 1 - Difficulty
            map(Type.UNSIGNED_BYTE);
            // 2 - GameMode
            map(Type.UNSIGNED_BYTE);
            // 3 - Level Type
            map(Type.STRING);
            // Track player's dimension
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    int dimensionId = wrapper.get(Type.INT, 0);
                    clientWorld.setEnvironment(dimensionId);
                }
            });
            handler(wrapper -> {
                // Client unloads chunks on respawn
                wrapper.user().get(ClientChunks.class).getLoadedChunks().clear();
                int gamemode = wrapper.get(Type.UNSIGNED_BYTE, 0);
                EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                tracker.setGameMode(GameMode.getById(gamemode));
            });
            // Fake permissions to get Commandblocks working
            handler(wrapper -> {
                CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                provider.sendPermission(wrapper.user());
                provider.unloadChunks(wrapper.user());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.GAME_EVENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Reason
            map(Type.UNSIGNED_BYTE);
            // 1 - Value
            map(Type.FLOAT);
            handler(wrapper -> {
                short reason = wrapper.get(Type.UNSIGNED_BYTE, 0);
                if (reason == 3) {
                    // Change gamemode
                    int gamemode = wrapper.get(Type.FLOAT, 0).intValue();
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.setGameMode(GameMode.getById(gamemode));
                } else if (reason == 4) {
                    // Open credits screen
                    wrapper.set(Type.FLOAT, 0, 1F);
                }
            });
        }
    });
    /* Removed packets */
    protocol.registerClientbound(ClientboundPackets1_8.SET_COMPRESSION, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                wrapper.cancel();
                CompressionProvider provider = Via.getManager().getProviders().get(CompressionProvider.class);
                provider.handlePlayCompression(wrapper.user(), wrapper.read(Type.VAR_INT));
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.TAB_COMPLETE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Requested Command
            map(Type.STRING);
            // 1 - Is Command Block
            map(Type.BOOLEAN, Type.NOTHING);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.CLIENT_SETTINGS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - locale
            map(Type.STRING);
            // 1 - View Distance
            map(Type.BYTE);
            // 2 - Chat Mode
            map(Type.VAR_INT, Type.BYTE);
            // 3 - If Chat Colours on
            map(Type.BOOLEAN);
            // 4 - Skin Parts
            map(Type.UNSIGNED_BYTE);
            handler(wrapper -> {
                int hand = wrapper.read(Type.VAR_INT);
                if (Via.getConfig().isLeftHandedHandling()) {
                    // Add 0x80 if left handed
                    if (hand == 0)
                        wrapper.set(Type.UNSIGNED_BYTE, 0, (short) (wrapper.get(Type.UNSIGNED_BYTE, 0).intValue() | 0x80));
                }
                wrapper.sendToServer(Protocol1_9To1_8.class);
                wrapper.cancel();
                Via.getManager().getProviders().get(MainHandProvider.class).setMainHand(wrapper.user(), hand);
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.ANIMATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Hand
            map(Type.VAR_INT, Type.NOTHING);
        }
    });
    protocol.cancelServerbound(ServerboundPackets1_9.TELEPORT_CONFIRM);
    protocol.cancelServerbound(ServerboundPackets1_9.VEHICLE_MOVE);
    protocol.cancelServerbound(ServerboundPackets1_9.STEER_BOAT);
    protocol.registerServerbound(ServerboundPackets1_9.PLUGIN_MESSAGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Channel Name
            map(Type.STRING);
            handler(wrapper -> {
                String name = wrapper.get(Type.STRING, 0);
                if (name.equalsIgnoreCase("MC|BSign")) {
                    Item item = wrapper.passthrough(Type.ITEM);
                    if (item != null) {
                        // Written Book
                        item.setIdentifier(387);
                        ItemRewriter.rewriteBookToServer(item);
                    }
                }
                if (name.equalsIgnoreCase("MC|AutoCmd")) {
                    wrapper.set(Type.STRING, 0, "MC|AdvCdm");
                    wrapper.write(Type.BYTE, (byte) 0);
                    // X
                    wrapper.passthrough(Type.INT);
                    // Y
                    wrapper.passthrough(Type.INT);
                    // Z
                    wrapper.passthrough(Type.INT);
                    // Command
                    wrapper.passthrough(Type.STRING);
                    // Flag
                    wrapper.passthrough(Type.BOOLEAN);
                    wrapper.clearInputBuffer();
                }
                if (name.equalsIgnoreCase("MC|AdvCmd")) {
                    wrapper.set(Type.STRING, 0, "MC|AdvCdm");
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.CLIENT_STATUS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Action ID
            map(Type.VAR_INT);
            handler(wrapper -> {
                int action = wrapper.get(Type.VAR_INT, 0);
                if (action == 2) {
                    // cancel any blocking >.>
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    if (tracker.isBlocking()) {
                        if (!Via.getConfig().isShowShieldWhenSwordInHand()) {
                            tracker.setSecondHand(null);
                        }
                        tracker.setBlocking(false);
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - X
            map(Type.DOUBLE);
            // 1 - Y
            map(Type.DOUBLE);
            // 2 - Z
            map(Type.DOUBLE);
            // 3 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_POSITION_AND_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - X
            map(Type.DOUBLE);
            // 1 - Y
            map(Type.DOUBLE);
            // 2 - Z
            map(Type.DOUBLE);
            // 3 - Yaw
            map(Type.FLOAT);
            // 4 - Pitch
            map(Type.FLOAT);
            // 5 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_ROTATION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Yaw
            map(Type.FLOAT);
            // 1 - Pitch
            map(Type.FLOAT);
            // 2 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_MOVEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Ground
            map(Type.BOOLEAN);
            handler(new PlayerMovementMapper());
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PlayerMovementMapper(com.viaversion.viaversion.protocols.protocol1_9to1_8.PlayerMovementMapper) JsonObject(com.google.gson.JsonObject) GameMode(com.viaversion.viaversion.protocols.protocol1_9to1_8.chat.GameMode) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.chat.ChatRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Via(com.viaversion.viaversion.api.Via) Entity1_10Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_10Types) Item(com.viaversion.viaversion.api.minecraft.item.Item) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) MainHandProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.MainHandProvider) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) ServerboundPackets1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.ServerboundPackets1_9) CompressionProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CompressionProvider) ClientboundPackets1_8(com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8) ItemRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) PlayerMovementMapper(com.viaversion.viaversion.protocols.protocol1_9to1_8.PlayerMovementMapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) JsonObject(com.google.gson.JsonObject) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) CompressionProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CompressionProvider) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Aggregations

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