Skip to content

Commit

Permalink
Clamp overworld coordinates to world height
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisawesomeness committed Sep 21, 2023
1 parent 184dc02 commit 7797b0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public BlockPos overworldToNether() {
return new BlockPos(horizontal().floorDiv(8).withY(netherY));
}
/**
* Converts nether to overworld coordinates. X/Z values are clamped to world border.
* Converts nether to overworld coordinates, clamped to world border/height.
* @return overworld block pos
*/
public BlockPos netherToOverworld() {
int overworldX = Mth.clamp(x * 8, -MAX_PORTAL_DISTANCE, MAX_PORTAL_DISTANCE);
int overworldY = Mth.clamp(y, -64, 319);
int overworldZ = Mth.clamp(z * 8, -MAX_PORTAL_DISTANCE, MAX_PORTAL_DISTANCE);
return new BlockPos(overworldX, y, overworldZ);
return new BlockPos(overworldX, overworldY, overworldZ);
}

public SectionPos getSection() {
Expand All @@ -44,6 +45,10 @@ public Vec3i getPosWithinSection() {
return floorMod(16);
}

/**
* @return true if the X/Z (not Y!) coordinates are within the world border
* @see #MAX_BORDER_DISTANCE
*/
public boolean isInBounds() {
return -MAX_BORDER_DISTANCE <= x && x <= MAX_BORDER_DISTANCE &&
-MAX_BORDER_DISTANCE <= z && z <= MAX_BORDER_DISTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ public void testNetherToOverworldZero() {
}
@Test
public void testNetherToOverworldLimit() {
assertThat(new BlockPos(10_000_000, 64, -10_000_000).netherToOverworld())
.isEqualTo(new BlockPos(BlockPos.MAX_PORTAL_DISTANCE, 64, -BlockPos.MAX_PORTAL_DISTANCE));
assertThat(new BlockPos(10_000_000, 999, -10_000_000).netherToOverworld())
.isEqualTo(new BlockPos(BlockPos.MAX_PORTAL_DISTANCE, 319, -BlockPos.MAX_PORTAL_DISTANCE));
}
@Test
public void testNetherToOverworldLimit2() {
assertThat(new BlockPos(10_000_000, -100, -10_000_000).netherToOverworld())
.isEqualTo(new BlockPos(BlockPos.MAX_PORTAL_DISTANCE, -64, -BlockPos.MAX_PORTAL_DISTANCE));
}

@Test
Expand Down

0 comments on commit 7797b0a

Please sign in to comment.