| 1 | package net.minecraft.world.attribute;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.function.Function;
|
| 6 | import net.minecraft.world.level.Level;
|
| 7 | import net.minecraft.world.phys.Vec3;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public class EnvironmentAttributeProbe {
|
| 11 | private final Map<EnvironmentAttribute<?>, EnvironmentAttributeProbe.ValueProbe<?>> valueProbes = new Reference2ObjectOpenHashMap<>();
|
| 12 | private final Function<EnvironmentAttribute<?>, EnvironmentAttributeProbe.ValueProbe<?>> valueProbeFactory = x$0 -> new EnvironmentAttributeProbe.ValueProbe<>(
|
| 13 | x$0
|
| 14 | );
|
| 15 | private @Nullable Level level;
|
| 16 | private @Nullable Vec3 position;
|
| 17 | private final SpatialAttributeInterpolator biomeInterpolator = new SpatialAttributeInterpolator();
|
| 18 |
|
| 19 | public void reset() {
|
| 20 | this.level = null;
|
| 21 | this.position = null;
|
| 22 | this.biomeInterpolator.clear();
|
| 23 | this.valueProbes.clear();
|
| 24 | }
|
| 25 |
|
| 26 | public void tick(final Level level, final Vec3 position) {
|
| 27 | this.level = level;
|
| 28 | this.position = position;
|
| 29 | this.valueProbes.values().removeIf(EnvironmentAttributeProbe.ValueProbe::tick);
|
| 30 | this.biomeInterpolator.clear();
|
| 31 | GaussianSampler.sample(
|
| 32 | position.scale(0.25),
|
| 33 | level.getBiomeManager()::getNoiseBiomeAtQuart,
|
| 34 | (weight, biome) -> this.biomeInterpolator.accumulate(weight, biome.value().getAttributes())
|
| 35 | );
|
| 36 | }
|
| 37 |
|
| 38 | public <Value> Value getValue(final EnvironmentAttribute<Value> attribute, final float partialTicks) {
|
| 39 | EnvironmentAttributeProbe.ValueProbe<Value> valueProbe = (EnvironmentAttributeProbe.ValueProbe<Value>)this.valueProbes
|
| 40 | .computeIfAbsent(attribute, this.valueProbeFactory);
|
| 41 | return valueProbe.get(attribute, partialTicks);
|
| 42 | }
|
| 43 |
|
| 44 | private class ValueProbe<Value> {
|
| 45 | private Value lastValue;
|
| 46 | private @Nullable Value newValue;
|
| 47 |
|
| 48 | public ValueProbe(final EnvironmentAttribute<Value> attribute) {
|
| 49 | Value value = this.getValueFromLevel(attribute);
|
| 50 | this.lastValue = value;
|
| 51 | this.newValue = value;
|
| 52 | }
|
| 53 |
|
| 54 | private Value getValueFromLevel(final EnvironmentAttribute<Value> attribute) {
|
| 55 | return EnvironmentAttributeProbe.this.level != null && EnvironmentAttributeProbe.this.position != null
|
| 56 | ? EnvironmentAttributeProbe.this.level
|
| 57 | .environmentAttributes()
|
| 58 | .getValue(attribute, EnvironmentAttributeProbe.this.position, EnvironmentAttributeProbe.this.biomeInterpolator)
|
| 59 | : attribute.defaultValue();
|
| 60 | }
|
| 61 |
|
| 62 | public boolean tick() {
|
| 63 | if (this.newValue == null) {
|
| 64 | return true;
|
| 65 | }
|
| 66 |
|
| 67 | this.lastValue = this.newValue;
|
| 68 | this.newValue = null;
|
| 69 | return false;
|
| 70 | }
|
| 71 |
|
| 72 | public Value get(final EnvironmentAttribute<Value> attribute, final float partialTicks) {
|
| 73 | if (this.newValue == null) {
|
| 74 | this.newValue = this.getValueFromLevel(attribute);
|
| 75 | }
|
| 76 |
|
| 77 | return attribute.type().partialTickLerp().apply(partialTicks, this.lastValue, this.newValue);
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|