| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterators;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.datafixers.util.Pair;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.Dynamic;
|
| 9 | import com.mojang.serialization.DynamicOps;
|
| 10 | import java.util.Comparator;
|
| 11 | import java.util.Iterator;
|
| 12 | import java.util.List;
|
| 13 | import java.util.stream.Stream;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 |
|
| 16 | public class ShufflingList<U> implements Iterable<U> {
|
| 17 | protected final List<ShufflingList.WeightedEntry<U>> entries;
|
| 18 | private final RandomSource random = RandomSource.create();
|
| 19 |
|
| 20 | public ShufflingList() {
|
| 21 | this.entries = Lists.newArrayList();
|
| 22 | }
|
| 23 |
|
| 24 | private ShufflingList(final List<ShufflingList.WeightedEntry<U>> entries) {
|
| 25 | this.entries = Lists.newArrayList(entries);
|
| 26 | }
|
| 27 |
|
| 28 | public static <U> Codec<ShufflingList<U>> codec(final Codec<U> elementCodec) {
|
| 29 | return ShufflingList.WeightedEntry.codec(elementCodec).listOf().xmap(ShufflingList::new, l -> l.entries);
|
| 30 | }
|
| 31 |
|
| 32 | public ShufflingList<U> add(final U data, final int weight) {
|
| 33 | this.entries.add(new ShufflingList.WeightedEntry<>(data, weight));
|
| 34 | return this;
|
| 35 | }
|
| 36 |
|
| 37 | public ShufflingList<U> shuffle() {
|
| 38 | this.entries.forEach(k -> k.setRandom(this.random.nextFloat()));
|
| 39 | this.entries.sort(Comparator.comparingDouble(ShufflingList.WeightedEntry::getRandWeight));
|
| 40 | return this;
|
| 41 | }
|
| 42 |
|
| 43 | public Stream<U> stream() {
|
| 44 | return this.entries.stream().map(ShufflingList.WeightedEntry::getData);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public Iterator<U> iterator() {
|
| 49 | return Iterators.transform(this.entries.iterator(), ShufflingList.WeightedEntry::getData);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public String toString() {
|
| 54 | return "ShufflingList[" + this.entries + "]";
|
| 55 | }
|
| 56 |
|
| 57 | public static class WeightedEntry<T> {
|
| 58 | private final T data;
|
| 59 | private final int weight;
|
| 60 | private double randWeight;
|
| 61 |
|
| 62 | private WeightedEntry(final T data, final int weight) {
|
| 63 | this.weight = weight;
|
| 64 | this.data = data;
|
| 65 | }
|
| 66 |
|
| 67 | private double getRandWeight() {
|
| 68 | return this.randWeight;
|
| 69 | }
|
| 70 |
|
| 71 | private void setRandom(final float random) {
|
| 72 | this.randWeight = -Math.pow(random, 1.0F / this.weight);
|
| 73 | }
|
| 74 |
|
| 75 | public T getData() {
|
| 76 | return this.data;
|
| 77 | }
|
| 78 |
|
| 79 | public int getWeight() {
|
| 80 | return this.weight;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public String toString() {
|
| 85 | return this.weight + ":" + this.data;
|
| 86 | }
|
| 87 |
|
| 88 | public static <E> Codec<ShufflingList.WeightedEntry<E>> codec(final Codec<E> elementCodec) {
|
| 89 | return new Codec<ShufflingList.WeightedEntry<E>>() {
|
| 90 | @Override
|
| 91 | public <T> DataResult<Pair<ShufflingList.WeightedEntry<E>, T>> decode(final DynamicOps<T> ops, final T input) {
|
| 92 | Dynamic<T> map = new Dynamic<>(ops, input);
|
| 93 | return map.get("data")
|
| 94 | .flatMap(elementCodec::parse)
|
| 95 | .map(data -> new ShufflingList.WeightedEntry<>(data, map.get("weight").asInt(1)))
|
| 96 | .map(r -> Pair.of((ShufflingList.WeightedEntry<E>)r, ops.empty()));
|
| 97 | }
|
| 98 |
|
| 99 | public <T> DataResult<T> encode(final ShufflingList.WeightedEntry<E> input, final DynamicOps<T> ops, final T prefix) {
|
| 100 | return ops.mapBuilder().add("weight", ops.createInt(input.weight)).add("data", elementCodec.encodeStart(ops, input.data)).build(prefix);
|
| 101 | }
|
| 102 | };
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 |
|