WeightedList .java

net.minecraft.util.random.WeightedList (class): members: getRandomOrThrow, FLAT_THRESHOLD, nonEmptyCodec, Compact, Flat, totalWeight, Selector, unwrap.

net.minecraft.util.randomclass218 linescommon6.1 KB

API Surface (25)

<E> WeightedList<E> of()
<E> WeightedList<E> of(final E value)
<E> WeightedList<E> of(final E... items)
<E> WeightedList<E> of(final Weighted<E>... items)
<E> WeightedList<E> of(final List<Weighted<E>> items)
<E> WeightedList.Builder<E> builder()
boolean isEmpty()
<T> WeightedList<T> map(final Function<E, T> mapper)
Optional<E> getRandom(final RandomSource random)
E getRandomOrThrow(final RandomSource random)
List<Weighted<E>> unwrap()
<E> Codec<WeightedList<E>> codec(final Codec<E> elementCodec)
<E> Codec<WeightedList<E>> codec(final MapCodec<E> elementCodec)
<E> Codec<WeightedList<E>> nonEmptyCodec(final Codec<E> elementCodec)
<E> Codec<WeightedList<E>> nonEmptyCodec(final MapCodec<E> elementCodec)
<E, B extends ByteBuf> StreamCodec<B, WeightedList<E>> streamCodec(final StreamCodec<B, E> elementCodec)
boolean contains(final E value)
boolean equals(final @Nullable Object obj)
int hashCode()
WeightedList.Builder<E> add(final E item)
WeightedList.Builder<E> add(final E item, final int weight)
WeightedList.Builder<E> addAll(final WeightedList<E> other)
WeightedList<E> build()
E get(int selection)
E get(final int selection)

Fields & Constants (4)

FLAT_THRESHOLD
totalWeight
items
selector

String literals (2)

"Weighted list has no elements"
" exceeded total weight"

Code Graph: Dependencies (5)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

