mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-11-07 17:36:07 +00:00
1.16+ Terrain and Movement support (#1353)
* First implementation * Improve chunk reading performance * Fix indentation * Remove debug information * Update MultiBlockChange packet * Move skip varint to a method * Fix crash when not using block palette * Fix DataTypes.cs not compiling on .NET 4.0 Binary (0b) values not handled so converted to Hexadecimal (0x) * Use the 1.16 chunk parsing code for 1.15 too Document the differences in padding and factor the code Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
parent
9b5fde0689
commit
53bd56100f
6 changed files with 1505 additions and 44 deletions
|
|
@ -267,6 +267,19 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
return i;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skip a VarInt from a cache of bytes with better performance
|
||||
/// </summary>
|
||||
/// <param name="cache">Cache of bytes to read from</param>
|
||||
public void SkipNextVarInt(Queue<byte> cache)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if ((ReadNextByte(cache) & 0x80) != 128)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read an "extended short", which is actually an int of some kind, from the cache of bytes.
|
||||
/// This is only done with forge. It looks like it's a normal short, except that if the high
|
||||
|
|
@ -286,6 +299,31 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
return ((high & 0xFF) << 15) | low;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a long from a cache of bytes and remove it from the cache
|
||||
/// </summary>
|
||||
/// <param name="cache">Cache of bytes to read from</param>
|
||||
/// <returns>The long value</returns>
|
||||
public long ReadNextVarLong(Queue<byte> cache)
|
||||
{
|
||||
int numRead = 0;
|
||||
long result = 0;
|
||||
byte read;
|
||||
do
|
||||
{
|
||||
read = ReadNextByte(cache);
|
||||
long value = (read & 0x7F);
|
||||
result |= (value << (7 * numRead));
|
||||
|
||||
numRead++;
|
||||
if (numRead > 10)
|
||||
{
|
||||
throw new OverflowException("VarLong is too big");
|
||||
}
|
||||
} while ((read & 0x80) != 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a single byte from a cache of bytes and remove it from the cache
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
this.pTerrain = new Protocol18Terrain(protocolVersion, dataTypes, handler);
|
||||
this.packetPalette = new PacketTypeHandler(protocolVersion).GetTypeHandler();
|
||||
|
||||
if (handler.GetTerrainEnabled() && protocolversion > MC1152Version)
|
||||
if (handler.GetTerrainEnabled() && protocolversion > MC1164Version)
|
||||
{
|
||||
Translations.WriteLineFormatted("extra.terrainandmovement_disabled");
|
||||
handler.SetTerrainEnabled(false);
|
||||
|
|
@ -101,9 +101,11 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
// Block palette
|
||||
if (protocolversion >= MC113Version)
|
||||
{
|
||||
if (protocolVersion > MC1152Version && handler.GetTerrainEnabled())
|
||||
if (protocolVersion > MC1164Version && handler.GetTerrainEnabled())
|
||||
throw new NotImplementedException(Translations.Get("exception.palette.block"));
|
||||
if (protocolVersion >= MC115Version)
|
||||
if (protocolVersion >= MC116Version)
|
||||
Block.Palette = new Palette116();
|
||||
else if (protocolVersion >= MC115Version)
|
||||
Block.Palette = new Palette115();
|
||||
else if (protocolVersion >= MC114Version)
|
||||
Block.Palette = new Palette114();
|
||||
|
|
@ -398,6 +400,8 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
int chunkX = dataTypes.ReadNextInt(packetData);
|
||||
int chunkZ = dataTypes.ReadNextInt(packetData);
|
||||
bool chunksContinuous = dataTypes.ReadNextBool(packetData);
|
||||
if (protocolversion >= MC116Version && protocolversion <= MC1161Version)
|
||||
dataTypes.ReadNextBool(packetData); // Ignore old data - 1.16 to 1.16.1 only
|
||||
ushort chunkMask = protocolversion >= MC19Version
|
||||
? (ushort)dataTypes.ReadNextVarInt(packetData)
|
||||
: dataTypes.ReadNextUShort(packetData);
|
||||
|
|
@ -413,8 +417,23 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
{
|
||||
if (protocolversion >= MC114Version)
|
||||
dataTypes.ReadNextNbt(packetData); // Heightmaps - 1.14 and above
|
||||
int biomesLength = 0;
|
||||
if (protocolversion >= MC1162Version)
|
||||
if (chunksContinuous)
|
||||
biomesLength = dataTypes.ReadNextVarInt(packetData); // Biomes length - 1.16.2 and above
|
||||
if (protocolversion >= MC115Version && chunksContinuous)
|
||||
dataTypes.ReadData(1024 * 4, packetData); // Biomes - 1.15 and above
|
||||
{
|
||||
if (protocolversion >= MC1162Version)
|
||||
{
|
||||
for (int i = 0; i < biomesLength; i++)
|
||||
{
|
||||
// Biomes - 1.16.2 and above
|
||||
// Don't use ReadNextVarInt because it cost too much time
|
||||
dataTypes.SkipNextVarInt(packetData);
|
||||
}
|
||||
}
|
||||
else dataTypes.ReadData(1024 * 4, packetData); // Biomes - 1.15 and above
|
||||
}
|
||||
int dataSize = dataTypes.ReadNextVarInt(packetData);
|
||||
pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData);
|
||||
}
|
||||
|
|
@ -514,35 +533,62 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
case PacketTypesIn.MultiBlockChange:
|
||||
if (handler.GetTerrainEnabled())
|
||||
{
|
||||
int chunkX = dataTypes.ReadNextInt(packetData);
|
||||
int chunkZ = dataTypes.ReadNextInt(packetData);
|
||||
int recordCount = protocolversion < MC18Version
|
||||
? (int)dataTypes.ReadNextShort(packetData)
|
||||
: dataTypes.ReadNextVarInt(packetData);
|
||||
|
||||
for (int i = 0; i < recordCount; i++)
|
||||
if (protocolversion >= MC1162Version)
|
||||
{
|
||||
byte locationXZ;
|
||||
ushort blockIdMeta;
|
||||
int blockY;
|
||||
|
||||
if (protocolversion < MC18Version)
|
||||
long chunkSection = dataTypes.ReadNextLong(packetData);
|
||||
int sectionX = (int)(chunkSection >> 42);
|
||||
int sectionY = (int)((chunkSection << 44) >> 44);
|
||||
int sectionZ = (int)((chunkSection << 22) >> 42);
|
||||
dataTypes.ReadNextBool(packetData); // Useless boolean
|
||||
int blocksSize = dataTypes.ReadNextVarInt(packetData);
|
||||
for (int i = 0; i < blocksSize; i++)
|
||||
{
|
||||
blockIdMeta = dataTypes.ReadNextUShort(packetData);
|
||||
blockY = (ushort)dataTypes.ReadNextByte(packetData);
|
||||
locationXZ = dataTypes.ReadNextByte(packetData);
|
||||
}
|
||||
else
|
||||
{
|
||||
locationXZ = dataTypes.ReadNextByte(packetData);
|
||||
blockY = (ushort)dataTypes.ReadNextByte(packetData);
|
||||
blockIdMeta = (ushort)dataTypes.ReadNextVarInt(packetData);
|
||||
}
|
||||
ulong block = (ulong)dataTypes.ReadNextVarLong(packetData);
|
||||
int blockId = (int)(block >> 12);
|
||||
int localX = (int)((block >> 8) & 0x0F);
|
||||
int localZ = (int)((block >> 4) & 0x0F);
|
||||
int localY = (int)(block & 0x0F);
|
||||
|
||||
int blockX = locationXZ >> 4;
|
||||
int blockZ = locationXZ & 0x0F;
|
||||
Block block = new Block(blockIdMeta);
|
||||
handler.GetWorld().SetBlock(new Location(chunkX, chunkZ, blockX, blockY, blockZ), block);
|
||||
Block b = new Block((ushort)blockId);
|
||||
int blockX = (sectionX * 16) + localX;
|
||||
int blockY = (sectionY * 16) + localY;
|
||||
int blockZ = (sectionZ * 16) + localZ;
|
||||
var l = new Location(blockX, blockY, blockZ);
|
||||
handler.GetWorld().SetBlock(l, b);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int chunkX = dataTypes.ReadNextInt(packetData);
|
||||
int chunkZ = dataTypes.ReadNextInt(packetData);
|
||||
int recordCount = protocolversion < MC18Version
|
||||
? (int)dataTypes.ReadNextShort(packetData)
|
||||
: dataTypes.ReadNextVarInt(packetData);
|
||||
|
||||
for (int i = 0; i < recordCount; i++)
|
||||
{
|
||||
byte locationXZ;
|
||||
ushort blockIdMeta;
|
||||
int blockY;
|
||||
|
||||
if (protocolversion < MC18Version)
|
||||
{
|
||||
blockIdMeta = dataTypes.ReadNextUShort(packetData);
|
||||
blockY = (ushort)dataTypes.ReadNextByte(packetData);
|
||||
locationXZ = dataTypes.ReadNextByte(packetData);
|
||||
}
|
||||
else
|
||||
{
|
||||
locationXZ = dataTypes.ReadNextByte(packetData);
|
||||
blockY = (ushort)dataTypes.ReadNextByte(packetData);
|
||||
blockIdMeta = (ushort)dataTypes.ReadNextVarInt(packetData);
|
||||
}
|
||||
|
||||
int blockX = locationXZ >> 4;
|
||||
int blockZ = locationXZ & 0x0F;
|
||||
Block block = new Block(blockIdMeta);
|
||||
handler.GetWorld().SetBlock(new Location(chunkX, chunkZ, blockX, blockY, blockZ), block);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -75,43 +75,86 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
// EG, if bitsPerBlock = 5, valueMask = 00011111 in binary
|
||||
uint valueMask = (uint)((1 << bitsPerBlock) - 1);
|
||||
|
||||
// Block IDs are packed in the array of 64-bits integers
|
||||
ulong[] dataArray = dataTypes.ReadNextULongArray(cache);
|
||||
|
||||
Chunk chunk = new Chunk();
|
||||
|
||||
if (dataArray.Length > 0)
|
||||
{
|
||||
int longIndex = 0;
|
||||
int startOffset = 0 - bitsPerBlock;
|
||||
|
||||
for (int blockY = 0; blockY < Chunk.SizeY; blockY++)
|
||||
{
|
||||
for (int blockZ = 0; blockZ < Chunk.SizeZ; blockZ++)
|
||||
{
|
||||
for (int blockX = 0; blockX < Chunk.SizeX; blockX++)
|
||||
{
|
||||
int blockNumber = (blockY * Chunk.SizeZ + blockZ) * Chunk.SizeX + blockX;
|
||||
|
||||
int startLong = (blockNumber * bitsPerBlock) / 64;
|
||||
int startOffset = (blockNumber * bitsPerBlock) % 64;
|
||||
int endLong = ((blockNumber + 1) * bitsPerBlock - 1) / 64;
|
||||
|
||||
// TODO: In the future a single ushort may not store the entire block id;
|
||||
// the Block code may need to change if block state IDs go beyond 65535
|
||||
// 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;
|
||||
if (startLong == endLong)
|
||||
|
||||
// Calculate location of next block ID inside the array of Longs
|
||||
startOffset += bitsPerBlock;
|
||||
bool overlap = false;
|
||||
|
||||
if ((startOffset + bitsPerBlock) > 64)
|
||||
{
|
||||
blockId = (ushort)((dataArray[startLong] >> startOffset) & valueMask);
|
||||
if (protocolversion >= Protocol18Handler.MC116Version)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Extract Block ID
|
||||
if (overlap)
|
||||
{
|
||||
int endOffset = 64 - startOffset;
|
||||
blockId = (ushort)((dataArray[longIndex] >> startOffset | dataArray[longIndex + 1] << endOffset) & valueMask);
|
||||
}
|
||||
else
|
||||
{
|
||||
int endOffset = 64 - startOffset;
|
||||
blockId = (ushort)((dataArray[startLong] >> startOffset | dataArray[endLong] << endOffset) & valueMask);
|
||||
blockId = (ushort)((dataArray[longIndex] >> startOffset) & valueMask);
|
||||
}
|
||||
|
||||
// Map small IDs to actual larger block IDs
|
||||
if (usePalette)
|
||||
{
|
||||
// Get the real block ID out of the palette
|
||||
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));
|
||||
}
|
||||
|
||||
blockId = (ushort)palette[blockId];
|
||||
}
|
||||
|
||||
// We have our block, save the block into the chunk
|
||||
chunk[blockX, blockY, blockZ] = new Block(blockId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue