Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 3a9cf7c

Browse files
committed
Fixes
1 parent 5ca6425 commit 3a9cf7c

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/main/java/com/redmagic/undefinedapi/extension/PlayerExtension.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ fun Player.sendActionBar(string: String, time: Int, timeUnit: TimeUnit = TimeUni
4545
*/
4646
fun Player.feed() { foodLevel = 20 }
4747

48+
/**
49+
* Restores the player's health to the maximum health value.
50+
*/
4851
fun Player.heal() { health = maxHealth }
4952

5053
/**
@@ -57,8 +60,6 @@ fun Player.resetWalkSpeed() { walkSpeed = 0.2F }
5760
*/
5861
fun Player.resetFlySpeed() { flySpeed = 0.1F }
5962

60-
61-
6263
/**
6364
* Hides the player from all online players.
6465
*

src/main/java/com/redmagic/undefinedapi/extension/RandomUtil.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ import kotlin.random.Random
1212
*/
1313
fun Random.weightedRandom(map: Map<Any, Double>): Any{
1414
val totalWeight = getTotalWeight(map)
15-
var idx = 0;
16-
var r = this.nextDouble() * totalWeight
17-
18-
while (idx < map.keys.size - 1){
19-
val s = map.keys.elementAt(idx)
20-
r -= map[s] ?: 0.0
21-
if (r <= 0) break
22-
++idx
15+
var randomWeight = this.nextDouble(totalWeight)
16+
for ((item, weight) in map) {
17+
randomWeight -= weight
18+
if (randomWeight <= 0) {
19+
return item
20+
}
2321
}
24-
25-
return map.keys.elementAt(idx)
22+
throw IllegalArgumentException("The map is empty.")
2623
}
2724

2825
/**
@@ -37,4 +34,3 @@ private fun getTotalWeight(map: Map<Any, Double>): Double {
3734

3835
return total
3936
}
40-

0 commit comments

Comments
 (0)