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