Skip to content

Commit 1b05fd2

Browse files
phoenixlzxclaude
andcommitted
Fix MathUtils.uniformRangeInclusive to handle inverted ranges
Instead of throwing IllegalArgumentException when min > max, automatically swap the values. This prevents crashes when NPC configs have misconfigured min/max trade values. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3fdad96 commit 1b05fd2

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/main/java/cat/nyaa/nyaacore/utils/MathUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ public final class MathUtils {
1313

1414
/**
1515
* Generate a random number in [min,max]
16+
* If min > max, the values are swapped automatically.
1617
*/
1718
public static int uniformRangeInclusive(int minInclusive, int maxInclusive) {
18-
if (maxInclusive < minInclusive) throw new IllegalArgumentException();
19+
if (maxInclusive < minInclusive) {
20+
// Swap values if min > max
21+
int temp = minInclusive;
22+
minInclusive = maxInclusive;
23+
maxInclusive = temp;
24+
}
1925
return rng.nextInt(maxInclusive - minInclusive + 1) + minInclusive;
2026
}
2127

0 commit comments

Comments
 (0)