From 597d60bed56249edc43a31814ae4398d96b73f27 Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sun, 21 Jun 2020 15:09:04 -0500 Subject: [PATCH] Fix wrong packet ids, clean up chunk padded array and fix tests --- .../mc/protocol/MinecraftProtocol.java | 16 +++--- .../data/game/chunk/FlexibleStorage.java | 49 ++++++++++--------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java b/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java index 9952f72b..07202086 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java +++ b/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java @@ -389,7 +389,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerIncoming(0x0C, ServerBossBarPacket.class); this.registerIncoming(0x0D, ServerDifficultyPacket.class); this.registerIncoming(0x0E, ServerChatPacket.class); - this.registerIncoming(0x1F, ServerMultiBlockChangePacket.class); + this.registerIncoming(0x0F, ServerMultiBlockChangePacket.class); this.registerIncoming(0x10, ServerTabCompletePacket.class); this.registerIncoming(0x11, ServerDeclareCommandsPacket.class); this.registerIncoming(0x12, ServerConfirmTransactionPacket.class); @@ -421,7 +421,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerIncoming(0x2C, ServerVehicleMovePacket.class); this.registerIncoming(0x2D, ServerOpenBookPacket.class); this.registerIncoming(0x2E, ServerOpenWindowPacket.class); - this.registerIncoming(0x3F, ServerOpenTileEntityEditorPacket.class); + this.registerIncoming(0x2F, ServerOpenTileEntityEditorPacket.class); this.registerIncoming(0x30, ServerPreparedCraftingGridPacket.class); this.registerIncoming(0x31, ServerPlayerAbilitiesPacket.class); this.registerIncoming(0x32, ServerCombatPacket.class); @@ -437,7 +437,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerIncoming(0x3C, ServerAdvancementTabPacket.class); this.registerIncoming(0x3D, ServerWorldBorderPacket.class); this.registerIncoming(0x3E, ServerSwitchCameraPacket.class); - this.registerIncoming(0x4F, ServerPlayerChangeHeldItemPacket.class); + this.registerIncoming(0x3F, ServerPlayerChangeHeldItemPacket.class); this.registerIncoming(0x40, ServerUpdateViewPositionPacket.class); this.registerIncoming(0x41, ServerUpdateViewDistancePacket.class); this.registerIncoming(0x42, ServerSpawnPositionPacket.class); @@ -453,7 +453,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerIncoming(0x4C, ServerTeamPacket.class); this.registerIncoming(0x4D, ServerUpdateScorePacket.class); this.registerIncoming(0x4E, ServerUpdateTimePacket.class); - this.registerIncoming(0x5F, ServerTitlePacket.class); + this.registerIncoming(0x4F, ServerTitlePacket.class); this.registerIncoming(0x50, ServerEntitySoundEffectPacket.class); this.registerIncoming(0x51, ServerPlayBuiltinSoundPacket.class); this.registerIncoming(0x52, ServerStopSoundPacket.class); @@ -482,7 +482,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerOutgoing(0x0C, ClientEditBookPacket.class); this.registerOutgoing(0x0D, ClientEntityNBTRequestPacket.class); this.registerOutgoing(0x0E, ClientPlayerInteractEntityPacket.class); - this.registerIncoming(0x0F, ClientGenerateStructuresPacket.class); + this.registerOutgoing(0x0F, ClientGenerateStructuresPacket.class); this.registerOutgoing(0x10, ClientKeepAlivePacket.class); this.registerOutgoing(0x11, ClientLockDifficultyPacket.class); this.registerOutgoing(0x12, ClientPlayerPositionPacket.class); @@ -596,7 +596,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerOutgoing(0x1C, ServerExplosionPacket.class); this.registerOutgoing(0x1D, ServerUnloadChunkPacket.class); this.registerOutgoing(0x1E, ServerNotifyClientPacket.class); - this.registerOutgoing(0x2F, ServerOpenHorseWindowPacket.class); + this.registerOutgoing(0x1F, ServerOpenHorseWindowPacket.class); this.registerOutgoing(0x20, ServerKeepAlivePacket.class); this.registerOutgoing(0x21, ServerChunkDataPacket.class); this.registerOutgoing(0x22, ServerPlayEffectPacket.class); @@ -612,7 +612,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerOutgoing(0x2C, ServerVehicleMovePacket.class); this.registerOutgoing(0x2D, ServerOpenBookPacket.class); this.registerOutgoing(0x2E, ServerOpenWindowPacket.class); - this.registerOutgoing(0x3F, ServerOpenTileEntityEditorPacket.class); + this.registerOutgoing(0x2F, ServerOpenTileEntityEditorPacket.class); this.registerOutgoing(0x30, ServerPreparedCraftingGridPacket.class); this.registerOutgoing(0x31, ServerPlayerAbilitiesPacket.class); this.registerOutgoing(0x32, ServerCombatPacket.class); @@ -628,7 +628,7 @@ public class MinecraftProtocol extends PacketProtocol { this.registerOutgoing(0x3C, ServerAdvancementTabPacket.class); this.registerOutgoing(0x3D, ServerWorldBorderPacket.class); this.registerOutgoing(0x3E, ServerSwitchCameraPacket.class); - this.registerOutgoing(0x4F, ServerPlayerChangeHeldItemPacket.class); + this.registerOutgoing(0x3F, ServerPlayerChangeHeldItemPacket.class); this.registerOutgoing(0x40, ServerUpdateViewPositionPacket.class); this.registerOutgoing(0x41, ServerUpdateViewDistancePacket.class); this.registerOutgoing(0x42, ServerSpawnPositionPacket.class); diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/FlexibleStorage.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/FlexibleStorage.java index 8845706d..27b4c38f 100644 --- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/FlexibleStorage.java +++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/FlexibleStorage.java @@ -17,20 +17,12 @@ public class FlexibleStorage { } public FlexibleStorage(int bitsPerEntry, @NonNull long[] data) { - if(bitsPerEntry < 4) { - bitsPerEntry = 4; - } - - char valuesPerLong = (char) (64 / bitsPerEntry); - int expectedLength = (4096 + valuesPerLong - 1) / valuesPerLong; - this.bitsPerEntry = bitsPerEntry; - this.data = padArray(bitsPerEntry, Arrays.copyOf(data, data.length), valuesPerLong, expectedLength); - - if (this.data.length != expectedLength) { - throw new IllegalArgumentException("Got " + this.data.length + " as the chunk data length, but was expecting " + expectedLength); + if(bitsPerEntry <= 8) { + this.data = padArray(bitsPerEntry, Arrays.copyOf(data, data.length)); + } else { + this.data = padArray(bitsPerEntry, 14, Arrays.copyOf(data, data.length)); } - this.size = data.length * 64 / this.bitsPerEntry; this.maxEntryValue = (1L << this.bitsPerEntry) - 1; } @@ -109,25 +101,34 @@ public class FlexibleStorage { 70409299, 70409299, 0, 69273666, 69273666, 0, 68174084, 68174084, 0, Integer.MIN_VALUE, 0, 5 }; - private static long[] padArray(int bitsPerEntry, long[] oldData, char valuesPerLong, int size) { + private static long[] padArray(int bitsPerEntry, long[] oldData) { + return padArray(bitsPerEntry, bitsPerEntry, oldData); + } + + private static long[] padArray(int bitsPerEntry, int newBitsPerEntry, long[] oldData) { + long maxEntryValue = (1L << bitsPerEntry) - 1; + char valuesPerLong = (char) (64 / newBitsPerEntry); int magicIndex = (valuesPerLong - 1) * 3; + long divideMultiply = Integer.toUnsignedLong(MAGIC_CHUNK_VALUES[magicIndex]); + long divideAdd = Integer.toUnsignedLong(MAGIC_CHUNK_VALUES[magicIndex + 1]); int divideShift = MAGIC_CHUNK_VALUES[magicIndex + 2]; - long maxEntries = (1L << bitsPerEntry) - 1; - long[] data = new long[size]; - for (int index = 0; index < 4096; index++) { - int startIndex = (index * bitsPerEntry) / 64; + long[] data = new long[(4096 + valuesPerLong - 1) / valuesPerLong]; + for(int index = 0; index < 4096; index++) { + int bitIndex = index * bitsPerEntry; + int startIndex = bitIndex / 64; int endIndex = ((index + 1) * bitsPerEntry - 1) / 64; - int startBitSubIndex = (index * bitsPerEntry) % 64; + int startBitSubIndex = bitIndex % 64; int value; - if (startIndex != endIndex) { - value = (int) ((oldData[startIndex] >>> startBitSubIndex | oldData[endIndex] << (64 - startBitSubIndex)) & maxEntries); + if(startIndex != endIndex) { + int endBitSubIndex = 64 - startBitSubIndex; + value = (int) ((oldData[startIndex] >>> startBitSubIndex | oldData[endIndex] << endBitSubIndex) & maxEntryValue); } else { - value = (int) (oldData[startIndex] >>> startBitSubIndex & maxEntries); + value = (int) (oldData[startIndex] >>> startBitSubIndex & maxEntryValue); } - int cellIndex = (int) (index * ((long) MAGIC_CHUNK_VALUES[magicIndex]) & 0xffffffffL + ((long) MAGIC_CHUNK_VALUES[magicIndex + 1]) & 0xffffffffL >> 32L >> divideShift); - int bitIndex = (index - cellIndex * valuesPerLong) * bitsPerEntry; - data[cellIndex] = data[cellIndex] & ~(maxEntries << bitIndex) | (value & maxEntries) << bitIndex; + int cellIndex = (int) (index * divideMultiply + divideAdd >> 32L >> divideShift); + int newBitIndex = (index - cellIndex * valuesPerLong) * newBitsPerEntry; + data[cellIndex] = data[cellIndex] & ~(maxEntryValue << newBitIndex) | (value & maxEntryValue) << newBitIndex; } return data; }