TrapezoidFloat .java

net.minecraft.util.valueproviders.TrapezoidFloat (record): members: min, max, plateau, sample, MAP_CODEC, codec. strings: "trapezoid(", "plateau".

net.minecraft.util.valueprovidersrecord48 linescommon1.6 KB

Implements

API Surface (4)

TrapezoidFloat of(final float min, final float max, final float plateau)
float sample(final RandomSource random)
MapCodec<TrapezoidFloat> codec()
String toString()

Fields & Constants (1)

MAP_CODEC

String literals (4)

"plateau"
"Max must be larger than min: ["
"Plateau can at most be the full span: ["
"trapezoid("

Code Graph: Dependencies (2)

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

Full Source

TrapezoidFloat.java48 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.valueproviders;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.DataResult;
5
import com.mojang.serialization.MapCodec;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import net.minecraft.util.RandomSource;
8
9
public record TrapezoidFloat(float min, float max, float plateau) implements FloatProvider {
10
	public static final MapCodec<TrapezoidFloat> MAP_CODEC = RecordCodecBuilder.<TrapezoidFloat>mapCodec(
11
			i -> i.group(
12
					Codec.FLOAT.fieldOf("min").forGetter(TrapezoidFloat::min),
13
					Codec.FLOAT.fieldOf("max").forGetter(TrapezoidFloat::max),
14
					Codec.FLOAT.fieldOf("plateau").forGetter(TrapezoidFloat::plateau)
15
				)
16
				.apply(i, TrapezoidFloat::new)
17
		)
18
		.validate(c -> {
19
			if (c.max < c.min) {
20
				return DataResult.error(() -> "Max must be larger than min: [" + c.min + ", " + c.max + "]");
21
			} else {
22
				return c.plateau > c.max - c.min ? DataResult.error(() -> "Plateau can at most be the full span: [" + c.min + ", " + c.max + "]") : DataResult.success(c);
23
			}
24
		});
25
26
	public static TrapezoidFloat of(final float min, final float max, final float plateau) {
27
		return new TrapezoidFloat(min, max, plateau);
28
	}
29
30
	@Override
31
	public float sample(final RandomSource random) {
32
		float range = this.max - this.min;
33
		float plateauStart = (range - this.plateau) / 2.0F;
34
		float plateauEnd = range - plateauStart;
35
		return this.min + random.nextFloat() * plateauEnd + random.nextFloat() * plateauStart;
36
	}
37
38
	@Override
39
	public MapCodec<TrapezoidFloat> codec() {
40
		return MAP_CODEC;
41
	}
42
43
	@Override
44
	public String toString() {
45
		return "trapezoid(" + this.plateau + ") in [" + this.min + "-" + this.max + "]";
46
	}
47
}
48

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