Search in sources :

Example 1 with ChunkSection

use of com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection in project HBot-Release by hhhzzzsss.

the class CommandCore method checkCore.

private boolean checkCore(ChunkPos pos) {
    ChunkColumn chunkColumn = world.getChunk(pos);
    if (chunkColumn == null)
        return false;
    ChunkSection section = chunkColumn.getChunks()[0];
    if (section == null)
        return false;
    BitStorage storage = section.getChunkData().getStorage();
    Palette palette = section.getChunkData().getPalette();
    for (int i = 0; i < 256 * targetCoreHeight; i++) {
        if (!isCommandBlock(palette.idToState(storage.get(i)))) {
            return false;
        }
    }
    return true;
}
Also used : Palette(com.github.steveice10.mc.protocol.data.game.chunk.palette.Palette) ChunkColumn(com.github.hhhzzzsss.hbot.block.ChunkColumn) ChunkSection(com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection) BitStorage(com.github.steveice10.mc.protocol.data.game.chunk.BitStorage)

Example 2 with ChunkSection

use of com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection in project Geyser by GeyserMC.

the class JavaLevelChunkWithLightTranslator method translate.

@Override
public void translate(GeyserSession session, ClientboundLevelChunkWithLightPacket packet) {
    if (session.isSpawned()) {
        ChunkUtils.updateChunkPosition(session, session.getPlayerEntity().getPosition().toInt());
    }
    // Ensure that, if the player is using lower world heights, the position is not offset
    int yOffset = session.getChunkCache().getChunkMinY();
    int chunkSize = session.getChunkCache().getChunkHeightY();
    int biomeGlobalPalette = session.getBiomeGlobalPalette();
    DataPalette[] javaChunks = new DataPalette[chunkSize];
    DataPalette[] javaBiomes = new DataPalette[chunkSize];
    final BlockEntityInfo[] blockEntities = packet.getBlockEntities();
    final List<NbtMap> bedrockBlockEntities = new ObjectArrayList<>(blockEntities.length);
    BitSet waterloggedPaletteIds = new BitSet();
    BitSet pistonOrFlowerPaletteIds = new BitSet();
    boolean overworld = session.getChunkCache().isExtendedHeight();
    int maxBedrockSectionY = ((overworld ? MAXIMUM_ACCEPTED_HEIGHT_OVERWORLD : MAXIMUM_ACCEPTED_HEIGHT) >> 4) - 1;
    int sectionCount;
    byte[] payload;
    ByteBuf byteBuf = null;
    GeyserChunkSection[] sections = new GeyserChunkSection[javaChunks.length - (yOffset + ((overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4))];
    try {
        NetInput in = new StreamNetInput(new ByteArrayInputStream(packet.getChunkData()));
        for (int sectionY = 0; sectionY < chunkSize; sectionY++) {
            ChunkSection javaSection = ChunkSection.read(in, biomeGlobalPalette);
            javaChunks[sectionY] = javaSection.getChunkData();
            javaBiomes[sectionY] = javaSection.getBiomeData();
            int bedrockSectionY = sectionY + (yOffset - ((overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4));
            if (bedrockSectionY < 0 || maxBedrockSectionY < bedrockSectionY) {
                // Ignore this chunk section since it goes outside the bounds accepted by the Bedrock client
                continue;
            }
            // No need to encode an empty section...
            if (javaSection.isBlockCountEmpty()) {
                continue;
            }
            Palette javaPalette = javaSection.getChunkData().getPalette();
            BitStorage javaData = javaSection.getChunkData().getStorage();
            if (javaPalette instanceof GlobalPalette) {
                // As this is the global palette, simply iterate through the whole chunk section once
                GeyserChunkSection section = new GeyserChunkSection(session.getBlockMappings().getBedrockAirId());
                for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
                    int javaId = javaData.get(yzx);
                    int bedrockId = session.getBlockMappings().getBedrockBlockId(javaId);
                    int xzy = indexYZXtoXZY(yzx);
                    section.getBlockStorageArray()[0].setFullBlock(xzy, bedrockId);
                    if (BlockRegistries.WATERLOGGED.get().contains(javaId)) {
                        section.getBlockStorageArray()[1].setFullBlock(xzy, session.getBlockMappings().getBedrockWaterId());
                    }
                    // Check if block is piston or flower to see if we'll need to create additional block entities, as they're only block entities in Bedrock
                    if (BlockStateValues.getFlowerPotValues().containsKey(javaId) || BlockStateValues.getPistonValues().containsKey(javaId)) {
                        bedrockBlockEntities.add(BedrockOnlyBlockEntity.getTag(session, Vector3i.from((packet.getX() << 4) + (yzx & 0xF), ((sectionY + yOffset) << 4) + ((yzx >> 8) & 0xF), (packet.getZ() << 4) + ((yzx >> 4) & 0xF)), javaId));
                    }
                }
                sections[bedrockSectionY] = section;
                continue;
            }
            if (javaPalette instanceof SingletonPalette) {
                // There's only one block here. Very easy!
                int javaId = javaPalette.idToState(0);
                int bedrockId = session.getBlockMappings().getBedrockBlockId(javaId);
                BlockStorage blockStorage = new BlockStorage(SingletonBitArray.INSTANCE, IntLists.singleton(bedrockId));
                if (BlockRegistries.WATERLOGGED.get().contains(javaId)) {
                    BlockStorage waterlogged = new BlockStorage(SingletonBitArray.INSTANCE, IntLists.singleton(session.getBlockMappings().getBedrockWaterId()));
                    sections[bedrockSectionY] = new GeyserChunkSection(new BlockStorage[] { blockStorage, waterlogged });
                } else {
                    sections[bedrockSectionY] = new GeyserChunkSection(new BlockStorage[] { blockStorage });
                }
                // If a chunk contains all of the same piston or flower pot then god help us
                continue;
            }
            IntList bedrockPalette = new IntArrayList(javaPalette.size());
            waterloggedPaletteIds.clear();
            pistonOrFlowerPaletteIds.clear();
            // Iterate through palette and convert state IDs to Bedrock, doing some additional checks as we go
            for (int i = 0; i < javaPalette.size(); i++) {
                int javaId = javaPalette.idToState(i);
                bedrockPalette.add(session.getBlockMappings().getBedrockBlockId(javaId));
                if (BlockRegistries.WATERLOGGED.get().contains(javaId)) {
                    waterloggedPaletteIds.set(i);
                }
                // Check if block is piston or flower to see if we'll need to create additional block entities, as they're only block entities in Bedrock
                if (BlockStateValues.getFlowerPotValues().containsKey(javaId) || BlockStateValues.getPistonValues().containsKey(javaId)) {
                    pistonOrFlowerPaletteIds.set(i);
                }
            }
            // for no reason, as most sections will not contain any pistons or flower pots
            if (!pistonOrFlowerPaletteIds.isEmpty()) {
                for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
                    int paletteId = javaData.get(yzx);
                    if (pistonOrFlowerPaletteIds.get(paletteId)) {
                        bedrockBlockEntities.add(BedrockOnlyBlockEntity.getTag(session, Vector3i.from((packet.getX() << 4) + (yzx & 0xF), ((sectionY + yOffset) << 4) + ((yzx >> 8) & 0xF), (packet.getZ() << 4) + ((yzx >> 4) & 0xF)), javaPalette.idToState(paletteId)));
                    }
                }
            }
            BitArray bedrockData = BitArrayVersion.forBitsCeil(javaData.getBitsPerEntry()).createArray(BlockStorage.SIZE);
            BlockStorage layer0 = new BlockStorage(bedrockData, bedrockPalette);
            BlockStorage[] layers;
            // Convert data array from YZX to XZY coordinate order
            if (waterloggedPaletteIds.isEmpty()) {
                // This could probably be optimized further...
                for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
                    bedrockData.set(indexYZXtoXZY(yzx), javaData.get(yzx));
                }
                layers = new BlockStorage[] { layer0 };
            } else {
                // The section contains waterlogged blocks, we need to convert coordinate order AND generate a V1 block storage for
                // layer 1 with palette ID 1 indicating water
                int[] layer1Data = new int[BlockStorage.SIZE >> 5];
                for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
                    int paletteId = javaData.get(yzx);
                    int xzy = indexYZXtoXZY(yzx);
                    bedrockData.set(xzy, paletteId);
                    if (waterloggedPaletteIds.get(paletteId)) {
                        layer1Data[xzy >> 5] |= 1 << (xzy & 0x1F);
                    }
                }
                // V1 palette
                IntList layer1Palette = new IntArrayList(2);
                // Air - see BlockStorage's constructor for more information
                layer1Palette.add(session.getBlockMappings().getBedrockAirId());
                layer1Palette.add(session.getBlockMappings().getBedrockWaterId());
                layers = new BlockStorage[] { layer0, new BlockStorage(BitArrayVersion.V1.createArray(BlockStorage.SIZE, layer1Data), layer1Palette) };
            }
            sections[bedrockSectionY] = new GeyserChunkSection(layers);
        }
        session.getChunkCache().addToCache(packet.getX(), packet.getZ(), javaChunks);
        final int chunkBlockX = packet.getX() << 4;
        final int chunkBlockZ = packet.getZ() << 4;
        for (BlockEntityInfo blockEntity : blockEntities) {
            BlockEntityType type = blockEntity.getType();
            if (type == null) {
                // Vanilla Minecraft gracefully handles this
                continue;
            }
            CompoundTag tag = blockEntity.getNbt();
            // Relative to chunk
            int x = blockEntity.getX();
            int y = blockEntity.getY();
            // Relative to chunk
            int z = blockEntity.getZ();
            // Get the Java block state ID from block entity position
            DataPalette section = javaChunks[(y >> 4) - yOffset];
            int blockState = section.get(x, y & 0xF, z);
            if (type == BlockEntityType.LECTERN && BlockStateValues.getLecternBookStates().get(blockState)) {
                // If getLecternBookStates is false, let's just treat it like a normal block entity
                bedrockBlockEntities.add(session.getGeyser().getWorldManager().getLecternDataAt(session, x + chunkBlockX, y, z + chunkBlockZ, true));
                continue;
            }
            BlockEntityTranslator blockEntityTranslator = BlockEntityUtils.getBlockEntityTranslator(type);
            bedrockBlockEntities.add(blockEntityTranslator.getBlockEntityTag(type, x + chunkBlockX, y, z + chunkBlockZ, tag, blockState));
            // Check for custom skulls
            if (session.getPreferencesCache().showCustomSkulls() && type == BlockEntityType.SKULL && tag != null && tag.contains("SkullOwner")) {
                SkullBlockEntityTranslator.spawnPlayer(session, tag, x + chunkBlockX, y, z + chunkBlockZ, blockState);
            }
        }
        // Find highest section
        sectionCount = sections.length - 1;
        while (sectionCount >= 0 && sections[sectionCount] == null) {
            sectionCount--;
        }
        sectionCount++;
        // Estimate chunk size
        int size = 0;
        for (int i = 0; i < sectionCount; i++) {
            GeyserChunkSection section = sections[i];
            if (section != null) {
                size += section.estimateNetworkSize();
            } else {
                size += SERIALIZED_CHUNK_DATA.length;
            }
        }
        // Consists only of biome data
        size += ChunkUtils.EMPTY_CHUNK_DATA.length;
        // Border blocks
        size += 1;
        // Extra data length (always 0)
        size += 1;
        // Conservative estimate of 64 bytes per tile entity
        size += bedrockBlockEntities.size() * 64;
        // Allocate output buffer
        byteBuf = ByteBufAllocator.DEFAULT.buffer(size);
        for (int i = 0; i < sectionCount; i++) {
            GeyserChunkSection section = sections[i];
            if (section != null) {
                section.writeToNetwork(byteBuf);
            } else {
                byteBuf.writeBytes(SERIALIZED_CHUNK_DATA);
            }
        }
        // As of 1.17.10, Bedrock hardcodes to always read 32 biome sections
        // As of 1.18, this hardcode was lowered to 25
        boolean isNewVersion = session.getUpstream().getProtocolVersion() >= Bedrock_v475.V475_CODEC.getProtocolVersion();
        int biomeCount = isNewVersion ? 25 : 32;
        int dimensionOffset = (overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4;
        for (int i = 0; i < biomeCount; i++) {
            int biomeYOffset = dimensionOffset + i;
            if (biomeYOffset < yOffset) {
                // Ignore this biome section since it goes below the height of the Java world
                byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA);
                continue;
            }
            if (biomeYOffset >= (chunkSize + yOffset)) {
                // This biome section goes above the height of the Java world
                if (isNewVersion) {
                    // A header that says to carry on the biome data from the previous chunk
                    // This notably fixes biomes in the End
                    byteBuf.writeByte((127 << 1) | 1);
                } else {
                    byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA);
                }
                continue;
            }
            BiomeTranslator.toNewBedrockBiome(session, javaBiomes[i + (dimensionOffset - yOffset)]).writeToNetwork(byteBuf);
        }
        // Border blocks - Edu edition only
        byteBuf.writeByte(0);
        // extra data length, 0 for now
        VarInts.writeUnsignedInt(byteBuf, 0);
        // Encode tile entities into buffer
        NBTOutputStream nbtStream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(byteBuf));
        for (NbtMap blockEntity : bedrockBlockEntities) {
            nbtStream.writeTag(blockEntity);
        }
        // Copy data into byte[], because the protocol lib really likes things that are s l o w
        byteBuf.readBytes(payload = new byte[byteBuf.readableBytes()]);
    } catch (IOException e) {
        session.getGeyser().getLogger().error("IO error while encoding chunk", e);
        return;
    } finally {
        if (byteBuf != null) {
            // Release buffer to allow buffer pooling to be useful
            byteBuf.release();
        }
    }
    LevelChunkPacket levelChunkPacket = new LevelChunkPacket();
    levelChunkPacket.setSubChunksLength(sectionCount);
    levelChunkPacket.setCachingEnabled(false);
    levelChunkPacket.setChunkX(packet.getX());
    levelChunkPacket.setChunkZ(packet.getZ());
    levelChunkPacket.setData(payload);
    session.sendUpstreamPacket(levelChunkPacket);
    for (Map.Entry<Vector3i, ItemFrameEntity> entry : session.getItemFrameCache().entrySet()) {
        Vector3i position = entry.getKey();
        if ((position.getX() >> 4) == packet.getX() && (position.getZ() >> 4) == packet.getZ()) {
            // Update this item frame so it doesn't get lost in the abyss
            // TODO optimize
            entry.getValue().updateBlock(true);
        }
    }
}
Also used : SingletonPalette(com.github.steveice10.mc.protocol.data.game.chunk.palette.SingletonPalette) DataPalette(com.github.steveice10.mc.protocol.data.game.chunk.DataPalette) GlobalPalette(com.github.steveice10.mc.protocol.data.game.chunk.palette.GlobalPalette) Palette(com.github.steveice10.mc.protocol.data.game.chunk.palette.Palette) ByteBuf(io.netty.buffer.ByteBuf) DataPalette(com.github.steveice10.mc.protocol.data.game.chunk.DataPalette) BlockStorage(org.geysermc.geyser.level.chunk.BlockStorage) ObjectArrayList(it.unimi.dsi.fastutil.objects.ObjectArrayList) NetInput(com.github.steveice10.packetlib.io.NetInput) StreamNetInput(com.github.steveice10.packetlib.io.stream.StreamNetInput) SingletonPalette(com.github.steveice10.mc.protocol.data.game.chunk.palette.SingletonPalette) SingletonBitArray(org.geysermc.geyser.level.chunk.bitarray.SingletonBitArray) BitArray(org.geysermc.geyser.level.chunk.bitarray.BitArray) BlockEntityInfo(com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityInfo) StreamNetInput(com.github.steveice10.packetlib.io.stream.StreamNetInput) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) BlockEntityType(com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityType) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) GeyserChunkSection(org.geysermc.geyser.level.chunk.GeyserChunkSection) NbtMap(com.nukkitx.nbt.NbtMap) LevelChunkPacket(com.nukkitx.protocol.bedrock.packet.LevelChunkPacket) IOException(java.io.IOException) NBTOutputStream(com.nukkitx.nbt.NBTOutputStream) GlobalPalette(com.github.steveice10.mc.protocol.data.game.chunk.palette.GlobalPalette) IntList(it.unimi.dsi.fastutil.ints.IntList) ItemFrameEntity(org.geysermc.geyser.entity.type.ItemFrameEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) SkullBlockEntityTranslator(org.geysermc.geyser.translator.level.block.entity.SkullBlockEntityTranslator) BlockEntityTranslator(org.geysermc.geyser.translator.level.block.entity.BlockEntityTranslator) Vector3i(com.nukkitx.math.vector.Vector3i) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) ChunkSection(com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection) GeyserChunkSection(org.geysermc.geyser.level.chunk.GeyserChunkSection) NbtMap(com.nukkitx.nbt.NbtMap) BitStorage(com.github.steveice10.mc.protocol.data.game.chunk.BitStorage)

