Skip to content

Commit

Permalink
fix: fix block entity can be spawned in negative-y space
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Oct 1, 2024
1 parent 409cf34 commit 07557bd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions api/src/main/java/org/allaymc/api/utils/HashUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public int getZFromHashXZ(long hashXZ) {

public int hashChunkXYZ(int x, int y, int z) {
Preconditions.checkArgument(x >= 0 && x <= 15);
Preconditions.checkArgument(y >= 0 && y <= 16777216);
Preconditions.checkArgument(y >= -8388608 && y <= 8388607);
Preconditions.checkArgument(z >= 0 && z <= 15);
//Make sure x and z are in the range of 0-15
x &= 0xF; //4 bits
Expand All @@ -149,7 +149,7 @@ public int hashChunkXYZ(int x, int y, int z) {
//Place x in the top 4 digits
result |= (x << 28);
//Place y in the middle 24 bits
result |= (y & 0xFFFFFF) << 4;
result |= ((y + 8388608) & 0xFFFFFF) << 4;
//Place z in the lowest 4 digits
result |= z;
return result;
Expand All @@ -174,7 +174,7 @@ public int getXFromHashChunkXYZ(int encoded) {
* @return The value of y.
*/
public int getYFromHashChunkXYZ(int encoded) {
return (encoded >>> 4) & 0xFFFFFF;
return ((encoded >>> 4) & 0xFFFFFF) - 8388608;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions server/src/test/java/org/allaymc/server/utils/HashUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ public void testGetXZFromHashXZ() {
@Test
public void testHashChunkXYZ() {
int l = HashUtils.hashChunkXYZ(8, 64, 8);
Assertions.assertEquals(-2147482616, l);
Assertions.assertEquals(-2013264888, l);

int l2 = HashUtils.hashChunkXYZ(8, -64, 8);
Assertions.assertEquals(-2013266936, l2);
}

@Test
public void testGetXYZFromHashChunkXYZ() {
Assertions.assertEquals(8, HashUtils.getXFromHashChunkXYZ(-2147482616));
Assertions.assertEquals(64, HashUtils.getYFromHashChunkXYZ(-2147482616));
Assertions.assertEquals(8, HashUtils.getZFromHashChunkXYZ(-2147482616));
Assertions.assertEquals(8, HashUtils.getXFromHashChunkXYZ(-2013264888));
Assertions.assertEquals(64, HashUtils.getYFromHashChunkXYZ(-2013264888));
Assertions.assertEquals(-64, HashUtils.getYFromHashChunkXYZ(-2013266936));
Assertions.assertEquals(8, HashUtils.getZFromHashChunkXYZ(-2013264888));
}
}

0 comments on commit 07557bd

Please sign in to comment.