Пример #1
0
    public void Serialize(IMinecraftPacket packet, bool fullChunk = false)
    {
        packet.WriteInt32(X);
        packet.WriteInt32(Z);
        packet.WriteBoolean(fullChunk);

        int mask = 0;

        // if full chunk
        using var chunkStream = new MinecraftPacket();
        for (int i = 0; i < Sections.Count(); i++)
        {
            IChunkSection section = Sections.ElementAt(i);

            if (fullChunk || section.IsDirty)
            {
                mask |= 1 << i;
                section.Serialize(chunkStream);
            }
        }

        packet.WriteVarInt32(mask);

        // Heightmap serialization
        var heightmapCompound = new NbtCompound("")
        {
            new NbtLongArray("MOTION_BLOCKING", Heightmap.ToArray()),
            new NbtLongArray("WORLD_SURFACE", WorldSurfaceHeightmap.ToArray())
        };
        var nbtFile = new NbtFile(heightmapCompound);

        packet.WriteBytes(nbtFile.GetBuffer());

        // Biomes
        if (fullChunk)
        {
            packet.WriteVarInt32(1024);

            for (int i = 0; i < 1024; i++)
            {
                packet.WriteVarInt32(0);
            }
        }

        chunkStream.Position = 0;

        packet.WriteVarInt32((int)chunkStream.Length);
        packet.WriteBytes(chunkStream.BaseBuffer);

        packet.WriteVarInt32(0); // block count
        // TODO: foreach block in blocks in chunk as NBT
    }