2019-05-01 15:23:30 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-25 01:34:07 +08:00
|
|
|
|
using System.Numerics;
|
2022-08-28 13:18:07 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2022-08-29 23:39:03 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2019-05-01 15:23:30 +02:00
|
|
|
|
//using System.Linq;
|
|
|
|
|
|
//using System.Text;
|
|
|
|
|
|
using MinecraftClient.Mapping;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Protocol.Handlers
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Terrain Decoding handler for Protocol18
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
class Protocol18Terrain
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
private readonly int protocolversion;
|
|
|
|
|
|
private readonly DataTypes dataTypes;
|
|
|
|
|
|
private readonly IMinecraftComHandler handler;
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize a new Terrain Decoder
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="protocolVersion">Minecraft Protocol Version</param>
|
|
|
|
|
|
/// <param name="dataTypes">Minecraft Protocol Data Types</param>
|
|
|
|
|
|
public Protocol18Terrain(int protocolVersion, DataTypes dataTypes, IMinecraftComHandler handler)
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
protocolversion = protocolVersion;
|
2019-05-01 15:23:30 +02:00
|
|
|
|
this.dataTypes = dataTypes;
|
|
|
|
|
|
this.handler = handler;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-07-24 21:41:56 +08:00
|
|
|
|
/// Reading the "Block states" field: consists of 4096 entries, representing all the blocks in the chunk section.
|
2022-07-23 22:34:16 +08:00
|
|
|
|
/// </summary>
|
2022-07-24 21:41:56 +08:00
|
|
|
|
/// <param name="cache">Cache for reading data</param>
|
2022-08-28 13:18:07 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
2022-08-25 10:40:55 +08:00
|
|
|
|
private Chunk? ReadBlockStatesField(Queue<byte> cache)
|
2022-07-23 22:34:16 +08:00
|
|
|
|
{
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// read Block states (Type: Paletted Container)
|
|
|
|
|
|
byte bitsPerEntry = dataTypes.ReadNextByte(cache);
|
|
|
|
|
|
|
|
|
|
|
|
// 1.18(1.18.1) add a pattle named "Single valued" to replace the vertical strip bitmask in the old
|
2022-08-19 16:35:55 +02:00
|
|
|
|
if (bitsPerEntry == 0 && protocolversion >= Protocol18Handler.MC_1_18_1_Version)
|
2022-07-23 22:34:16 +08:00
|
|
|
|
{
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// Palettes: Single valued - 1.18(1.18.1) and above
|
2022-08-25 01:34:07 +08:00
|
|
|
|
ushort blockId = (ushort)dataTypes.ReadNextVarInt(cache);
|
2022-08-25 10:40:55 +08:00
|
|
|
|
Block block = new(blockId);
|
2022-07-24 21:41:56 +08:00
|
|
|
|
|
|
|
|
|
|
dataTypes.SkipNextVarInt(cache); // Data Array Length will be zero
|
|
|
|
|
|
|
|
|
|
|
|
// Empty chunks will not be stored
|
2022-08-25 10:40:55 +08:00
|
|
|
|
if (block.Type == Material.Air)
|
2022-07-24 21:41:56 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
2022-08-30 14:04:27 +08:00
|
|
|
|
// Warning: If you need to support modification of block data, you need to create 4096 objects here
|
2022-08-31 20:46:21 +08:00
|
|
|
|
Chunk chunk = new();
|
2022-07-24 21:41:56 +08:00
|
|
|
|
for (int blockY = 0; blockY < Chunk.SizeY; blockY++)
|
|
|
|
|
|
for (int blockZ = 0; blockZ < Chunk.SizeZ; blockZ++)
|
|
|
|
|
|
for (int blockX = 0; blockX < Chunk.SizeX; blockX++)
|
2022-08-31 20:46:21 +08:00
|
|
|
|
chunk.SetWithoutCheck(blockX, blockY, blockZ, block);
|
|
|
|
|
|
return chunk;
|
2022-07-24 21:41:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Palettes: Indirect or Direct
|
|
|
|
|
|
bool usePalette = (bitsPerEntry <= 8);
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// Indirect Mode: For block states with bits per entry <= 4, 4 bits are used to represent a block.
|
|
|
|
|
|
if (bitsPerEntry < 4) bitsPerEntry = 4;
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// Direct Mode: Bit mask covering bitsPerEntry bits
|
|
|
|
|
|
// EG, if bitsPerEntry = 5, valueMask = 00011111 in binary
|
|
|
|
|
|
uint valueMask = (uint)((1 << bitsPerEntry) - 1);
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-08-31 18:00:00 +08:00
|
|
|
|
int paletteLength = usePalette ? dataTypes.ReadNextVarInt(cache) : 0; // Assume zero when length is absent
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-08-29 23:39:03 +08:00
|
|
|
|
Span<uint> palette = paletteLength < 256 ? stackalloc uint[paletteLength] : new uint[paletteLength];
|
2022-07-24 21:41:56 +08:00
|
|
|
|
for (int i = 0; i < paletteLength; i++)
|
2022-08-29 23:39:03 +08:00
|
|
|
|
palette[i] = (uint)dataTypes.ReadNextVarInt(cache);
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-08-29 23:39:03 +08:00
|
|
|
|
//// Block IDs are packed in the array of 64-bits integers
|
2022-08-30 14:04:27 +08:00
|
|
|
|
dataTypes.SkipNextVarInt(cache); // Entry length
|
2022-08-29 23:39:03 +08:00
|
|
|
|
Span<byte> entryDataByte = stackalloc byte[8];
|
2022-08-30 20:05:53 +08:00
|
|
|
|
Span<long> entryDataLong = MemoryMarshal.Cast<byte, long>(entryDataByte); // Faster than MemoryMarshal.Read<long>
|
2022-07-24 21:41:56 +08:00
|
|
|
|
|
2022-08-31 20:46:21 +08:00
|
|
|
|
Chunk chunk = new();
|
2022-08-30 14:04:27 +08:00
|
|
|
|
int startOffset = 64; // Read the first data immediately
|
2022-07-24 21:41:56 +08:00
|
|
|
|
for (int blockY = 0; blockY < Chunk.SizeY; blockY++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int blockZ = 0; blockZ < Chunk.SizeZ; blockZ++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int blockX = 0; blockX < Chunk.SizeX; blockX++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Calculate location of next block ID inside the array of Longs
|
2022-08-30 14:04:27 +08:00
|
|
|
|
if ((startOffset += bitsPerEntry) > (64 - bitsPerEntry))
|
2022-07-24 21:41:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
// In MC 1.16+, padding is applied to prevent overlapping between Longs:
|
2022-08-30 14:04:27 +08:00
|
|
|
|
// [ LONG INTEGER ][ LONG INTEGER ]
|
|
|
|
|
|
// [Block][Block][Block]XXX[Block][Block][Block]XXX
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// When overlapping, move forward to the beginning of the next Long
|
|
|
|
|
|
startOffset = 0;
|
2022-08-29 23:39:03 +08:00
|
|
|
|
dataTypes.ReadDataReverse(cache, entryDataByte); // read long
|
2022-07-24 21:41:56 +08:00
|
|
|
|
}
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-08-30 12:33:13 +08:00
|
|
|
|
uint blockId = (uint)(entryDataLong[0] >> startOffset) & valueMask;
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// Map small IDs to actual larger block IDs
|
|
|
|
|
|
if (usePalette)
|
2022-07-23 22:34:16 +08:00
|
|
|
|
{
|
2022-07-24 21:41:56 +08:00
|
|
|
|
if (paletteLength <= blockId)
|
2022-07-23 22:34:16 +08:00
|
|
|
|
{
|
2022-07-24 21:41:56 +08:00
|
|
|
|
int blockNumber = (blockY * Chunk.SizeZ + blockZ) * Chunk.SizeX + blockX;
|
|
|
|
|
|
throw new IndexOutOfRangeException(String.Format("Block ID {0} is outside Palette range 0-{1}! (bitsPerBlock: {2}, blockNumber: {3})",
|
|
|
|
|
|
blockId,
|
|
|
|
|
|
paletteLength - 1,
|
|
|
|
|
|
bitsPerEntry,
|
|
|
|
|
|
blockNumber));
|
2022-07-23 22:34:16 +08:00
|
|
|
|
}
|
2022-07-24 21:41:56 +08:00
|
|
|
|
|
2022-08-30 12:33:13 +08:00
|
|
|
|
blockId = palette[(int)blockId];
|
2022-07-23 22:34:16 +08:00
|
|
|
|
}
|
2022-07-24 21:41:56 +08:00
|
|
|
|
|
2022-08-30 14:04:27 +08:00
|
|
|
|
// NOTICE: In the future a single ushort may not store the entire block id;
|
|
|
|
|
|
// the Block class may need to change if block state IDs go beyond 65535
|
2022-08-30 12:33:13 +08:00
|
|
|
|
Block block = new((ushort)blockId);
|
|
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// We have our block, save the block into the chunk
|
2022-08-31 20:46:21 +08:00
|
|
|
|
chunk.SetWithoutCheck(blockX, blockY, blockZ, block);
|
2022-07-23 22:34:16 +08:00
|
|
|
|
}
|
2022-07-24 21:41:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-08-31 20:46:21 +08:00
|
|
|
|
return chunk;
|
2022-07-24 21:41:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Process chunk column data from the server and (un)load the chunk from the Minecraft world - 1.17 and above
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="chunkX">Chunk X location</param>
|
|
|
|
|
|
/// <param name="chunkZ">Chunk Z location</param>
|
|
|
|
|
|
/// <param name="verticalStripBitmask">Chunk mask for reading data, store in bitset, used in 1.17 and 1.17.1</param>
|
|
|
|
|
|
/// <param name="cache">Cache for reading chunk data</param>
|
2022-08-24 18:16:16 +08:00
|
|
|
|
/// <param name="cancellationToken">token to cancel the task</param>
|
2022-08-28 13:18:07 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
2022-08-30 19:09:07 +08:00
|
|
|
|
public void ProcessChunkColumnData(int chunkX, int chunkZ, ulong[]? verticalStripBitmask, Queue<byte> cache)
|
2022-07-24 21:41:56 +08:00
|
|
|
|
{
|
2022-08-25 01:34:07 +08:00
|
|
|
|
World world = handler.GetWorld();
|
2022-07-24 21:41:56 +08:00
|
|
|
|
|
2022-08-18 20:58:49 +02:00
|
|
|
|
int chunkColumnSize = (World.GetDimension().height + 15) / 16; // Round up
|
2022-07-24 21:41:56 +08:00
|
|
|
|
|
2022-08-19 16:35:55 +02:00
|
|
|
|
if (protocolversion >= Protocol18Handler.MC_1_17_Version)
|
2022-07-24 21:41:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 1.17 and above chunk format
|
|
|
|
|
|
// Unloading chunks is handled by a separate packet
|
2022-08-25 10:40:55 +08:00
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
for (int chunkY = 0; chunkY < chunkColumnSize; chunkY++)
|
|
|
|
|
|
{
|
2022-08-25 10:40:55 +08:00
|
|
|
|
int lastChunkY;
|
2022-08-25 02:21:36 +08:00
|
|
|
|
if (protocolversion == Protocol18Handler.MC_1_17_Version || protocolversion == Protocol18Handler.MC_1_17_1_Version)
|
|
|
|
|
|
{
|
2022-08-25 10:40:55 +08:00
|
|
|
|
lastChunkY = 0;
|
2022-08-25 02:21:36 +08:00
|
|
|
|
for (int i = verticalStripBitmask!.Length - 1; i >= 0; --i)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (verticalStripBitmask![i] != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
lastChunkY = (sizeof(ulong) * 8 * i) + (sizeof(ulong) * 8 - 1 - BitOperations.LeadingZeroCount(verticalStripBitmask![i]));
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
lastChunkY = chunkColumnSize - 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// 1.18 and above always contains all chunk section in data
|
|
|
|
|
|
// 1.17 and 1.17.1 need vertical strip bitmask to know if the chunk section is included
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if ((protocolversion >= Protocol18Handler.MC_1_18_1_Version) ||
|
2022-08-25 01:34:07 +08:00
|
|
|
|
((verticalStripBitmask![chunkY / 64] & (1UL << (chunkY % 64))) != 0))
|
2022-07-24 21:41:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
// Non-air block count inside chunk section, for lighting purposes
|
|
|
|
|
|
int blockCnt = dataTypes.ReadNextShort(cache);
|
|
|
|
|
|
|
|
|
|
|
|
// Read Block states (Type: Paletted Container)
|
2022-08-25 10:40:55 +08:00
|
|
|
|
Chunk? chunk = ReadBlockStatesField(cache);
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
|
|
|
|
|
//We have our chunk, save the chunk into the world
|
2022-08-31 19:50:11 +08:00
|
|
|
|
world.StoreChunk(chunkX, chunkY, chunkZ, chunkColumnSize, chunk, chunkY == lastChunkY);
|
2022-07-23 22:34:16 +08:00
|
|
|
|
|
2022-07-24 21:41:56 +08:00
|
|
|
|
// Skip Read Biomes (Type: Paletted Container) - 1.18(1.18.1) and above
|
2022-08-19 16:35:55 +02:00
|
|
|
|
if (protocolversion >= Protocol18Handler.MC_1_18_1_Version)
|
2022-07-23 22:34:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
byte bitsPerEntryBiome = dataTypes.ReadNextByte(cache); // Bits Per Entry
|
2022-07-24 21:41:56 +08:00
|
|
|
|
if (bitsPerEntryBiome == 0)
|
2022-07-23 22:34:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
dataTypes.SkipNextVarInt(cache); // Value
|
|
|
|
|
|
dataTypes.SkipNextVarInt(cache); // Data Array Length
|
|
|
|
|
|
// Data Array must be empty
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bitsPerEntryBiome <= 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
int paletteLength = dataTypes.ReadNextVarInt(cache); // Palette Length
|
|
|
|
|
|
for (int i = 0; i < paletteLength; i++)
|
|
|
|
|
|
dataTypes.SkipNextVarInt(cache); // Palette
|
|
|
|
|
|
}
|
|
|
|
|
|
int dataArrayLength = dataTypes.ReadNextVarInt(cache); // Data Array Length
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData(dataArrayLength * 8, cache); // Data Array
|
2022-07-23 22:34:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Don't worry about skipping remaining data since there is no useful data afterwards in 1.9
|
|
|
|
|
|
// (plus, it would require parsing the tile entity lists' NBT)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Process chunk column data from the server and (un)load the chunk from the Minecraft world - 1.17 below
|
2019-05-01 15:23:30 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="chunkX">Chunk X location</param>
|
|
|
|
|
|
/// <param name="chunkZ">Chunk Z location</param>
|
|
|
|
|
|
/// <param name="chunkMask">Chunk mask for reading data</param>
|
|
|
|
|
|
/// <param name="chunkMask2">Chunk mask for some additional 1.7 metadata</param>
|
|
|
|
|
|
/// <param name="hasSkyLight">Contains skylight info</param>
|
|
|
|
|
|
/// <param name="chunksContinuous">Are the chunk continuous</param>
|
|
|
|
|
|
/// <param name="currentDimension">Current dimension type (0 = overworld)</param>
|
|
|
|
|
|
/// <param name="cache">Cache for reading chunk data</param>
|
2022-08-24 18:16:16 +08:00
|
|
|
|
/// <param name="cancellationToken">token to cancel the task</param>
|
2022-08-28 13:18:07 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
2022-08-30 19:09:07 +08:00
|
|
|
|
public void ProcessChunkColumnData(int chunkX, int chunkZ, ushort chunkMask, ushort chunkMask2, bool hasSkyLight, bool chunksContinuous, int currentDimension, Queue<byte> cache)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
2022-08-25 01:34:07 +08:00
|
|
|
|
World world = handler.GetWorld();
|
|
|
|
|
|
|
2022-08-18 20:58:49 +02:00
|
|
|
|
const int chunkColumnSize = 16;
|
2022-08-15 23:55:44 +08:00
|
|
|
|
if (protocolversion >= Protocol18Handler.MC_1_9_Version)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
// 1.9 and above chunk format
|
|
|
|
|
|
// Unloading chunks is handled by a separate packet
|
2022-08-25 01:34:07 +08:00
|
|
|
|
int maxChunkY = sizeof(int) * 8 - 1 - BitOperations.LeadingZeroCount(chunkMask);
|
|
|
|
|
|
for (int chunkY = 0; chunkY <= maxChunkY; chunkY++)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
if ((chunkMask & (1 << chunkY)) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1.14 and above Non-air block count inside chunk section, for lighting purposes
|
2022-08-15 23:55:44 +08:00
|
|
|
|
if (protocolversion >= Protocol18Handler.MC_1_14_Version)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
dataTypes.ReadNextShort(cache);
|
|
|
|
|
|
|
|
|
|
|
|
byte bitsPerBlock = dataTypes.ReadNextByte(cache);
|
|
|
|
|
|
bool usePalette = (bitsPerBlock <= 8);
|
|
|
|
|
|
|
|
|
|
|
|
// Vanilla Minecraft will use at least 4 bits per block
|
|
|
|
|
|
if (bitsPerBlock < 4)
|
|
|
|
|
|
bitsPerBlock = 4;
|
|
|
|
|
|
|
|
|
|
|
|
// MC 1.9 to 1.12 will set palette length field to 0 when palette
|
|
|
|
|
|
// is not used, MC 1.13+ does not send the field at all in this case
|
|
|
|
|
|
int paletteLength = 0; // Assume zero when length is absent
|
2022-08-15 23:55:44 +08:00
|
|
|
|
if (usePalette || protocolversion < Protocol18Handler.MC_1_13_Version)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
paletteLength = dataTypes.ReadNextVarInt(cache);
|
|
|
|
|
|
|
|
|
|
|
|
int[] palette = new int[paletteLength];
|
|
|
|
|
|
for (int i = 0; i < paletteLength; i++)
|
|
|
|
|
|
palette[i] = dataTypes.ReadNextVarInt(cache);
|
|
|
|
|
|
|
|
|
|
|
|
// Bit mask covering bitsPerBlock bits
|
|
|
|
|
|
// EG, if bitsPerBlock = 5, valueMask = 00011111 in binary
|
|
|
|
|
|
uint valueMask = (uint)((1 << bitsPerBlock) - 1);
|
|
|
|
|
|
|
2020-11-29 03:48:35 +08:00
|
|
|
|
// Block IDs are packed in the array of 64-bits integers
|
2019-05-01 15:23:30 +02:00
|
|
|
|
ulong[] dataArray = dataTypes.ReadNextULongArray(cache);
|
|
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Chunk chunk = new();
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
if (dataArray.Length > 0)
|
|
|
|
|
|
{
|
2020-11-29 03:48:35 +08:00
|
|
|
|
int longIndex = 0;
|
|
|
|
|
|
int startOffset = 0 - bitsPerBlock;
|
|
|
|
|
|
|
2019-05-01 15:23:30 +02:00
|
|
|
|
for (int blockY = 0; blockY < Chunk.SizeY; blockY++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int blockZ = 0; blockZ < Chunk.SizeZ; blockZ++)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int blockX = 0; blockX < Chunk.SizeX; blockX++)
|
|
|
|
|
|
{
|
2020-11-29 03:48:35 +08:00
|
|
|
|
// NOTICE: In the future a single ushort may not store the entire block id;
|
|
|
|
|
|
// the Block class may need to change if block state IDs go beyond 65535
|
|
|
|
|
|
ushort blockId;
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
2020-11-29 03:48:35 +08:00
|
|
|
|
// Calculate location of next block ID inside the array of Longs
|
|
|
|
|
|
startOffset += bitsPerBlock;
|
|
|
|
|
|
bool overlap = false;
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
2020-11-29 03:48:35 +08:00
|
|
|
|
if ((startOffset + bitsPerBlock) > 64)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
2022-08-15 23:55:44 +08:00
|
|
|
|
if (protocolversion >= Protocol18Handler.MC_1_16_Version)
|
2020-11-29 03:48:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
// In MC 1.16+, padding is applied to prevent overlapping between Longs:
|
|
|
|
|
|
// [ LONG INTEGER ][ LONG INTEGER ]
|
|
|
|
|
|
// [Block][Block][Block]XXXXX[Block][Block][Block]XXXXX
|
|
|
|
|
|
|
|
|
|
|
|
// When overlapping, move forward to the beginning of the next Long
|
|
|
|
|
|
startOffset = 0;
|
|
|
|
|
|
longIndex++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// In MC 1.15 and lower, block IDs can overlap between Longs:
|
|
|
|
|
|
// [ LONG INTEGER ][ LONG INTEGER ]
|
|
|
|
|
|
// [Block][Block][Block][Blo ck][Block][Block][Block][
|
|
|
|
|
|
|
|
|
|
|
|
// Detect when we reached the next Long or switch to overlap mode
|
|
|
|
|
|
if (startOffset >= 64)
|
|
|
|
|
|
{
|
|
|
|
|
|
startOffset -= 64;
|
|
|
|
|
|
longIndex++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else overlap = true;
|
|
|
|
|
|
}
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
2020-11-29 03:48:35 +08:00
|
|
|
|
|
|
|
|
|
|
// Extract Block ID
|
|
|
|
|
|
if (overlap)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
int endOffset = 64 - startOffset;
|
2020-11-29 03:48:35 +08:00
|
|
|
|
blockId = (ushort)((dataArray[longIndex] >> startOffset | dataArray[longIndex + 1] << endOffset) & valueMask);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
blockId = (ushort)((dataArray[longIndex] >> startOffset) & valueMask);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-29 03:48:35 +08:00
|
|
|
|
// Map small IDs to actual larger block IDs
|
2019-05-01 15:23:30 +02:00
|
|
|
|
if (usePalette)
|
|
|
|
|
|
{
|
2020-11-29 03:48:35 +08:00
|
|
|
|
if (paletteLength <= blockId)
|
|
|
|
|
|
{
|
|
|
|
|
|
int blockNumber = (blockY * Chunk.SizeZ + blockZ) * Chunk.SizeX + blockX;
|
|
|
|
|
|
throw new IndexOutOfRangeException(String.Format("Block ID {0} is outside Palette range 0-{1}! (bitsPerBlock: {2}, blockNumber: {3})",
|
|
|
|
|
|
blockId,
|
|
|
|
|
|
paletteLength - 1,
|
|
|
|
|
|
bitsPerBlock,
|
|
|
|
|
|
blockNumber));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-01 15:23:30 +02:00
|
|
|
|
blockId = (ushort)palette[blockId];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-29 03:48:35 +08:00
|
|
|
|
// We have our block, save the block into the chunk
|
2022-08-25 01:34:07 +08:00
|
|
|
|
chunk.SetWithoutCheck(blockX, blockY, blockZ, new Block(blockId));
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//We have our chunk, save the chunk into the world
|
2022-08-31 19:50:11 +08:00
|
|
|
|
world.StoreChunk(chunkX, chunkY, chunkZ, chunkColumnSize, chunk, chunkY == maxChunkY);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
//Pre-1.14 Lighting data
|
2022-08-15 23:55:44 +08:00
|
|
|
|
if (protocolversion < Protocol18Handler.MC_1_14_Version)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
//Skip block light
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ) / 2, cache);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
//Skip sky light
|
|
|
|
|
|
if (currentDimension == 0)
|
|
|
|
|
|
// Sky light is not sent in the nether or the end
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ) / 2, cache);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Don't worry about skipping remaining data since there is no useful data afterwards in 1.9
|
|
|
|
|
|
// (plus, it would require parsing the tile entity lists' NBT)
|
|
|
|
|
|
}
|
2022-08-15 23:55:44 +08:00
|
|
|
|
else if (protocolversion >= Protocol18Handler.MC_1_8_Version)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
// 1.8 chunk format
|
|
|
|
|
|
if (chunksContinuous && chunkMask == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Unload the entire chunk column
|
2021-05-15 16:31:02 +02:00
|
|
|
|
handler.InvokeOnMainThread(() =>
|
2021-05-13 02:07:53 +08:00
|
|
|
|
{
|
2022-08-25 01:34:07 +08:00
|
|
|
|
world[chunkX, chunkZ] = null;
|
2021-05-15 16:31:02 +02:00
|
|
|
|
});
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//Load chunk data from the server
|
2022-08-25 01:34:07 +08:00
|
|
|
|
int maxChunkY = sizeof(int) * 8 - 1 - BitOperations.LeadingZeroCount(chunkMask);
|
|
|
|
|
|
for (int chunkY = 0; chunkY <= maxChunkY; chunkY++)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
if ((chunkMask & (1 << chunkY)) != 0)
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Chunk chunk = new();
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
//Read chunk data, all at once for performance reasons, and build the chunk object
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Queue<ushort> queue = new(dataTypes.ReadNextUShortsLittleEndian(Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ, cache));
|
2019-05-01 15:23:30 +02:00
|
|
|
|
for (int blockY = 0; blockY < Chunk.SizeY; blockY++)
|
|
|
|
|
|
for (int blockZ = 0; blockZ < Chunk.SizeZ; blockZ++)
|
|
|
|
|
|
for (int blockX = 0; blockX < Chunk.SizeX; blockX++)
|
2022-08-25 01:34:07 +08:00
|
|
|
|
chunk.SetWithoutCheck(blockX, blockY, blockZ, new Block(queue.Dequeue()));
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
//We have our chunk, save the chunk into the world
|
2022-08-31 19:50:11 +08:00
|
|
|
|
world.StoreChunk(chunkX, chunkY, chunkZ, chunkColumnSize, chunk, chunkY == maxChunkY);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Skip light information
|
2022-07-23 22:34:16 +08:00
|
|
|
|
for (int chunkY = 0; chunkY < chunkColumnSize; chunkY++)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
if ((chunkMask & (1 << chunkY)) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Skip block light
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ) / 2, cache);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
//Skip sky light
|
|
|
|
|
|
if (hasSkyLight)
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ) / 2, cache);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Skip biome metadata
|
|
|
|
|
|
if (chunksContinuous)
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData(Chunk.SizeX * Chunk.SizeZ, cache);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1.7 chunk format
|
|
|
|
|
|
if (chunksContinuous && chunkMask == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Unload the entire chunk column
|
2021-05-15 16:31:02 +02:00
|
|
|
|
handler.InvokeOnMainThread(() =>
|
2021-05-13 02:07:53 +08:00
|
|
|
|
{
|
2022-08-25 01:34:07 +08:00
|
|
|
|
world[chunkX, chunkZ] = null;
|
2021-05-15 16:31:02 +02:00
|
|
|
|
});
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//Count chunk sections
|
|
|
|
|
|
int sectionCount = 0;
|
|
|
|
|
|
int addDataSectionCount = 0;
|
2022-07-23 22:34:16 +08:00
|
|
|
|
for (int chunkY = 0; chunkY < chunkColumnSize; chunkY++)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
if ((chunkMask & (1 << chunkY)) != 0)
|
|
|
|
|
|
sectionCount++;
|
|
|
|
|
|
if ((chunkMask2 & (1 << chunkY)) != 0)
|
|
|
|
|
|
addDataSectionCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Read chunk data, unpacking 4-bit values into 8-bit values for block metadata
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Queue<byte> blockTypes = new(dataTypes.ReadData(Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ * sectionCount, cache));
|
|
|
|
|
|
Queue<byte> blockMeta = new();
|
2019-05-01 15:23:30 +02:00
|
|
|
|
foreach (byte packed in dataTypes.ReadData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ * sectionCount) / 2, cache))
|
|
|
|
|
|
{
|
|
|
|
|
|
byte hig = (byte)(packed >> 4);
|
|
|
|
|
|
byte low = (byte)(packed & (byte)0x0F);
|
|
|
|
|
|
blockMeta.Enqueue(hig);
|
|
|
|
|
|
blockMeta.Enqueue(low);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Skip data we don't need
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ * sectionCount) / 2, cache); //Block light
|
2019-05-01 15:23:30 +02:00
|
|
|
|
if (hasSkyLight)
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ * sectionCount) / 2, cache); //Sky light
|
|
|
|
|
|
dataTypes.DropData((Chunk.SizeX * Chunk.SizeY * Chunk.SizeZ * addDataSectionCount) / 2, cache); //BlockAdd
|
2019-05-01 15:23:30 +02:00
|
|
|
|
if (chunksContinuous)
|
2022-08-28 14:57:44 +08:00
|
|
|
|
dataTypes.DropData(Chunk.SizeX * Chunk.SizeZ, cache); //Biomes
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
//Load chunk data
|
2022-08-25 01:34:07 +08:00
|
|
|
|
int maxChunkY = sizeof(int) * 8 - 1 - BitOperations.LeadingZeroCount(chunkMask);
|
|
|
|
|
|
for (int chunkY = 0; chunkY <= maxChunkY; chunkY++)
|
2019-05-01 15:23:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
if ((chunkMask & (1 << chunkY)) != 0)
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
Chunk chunk = new();
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
for (int blockY = 0; blockY < Chunk.SizeY; blockY++)
|
|
|
|
|
|
for (int blockZ = 0; blockZ < Chunk.SizeZ; blockZ++)
|
|
|
|
|
|
for (int blockX = 0; blockX < Chunk.SizeX; blockX++)
|
2022-08-25 01:34:07 +08:00
|
|
|
|
chunk.SetWithoutCheck(blockX, blockY, blockZ, new Block(blockTypes.Dequeue(), blockMeta.Dequeue()));
|
2019-05-01 15:23:30 +02:00
|
|
|
|
|
2022-08-31 19:50:11 +08:00
|
|
|
|
world.StoreChunk(chunkX, chunkY, chunkZ, chunkColumnSize, chunk, chunkY == maxChunkY);
|
2019-05-01 15:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|