mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Terrain: Fix coordinate parsing (negative coords)
- Optimize readNextUShortsLittleEndian network reading method - Various coordinate computation issues, negative chunk offsets - Properly parse negative coordinates for block change events - Properly reach ground if less than 1 block over the ground
This commit is contained in:
parent
cb00c28b6e
commit
5d8d42e3d1
4 changed files with 43 additions and 22 deletions
|
|
@ -57,7 +57,7 @@ namespace MinecraftClient.Mapping
|
|||
/// <returns>The block</returns>
|
||||
public Block GetBlock(Location location)
|
||||
{
|
||||
return this[((int)location.X) % Chunk.SizeX, ((int)location.Y) % Chunk.SizeY, ((int)location.Z) % Chunk.SizeZ];
|
||||
return this[location.ChunkBlockX, location.ChunkBlockY, location.ChunkBlockZ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,6 +46,21 @@ namespace MinecraftClient.Mapping
|
|||
Z = z;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new location
|
||||
/// </summary>
|
||||
/// <param name="chunkX">Location of the chunk into the world</param>
|
||||
/// <param name="chunkZ">Location of the chunk into the world</param>
|
||||
/// <param name="blockX">Location of the block into the chunk</param>
|
||||
/// <param name="blockY">Location of the block into the world</param>
|
||||
/// <param name="blockZ">Location of the block into the chunk</param>
|
||||
public Location(int chunkX, int chunkZ, int blockX, int blockY, int blockZ)
|
||||
{
|
||||
X = chunkX * Chunk.SizeX + blockX;
|
||||
Y = blockY;
|
||||
Z = chunkZ * Chunk.SizeZ + blockZ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The X index of the corresponding chunk in the world
|
||||
/// </summary>
|
||||
|
|
@ -53,7 +68,7 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
get
|
||||
{
|
||||
return ((int)X) / Chunk.SizeX;
|
||||
return (int)Math.Floor(X / Chunk.SizeX);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -64,7 +79,7 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
get
|
||||
{
|
||||
return ((int)Y) / Chunk.SizeY;
|
||||
return (int)Math.Floor(Y / Chunk.SizeY);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +90,7 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
get
|
||||
{
|
||||
return ((int)Z) / Chunk.SizeY;
|
||||
return (int)Math.Floor(Z / Chunk.SizeZ);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -86,7 +101,7 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
get
|
||||
{
|
||||
return ((int)X) % Chunk.SizeX;
|
||||
return ((int)Math.Floor(X) % Chunk.SizeX + Chunk.SizeX) % Chunk.SizeX;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -97,7 +112,7 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
get
|
||||
{
|
||||
return ((int)Y) % Chunk.SizeY;
|
||||
return ((int)Math.Floor(Y) % Chunk.SizeY + Chunk.SizeY) % Chunk.SizeY;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -108,7 +123,7 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
get
|
||||
{
|
||||
return ((int)Z) % Chunk.SizeZ;
|
||||
return ((int)Math.Floor(Z) % Chunk.SizeZ + Chunk.SizeZ) % Chunk.SizeZ;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -153,7 +168,16 @@ namespace MinecraftClient.Mapping
|
|||
|
||||
public static Location FromLongRepresentation(ulong location)
|
||||
{
|
||||
return new Location(location >> 38, (location >> 26) & 0xFFF, location << 38 >> 38);
|
||||
int x = (int)(location >> 38);
|
||||
int y = (int)((location >> 26) & 0xFFF);
|
||||
int z = (int)(location << 38 >> 38);
|
||||
if (x >= 33554432)
|
||||
x -= 67108864;
|
||||
if (y >= 2048)
|
||||
y -= 4096;
|
||||
if (z >= 33554432)
|
||||
z -= 67108864;
|
||||
return new Location(x, y, z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -456,11 +456,15 @@ namespace MinecraftClient
|
|||
{
|
||||
lock (locationLock)
|
||||
{
|
||||
Location belowMe = location + new Location(0, -1, 0);
|
||||
Block blockBelowMe = world.GetBlock(belowMe);
|
||||
handler.SendLocationUpdate(location, blockBelowMe.Solid);
|
||||
if (!blockBelowMe.Solid)
|
||||
location = belowMe;
|
||||
Location onFoots = new Location(location.X, Math.Floor(location.Y), location.Z);
|
||||
Location belowFoots = location + new Location(0, -1, 0);
|
||||
Block blockOnFoots = world.GetBlock(onFoots);
|
||||
Block blockBelowFoots = world.GetBlock(belowFoots);
|
||||
handler.SendLocationUpdate(location, blockBelowFoots.Solid);
|
||||
if (!blockBelowFoots.Solid)
|
||||
location = belowFoots;
|
||||
else if (!blockOnFoots.Solid)
|
||||
location = onFoots;
|
||||
}
|
||||
updateTicks = 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -209,7 +209,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
int blockZ = locationXZ & 0x0F;
|
||||
int blockY = (ushort)readNextByte(packetData);
|
||||
Block block = new Block((ushort)readNextVarInt(packetData));
|
||||
handler.GetWorld().SetBlock(new Location(blockX + chunkX * Chunk.SizeX, blockY, blockZ + chunkZ * Chunk.SizeZ), block);
|
||||
handler.GetWorld().SetBlock(new Location(chunkX, chunkZ, blockX, blockY, blockZ), block);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
@ -653,16 +653,9 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
private static ushort[] readNextUShortsLittleEndian(int amount, List<byte> cache)
|
||||
{
|
||||
byte[] rawValues = readData(2 * amount, cache);
|
||||
byte[] rawValue = new byte[2];
|
||||
ushort[] result = new ushort[amount];
|
||||
|
||||
for (int i = 0; i < amount; i++)
|
||||
{
|
||||
rawValue[0] = rawValues[i * 2];
|
||||
rawValue[1] = rawValues[i * 2 + 1];
|
||||
result[i] = BitConverter.ToUInt16(rawValue, 0);
|
||||
}
|
||||
|
||||
result[i] = BitConverter.ToUInt16(rawValues, i * 2);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue