package net.minecraft.util; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; 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(T value) { this(value, value); } public static > Codec> codec(Codec codec) { return ExtraCodecs.intervalCodec(codec, "min_inclusive", "max_inclusive", InclusiveRange::create, InclusiveRange::minInclusive, InclusiveRange::maxInclusive); } public static > Codec> codec(Codec codec, T min, T max) { return codec(codec) .validate( inclusiveRange -> { if (inclusiveRange.minInclusive().compareTo(min) < 0) { return DataResult.error( () -> "Range limit too low, expected at least " + min + " [" + inclusiveRange.minInclusive() + "-" + inclusiveRange.maxInclusive() + "]" ); } else { return inclusiveRange.maxInclusive().compareTo(max) > 0 ? DataResult.error( () -> "Range limit too high, expected at most " + max + " [" + inclusiveRange.minInclusive() + "-" + inclusiveRange.maxInclusive() + "]" ) : DataResult.success(inclusiveRange); } } ); } public static > DataResult> create(T min, T max) { return min.compareTo(max) <= 0 ? DataResult.success(new InclusiveRange<>(min, max)) : DataResult.error(() -> "min_inclusive must be less than or equal to max_inclusive"); } public boolean isValueInRange(T value) { return value.compareTo(this.minInclusive) >= 0 && value.compareTo(this.maxInclusive) <= 0; } public boolean contains(InclusiveRange value) { return value.minInclusive().compareTo(this.minInclusive) >= 0 && value.maxInclusive.compareTo(this.maxInclusive) <= 0; } public String toString() { return "[" + this.minInclusive + ", " + this.maxInclusive + "]"; } }