Example 3 with ChunkSection

use of com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol protocol) {
    // Outgoing packets
    protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    PaintingProvider provider = Via.getManager().getProviders().get(PaintingProvider.class);
                    String motive = wrapper.read(Type.STRING);
                    Optional<Integer> id = provider.getIntByIdentifier(motive);
                    if (!id.isPresent() && (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug())) {
                        Via.getPlatform().getLogger().warning("Could not find painting motive: " + motive + " falling back to default (0)");
                    }
                    wrapper.write(Type.VAR_INT, id.orElse(0));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Location
            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 {
                    Position position = wrapper.get(Type.POSITION, 0);
                    short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    CompoundTag tag = wrapper.get(Type.NBT, 0);
                    BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
                    int newId = provider.transform(wrapper.user(), position, tag, true);
                    if (newId != -1) {
                        BlockStorage storage = wrapper.user().get(BlockStorage.class);
                        BlockStorage.ReplacementData replacementData = storage.get(position);
                        if (replacementData != null) {
                            replacementData.setReplacement(newId);
                        }
                    }
                    if (action == 5) {
                        // Set type of flower in flower pot
                        // Removed
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Location
            map(Type.POSITION);
            // Action Id
            map(Type.UNSIGNED_BYTE);
            // Action param
            map(Type.UNSIGNED_BYTE);
            // Block Id - /!\ NOT BLOCK STATE ID
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Position pos = wrapper.get(Type.POSITION, 0);
                    short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    short param = wrapper.get(Type.UNSIGNED_BYTE, 1);
                    int blockId = wrapper.get(Type.VAR_INT, 0);
                    if (blockId == 25)
                        blockId = 73;
                    else if (blockId == 33)
                        blockId = 99;
                    else if (blockId == 29)
                        blockId = 92;
                    else if (blockId == 54)
                        blockId = 142;
                    else if (blockId == 146)
                        blockId = 305;
                    else if (blockId == 130)
                        blockId = 249;
                    else if (blockId == 138)
                        blockId = 257;
                    else if (blockId == 52)
                        blockId = 140;
                    else if (blockId == 209)
                        blockId = 472;
                    else if (blockId >= 219 && blockId <= 234)
                        blockId = blockId - 219 + 483;
                    if (blockId == 73) {
                        // Note block
                        // block change
                        PacketWrapper blockChange = wrapper.create(0x0B);
                        blockChange.write(Type.POSITION, pos);
                        blockChange.write(Type.VAR_INT, 249 + (action * 24 * 2) + (param * 2));
                        blockChange.send(Protocol1_13To1_12_2.class);
                    }
                    wrapper.set(Type.VAR_INT, 0, blockId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_CHANGE, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Position position = wrapper.get(Type.POSITION, 0);
                    int newId = toNewId(wrapper.get(Type.VAR_INT, 0));
                    UserConnection userConnection = wrapper.user();
                    if (Via.getConfig().isServersideBlockConnections()) {
                        ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newId);
                        newId = ConnectionData.connect(userConnection, position, newId);
                    }
                    wrapper.set(Type.VAR_INT, 0, checkStorage(wrapper.user(), position, newId));
                    if (Via.getConfig().isServersideBlockConnections()) {
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_13To1_12_2.class);
                        wrapper.cancel();
                        ConnectionData.update(userConnection, position);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Chunk X
            map(Type.INT);
            // 1 - Chunk Z
            map(Type.INT);
            // 2 - Records
            map(Type.BLOCK_CHANGE_RECORD_ARRAY);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int chunkX = wrapper.get(Type.INT, 0);
                    int chunkZ = wrapper.get(Type.INT, 1);
                    UserConnection userConnection = wrapper.user();
                    BlockChangeRecord[] records = wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0);
                    // Convert ids
                    for (BlockChangeRecord record : records) {
                        int newBlock = toNewId(record.getBlockId());
                        Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
                        if (Via.getConfig().isServersideBlockConnections()) {
                            ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newBlock);
                        }
                        record.setBlockId(checkStorage(wrapper.user(), position, newBlock));
                    }
                    if (Via.getConfig().isServersideBlockConnections()) {
                        for (BlockChangeRecord record : records) {
                            int blockState = record.getBlockId();
                            Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
                            ConnectionHandler handler = ConnectionData.getConnectionHandler(blockState);
                            if (handler != null) {
                                blockState = handler.connect(userConnection, position, blockState);
                                record.setBlockId(blockState);
                            }
                        }
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_13To1_12_2.class);
                        wrapper.cancel();
                        for (BlockChangeRecord record : records) {
                            Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
                            ConnectionData.update(userConnection, position);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            if (!Via.getConfig().isServersideBlockConnections())
                return;
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Radius
            map(Type.FLOAT);
            // Record Count
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    UserConnection userConnection = wrapper.user();
                    int x = (int) Math.floor(wrapper.get(Type.FLOAT, 0));
                    int y = (int) Math.floor(wrapper.get(Type.FLOAT, 1));
                    int z = (int) Math.floor(wrapper.get(Type.FLOAT, 2));
                    int recordCount = wrapper.get(Type.INT, 0);
                    Position[] records = new Position[recordCount];
                    for (int i = 0; i < recordCount; i++) {
                        Position position = new Position(x + wrapper.passthrough(Type.BYTE), (short) (y + wrapper.passthrough(Type.BYTE)), z + wrapper.passthrough(Type.BYTE));
                        records[i] = position;
                        // Set to air
                        ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), 0);
                    }
                    // Workaround for packet order issue
                    wrapper.send(Protocol1_13To1_12_2.class);
                    wrapper.cancel();
                    for (int i = 0; i < recordCount; i++) {
                        ConnectionData.update(userConnection, records[i]);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.UNLOAD_CHUNK, new PacketRemapper() {

        @Override
        public void registerMap() {
            if (Via.getConfig().isServersideBlockConnections()) {
                handler(new PacketHandler() {

                    @Override
                    public void handle(PacketWrapper wrapper) throws Exception {
                        int x = wrapper.passthrough(Type.INT);
                        int z = wrapper.passthrough(Type.INT);
                        ConnectionData.blockConnectionProvider.unloadChunk(wrapper.user(), x, z);
                    }
                });
            }
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.NAMED_SOUND, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String sound = wrapper.get(Type.STRING, 0).replace("minecraft:", "");
                    String newSoundId = NamedSoundRewriter.getNewId(sound);
                    wrapper.set(Type.STRING, 0, newSoundId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.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);
                    BlockStorage storage = wrapper.user().get(BlockStorage.class);
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk1_13Type type1_13 = new Chunk1_13Type(clientWorld);
                    Chunk chunk = wrapper.read(type);
                    wrapper.write(type1_13, chunk);
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection section = chunk.getSections()[i];
                        if (section == null)
                            continue;
                        for (int p = 0; p < section.getPaletteSize(); p++) {
                            int old = section.getPaletteEntry(p);
                            int newId = toNewId(old);
                            section.setPaletteEntry(p, newId);
                        }
                        boolean willSaveToStorage = false;
                        for (int p = 0; p < section.getPaletteSize(); p++) {
                            int newId = section.getPaletteEntry(p);
                            if (storage.isWelcome(newId)) {
                                willSaveToStorage = true;
                                break;
                            }
                        }
                        boolean willSaveConnection = false;
                        if (Via.getConfig().isServersideBlockConnections() && ConnectionData.needStoreBlocks()) {
                            for (int p = 0; p < section.getPaletteSize(); p++) {
                                int newId = section.getPaletteEntry(p);
                                if (ConnectionData.isWelcome(newId)) {
                                    willSaveConnection = true;
                                    break;
                                }
                            }
                        }
                        if (willSaveToStorage) {
                            for (int y = 0; y < 16; y++) {
                                for (int z = 0; z < 16; z++) {
                                    for (int x = 0; x < 16; x++) {
                                        int block = section.getFlatBlock(x, y, z);
                                        if (storage.isWelcome(block)) {
                                            storage.store(new Position((x + (chunk.getX() << 4)), (short) (y + (i << 4)), (z + (chunk.getZ() << 4))), block);
                                        }
                                    }
                                }
                            }
                        }
                        if (willSaveConnection) {
                            for (int y = 0; y < 16; y++) {
                                for (int z = 0; z < 16; z++) {
                                    for (int x = 0; x < 16; x++) {
                                        int block = section.getFlatBlock(x, y, z);
                                        if (ConnectionData.isWelcome(block)) {
                                            ConnectionData.blockConnectionProvider.storeBlock(wrapper.user(), x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Rewrite biome id 255 to plains
                    if (chunk.isBiomeData()) {
                        int latestBiomeWarn = Integer.MIN_VALUE;
                        for (int i = 0; i < 256; i++) {
                            int biome = chunk.getBiomeData()[i];
                            if (!VALID_BIOMES.contains(biome)) {
                                if (// is it generated naturally? *shrug*
                                biome != 255 && latestBiomeWarn != biome) {
                                    if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                                        Via.getPlatform().getLogger().warning("Received invalid biome id " + biome);
                                    }
                                    latestBiomeWarn = biome;
                                }
                                // Plains
                                chunk.getBiomeData()[i] = 1;
                            }
                        }
                    }
                    // Rewrite BlockEntities to normal blocks
                    BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
                    final Iterator<CompoundTag> iterator = chunk.getBlockEntities().iterator();
                    while (iterator.hasNext()) {
                        CompoundTag tag = iterator.next();
                        int newId = provider.transform(wrapper.user(), null, tag, false);
                        if (newId != -1) {
                            int x = ((NumberTag) tag.get("x")).asInt();
                            int y = ((NumberTag) tag.get("y")).asInt();
                            int z = ((NumberTag) tag.get("z")).asInt();
                            Position position = new Position(x, (short) y, z);
                            // Store the replacement blocks for blockupdates
                            BlockStorage.ReplacementData replacementData = storage.get(position);
                            if (replacementData != null) {
                                replacementData.setReplacement(newId);
                            }
                            chunk.getSections()[y >> 4].setFlatBlock(x & 0xF, y & 0xF, z & 0xF, newId);
                        }
                        final Tag idTag = tag.get("id");
                        if (idTag instanceof StringTag) {
                            // No longer block entities
                            final String id = ((StringTag) idTag).getValue();
                            if (id.equals("minecraft:noteblock") || id.equals("minecraft:flower_pot")) {
                                iterator.remove();
                            }
                        }
                    }
                    if (Via.getConfig().isServersideBlockConnections()) {
                        ConnectionData.connectBlocks(wrapper.user(), chunk);
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_13To1_12_2.class);
                        wrapper.cancel();
                        for (int i = 0; i < chunk.getSections().length; i++) {
                            ChunkSection section = chunk.getSections()[i];
                            if (section == null)
                                continue;
                            ConnectionData.updateChunkSectionNeighbours(wrapper.user(), chunk.getX(), chunk.getZ(), i);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Particle ID
            map(Type.INT);
            // 1 - Long Distance
            map(Type.BOOLEAN);
            // 2 - X
            map(Type.FLOAT);
            // 3 - Y
            map(Type.FLOAT);
            // 4 - Z
            map(Type.FLOAT);
            // 5 - Offset X
            map(Type.FLOAT);
            // 6 - Offset Y
            map(Type.FLOAT);
            // 7 - Offset Z
            map(Type.FLOAT);
            // 8 - Particle Data
            map(Type.FLOAT);
            // 9 - Particle Count
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int particleId = wrapper.get(Type.INT, 0);
                    // Get the data (Arrays are overrated)
                    int dataCount = 0;
                    // Particles with 1 data [BlockCrack,BlockDust,FallingDust]
                    if (particleId == 37 || particleId == 38 || particleId == 46)
                        dataCount = 1;
                    else // Particles with 2 data [IconCrack]
                    if (particleId == 36)
                        dataCount = 2;
                    Integer[] data = new Integer[dataCount];
                    for (int i = 0; i < data.length; i++) data[i] = wrapper.read(Type.VAR_INT);
                    Particle particle = ParticleRewriter.rewriteParticle(particleId, data);
                    // Cancel if null or completely removed
                    if (particle == null || particle.getId() == -1) {
                        wrapper.cancel();
                        return;
                    }
                    // Handle reddust particle color
                    if (particle.getId() == 11) {
                        int count = wrapper.get(Type.INT, 1);
                        float speed = wrapper.get(Type.FLOAT, 6);
                        // Only handle for count = 0
                        if (count == 0) {
                            wrapper.set(Type.INT, 1, 1);
                            wrapper.set(Type.FLOAT, 6, 0f);
                            List<Particle.ParticleData> arguments = particle.getArguments();
                            for (int i = 0; i < 3; i++) {
                                // RGB values are represented by the X/Y/Z offset
                                float colorValue = wrapper.get(Type.FLOAT, i + 3) * speed;
                                if (colorValue == 0 && i == 0) {
                                    // https://minecraft.gamepedia.com/User:Alphappy/reddust
                                    colorValue = 1;
                                }
                                arguments.get(i).setValue(colorValue);
                                wrapper.set(Type.FLOAT, i + 3, 0f);
                            }
                        }
                    }
                    wrapper.set(Type.INT, 0, particle.getId());
                    for (Particle.ParticleData particleData : particle.getArguments()) wrapper.write(particleData.getType(), particleData.getValue());
                }
            });
        }
    });
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) Particle(com.viaversion.viaversion.api.type.types.Particle) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) BlockStorage(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) Position(com.viaversion.viaversion.api.minecraft.Position) PaintingProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.PaintingProvider) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ConnectionHandler(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionHandler) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) BlockEntityProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 4 with ChunkSection

