Search in sources :

Example 36 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_15Type method read.

@Override
public Chunk read(ByteBuf input) throws Exception {
    int chunkX = input.readInt();
    int chunkZ = input.readInt();
    boolean fullChunk = input.readBoolean();
    int primaryBitmask = Type.VAR_INT.readPrimitive(input);
    CompoundTag heightMap = Type.NBT.read(input);
    int[] biomeData = fullChunk ? new int[1024] : null;
    if (fullChunk) {
        for (int i = 0; i < 1024; i++) {
            biomeData[i] = input.readInt();
        }
    }
    // data size in bytes
    Type.VAR_INT.readPrimitive(input);
    // Read sections
    ChunkSection[] sections = new ChunkSection[16];
    for (int i = 0; i < 16; i++) {
        // Section not set
        if ((primaryBitmask & (1 << i)) == 0)
            continue;
        short nonAirBlocksCount = input.readShort();
        ChunkSection section = Types1_13.CHUNK_SECTION.read(input);
        section.setNonAirBlocksCount(nonAirBlocksCount);
        sections[i] = section;
    }
    List<CompoundTag> nbtData = new ArrayList<>(Arrays.asList(Type.NBT_ARRAY.read(input)));
    // Read all the remaining bytes (workaround for #681)
    if (input.readableBytes() > 0) {
        byte[] array = Type.REMAINING_BYTES.read(input);
        if (Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("Found " + array.length + " more bytes than expected while reading the chunk: " + chunkX + "/" + chunkZ);
        }
    }
    return new BaseChunk(chunkX, chunkZ, fullChunk, false, primaryBitmask, sections, biomeData, heightMap, nbtData);
}
Also used : BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) ArrayList(java.util.ArrayList) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 37 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_13Type method write.

