Skip to content

Commit c999da0

Browse files
fix: ListKu casting issues
1 parent 38bb403 commit c999da0

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Kube Utils Changelog
22

3+
## [21.1.3]
4+
5+
### Fixed
6+
7+
- `ListKu.getRandomWeightedEntry` no longer breaks when given valid data
8+
39
## [21.1.2]
410

511
### Fixed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ minecraft_version=1.21.1
77
neoforge_version=199
88
neoforge_full_version=21.1.199
99
neoforge_loader_version=4
10-
mod_version=2
10+
mod_version=3
1111
neo_version_range=[21.1.0-beta,)
1212

1313
maven_group=pro.mikey.mods

src/main/java/pro/mikey/kubeutils/kubejs/modules/ListsKu.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package pro.mikey.kubeutils.kubejs.modules;
22

33
import dev.latvian.mods.rhino.Context;
4-
import dev.latvian.mods.rhino.NativeObject;
54
import dev.latvian.mods.rhino.type.TypeInfo;
5+
import net.neoforged.bus.api.SubscribeEvent;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

@@ -23,22 +23,23 @@ public ListsKu() {
2323
*
2424
* @return one of the items from the array
2525
*/
26-
public <T> T getEntryBasedOnWeight(WeightedEntry<T>... items) {
27-
var inputs = Arrays.asList(items);
28-
26+
@SuppressWarnings("unchecked")
27+
public Object getEntryBasedOnWeight(Context ctx, List<Object> items) {
2928
double totalWeight = 0.0D;
3029

31-
for (WeightedEntry<T> input : items) {
30+
List<WeightedEntry<?>> entires = (List<WeightedEntry<?>>) ctx.listOf(items, TypeInfo.of(WeightedEntry.class));
31+
32+
for (WeightedEntry<?> input : entires) {
3233
totalWeight += input.weight;
3334
}
3435

3536
int idx = 0;
36-
for (double r = Math.random() * totalWeight; idx < inputs.size() - 1; ++idx) {
37-
r -= inputs.get(idx).weight;
37+
for (double r = Math.random() * totalWeight; idx < entires.size() - 1; ++idx) {
38+
r -= entires.get(idx).weight;
3839
if (r <= 0.0) break;
3940
}
4041

41-
return inputs.get(idx).entry;
42+
return entires.get(idx).entry;
4243
}
4344

4445
public record WeightedEntry<T>(

0 commit comments

Comments
 (0)