Skip to content

Commit 646301e

Browse files
committed
Fix rare crash in RandomSpriteProvider
This was caused by RandomIndexProvider.Unweighted using Math.abs, meaning it could return a negative value if the passed seed was equal to Integer.MIN_VALUE. This commit replaces Math.abs with a function that sets the sign bit to 0. Fix #616
1 parent 80f742e commit 646301e

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/main/java/me/pepperbell/continuity/client/util/MathUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ public static int mix32(long z) {
4848
public static int mix(int x, int y, int z, int face, int loops) {
4949
return mix32((MathHelper.hashCode(x, y, z) ^ mix64(GOLDEN_GAMMA * (1 + face))) + GOLDEN_GAMMA * (1 + loops));
5050
}
51+
52+
public static int removeSignBit(int value) {
53+
return value & 0x7FFFFFFF;
54+
}
5155
}

src/main/java/me/pepperbell/continuity/client/util/RandomIndexProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Unweighted(int size) {
1616

1717
@Override
1818
public int getRandomIndex(int random) {
19-
return Math.abs(random) % size;
19+
return MathUtil.removeSignBit(random) % size;
2020
}
2121
}
2222

@@ -43,7 +43,7 @@ public Weighted(int[] weights, int weightSum) {
4343
@Override
4444
public int getRandomIndex(int random) {
4545
int index;
46-
int tempWeight = Math.abs(random) % weightSum;
46+
int tempWeight = MathUtil.removeSignBit(random) % weightSum;
4747
for (index = 0; index < maxIndex && tempWeight >= weights[index]; index++) {
4848
tempWeight -= weights[index];
4949
}

0 commit comments

Comments
 (0)