Skip to content

Commit 5bb7fbe

Browse files
committed
add bounds checks to x coordinate
1 parent e065894 commit 5bb7fbe

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

1.13.x/src/main/java/net/set/spawn/mod/mixin/ServerPlayerEntityMixin.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ private int setSpawn(Random random, int bounds, Operation<Integer> original, @Lo
4545
return originalResult;
4646
}
4747

48-
// Transform x and z coordinates into correct Random#nextInt result.
49-
int result = ((MathHelper.floor(seedObject.getX()) - worldSpawn.getX()) + spawnRadius) + ((MathHelper.floor(seedObject.getZ()) - worldSpawn.getZ()) + spawnRadius) * (spawnRadius * 2 + 1);
48+
// Transform x and z coordinates into corresponding Random#nextInt result.
49+
int spawnDiameter = spawnRadius * 2 + 1;
50+
int x = MathHelper.floor(seedObject.getX());
51+
int z = MathHelper.floor(seedObject.getZ());
52+
int xLocal = x - worldSpawn.getX() + spawnRadius;
53+
int result = xLocal + (z - worldSpawn.getZ() + spawnRadius) * spawnDiameter;
5054

51-
if (result >= 0 && result < bounds) {
55+
if (xLocal >=0 && xLocal < spawnDiameter && result >= 0 && result < bounds) {
5256
// we save the original result in case the set spawn is invalid, see fallbackOnInvalidSpawn
5357
System.out.println("Setting spawn");
5458
originalRandomResult.set(originalResult);

1.14-1.18.2/src/main/java/net/set/spawn/mod/mixin/ServerPlayerEntityMixin.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ private int setSpawn(Random random, int bounds, Operation<Integer> original, @Lo
4747
int spawnDiameter = spawnRadius * 2 + 1;
4848
int x = MathHelper.floor(seedObject.getX());
4949
int z = MathHelper.floor(seedObject.getZ());
50-
int result = (x - worldSpawn.getX() + spawnRadius) + (z - worldSpawn.getZ() + spawnRadius) * spawnDiameter;
50+
int xLocal = x - worldSpawn.getX() + spawnRadius;
51+
int result = xLocal + (z - worldSpawn.getZ() + spawnRadius) * spawnDiameter;
5152

52-
if (result >= 0 && result < bounds) {
53+
if (xLocal >=0 && xLocal < spawnDiameter && result >= 0 && result < bounds) {
5354
// we save the original result in case the set spawn is invalid, see fallbackOnInvalidSpawn
5455
System.out.println("Setting spawn");
5556
originalRandomResult.set(originalResult);

1.19-1.21.4/src/main/java/net/set/spawn/mod/mixin/ServerPlayerEntityMixin.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ private int setSpawn(Random random, int bounds, Operation<Integer> original, @Lo
5050
int spawnDiameter = spawnRadius * 2 + 1;
5151
int x = MathHelper.floor(seedObject.getX());
5252
int z = MathHelper.floor(seedObject.getZ());
53-
int result = (x - worldSpawn.getX() + spawnRadius) + (z - worldSpawn.getZ() + spawnRadius) * spawnDiameter;
53+
int xLocal = x - worldSpawn.getX() + spawnRadius;
54+
int result = xLocal + (z - worldSpawn.getZ() + spawnRadius) * spawnDiameter;
5455

55-
if (result >= 0 && result < bounds) {
56+
if (xLocal >=0 && xLocal < spawnDiameter && result >= 0 && result < bounds) {
5657
// we save the original result in case the set spawn is invalid, see fallbackOnInvalidSpawn
5758
originalRandomResult.set(originalResult);
5859
System.out.println("Setting spawn");

0 commit comments

Comments
 (0)