From 54bfa6d0ee6ac06b265fde981c12ca276eaa7f6b Mon Sep 17 00:00:00 2001 From: granny Date: Thu, 7 Nov 2024 01:26:36 -0800 Subject: [PATCH] Fix reversed byte order in custom file protocol Before, the packed integer was being turned into a reversed byte array. This isn't really an issue as long as it's converted back into a packed integer using the same logic. `settings.web-directory.read-only` will need to be set to `false` in the config when updating Pl3xMap to the version with this fix, since it includes changes to the webmap files. --- .../src/main/java/net/pl3x/map/core/util/ByteUtil.java | 10 +++++----- webmap/src/palette/BlockInfo.ts | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/net/pl3x/map/core/util/ByteUtil.java b/core/src/main/java/net/pl3x/map/core/util/ByteUtil.java index b2b85a8d7..b6a9a1b40 100644 --- a/core/src/main/java/net/pl3x/map/core/util/ByteUtil.java +++ b/core/src/main/java/net/pl3x/map/core/util/ByteUtil.java @@ -31,17 +31,17 @@ private ByteUtil() { } public static byte[] toBytes(int packed) { - byte[] bytes = new byte[4]; - for (int i = 0; i < 4; i++) { - bytes[i] = (byte) (packed >>> (i * 8)); + byte[] bytes = new byte[Integer.BYTES]; + for (int i = 0; i < Integer.BYTES; i++) { + bytes[i] = (byte) (packed >>> (Byte.SIZE * (Integer.BYTES - 1 - i))); } return bytes; } public static int getInt(@NotNull ByteBuffer buffer, int index) { int value = 0; - for (int i = 0; i < 4; i++) { - value |= (buffer.get(index + i) & 0xFF) << (i * 8); + for (int i = 0; i < Integer.BYTES; i++) { + value |= (buffer.get(index + i) & 0xFF) << (Byte.SIZE * (Integer.BYTES - 1 - i)); } return value; } diff --git a/webmap/src/palette/BlockInfo.ts b/webmap/src/palette/BlockInfo.ts index 254607926..9a0ce0091 100644 --- a/webmap/src/palette/BlockInfo.ts +++ b/webmap/src/palette/BlockInfo.ts @@ -1,6 +1,9 @@ import {Block} from "./Block"; export class BlockInfo { + public readonly BYTE_SIZE: number = 8; + public readonly INTEGER_BYTES: number = 4; + private readonly _data: Uint8Array; constructor(data: Uint8Array) { @@ -17,8 +20,8 @@ export class BlockInfo { private getInt(position: number): number { let val: number = 0; - for (let i: number = 0; i < 4; i++) { - val |= (this._data[position + i] & 0xFF) << (i * 8); + for (let i: number = 0; i < this.INTEGER_BYTES; i++) { + val |= (this._data[position + i] & 0xFF) << (this.BYTE_SIZE * ((this.INTEGER_BYTES - 1) - i)); } return val; }