From 6406100bef8483c598f1e1f8eb27f257a0769622 Mon Sep 17 00:00:00 2001
From: CursedFlames <18627001+CursedFlames@users.noreply.github.com>
Date: Tue, 9 Jan 2024 19:14:29 +1300
Subject: [PATCH] Address PR review comments; fix broken test
---
.../world/level/chunklike/CloPos.java | 20 +++++++++----------
.../level/TestCubicDistanceManager.java | 8 ++++----
2 files changed, 13 insertions(+), 15 deletions(-)
diff --git a/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/chunklike/CloPos.java b/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/chunklike/CloPos.java
index 30a83ff1..1e981059 100644
--- a/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/chunklike/CloPos.java
+++ b/src/main/java/io/github/opencubicchunks/cubicchunks/world/level/chunklike/CloPos.java
@@ -17,7 +17,13 @@
* When packed as a long, chunk positions are encoded the same as {@link ChunkPos#toLong}; cube positions are packed with 21 bits per axis. The parity of the top two bits of the long is
* used to distinguish between chunks and cubes (if bit 0 XOR bit 1, it is a cube, otherwise it is a chunk).
*
- * Also note that the top two bits (the parity bit, and the top bit of the Z coordinate) are inverted, as otherwise {@link Long#MAX_VALUE} would be a valid position (-1, -1, -1).
+ * Also note that for cubes the top two bits (the parity bit, and the top bit of the Z coordinate) are inverted, as otherwise {@link Long#MAX_VALUE} would be a valid position (-1, -1, -1).
+ *
+ * Invalid CloPos long:
0b01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
+ * Positive Z cube CloPos long:
0b01ZZZZZZ ZZZZZZZZ ZZZZZZYY YYYYYYYY YYYYYYYY YYYXXXXX XXXXXXXX XXXXXXXX
+ * Negative Z cube CloPos long:
0b10ZZZZZZ ZZZZZZZZ ZZZZZZYY YYYYYYYY YYYYYYYY YYYXXXXX XXXXXXXX XXXXXXXX
+ * Positive Z chunk CloPos long:
0b00ZZZZZZ ZZZZZZZZ ZZZZZZZZ ZZZZZZZZ XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
+ * Negative Z chunk CloPos long:
0b11ZZZZZZ ZZZZZZZZ ZZZZZZZZ ZZZZZZZZ XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
*/
public class CloPos {
private static final int CLO_Y_COLUMN_INDICATOR = Integer.MAX_VALUE;
@@ -132,17 +138,9 @@ public static CloPos fromLong(long cloPos) {
public long asLong() {
if (isCube()) {
- long i = 0L;
- i |= ((long) this.x & (1 << 21) - 1);
- i |= ((long) this.y & (1 << 21) - 1) << 21;
- i |= ((long) this.z & (1 << 21) - 1) << 42;
- // If 2nd bit isn't set, set 1st bit, since cubes are marked by starting with 0b01 or 0b10
- if (i < (1L << 62)) i |= (1L << 63);
- // invert the top two bits for storage
- i ^= TOP_TWO_BITS_MASK;
- return i;
+ return CloPos.asLong(x, y, z);
} else {
- return ChunkPos.asLong(this.x, this.z);
+ return CloPos.asLong(x, z);
}
}
diff --git a/src/test/java/io/github/opencubicchunks/cubicchunks/test/server/level/TestCubicDistanceManager.java b/src/test/java/io/github/opencubicchunks/cubicchunks/test/server/level/TestCubicDistanceManager.java
index b691302c..ce2c21c9 100644
--- a/src/test/java/io/github/opencubicchunks/cubicchunks/test/server/level/TestCubicDistanceManager.java
+++ b/src/test/java/io/github/opencubicchunks/cubicchunks/test/server/level/TestCubicDistanceManager.java
@@ -273,18 +273,18 @@ private void testAddAndRemove(DistanceManager distanceManager, int iterations, i
@Test public void testShouldForceTicksVanilla() {
var distanceManager = setupDistanceManager();
distanceManager.addRegionTicket(TicketType.START, new ChunkPos(0, 0), 0, Unit.INSTANCE, true);
- assertTrue(distanceManager.shouldForceTicks(0));
+ assertTrue(distanceManager.shouldForceTicks(ChunkPos.asLong(0, 0)));
distanceManager.removeRegionTicket(TicketType.START, new ChunkPos(0, 0), 0, Unit.INSTANCE, true);
- assertFalse(distanceManager.shouldForceTicks(0));
+ assertFalse(distanceManager.shouldForceTicks(ChunkPos.asLong(0, 0)));
}
@Test public void testShouldForceTicks() {
var distanceManager = setupDistanceManager();
((MarkableAsCubic) distanceManager).cc_setCubic();
((CubicDistanceManager)distanceManager).addRegionTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE, true);
- assertTrue(distanceManager.shouldForceTicks(0));
+ assertTrue(distanceManager.shouldForceTicks(CloPos.asLong(0, 0, 0)));
((CubicDistanceManager)distanceManager).removeRegionTicket(CubicTicketType.START, CloPos.cube(0, 0, 0), 0, Unit.INSTANCE, true);
- assertFalse(distanceManager.shouldForceTicks(0));
+ assertFalse(distanceManager.shouldForceTicks(CloPos.asLong(0, 0, 0)));
}
// Tests for PlayerTicketTracker