Skip to content

Commit

Permalink
fix index out of bounds for blocks at world max height
Browse files Browse the repository at this point in the history
  • Loading branch information
nossr50 committed Nov 25, 2024
1 parent 1b6b127 commit 6dd1753
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 2.2.029
Fixed bug where block checks at world height would throw IndexOutOfBounds exceptions

Version 2.2.028
Fixed stack overflow during ChunkUnloadEvent
Fixed a bug where you had to wait to summon another COTW summon if one or more of them had died or otherwise expired before their time limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private int coordToIndex(int x, int y, int z) {
}

private static int coordToIndex(int x, int y, int z, int worldMin, int worldMax) {
if (x < 0 || x >= 16 || y < worldMin || y >= worldMax || z < 0 || z >= 16)
if (x < 0 || x >= 16 || y < worldMin || y > worldMax || z < 0 || z >= 16)
throw new IndexOutOfBoundsException(String.format("x: %d y: %d z: %d World Min: %d World Max: %d", x, y, z, worldMin, worldMax));
int yOffset = -worldMin; // Ensures y multiplier remains positive
return (z * 16 + x) + (256 * (y + yOffset));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ void setIneligibleShouldThrowIndexOutOfBoundsException() {
final HashChunkManager hashChunkManager = new HashChunkManager();

// Top Block
final Block illegalHeightBlock = initMockBlock(1337, 256, -1337);
int illegalMaxHeight = 256 + 1;
final Block illegalHeightBlock = initMockBlock(1337, illegalMaxHeight, -1337);
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> hashChunkManager.setIneligible(illegalHeightBlock));

int illegalMinHeight = -65;
final Block otherIllegalHeightBlock = initMockBlock(1337, illegalMinHeight, -1337);
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> hashChunkManager.setIneligible(otherIllegalHeightBlock));
}

@Test
Expand All @@ -85,7 +90,7 @@ void testSetEligibility() {
int radius = 2; // Could be anything but drastically changes test time

for (int x = -radius; x <= radius; x++) {
for (int y = mockWorld.getMinHeight(); y < mockWorld.getMaxHeight(); y++) {
for (int y = mockWorld.getMinHeight(); y <= mockWorld.getMaxHeight(); y++) {
for (int z = -radius; z <= radius; z++) {
final Block testBlock = initMockBlock(x, y, z);
// mark ineligible
Expand Down Expand Up @@ -147,7 +152,8 @@ void testChunkStoreRejectsOutOfBounds() {
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, -1, 0));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, 0, -1));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(16, 0, 0));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, mockWorld.getMaxHeight(), 0));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, mockWorld.getMaxHeight()+1, 0));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, mockWorld.getMinHeight()-1, 0));
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> chunkStore.setTrue(0, 0, 16));
}

Expand Down

0 comments on commit 6dd1753

Please sign in to comment.