diff --git a/MinecraftClient/Mapping/Chunk.cs b/MinecraftClient/Mapping/Chunk.cs index a64cc329..5b84ecf2 100644 --- a/MinecraftClient/Mapping/Chunk.cs +++ b/MinecraftClient/Mapping/Chunk.cs @@ -57,7 +57,7 @@ namespace MinecraftClient.Mapping /// The block 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]; } } } diff --git a/MinecraftClient/Mapping/Location.cs b/MinecraftClient/Mapping/Location.cs index a34f7a0e..e55c5a88 100644 --- a/MinecraftClient/Mapping/Location.cs +++ b/MinecraftClient/Mapping/Location.cs @@ -46,6 +46,21 @@ namespace MinecraftClient.Mapping Z = z; } + /// + /// Create a new location + /// + /// Location of the chunk into the world + /// Location of the chunk into the world + /// Location of the block into the chunk + /// Location of the block into the world + /// Location of the block into the chunk + public Location(int chunkX, int chunkZ, int blockX, int blockY, int blockZ) + { + X = chunkX * Chunk.SizeX + blockX; + Y = blockY; + Z = chunkZ * Chunk.SizeZ + blockZ; + } + /// /// The X index of the corresponding chunk in the world /// @@ -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); } /// diff --git a/MinecraftClient/McTcpClient.cs b/MinecraftClient/McTcpClient.cs index 0e74b0e3..88baf667 100644 --- a/MinecraftClient/McTcpClient.cs +++ b/MinecraftClient/McTcpClient.cs @@ -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; } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 06f4fa30..6d395676 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -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 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; }