| 1 | package net.minecraft.world.level.storage.loot;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.util.Mth;
|
| 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 class IntLimit implements Validatable {
|
| 13 | public static final Codec<IntLimit> CODEC = RecordCodecBuilder.create(
|
| 14 | i -> i.group(ContextIntProviders.CODEC.optionalFieldOf("min").forGetter(r -> r.min), ContextIntProviders.CODEC.optionalFieldOf("max").forGetter(r -> r.max))
|
| 15 | .apply(i, IntLimit::new)
|
| 16 | );
|
| 17 | private final Optional<Holder<ContextIntProvider>> min;
|
| 18 | private final Optional<Holder<ContextIntProvider>> max;
|
| 19 | private final IntLimit.IntLimiter limiter;
|
| 20 |
|
| 21 | private IntLimit(final Optional<Holder<ContextIntProvider>> min, final Optional<Holder<ContextIntProvider>> max) {
|
| 22 | this.min = min;
|
| 23 | this.max = max;
|
| 24 | this.limiter = createLimiter(min.orElse(null), max.orElse(null));
|
| 25 | }
|
| 26 |
|
| 27 | private static IntLimit.IntLimiter createLimiter(final @Nullable Holder<ContextIntProvider> min, final @Nullable Holder<ContextIntProvider> max) {
|
| 28 | if (min == null) {
|
| 29 | return max == null ? (var0, input) -> input : (context, input) -> Math.min(max.value().getInt(context), input);
|
| 30 | } else {
|
| 31 | return max == null
|
| 32 | ? (context, input) -> Math.max(min.value().getInt(context), input)
|
| 33 | : (context, input) -> Mth.clamp(input, min.value().getInt(context), max.value().getInt(context));
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | public static IntLimit range(final int min, final int max) {
|
| 38 | return new IntLimit(Optional.of(ContextIntProviders.exactly(min)), Optional.of(ContextIntProviders.exactly(max)));
|
| 39 | }
|
| 40 |
|
| 41 | public static IntLimit lowerBound(final int value) {
|
| 42 | return new IntLimit(Optional.of(ContextIntProviders.exactly(value)), Optional.empty());
|
| 43 | }
|
| 44 |
|
| 45 | public static IntLimit upperBound(final int value) {
|
| 46 | return new IntLimit(Optional.empty(), Optional.of(ContextIntProviders.exactly(value)));
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void validate(final ValidationContext context) {
|
| 51 | Validatable.validateHolder(context, "min", this.min);
|
| 52 | Validatable.validateHolder(context, "max", this.max);
|
| 53 | }
|
| 54 |
|
| 55 | public int clamp(final LootContext context, final int input) {
|
| 56 | return this.limiter.apply(context, input);
|
| 57 | }
|
| 58 |
|
| 59 | @FunctionalInterface
|
| 60 | private interface IntLimiter {
|
| 61 | int apply(LootContext context, int value);
|
| 62 | }
|
| 63 | }
|
| 64 |
|