Search in sources :

Example 41 with ChunkSection

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

the class Chunk1_17Type method write.

@Override
public void write(ByteBuf output, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());
    Type.LONG_ARRAY_PRIMITIVE.write(output, chunk.getChunkMask().toLongArray());
    Type.NBT.write(output, chunk.getHeightMap());
    // Write biome data
    Type.VAR_INT_ARRAY_PRIMITIVE.write(output, chunk.getBiomeData());
    ByteBuf buf = output.alloc().buffer();
    try {
        ChunkSection[] sections = chunk.getSections();
        for (ChunkSection section : sections) {
            // Section not set
            if (section == null)
                continue;
            buf.writeShort(section.getNonAirBlocksCount());
            Types1_16.CHUNK_SECTION.write(buf, section);
        }
        buf.readerIndex(0);
        Type.VAR_INT.writePrimitive(output, buf.readableBytes());
        output.writeBytes(buf);
    } finally {
        // release buffer
        buf.release();
    }
    // Write Block Entities
    Type.NBT_ARRAY.write(output, chunk.getBlockEntities().toArray(EMPTY_COMPOUNDS));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 42 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_16To1_15_2 protocol) {
    BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
    blockRewriter.registerBlockAction(ClientboundPackets1_15.BLOCK_ACTION);
    blockRewriter.registerBlockChange(ClientboundPackets1_15.BLOCK_CHANGE);
    blockRewriter.registerMultiBlockChange(ClientboundPackets1_15.MULTI_BLOCK_CHANGE);
    blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_15.ACKNOWLEDGE_PLAYER_DIGGING);
    protocol.registerClientbound(ClientboundPackets1_15.UPDATE_LIGHT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // x
            map(Type.VAR_INT);
            // y
            map(Type.VAR_INT);
            // Take neighbour's light into account as well
            handler(wrapper -> wrapper.write(Type.BOOLEAN, true));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Chunk chunk = wrapper.read(new Chunk1_15Type());
                wrapper.write(new Chunk1_16Type(), chunk);
                chunk.setIgnoreOldLightData(chunk.isFullChunk());
                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));
                    }
                }
                CompoundTag heightMaps = chunk.getHeightMap();
                for (Tag heightMapTag : heightMaps.values()) {
                    LongArrayTag heightMap = (LongArrayTag) heightMapTag;
                    int[] heightMapData = new int[256];
                    CompactArrayUtil.iterateCompactArray(9, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = v);
                    heightMap.setValue(CompactArrayUtil.createCompactArrayWithPadding(9, heightMapData.length, i -> heightMapData[i]));
                }
                if (chunk.getBlockEntities() == null)
                    return;
                for (CompoundTag blockEntity : chunk.getBlockEntities()) {
                    handleBlockEntity(blockEntity);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Position position = wrapper.passthrough(Type.POSITION1_14);
                short action = wrapper.passthrough(Type.UNSIGNED_BYTE);
                CompoundTag tag = wrapper.passthrough(Type.NBT);
                handleBlockEntity(tag);
            });
        }
    });
    blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001);
}
Also used : Position(com.viaversion.viaversion.api.minecraft.Position) IntArrayTag(com.github.steveice10.opennbt.tag.builtin.IntArrayTag) UUID(java.util.UUID) UUIDIntArrayType(com.viaversion.viaversion.api.type.types.UUIDIntArrayType) Protocol1_16To1_15_2(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2) CompactArrayUtil(com.viaversion.viaversion.util.CompactArrayUtil) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) Map(java.util.Map) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) LongArrayTag(com.github.steveice10.opennbt.tag.builtin.LongArrayTag) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) BlockRewriter(com.viaversion.viaversion.rewriter.BlockRewriter) LongArrayTag(com.github.steveice10.opennbt.tag.builtin.LongArrayTag) Position(com.viaversion.viaversion.api.minecraft.Position) Chunk1_16Type(com.viaversion.viaversion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_15Type(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type) IntArrayTag(com.github.steveice10.opennbt.tag.builtin.IntArrayTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) LongArrayTag(com.github.steveice10.opennbt.tag.builtin.LongArrayTag) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 43 with ChunkSection

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

the class Chunk1_16Type method read.

@Override
public Chunk read(ByteBuf input) throws Exception {
    int chunkX = input.readInt();
    int chunkZ = input.readInt();
    boolean fullChunk = input.readBoolean();
    boolean ignoreOldLightData = 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_16.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, ignoreOldLightData, 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 44 with ChunkSection

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

the class Chunk1_9_3_4Type 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_9.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() ? 256 : 0));
        output.writeBytes(buf);
    } finally {
        // release buffer
        buf.release();
    }
    // Write biome data
    if (chunk.isBiomeData()) {
        for (int biome : chunk.getBiomeData()) {
            output.writeByte((byte) biome);
        }
    }
    // 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 45 with ChunkSection

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

the class Protocol1_9_3To1_9_1_2 method registerPackets.

@Override
protected void registerPackets() {
    // Sign update packet
    registerClientbound(ClientboundPackets1_9.UPDATE_SIGN, null, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    // read data
                    Position position = wrapper.read(Type.POSITION);
                    JsonElement[] lines = new JsonElement[4];
                    for (int i = 0; i < 4; i++) {
                        lines[i] = wrapper.read(Type.COMPONENT);
                    }
                    wrapper.clearInputBuffer();
                    // write data
                    // Update block entity
                    wrapper.setId(0x09);
                    // Block location
                    wrapper.write(Type.POSITION, position);
                    // Action type (9 update sign)
                    wrapper.write(Type.UNSIGNED_BYTE, (short) 9);
                    // Create nbt
                    CompoundTag tag = new CompoundTag();
                    tag.put("id", new StringTag("Sign"));
                    tag.put("x", new IntTag(position.x()));
                    tag.put("y", new IntTag(position.y()));
                    tag.put("z", new IntTag(position.z()));
                    for (int i = 0; i < lines.length; i++) {
                        tag.put("Text" + (i + 1), new StringTag(lines[i].toString()));
                    }
                    wrapper.write(Type.NBT, tag);
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9.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.read(new Chunk1_9_1_2Type(clientWorld));
                    wrapper.write(new Chunk1_9_3_4Type(clientWorld), chunk);
                    List<CompoundTag> tags = chunk.getBlockEntities();
                    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);
                                    if (FakeTileEntity.isTileEntity(block)) {
                                        tags.add(FakeTileEntity.createTileEntity(x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block));
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9.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);
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_9.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);
                }
            });
        }
    });
    // Sound effect
    registerClientbound(ClientboundPackets1_9.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(ADJUST_PITCH);
        }
    });
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Position(com.viaversion.viaversion.api.minecraft.Position) 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) Chunk1_9_1_2Type(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.types.Chunk1_9_1_2Type) 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)

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