use of com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_14Type method write.

@Override
public void write(ByteBuf output, Chunk chunk) throws Exception {
    output.writeInt(chunk.getX());
    output.writeInt(chunk.getZ());
    output.writeBoolean(chunk.isFullChunk());
    Type.VAR_INT.writePrimitive(output, chunk.getBitmask());
    Type.NBT.write(output, chunk.getHeightMap());
    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;
            buf.writeShort(section.getNonAirBlocksCount());
            Types1_13.CHUNK_SECTION.write(buf, section);
        }
        buf.readerIndex(0);
        // 256 * 4
        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()) {
            // This is a temporary workaround, we'll look into fixing this soon :)
            output.writeInt(value & 0xFF);
        }
    }
    // 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 5 with ChunkSection

use of com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection in project ViaVersion by ViaVersion.

the class Chunk1_13Type method read.

@Override
public Chunk read(ByteBuf input, ClientWorld world) throws Exception {
    int chunkX = input.readInt();
    int chunkZ = input.readInt();
    boolean fullChunk = input.readBoolean();
    int primaryBitmask = Type.VAR_INT.readPrimitive(input);
    ByteBuf data = input.readSlice(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;
        ChunkSection section = Types1_13.CHUNK_SECTION.read(data);
        sections[i] = section;
        section.getLight().readBlockLight(data);
        if (world.getEnvironment() == Environment.NORMAL) {
            section.getLight().readSkyLight(data);
        }
    }
    int[] biomeData = fullChunk ? new int[256] : null;
    if (fullChunk) {
        if (data.readableBytes() >= 256 * 4) {
            for (int i = 0; i < 256; i++) {
                biomeData[i] = data.readInt();
            }
        } else {
            Via.getPlatform().getLogger().log(Level.WARNING, "Chunk x=" + chunkX + " z=" + chunkZ + " doesn't have biome data!");
        }
    }
    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, nbtData);
}
Also used : BaseChunk(com.viaversion.viaversion.api.minecraft.chunks.BaseChunk) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Aggregations

CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)21 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)19 ArrayList (java.util.ArrayList)11 BaseChunk (com.viaversion.viaversion.api.minecraft.chunks.BaseChunk)9 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)8 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)7 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)6 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)6 ByteBuf (io.netty.buffer.ByteBuf)6 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)5 Type (com.viaversion.viaversion.api.type.Type)5 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)5 Via (com.viaversion.viaversion.api.Via)4 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)4 Position (com.viaversion.viaversion.api.minecraft.Position)4 BitSet (java.util.BitSet)4 ChunkSection (com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection)3 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)3 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)3 BlockRewriter (com.viaversion.viaversion.rewriter.BlockRewriter)3