diff --git a/src/main/java/net/montoyo/wd/block/BlockPeripheral.java b/src/main/java/net/montoyo/wd/block/BlockPeripheral.java index 76a8a5e..4acf076 100644 --- a/src/main/java/net/montoyo/wd/block/BlockPeripheral.java +++ b/src/main/java/net/montoyo/wd/block/BlockPeripheral.java @@ -80,7 +80,7 @@ public class BlockPeripheral extends WDBlockContainer { return ((TileEntityPeripheralBase) te).onRightClick(player, hand); else if(te instanceof TileEntityServer) { ((TileEntityServer) te).onPlayerRightClick(player); - return InteractionResult.PASS; + return InteractionResult.SUCCESS; } else return InteractionResult.FAIL; } diff --git a/src/main/java/net/montoyo/wd/net/BufferUtils.java b/src/main/java/net/montoyo/wd/net/BufferUtils.java index f310fa4..cb10fe0 100644 --- a/src/main/java/net/montoyo/wd/net/BufferUtils.java +++ b/src/main/java/net/montoyo/wd/net/BufferUtils.java @@ -10,12 +10,17 @@ import java.util.function.Supplier; public class BufferUtils { public static void writeUShort(FriendlyByteBuf buf, int v) { - // TODO: write a pair of bytes manually - buf.writeInt(v); + short s = (short) (v + Short.MIN_VALUE); + buf.writeByte((byte) (s & 0xFF)); + // I have no idea where this 128 came from + buf.writeByte((byte) (((s + 128) >> 8) & 0xFF)); } public static int readUShort(FriendlyByteBuf buf) { - return buf.readInt(); + int upack = (buf.readByte() + (buf.readByte() << 8)) - Short.MIN_VALUE; + // ???? + if (upack < 0) upack += 65536; + return upack; } public static void writeBytes(FriendlyByteBuf buf, byte[] data) { diff --git a/src/test/java/UShortToBytes.java b/src/test/java/UShortToBytes.java new file mode 100644 index 0000000..de4da4f --- /dev/null +++ b/src/test/java/UShortToBytes.java @@ -0,0 +1,24 @@ +public class UShortToBytes { + public static void main(String[] args) { + for (int i = 0; i < Short.MAX_VALUE * 2; i++) { + short s = (short) (i + Short.MIN_VALUE); + + byte[] data = new byte[]{ + (byte) (s & 0xFF), + (byte) (((s + 128) >> 8) & 0xFF), + }; + + int upack = data[0] + (data[1] << 8); + upack -= Short.MIN_VALUE; + if (upack < 0) + upack += 65536; + + if (upack != i) { + System.out.println(upack); + System.out.println(i); + } + } + + System.out.println("done"); + } +}