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.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 p_300479_) { this(p_300479_, p_300479_); } public static > Codec> codec(Codec p_184573_) { return ExtraCodecs.intervalCodec( p_184573_, "min_inclusive", "max_inclusive", InclusiveRange::create, InclusiveRange::minInclusive, InclusiveRange::maxInclusive ); } public static > Codec> codec(Codec p_184575_, T p_184576_, T p_184577_) { return codec(p_184575_) .validate( p_274898_ -> { if (p_274898_.minInclusive().compareTo(p_184576_) < 0) { return DataResult.error( () -> "Range limit too low, expected at least " + p_184576_ + " [" + p_274898_.minInclusive() + "-" + p_274898_.maxInclusive() + "]" ); } else { return p_274898_.maxInclusive().compareTo(p_184577_) > 0 ? DataResult.error( () -> "Range limit too high, expected at most " + p_184577_ + " [" + p_274898_.minInclusive() + "-" + p_274898_.maxInclusive() + "]" ) : DataResult.success(p_274898_); } } ); } public static > DataResult> create(T p_184581_, T p_184582_) { return p_184581_.compareTo(p_184582_) <= 0 ? DataResult.success(new InclusiveRange<>(p_184581_, p_184582_)) : DataResult.error(() -> "min_inclusive must be less than or equal to max_inclusive"); } public boolean isValueInRange(T p_184579_) { return p_184579_.compareTo(this.minInclusive) >= 0 && p_184579_.compareTo(this.maxInclusive) <= 0; } public boolean contains(InclusiveRange p_184571_) { return p_184571_.minInclusive().compareTo(this.minInclusive) >= 0 && p_184571_.maxInclusive.compareTo(this.maxInclusive) <= 0; } @Override public String toString() { return "[" + this.minInclusive + ", " + this.maxInclusive + "]"; } }