Search in sources :

Example 1 with LevelChunkPacket

use of com.nukkitx.protocol.bedrock.packet.LevelChunkPacket in project Geyser by GeyserMC.

the class ChunkUtils method sendEmptyChunk.

public static void sendEmptyChunk(GeyserSession session, int chunkX, int chunkZ, boolean forceUpdate) {
    LevelChunkPacket data = new LevelChunkPacket();
    data.setChunkX(chunkX);
    data.setChunkZ(chunkZ);
    data.setSubChunksLength(0);
    data.setData(EMPTY_CHUNK_DATA);
    data.setCachingEnabled(false);
    session.sendUpstreamPacket(data);
    if (forceUpdate) {
        Vector3i pos = Vector3i.from(chunkX << 4, 80, chunkZ << 4);
        UpdateBlockPacket blockPacket = new UpdateBlockPacket();
        blockPacket.setBlockPosition(pos);
        blockPacket.setDataLayer(0);
        blockPacket.setRuntimeId(1);
        session.sendUpstreamPacket(blockPacket);
    }
}
Also used : LevelChunkPacket(com.nukkitx.protocol.bedrock.packet.LevelChunkPacket) Vector3i(com.nukkitx.math.vector.Vector3i) UpdateBlockPacket(com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)

Example 2 with LevelChunkPacket

use of com.nukkitx.protocol.bedrock.packet.LevelChunkPacket 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)

Aggregations

Vector3i (com.nukkitx.math.vector.Vector3i)2 LevelChunkPacket (com.nukkitx.protocol.bedrock.packet.LevelChunkPacket)2 BitStorage (com.github.steveice10.mc.protocol.data.game.chunk.BitStorage)1 ChunkSection (com.github.steveice10.mc.protocol.data.game.chunk.ChunkSection)1 DataPalette (com.github.steveice10.mc.protocol.data.game.chunk.DataPalette)1 GlobalPalette (com.github.steveice10.mc.protocol.data.game.chunk.palette.GlobalPalette)1 Palette (com.github.steveice10.mc.protocol.data.game.chunk.palette.Palette)1 SingletonPalette (com.github.steveice10.mc.protocol.data.game.chunk.palette.SingletonPalette)1 BlockEntityInfo (com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityInfo)1 BlockEntityType (com.github.steveice10.mc.protocol.data.game.level.block.BlockEntityType)1 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)1 NetInput (com.github.steveice10.packetlib.io.NetInput)1 StreamNetInput (com.github.steveice10.packetlib.io.stream.StreamNetInput)1 NBTOutputStream (com.nukkitx.nbt.NBTOutputStream)1 NbtMap (com.nukkitx.nbt.NbtMap)1 UpdateBlockPacket (com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1