WeightedList.java218 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.random;
2
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.Lists;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.DataResult;
7
import com.mojang.serialization.MapCodec;
8
import io.netty.buffer.ByteBuf;
9
import java.util.Arrays;
10
import java.util.List;
11
import java.util.Objects;
12
import java.util.Optional;
13
import java.util.function.Function;
14
import net.minecraft.network.codec.ByteBufCodecs;
15
import net.minecraft.network.codec.StreamCodec;
16
import net.minecraft.util.RandomSource;
17
import org.jspecify.annotations.Nullable;
18
19
public final class WeightedList<E> {
20
	private static final int FLAT_THRESHOLD = 64;
21
	private final int totalWeight;
22
	private final List<Weighted<E>> items;
23
	private final WeightedList.@Nullable Selector<E> selector;
24
25
	private WeightedList(final List<? extends Weighted<E>> items) {
26
		this.items = List.copyOf(items);
27
		this.totalWeight = WeightedRandom.getTotalWeight(items, Weighted::weight);
28
		if (this.totalWeight == 0) {
29
			this.selector = null;
30
		} else if (this.totalWeight < 64) {
31
			this.selector = new WeightedList.Flat<>(this.items, this.totalWeight);
32
		} else {
33
			this.selector = new WeightedList.Compact<>(this.items);
34
		}
35
	}
36
37
	public static <E> WeightedList<E> of() {
38
		return new WeightedList<>(List.of());
39
	}
40
41
	public static <E> WeightedList<E> of(final E value) {
42
		return new WeightedList<>(List.of(new Weighted<>(value, 1)));
43
	}
44
45
	public static <E> WeightedList<E> of(final E... items) {
46
		WeightedList.Builder<E> builder = builder();
47
48
		for (E item : items) {
49
			builder.add(item);
50
		}
51
52
		return builder.build();
53
	}
54
55
	@SafeVarargs
56
	public static <E> WeightedList<E> of(final Weighted<E>... items) {
57
		return new WeightedList<>(List.of(items));
58
	}
59
60
	public static <E> WeightedList<E> of(final List<Weighted<E>> items) {
61
		return new WeightedList<>(items);
62
	}
63
64
	public static <E> WeightedList.Builder<E> builder() {
65
		return new WeightedList.Builder<>();
66
	}
67
68
	public boolean isEmpty() {
69
		return this.selector == null;
70
	}
71
72
	public <T> WeightedList<T> map(final Function<E, T> mapper) {
73
		return new WeightedList(Lists.transform(this.items, e -> e.map((Function<E, E>)mapper)));
74
	}
75
76
	public Optional<E> getRandom(final RandomSource random) {
77
		if (this.selector == null) {
78
			return Optional.empty();
79
		}
80
81
		int selection = random.nextInt(this.totalWeight);
82
		return Optional.of(this.selector.get(selection));
83
	}
84
85
	public E getRandomOrThrow(final RandomSource random) {
86
		if (this.selector == null) {
87
			throw new IllegalStateException("Weighted list has no elements");
88
		}
89
90
		int selection = random.nextInt(this.totalWeight);
91
		return this.selector.get(selection);
92
	}
93
94
	public List<Weighted<E>> unwrap() {
95
		return this.items;
96
	}
97
98
	private static <E> Codec<WeightedList<E>> entryToListCodec(final Codec<Weighted<E>> weightedElementCodec) {
99
		return weightedElementCodec.listOf().xmap(WeightedList::of, WeightedList::unwrap);
100
	}
101
102
	public static <E> Codec<WeightedList<E>> codec(final Codec<E> elementCodec) {
103
		return entryToListCodec(Weighted.codec(elementCodec));
104
	}
105
106
	public static <E> Codec<WeightedList<E>> codec(final MapCodec<E> elementCodec) {
107
		return entryToListCodec(Weighted.codec(elementCodec));
108
	}
109
110
	private static <E> Codec<WeightedList<E>> entryToNonEmptyListCodec(final Codec<Weighted<E>> weightedElementCodec) {
111
		return entryToListCodec(weightedElementCodec)
112
			.validate(list -> list.isEmpty() ? DataResult.error(() -> "Weighted list must contain at least one entry with non-zero weight") : DataResult.success(list));
113
	}
114
115
	public static <E> Codec<WeightedList<E>> nonEmptyCodec(final Codec<E> elementCodec) {
116
		return entryToNonEmptyListCodec(Weighted.codec(elementCodec));
117
	}
118
119
	public static <E> Codec<WeightedList<E>> nonEmptyCodec(final MapCodec<E> elementCodec) {
120
		return entryToNonEmptyListCodec(Weighted.codec(elementCodec));
121
	}
122
123
	public static <E, B extends ByteBuf> StreamCodec<B, WeightedList<E>> streamCodec(final StreamCodec<B, E> elementCodec) {
124
		return Weighted.streamCodec(elementCodec).apply(ByteBufCodecs.list()).map(WeightedList::of, WeightedList::unwrap);
125
	}
126
127
	public boolean contains(final E value) {
128
		for (Weighted<E> item : this.items) {
129
			if (item.value().equals(value)) {
130
				return true;
131
			}
132
		}
133
134
		return false;
135
	}
136
137
	@Override
138
	public boolean equals(final @Nullable Object obj) {
139
		if (this == obj) {
140
			return true;
141
		} else {
142
			return !(obj instanceof WeightedList<?> list) ? false : this.totalWeight == list.totalWeight && Objects.equals(this.items, list.items);
143
		}
144
	}
145
146
	@Override
147
	public int hashCode() {
148
		int result = this.totalWeight;
149
		return 31 * result + this.items.hashCode();
150
	}
151
152
	public static class Builder<E> {
153
		private final ImmutableList.Builder<Weighted<E>> result = ImmutableList.builder();
154
155
		public WeightedList.Builder<E> add(final E item) {
156
			return this.add(item, 1);
157
		}
158
159
		public WeightedList.Builder<E> add(final E item, final int weight) {
160
			this.result.add(new Weighted<>(item, weight));
161
			return this;
162
		}
163
164
		public WeightedList.Builder<E> addAll(final WeightedList<E> other) {
165
			this.result.addAll(other.unwrap());
166
			return this;
167
		}
168
169
		public WeightedList<E> build() {
170
			return new WeightedList<>(this.result.build());
171
		}
172
	}
173
174
	private static class Compact<E> implements WeightedList.Selector<E> {
175
		private final Weighted<?>[] entries;
176
177
		private Compact(final List<Weighted<E>> entries) {
178
			this.entries = entries.toArray(Weighted[]::new);
179
		}
180
181
		@Override
182
		public E get(int selection) {
183
			for (Weighted<?> entry : this.entries) {
184
				selection -= entry.weight();
185
				if (selection < 0) {
186
					return (E)entry.value();
187
				}
188
			}
189
190
			throw new IllegalStateException(selection + " exceeded total weight");
191
		}
192
	}
193
194
	private static class Flat<E> implements WeightedList.Selector<E> {
195
		private final Object[] entries;
196
197
		private Flat(final List<Weighted<E>> entries, final int totalWeight) {
198
			this.entries = new Object[totalWeight];
199
			int i = 0;
200
201
			for (Weighted<E> entry : entries) {
202
				int weight = entry.weight();
203
				Arrays.fill(this.entries, i, i + weight, entry.value());
204
				i += weight;
205
			}
206
		}
207
208
		@Override
209
		public E get(final int selection) {
210
			return (E)this.entries[selection];
211
		}
212
	}
213
214
	private interface Selector<E> {
215
		E get(int selection);
216
	}
217
}
218

Decompiled Minecraft 26.3 source of WeightedList (net/minecraft/util/random/WeightedList.java) with Mojang official mappings, stored verbatim on the site.