| 1 | package net.minecraft.world.level.storage.loot;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProvider;
|
| 9 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProviders;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public sealed interface FloatRangePredicate extends Validatable permits FloatRangePredicate.Point, FloatRangePredicate.Line {
|
| 13 | Codec<FloatRangePredicate.Point> POINT_CODEC = ContextFloatProviders.CODEC.xmap(FloatRangePredicate.Point::new, FloatRangePredicate.Point::value);
|
| 14 | Codec<FloatRangePredicate.Line> LINE_CODEC = RecordCodecBuilder.create(
|
| 15 | i -> i.group(
|
| 16 | ContextFloatProviders.CODEC.optionalFieldOf("min").forGetter(r -> r.min), ContextFloatProviders.CODEC.optionalFieldOf("max").forGetter(r -> r.max)
|
| 17 | )
|
| 18 | .apply(i, FloatRangePredicate.Line::new)
|
| 19 | );
|
| 20 | Codec<FloatRangePredicate> CODEC = Codec.either(POINT_CODEC, LINE_CODEC).xmap(Either::unwrap, range -> {
|
| 21 | return switch (range) {
|
| 22 | case FloatRangePredicate.Point point -> Either.left(point);
|
| 23 | case FloatRangePredicate.Line line -> Either.right(line);
|
| 24 | default -> throw new MatchException(null, null);
|
| 25 | };
|
| 26 | });
|
| 27 |
|
| 28 | static FloatRangePredicate exact(final float value) {
|
| 29 | return new FloatRangePredicate.Point(ContextFloatProviders.exactly(value));
|
| 30 | }
|
| 31 |
|
| 32 | static FloatRangePredicate range(final float min, final float max) {
|
| 33 | return new FloatRangePredicate.Line(Optional.of(ContextFloatProviders.exactly(min)), Optional.of(ContextFloatProviders.exactly(max)));
|
| 34 | }
|
| 35 |
|
| 36 | static FloatRangePredicate lowerBound(final float value) {
|
| 37 | return new FloatRangePredicate.Line(Optional.of(ContextFloatProviders.exactly(value)), Optional.empty());
|
| 38 | }
|
| 39 |
|
| 40 | static FloatRangePredicate upperBound(final float value) {
|
| 41 | return new FloatRangePredicate.Line(Optional.empty(), Optional.of(ContextFloatProviders.exactly(value)));
|
| 42 | }
|
| 43 |
|
| 44 | boolean test(LootContext context, float input);
|
| 45 |
|
| 46 | default boolean test(final LootContext context, final ContextFloatProvider input) {
|
| 47 | return this.test(context, input.getFloat(context));
|
| 48 | }
|
| 49 |
|
| 50 | final class Line implements FloatRangePredicate {
|
| 51 | private final Optional<Holder<ContextFloatProvider>> min;
|
| 52 | private final Optional<Holder<ContextFloatProvider>> max;
|
| 53 | private final FloatRangePredicate.Line.@Nullable FloatChecker predicate;
|
| 54 |
|
| 55 | private Line(final Optional<Holder<ContextFloatProvider>> min, final Optional<Holder<ContextFloatProvider>> max) {
|
| 56 | this.min = min;
|
| 57 | this.max = max;
|
| 58 | this.predicate = createPredicate(min.orElse(null), max.orElse(null));
|
| 59 | }
|
| 60 |
|
| 61 | private static FloatRangePredicate.Line.@Nullable FloatChecker createPredicate(
|
| 62 | final @Nullable Holder<ContextFloatProvider> min, final @Nullable Holder<ContextFloatProvider> max
|
| 63 | ) {
|
| 64 | if (min == null) {
|
| 65 | return max == null ? null : (context, input) -> input <= max.value().getFloat(context);
|
| 66 | } else {
|
| 67 | return max == null
|
| 68 | ? (context, input) -> input >= min.value().getFloat(context)
|
| 69 | : (context, input) -> input >= min.value().getFloat(context) && input <= max.value().getFloat(context);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void validate(final ValidationContext context) {
|
| 75 | Validatable.validateHolder(context, "min", this.min);
|
| 76 | Validatable.validateHolder(context, "max", this.max);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public boolean test(final LootContext context, final float input) {
|
| 81 | return this.predicate == null || this.predicate.test(context, input);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public boolean test(final LootContext context, final ContextFloatProvider input) {
|
| 86 | return this.predicate == null || this.predicate.test(context, input.getFloat(context));
|
| 87 | }
|
| 88 |
|
| 89 | @FunctionalInterface
|
| 90 | private interface FloatChecker {
|
| 91 | boolean test(LootContext context, float value);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | record Point(Holder<ContextFloatProvider> value) implements FloatRangePredicate {
|
| 96 | @Override
|
| 97 | public void validate(final ValidationContext context) {
|
| 98 | Validatable.validateHolder(context, this.value);
|
| 99 | }
|
| 100 |
|
| 101 | private float computeValue(final LootContext context) {
|
| 102 | return this.value.value().getFloat(context);
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public boolean test(final LootContext context, final float input) {
|
| 107 | return this.computeValue(context) == input;
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|