@Override
public void write(ByteBuf output, ClientWorld world, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());
    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
    ByteBuf buf = output.alloc().buffer();
    try {
        for (int i = 0; i < 16; i++) {
            ChunkSection section = chunk.getSections()[i];
            // Section not set
            if (section == null)
                continue;
            Types1_13.CHUNK_SECTION.write(buf, section);
            section.getLight().writeBlockLight(buf);
            // No sky light, we're done here.
            if (!section.getLight().hasSkyLight())
                continue;
            section.getLight().writeSkyLight(buf);
        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes() + (chunk.isBiomeData() ? 1024 : 0));
        output.writeBytes(buf);
    } finally {
        // release buffer
        buf.release();
    }
    // Write biome data
    if (chunk.isBiomeData()) {
        for (int value : chunk.getBiomeData()) {
            output.writeInt(value);
        }
    }
    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(new CompoundTag[0]));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 38 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol protocol) {
    protocol.registerClientbound(ClientboundPackets1_8.UPDATE_SIGN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sign Position
            map(Type.POSITION);
            // 1 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 2 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 3 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 4 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Effect ID
            map(Type.INT);
            // 1 - Position
            map(Type.POSITION);
            // 2 - Data
            map(Type.INT);
            // 3 - Disable relative volume
            map(Type.BOOLEAN);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    id = Effect.getNewId(id);
                    wrapper.set(Type.INT, 0, id);
                }
            });
            // Rewrite potion effect as it changed to use a dynamic registry
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.INT, 0);
                    if (id == 2002) {
                        int data = wrapper.get(Type.INT, 1);
                        int newData = ItemRewriter.getNewEffectID(data);
                        wrapper.set(Type.INT, 1, newData);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.NAMED_SOUND, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sound Name
            map(Type.STRING);
            // 1 - Sound Category ID
            // Everything else get's written through
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String name = wrapper.get(Type.STRING, 0);
                    SoundEffect effect = SoundEffect.getByName(name);
                    int catid = 0;
                    String newname = name;
                    if (effect != null) {
                        catid = effect.getCategory().getId();
                        newname = effect.getNewName();
                    }
                    wrapper.set(Type.STRING, 0, newname);
                    // Write Category ID
                    wrapper.write(Type.VAR_INT, catid);
                    if (effect != null && effect.isBreaksound()) {
                        EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        // Position X
                        int x = wrapper.passthrough(Type.INT);
                        // Position Y
                        int y = wrapper.passthrough(Type.INT);
                        // Position Z
                        int z = wrapper.passthrough(Type.INT);
                        if (tracker.interactedBlockRecently((int) Math.floor(x / 8.0), (int) Math.floor(y / 8.0), (int) Math.floor(z / 8.0))) {
                            wrapper.cancel();
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.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);
                    ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
                    Chunk chunk = wrapper.read(new Chunk1_8Type(clientWorld));
                    long chunkHash = ClientChunks.toLong(chunk.getX(), chunk.getZ());
                    // Check if the chunk should be handled as an unload packet
                    if (chunk.isFullChunk() && chunk.getBitmask() == 0) {
                        wrapper.setPacketType(ClientboundPackets1_9.UNLOAD_CHUNK);
                        wrapper.write(Type.INT, chunk.getX());
                        wrapper.write(Type.INT, chunk.getZ());
                        // Remove commandBlocks on chunk unload
                        CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                        provider.unloadChunk(wrapper.user(), chunk.getX(), chunk.getZ());
                        clientChunks.getLoadedChunks().remove(chunkHash);
                        // Unload the empty chunks
                        if (Via.getConfig().isChunkBorderFix()) {
                            for (BlockFace face : BlockFace.HORIZONTAL) {
                                int chunkX = chunk.getX() + face.modX();
                                int chunkZ = chunk.getZ() + face.modZ();
                                if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
                                    PacketWrapper unloadChunk = wrapper.create(ClientboundPackets1_9.UNLOAD_CHUNK);
                                    unloadChunk.write(Type.INT, chunkX);
                                    unloadChunk.write(Type.INT, chunkZ);
                                    unloadChunk.send(Protocol1_9To1_8.class);
                                }
                            }
                        }
                    } else {
                        Type<Chunk> chunkType = new Chunk1_9_1_2Type(clientWorld);
                        wrapper.write(chunkType, chunk);
                        clientChunks.getLoadedChunks().add(chunkHash);
                        // Send empty chunks surrounding the loaded chunk to force 1.9+ clients to render the new chunk
                        if (Via.getConfig().isChunkBorderFix()) {
                            for (BlockFace face : BlockFace.HORIZONTAL) {
                                int chunkX = chunk.getX() + face.modX();
                                int chunkZ = chunk.getZ() + face.modZ();
                                if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
                                    PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
                                    Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>());
                                    emptyChunk.write(chunkType, c);
                                    emptyChunk.send(Protocol1_9To1_8.class);
                                }
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.MAP_BULK_CHUNK, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Cancel the packet from being sent
                wrapper.cancel();
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                ClientChunks clientChunks = wrapper.user().get(ClientChunks.class);
                Chunk[] chunks = wrapper.read(new ChunkBulk1_8Type(clientWorld));
                Type<Chunk> chunkType = new Chunk1_9_1_2Type(clientWorld);
                // Split into multiple chunk packets
                for (Chunk chunk : chunks) {
                    PacketWrapper chunkData = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
                    chunkData.write(chunkType, chunk);
                    chunkData.send(Protocol1_9To1_8.class);
                    clientChunks.getLoadedChunks().add(ClientChunks.toLong(chunk.getX(), chunk.getZ()));
                    // Send empty chunks surrounding the loaded chunk to force 1.9+ clients to render the new chunk
                    if (Via.getConfig().isChunkBorderFix()) {
                        for (BlockFace face : BlockFace.HORIZONTAL) {
                            int chunkX = chunk.getX() + face.modX();
                            int chunkZ = chunk.getZ() + face.modZ();
                            if (!clientChunks.getLoadedChunks().contains(ClientChunks.toLong(chunkX, chunkZ))) {
                                PacketWrapper emptyChunk = wrapper.create(ClientboundPackets1_9.CHUNK_DATA);
                                Chunk c = new BaseChunk(chunkX, chunkZ, true, false, 0, new ChunkSection[16], new int[256], new ArrayList<>());
                                emptyChunk.write(chunkType, c);
                                emptyChunk.send(Protocol1_9To1_8.class);
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_8.BLOCK_ENTITY_DATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int action = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    if (action == 1) {
                        // Update Spawner
                        CompoundTag tag = wrapper.get(Type.NBT, 0);
                        if (tag != null) {
                            if (tag.contains("EntityId")) {
                                String entity = (String) tag.get("EntityId").getValue();
                                CompoundTag spawn = new CompoundTag();
                                spawn.put("id", new StringTag(entity));
                                tag.put("SpawnData", spawn);
                            } else {
                                // EntityID does not exist
                                CompoundTag spawn = new CompoundTag();
                                // Make spawners show up as empty when no EntityId is given.
                                spawn.put("id", new StringTag("AreaEffectCloud"));
                                tag.put("SpawnData", spawn);
                            }
                        }
                    }
                    if (action == 2) {
                        // Update Command Block
                        CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                        provider.addOrUpdateBlock(wrapper.user(), wrapper.get(Type.POSITION, 0), wrapper.get(Type.NBT, 0));
                        // To prevent window issues don't send updates
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    /* Incoming Packets */
    protocol.registerServerbound(ServerboundPackets1_9.UPDATE_SIGN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Sign Position
            map(Type.POSITION);
            // 1 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 2 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 3 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
            // 4 - Sign Line (json)
            map(Type.STRING, Protocol1_9To1_8.FIX_JSON);
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_DIGGING, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int status = wrapper.get(Type.VAR_INT, 0);
                    if (status == 6)
                        wrapper.cancel();
                }
            });
            // Blocking
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int status = wrapper.get(Type.VAR_INT, 0);
                    if (status == 5 || status == 4 || status == 3) {
                        EntityTracker1_9 entityTracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        if (entityTracker.isBlocking()) {
                            entityTracker.setBlocking(false);
                            if (!Via.getConfig().isShowShieldWhenSwordInHand()) {
                                entityTracker.setSecondHand(null);
                            }
                        }
                    }
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.USE_ITEM, null, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int hand = wrapper.read(Type.VAR_INT);
                    // Wipe the input buffer
                    wrapper.clearInputBuffer();
                    // First set this packet ID to Block placement
                    wrapper.setId(0x08);
                    wrapper.write(Type.POSITION, new Position(-1, (short) -1, -1));
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 255);
                    // Write item in hand
                    Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
                    // Blocking patch
                    if (Via.getConfig().isShieldBlocking()) {
                        EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                        // Check if the shield is already there or if we have to give it here
                        boolean showShieldWhenSwordInHand = Via.getConfig().isShowShieldWhenSwordInHand();
                        // Method to identify the sword in hand
                        boolean isSword = showShieldWhenSwordInHand ? tracker.hasSwordInHand() : item != null && Protocol1_9To1_8.isSword(item.identifier());
                        if (isSword) {
                            if (hand == 0) {
                                if (!tracker.isBlocking()) {
                                    tracker.setBlocking(true);
                                    // Check if the shield is already in the offhand
                                    if (!showShieldWhenSwordInHand && tracker.getItemInSecondHand() == null) {
                                        // Set shield in offhand when interacting with main hand
                                        Item shield = new DataItem(442, (byte) 1, (short) 0, null);
                                        tracker.setSecondHand(shield);
                                    }
                                }
                            }
                            // Use the main hand to trigger the blocking
                            boolean blockUsingMainHand = Via.getConfig().isNoDelayShieldBlocking() && !showShieldWhenSwordInHand;
                            if (blockUsingMainHand && hand == 1 || !blockUsingMainHand && hand == 0) {
                                wrapper.cancel();
                            }
                        } else {
                            if (!showShieldWhenSwordInHand) {
                                // Remove the shield from the offhand
                                tracker.setSecondHand(null);
                            }
                            tracker.setBlocking(false);
                        }
                    }
                    wrapper.write(Type.ITEM, item);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 0);
                }
            });
        }
    });
    protocol.registerServerbound(ServerboundPackets1_9.PLAYER_BLOCK_PLACEMENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Position
            map(Type.POSITION);
            // 1 - Block Face
            map(Type.VAR_INT, Type.UNSIGNED_BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // 2 - Hand
                    final int hand = wrapper.read(Type.VAR_INT);
                    if (hand != 0)
                        wrapper.cancel();
                }
            });
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Item item = Protocol1_9To1_8.getHandItem(wrapper.user());
                    // 3 - Item
                    wrapper.write(Type.ITEM, item);
                }
            });
            // 4 - X
            map(Type.UNSIGNED_BYTE);
            // 5 - Y
            map(Type.UNSIGNED_BYTE);
            // 6 - Z
            map(Type.UNSIGNED_BYTE);
            // Register block place to fix sounds
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int face = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    if (face == 255)
                        return;
                    Position p = wrapper.get(Type.POSITION, 0);
                    int x = p.x();
                    int y = p.y();
                    int z = p.z();
                    switch(face) {
                        case 0:
                            y--;
                            break;
                        case 1:
                            y++;
                            break;
                        case 2:
                            z--;
                            break;
                        case 3:
                            z++;
                            break;
                        case 4:
                            x--;
                            break;
                        case 5:
                            x++;
                            break;
                    }
                    EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_9To1_8.class);
                    tracker.addBlockInteraction(new Position(x, y, z));
                }
            });
            // Handle CommandBlocks
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    CommandBlockProvider provider = Via.getManager().getProviders().get(CommandBlockProvider.class);
                    Position pos = wrapper.get(Type.POSITION, 0);
                    Optional<CompoundTag> tag = provider.get(wrapper.user(), pos);
                    // Send the Update Block Entity packet if present
                    if (tag.isPresent()) {
                        PacketWrapper updateBlockEntity = PacketWrapper.create(ClientboundPackets1_9.BLOCK_ENTITY_DATA, null, wrapper.user());
                        updateBlockEntity.write(Type.POSITION, pos);
                        updateBlockEntity.write(Type.UNSIGNED_BYTE, (short) 2);
                        updateBlockEntity.write(Type.NBT, tag.get());
                        updateBlockEntity.scheduleSend(Protocol1_9To1_8.class);
                    }
                }
            });
        }
    });
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) Effect(com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.Effect) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Item(com.viaversion.viaversion.api.minecraft.item.Item) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) 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) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) Chunk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.Chunk1_8Type) ClientboundPackets1_8(com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8) ItemRewriter(com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter) SoundEffect(com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.SoundEffect) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) Position(com.viaversion.viaversion.api.minecraft.Position) Chunk1_9_1_2Type(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.types.Chunk1_9_1_2Type) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Protocol(com.viaversion.viaversion.api.protocol.Protocol) ChunkBulk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.ChunkBulk1_8Type) Via(com.viaversion.viaversion.api.Via) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ServerboundPackets1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.ServerboundPackets1_9) Optional(java.util.Optional) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) ClientboundPackets1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ClientChunks(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ArrayList(java.util.ArrayList) CommandBlockProvider(com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CommandBlockProvider) ChunkBulk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.ChunkBulk1_8Type) Item(com.viaversion.viaversion.api.minecraft.item.Item) DataItem(com.viaversion.viaversion.api.minecraft.item.DataItem) Chunk1_9_1_2Type(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.types.Chunk1_9_1_2Type) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) SoundEffect(com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.SoundEffect) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) EntityTracker1_9(com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9) Position(com.viaversion.viaversion.api.minecraft.Position) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) Chunk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.Chunk1_8Type) Type(com.viaversion.viaversion.api.type.Type) Chunk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.Chunk1_8Type) Chunk1_9_1_2Type(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.types.Chunk1_9_1_2Type) ChunkBulk1_8Type(com.viaversion.viaversion.protocols.protocol1_9to1_8.types.ChunkBulk1_8Type) 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) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) Protocol1_9To1_8(com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8)

