package net.minecraft.util; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; import java.util.function.Function; public record InclusiveRange>(T minInclusive, T maxInclusive) { public static final Codec> INT = codec((Codec)Codec.INT); public InclusiveRange(T minInclusive, T maxInclusive) { if (minInclusive.compareTo(maxInclusive) > 0) { throw new IllegalArgumentException("min_inclusive must be less than or equal to max_inclusive"); } else { this.minInclusive = minInclusive; this.maxInclusive = maxInclusive; } } public InclusiveRange(final T value) { this(value, value); } public static > Codec> codec(final Codec elementCodec) { return ExtraCodecs.intervalCodec( elementCodec, "min_inclusive", "max_inclusive", InclusiveRange::create, InclusiveRange::minInclusive, InclusiveRange::maxInclusive ); } public static > Codec> codec(final Codec elementCodec, final T minAllowedInclusive, final T maxAllowedInclusive) { return codec(elementCodec) .validate( value -> { if (value.minInclusive().compareTo(minAllowedInclusive) < 0) { return DataResult.error( () -> "Range limit too low, expected at least " + minAllowedInclusive + " [" + value.minInclusive() + "-" + value.maxInclusive() + "]" ); } else { return value.maxInclusive().compareTo(maxAllowedInclusive) > 0 ? DataResult.error( () -> "Range limit too high, expected at most " + maxAllowedInclusive + " [" + value.minInclusive() + "-" + value.maxInclusive() + "]" ) : DataResult.success(value); } } ); } public static > DataResult> create(final T minInclusive, final T maxInclusive) { return minInclusive.compareTo(maxInclusive) <= 0 ? DataResult.success(new InclusiveRange<>(minInclusive, maxInclusive)) : DataResult.error(() -> "min_inclusive must be less than or equal to max_inclusive"); } public > InclusiveRange map(final Function mapper) { return new InclusiveRange<>((S)mapper.apply(this.minInclusive), (S)mapper.apply(this.maxInclusive)); } public boolean isValueInRange(final T value) { return value.compareTo(this.minInclusive) >= 0 && value.compareTo(this.maxInclusive) <= 0; } public boolean contains(final InclusiveRange subRange) { return subRange.minInclusive().compareTo(this.minInclusive) >= 0 && subRange.maxInclusive.compareTo(this.maxInclusive) <= 0; } public String toString() { return "[" + this.minInclusive + ", " + this.maxInclusive + "]"; } }