WeightedListInt .java

net.minecraft.util.valueproviders.WeightedListInt (class): members: maxValue, minValue, distribution, maxInclusive, minInclusive, sample, MAP_CODEC, codec.

net.minecraft.util.valueprovidersclass53 linescommon1.4 KB

Implements

API Surface (4)

int sample(final RandomSource random)
int minInclusive()
int maxInclusive()
MapCodec<WeightedListInt> codec()

Fields & Constants (4)

MAP_CODEC
distribution
minValue
maxValue

String literals (1)

"distribution"

Code Graph: Dependencies (4)

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

Full Source

WeightedListInt.java53 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.valueproviders;
2
3
import com.mojang.serialization.MapCodec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import net.minecraft.util.RandomSource;
6
import net.minecraft.util.random.Weighted;
7
import net.minecraft.util.random.WeightedList;
8
9
public class WeightedListInt implements IntProvider {
10
	public static final MapCodec<WeightedListInt> MAP_CODEC = RecordCodecBuilder.mapCodec(
11
		i -> i.group(WeightedList.nonEmptyCodec(IntProviders.CODEC).fieldOf("distribution").forGetter(c -> c.distribution)).apply(i, WeightedListInt::new)
12
	);
13
	private final WeightedList<IntProvider> distribution;
14
	private final int minValue;
15
	private final int maxValue;
16
17
	public WeightedListInt(final WeightedList<IntProvider> distribution) {
18
		this.distribution = distribution;
19
		int min = Integer.MAX_VALUE;
20
		int max = Integer.MIN_VALUE;
21
22
		for (Weighted<IntProvider> value : distribution.unwrap()) {
23
			int entryMin = value.value().minInclusive();
24
			int entryMax = value.value().maxInclusive();
25
			min = Math.min(min, entryMin);
26
			max = Math.max(max, entryMax);
27
		}
28
29
		this.minValue = min;
30
		this.maxValue = max;
31
	}
32
33
	@Override
34
	public int sample(final RandomSource random) {
35
		return this.distribution.getRandomOrThrow(random).sample(random);
36
	}
37
38
	@Override
39
	public int minInclusive() {
40
		return this.minValue;
41
	}
42
43
	@Override
44
	public int maxInclusive() {
45
		return this.maxValue;
46
	}
47
48
	@Override
49
	public MapCodec<WeightedListInt> codec() {
50
		return MAP_CODEC;
51
	}
52
}
53

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