Example 39 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class WorldPackets method writeMultiBlockChangePacket.

private static void writeMultiBlockChangePacket(PacketWrapper wrapper, Chunk chunk) throws Exception {
    long chunkPosition = (chunk.getX() & 0x3FFFFFL) << 42;
    chunkPosition |= (chunk.getZ() & 0x3FFFFFL) << 20;
    ChunkSection[] sections = chunk.getSections();
    for (int chunkY = 0; chunkY < sections.length; chunkY++) {
        ChunkSection section = sections[chunkY];
        if (section == null)
            continue;
        PacketWrapper blockChangePacket = wrapper.create(ClientboundPackets1_17.MULTI_BLOCK_CHANGE);
        blockChangePacket.write(Type.LONG, chunkPosition | (chunkY & 0xFFFFFL));
        // Suppress light updates
        blockChangePacket.write(Type.BOOLEAN, true);
        // TODO this can be optimized
        BlockChangeRecord[] blockChangeRecords = new BlockChangeRecord[4096];
        int j = 0;
        for (int x = 0; x < 16; x++) {
            for (int y = 0; y < 16; y++) {
                for (int z = 0; z < 16; z++) {
                    int blockStateId = Protocol1_17To1_16_4.MAPPINGS.getNewBlockStateId(section.getFlatBlock(x, y, z));
                    blockChangeRecords[j++] = new BlockChangeRecord1_16_2(x, y, z, blockStateId);
                }
            }
        }
        blockChangePacket.write(Type.VAR_LONG_BLOCK_CHANGE_RECORD_ARRAY, blockChangeRecords);
        blockChangePacket.send(Protocol1_17To1_16_4.class);
    }
}
Also used : BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) BlockChangeRecord1_16_2(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 40 with ChunkSection

use of com.viaversion.viaversion.api.minecraft.chunks.ChunkSection in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol1_17To1_16_4 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_16_2.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_16_2.BLOCK_CHANGE);
    blockRewriter.registerVarLongMultiBlockChange(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16_2.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_16_2.WORLD_BORDER, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                // Border 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.WORLD_BORDER_SIZE;
                        break;
                    case 1:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_LERP_SIZE;
                        break;
                    case 2:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_CENTER;
                        break;
                    case 3:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_INIT;
                        break;
                    case 4:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DELAY;
                        break;
                    case 5:
                        packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DISTANCE;
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid world border type received: " + type);
                }
                wrapper.setId(packetType.getId());
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // x
            map(Type.VAR_INT);
            // y
            map(Type.VAR_INT);
            // trust edges
            map(Type.BOOLEAN);
            handler(wrapper -> {
                int skyLightMask = wrapper.read(Type.VAR_INT);
                int blockLightMask = wrapper.read(Type.VAR_INT);
                // Now all written as a representation of BitSets
                // Sky light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(skyLightMask));
                // Block light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(blockLightMask));
                // Empty sky light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
                // Empty block light mask
                wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
                writeLightArrays(wrapper, skyLightMask);
                writeLightArrays(wrapper, blockLightMask);
            });
        }

        private void writeLightArrays(PacketWrapper wrapper, int bitMask) throws Exception {
            List<byte[]> light = new ArrayList<>();
            for (int i = 0; i < 18; i++) {
                if (isSet(bitMask, i)) {
                    light.add(wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
                }
            }
            // Now needs the length of the bytearray-array
            wrapper.write(Type.VAR_INT, light.size());
            for (byte[] bytes : light) {
                wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, bytes);
            }
        }

        private long[] toBitSetLongArray(int bitmask) {
            return new long[] { bitmask };
        }

        private boolean isSet(int mask, int i) {
            return (mask & (1 << i)) != 0;
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Chunk chunk = wrapper.read(new Chunk1_16_2Type());
                if (!chunk.isFullChunk()) {
                    // All chunks are full chunk packets now (1.16 already stopped sending non-full chunks)
                    // Construct multi block change packets instead
                    // Height map updates are lost (unless we want to fully cache and resend entire chunks)
                    // Block entities are always empty for non-full chunks in Vanilla
                    writeMultiBlockChangePacket(wrapper, chunk);
                    wrapper.cancel();
                    return;
                }
                // Normal full chunk writing
                wrapper.write(new Chunk1_17Type(chunk.getSections().length), chunk);
                // 1.17 uses a bitset for the mask
                chunk.setChunkMask(BitSet.valueOf(new long[] { chunk.getBitmask() }));
                for (int s = 0; s < chunk.getSections().length; s++) {
                    ChunkSection section = chunk.getSections()[s];
                    if (section == null)
                        continue;
                    for (int i = 0; i < section.getPaletteSize(); i++) {
                        int old = section.getPaletteEntry(i);
                        section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(old));
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Hardcore
            map(Type.BOOLEAN);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous Gamemode
            map(Type.BYTE);
            // World List
            map(Type.STRING_ARRAY);
            // Registry
            map(Type.NBT);
            // Current dimension
            map(Type.NBT);
            handler(wrapper -> {
                // Add new dimension fields
                CompoundTag dimensionRegistry = wrapper.get(Type.NBT, 0).get("minecraft:dimension_type");
                ListTag dimensions = dimensionRegistry.get("value");
                for (Tag dimension : dimensions) {
                    CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element");
                    addNewDimensionData(dimensionCompound);
                }
                CompoundTag currentDimensionTag = wrapper.get(Type.NBT, 1);
                addNewDimensionData(currentDimensionTag);
                UserConnection user = wrapper.user();
                user.getEntityTracker(Protocol1_17To1_16_4.class).addEntity(wrapper.get(Type.INT, 0), Entity1_17Types.PLAYER);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                CompoundTag dimensionData = wrapper.passthrough(Type.NBT);
                addNewDimensionData(dimensionData);
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001);
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) BlockChangeRecord1_16_2(com.viaversion.viaversion.api.minecraft.BlockChangeRecord1_16_2) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) ClientboundPackets1_16_2(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2) ArrayList(java.util.ArrayList) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Protocol1_17To1_16_4(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.Protocol1_17To1_16_4) Type(com.viaversion.viaversion.api.type.Type) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) List(java.util.List) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) Entity1_17Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BitSet(java.util.BitSet) Chunk1_17Type(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.types.Chunk1_17Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) ClientboundPacketType(com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Chunk1_16_2Type(com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.types.Chunk1_16_2Type) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Aggregations

ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)48 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)20 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)19 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)19 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)18 ArrayList (java.util.ArrayList)16 Type (com.viaversion.viaversion.api.type.Type)14 ByteBuf (io.netty.buffer.ByteBuf)13 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)12 BlockRewriter (com.viaversion.viaversion.rewriter.BlockRewriter)12 BaseChunk (com.viaversion.viaversion.api.minecraft.chunks.BaseChunk)11 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)10 Position (com.viaversion.viaversion.api.minecraft.Position)8 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)7 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)7 List (java.util.List)7 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)6 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)6 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)5 Via (com.viaversion.viaversion.api.Via)5