From d2b4154cc0dbb4593acd6aff0bfaa4b055bc9f73 Mon Sep 17 00:00:00 2001 From: Jordan Date: Fri, 17 Jun 2022 15:39:27 +0100 Subject: [PATCH] Properly support extended world heights (y > 255) in tripleBlockCoord (#1805) --- .../fastasyncworldedit/core/util/MathMan.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MathMan.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MathMan.java index ba9aae2d6..1885a9e12 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MathMan.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MathMan.java @@ -167,9 +167,20 @@ public class MathMan { return (((triple >> 40) & 0xffffff) << 38) >> 38; } + /** + * Pack a chunk-block coordinate into an int. Supports x and z 0 -> 15 and y = -2048 -> 2047 + * + * @param x x coordinate + * @param y y coordinate + * @param z z coordinate + * @return int packed by x, y and z coordinates + */ public static int tripleBlockCoord(int x, int y, int z) { - // account for the fact y can be negative now. Assume it won't be less than -256 - y += 256; + // We have 12 bits available to Y value, so keep it 0 -> 4095 + if (y < -2048 || y > 2047) { + throw new UnsupportedOperationException("TripleBlockCoord Y value cannot be outside range -2048 <= y <= 2047"); + } + y += 2048; return ((x & 15) << 16 | (z & 15) << 12 | y); } @@ -182,7 +193,7 @@ public class MathMan { } public static int untripleBlockCoordY(int triple) { - return (triple & 0x1ff) - 256; + return (triple & 0xfff) - 2048; } public static int untripleBlockCoordZ(int triple) {