Merge remote-tracking branch 'origin/1.16-fabric' into 1.16-fabric

# Conflicts:
#	src/main/java/org/dimdev/dimdoors/util/WeightedList.java
This commit is contained in:
CreepyCre 2021-01-31 13:41:31 +01:00
commit b4735a7771

View file

@ -1,17 +1,13 @@
package org.dimdev.dimdoors.util;
import com.google.common.collect.Lists;
import java.util.*;
public class WeightedList<T extends Weighted<P>, P> {
private final List<T> list;
public class WeightedList<T extends Weighted<P>, P> extends ArrayList<T> {
private final Random random = new Random();
private T peekedRandom;
private boolean peeked = false;
public WeightedList() {
this.list = Lists.newArrayList();
}
public T getNextRandomWeighted(P parameters) {
@ -23,10 +19,10 @@ public class WeightedList<T extends Weighted<P>, P> {
}
private T getNextRandomWeighted(P parameters, boolean peek) {
int totalWeight = list.stream().mapToInt(weighted -> weighted.getWeight(parameters)).sum();
if (!peeked) {
int totalWeight = stream().mapToInt(weighted -> weighted.getWeight(parameters)).sum();
int cursor = random.nextInt(totalWeight);
for (T weighted : list) {
for (T weighted : this) {
cursor -= weighted.getWeight(parameters);
if (cursor <= 0) {
if (peek) {
@ -34,24 +30,15 @@ public class WeightedList<T extends Weighted<P>, P> {
peeked = true;
}
return weighted; // should never return an entry with weight 0, unless there are only weight 0 entries
}
} // TODO: fix bug, if first entry has weight 0 and random.nextInt(totalWeight) is 0, then it will return first entry
}
if (peek) {
peekedRandom = null;
peeked = true;
}
return null;
}
if (!peek) peeked = false;
return peekedRandom;
}
public boolean add(T t) {
return list.add(t);
}
public boolean remove(T t){
return list.remove(t);
}
// TODO: make weightedList implement List instead
public List<T> getList() {
return list;
}
}