| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.google.common.collect.Range;
|
| 4 | import com.mojang.brigadier.StringReader;
|
| 5 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 6 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import com.mojang.datafixers.util.Either;
|
| 9 | import com.mojang.serialization.Codec;
|
| 10 | import com.mojang.serialization.DataResult;
|
| 11 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 12 | import io.netty.buffer.ByteBuf;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.function.Function;
|
| 15 | import java.util.function.Supplier;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 18 | import net.minecraft.network.codec.StreamCodec;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 |
|
| 21 | public interface MinMaxBounds<T extends Number & Comparable<T>> {
|
| 22 | SimpleCommandExceptionType ERROR_EMPTY = new SimpleCommandExceptionType(Component.translatable("argument.range.empty"));
|
| 23 | SimpleCommandExceptionType ERROR_SWAPPED = new SimpleCommandExceptionType(Component.translatable("argument.range.swapped"));
|
| 24 |
|
| 25 | MinMaxBounds.Bounds<T> bounds();
|
| 26 |
|
| 27 | default Optional<T> min() {
|
| 28 | return this.bounds().min;
|
| 29 | }
|
| 30 |
|
| 31 | default Optional<T> max() {
|
| 32 | return this.bounds().max;
|
| 33 | }
|
| 34 |
|
| 35 | default boolean isAny() {
|
| 36 | return this.bounds().isAny();
|
| 37 | }
|
| 38 |
|
| 39 | static <V extends Number & Comparable<V>, B extends MinMaxBounds<V>> Function<B, DataResult<B>> validateContainedInRange(final MinMaxBounds<V> allowed) {
|
| 40 | Range<V> allowedRange = allowed.bounds().asRange();
|
| 41 | return target -> {
|
| 42 | Range<V> selfAsRange = target.bounds().asRange();
|
| 43 | return !allowedRange.encloses(selfAsRange)
|
| 44 | ? DataResult.error(() -> "Range must be within " + allowedRange + ", but was " + selfAsRange)
|
| 45 | : DataResult.success(target);
|
| 46 | };
|
| 47 | }
|
| 48 |
|
| 49 | record Bounds<T extends Number & Comparable<T>>(Optional<T> min, Optional<T> max) {
|
| 50 | public boolean isAny() {
|
| 51 | return this.min().isEmpty() && this.max().isEmpty();
|
| 52 | }
|
| 53 |
|
| 54 | public DataResult<MinMaxBounds.Bounds<T>> validateSwappedBoundsInCodec() {
|
| 55 | return this.areSwapped() ? DataResult.error(() -> "Swapped bounds in range: " + this.min() + " is higher than " + this.max()) : DataResult.success(this);
|
| 56 | }
|
| 57 |
|
| 58 | public boolean areSwapped() {
|
| 59 | return this.min.isPresent() && this.max.isPresent() && this.min.get().compareTo(this.max.get()) > 0;
|
| 60 | }
|
| 61 |
|
| 62 | public Range<T> asRange() {
|
| 63 | if (this.min.isPresent()) {
|
| 64 | return this.max.isPresent() ? Range.closed(this.min.get(), this.max.get()) : Range.atLeast(this.min.get());
|
| 65 | } else {
|
| 66 | return this.max.isPresent() ? Range.atMost(this.max.get()) : Range.all();
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public Optional<T> asPoint() {
|
| 71 | Optional<T> min = this.min();
|
| 72 | Optional<T> max = this.max();
|
| 73 | return min.equals(max) ? min : Optional.empty();
|
| 74 | }
|
| 75 |
|
| 76 | public static <T extends Number & Comparable<T>> MinMaxBounds.Bounds<T> any() {
|
| 77 | return new MinMaxBounds.Bounds<>(Optional.empty(), Optional.empty());
|
| 78 | }
|
| 79 |
|
| 80 | public static <T extends Number & Comparable<T>> MinMaxBounds.Bounds<T> exactly(final T value) {
|
| 81 | Optional<T> wrapped = Optional.of(value);
|
| 82 | return new MinMaxBounds.Bounds<>(wrapped, wrapped);
|
| 83 | }
|
| 84 |
|
| 85 | public static <T extends Number & Comparable<T>> MinMaxBounds.Bounds<T> between(final T min, final T max) {
|
| 86 | return new MinMaxBounds.Bounds<>(Optional.of(min), Optional.of(max));
|
| 87 | }
|
| 88 |
|
| 89 | public static <T extends Number & Comparable<T>> MinMaxBounds.Bounds<T> atLeast(final T value) {
|
| 90 | return new MinMaxBounds.Bounds<>(Optional.of(value), Optional.empty());
|
| 91 | }
|
| 92 |
|
| 93 | public static <T extends Number & Comparable<T>> MinMaxBounds.Bounds<T> atMost(final T value) {
|
| 94 | return new MinMaxBounds.Bounds<>(Optional.empty(), Optional.of(value));
|
| 95 | }
|
| 96 |
|
| 97 | public <U extends Number & Comparable<U>> MinMaxBounds.Bounds<U> map(final Function<T, U> mapper) {
|
| 98 | return new MinMaxBounds.Bounds<>(this.min.map(mapper), this.max.map(mapper));
|
| 99 | }
|
| 100 |
|
| 101 | public static <T extends Number & Comparable<T>> Codec<MinMaxBounds.Bounds<T>> createCodec(final Codec<T> numberCodec) {
|
| 102 | Codec<MinMaxBounds.Bounds<T>> rangeCodec = RecordCodecBuilder.create(
|
| 103 | i -> i.group(numberCodec.optionalFieldOf("min").forGetter(MinMaxBounds.Bounds::min), numberCodec.optionalFieldOf("max").forGetter(MinMaxBounds.Bounds::max))
|
| 104 | .apply(i, MinMaxBounds.Bounds::new)
|
| 105 | );
|
| 106 | return Codec.either(rangeCodec, numberCodec).xmap(either -> either.map(v -> v, x$0 -> exactly((T)x$0)), bounds -> {
|
| 107 | Optional<T> point = bounds.asPoint();
|
| 108 | return point.isPresent() ? Either.right(point.get()) : Either.left((MinMaxBounds.Bounds<T>)bounds);
|
| 109 | });
|
| 110 | }
|
| 111 |
|
| 112 | public static <B extends ByteBuf, T extends Number & Comparable<T>> StreamCodec<B, MinMaxBounds.Bounds<T>> createStreamCodec(
|
| 113 | final StreamCodec<B, T> numberCodec
|
| 114 | ) {
|
| 115 | return new StreamCodec<B, MinMaxBounds.Bounds<T>>() {
|
| 116 | private static final int MIN_FLAG = 1;
|
| 117 | private static final int MAX_FLAG = 2;
|
| 118 |
|
| 119 | public MinMaxBounds.Bounds<T> decode(final B input) {
|
| 120 | byte flags = input.readByte();
|
| 121 | Optional<T> min = (flags & 1) != 0 ? Optional.of(numberCodec.decode(input)) : Optional.empty();
|
| 122 | Optional<T> max = (flags & 2) != 0 ? Optional.of(numberCodec.decode(input)) : Optional.empty();
|
| 123 | return new MinMaxBounds.Bounds<>(min, max);
|
| 124 | }
|
| 125 |
|
| 126 | public void encode(final B output, final MinMaxBounds.Bounds<T> value) {
|
| 127 | Optional<T> min = value.min();
|
| 128 | Optional<T> max = value.max();
|
| 129 | output.writeByte((min.isPresent() ? 1 : 0) | (max.isPresent() ? 2 : 0));
|
| 130 | min.ifPresent(v -> numberCodec.encode(output, (T)v));
|
| 131 | max.ifPresent(v -> numberCodec.encode(output, (T)v));
|
| 132 | }
|
| 133 | };
|
| 134 | }
|
| 135 |
|
| 136 | public static <T extends Number & Comparable<T>> MinMaxBounds.Bounds<T> fromReader(
|
| 137 | final StringReader reader, final Function<String, T> converter, final Supplier<DynamicCommandExceptionType> parseExc
|
| 138 | ) throws CommandSyntaxException {
|
| 139 | if (!reader.canRead()) {
|
| 140 | throw MinMaxBounds.ERROR_EMPTY.createWithContext(reader);
|
| 141 | }
|
| 142 |
|
| 143 | int start = reader.getCursor();
|
| 144 |
|
| 145 | try {
|
| 146 | Optional<T> min = readNumber(reader, converter, parseExc);
|
| 147 | Optional<T> max;
|
| 148 | if (reader.canRead(2) && reader.peek() == '.' && reader.peek(1) == '.') {
|
| 149 | reader.skip();
|
| 150 | reader.skip();
|
| 151 | max = readNumber(reader, converter, parseExc);
|
| 152 | } else {
|
| 153 | max = min;
|
| 154 | }
|
| 155 |
|
| 156 | if (min.isEmpty() && max.isEmpty()) {
|
| 157 | throw MinMaxBounds.ERROR_EMPTY.createWithContext(reader);
|
| 158 | } else {
|
| 159 | return new MinMaxBounds.Bounds<>(min, max);
|
| 160 | }
|
| 161 | } catch (CommandSyntaxException e) {
|
| 162 | reader.setCursor(start);
|
| 163 | throw new CommandSyntaxException(e.getType(), e.getRawMessage(), e.getInput(), start);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | private static <T extends Number> Optional<T> readNumber(
|
| 168 | final StringReader reader, final Function<String, T> converter, final Supplier<DynamicCommandExceptionType> parseExc
|
| 169 | ) throws CommandSyntaxException {
|
| 170 | int start = reader.getCursor();
|
| 171 |
|
| 172 | while (reader.canRead() && isAllowedInputChar(reader)) {
|
| 173 | reader.skip();
|
| 174 | }
|
| 175 |
|
| 176 | String number = reader.getString().substring(start, reader.getCursor());
|
| 177 | if (number.isEmpty()) {
|
| 178 | return Optional.empty();
|
| 179 | }
|
| 180 |
|
| 181 | try {
|
| 182 | return Optional.of(converter.apply(number));
|
| 183 | } catch (NumberFormatException ex) {
|
| 184 | throw parseExc.get().createWithContext(reader, number);
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | private static boolean isAllowedInputChar(final StringReader reader) {
|
| 189 | char c = reader.peek();
|
| 190 | if ((c < '0' || c > '9') && c != '-') {
|
| 191 | return c != '.' ? false : !reader.canRead(2) || reader.peek(1) != '.';
|
| 192 | } else {
|
| 193 | return true;
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | record Doubles(MinMaxBounds.Bounds<Double> bounds, MinMaxBounds.Bounds<Double> boundsSqr) implements MinMaxBounds<Double> {
|
| 199 | public static final MinMaxBounds.Doubles ANY = new MinMaxBounds.Doubles(MinMaxBounds.Bounds.any());
|
| 200 | public static final Codec<MinMaxBounds.Doubles> CODEC = MinMaxBounds.Bounds.createCodec(Codec.DOUBLE)
|
| 201 | .validate(MinMaxBounds.Bounds::validateSwappedBoundsInCodec)
|
| 202 | .xmap(MinMaxBounds.Doubles::new, MinMaxBounds.Doubles::bounds);
|
| 203 | public static final StreamCodec<ByteBuf, MinMaxBounds.Doubles> STREAM_CODEC = MinMaxBounds.Bounds.createStreamCodec(ByteBufCodecs.DOUBLE)
|
| 204 | .map(MinMaxBounds.Doubles::new, MinMaxBounds.Doubles::bounds);
|
| 205 |
|
| 206 | private Doubles(final MinMaxBounds.Bounds<Double> bounds) {
|
| 207 | this(bounds, bounds.map(Mth::square));
|
| 208 | }
|
| 209 |
|
| 210 | public static MinMaxBounds.Doubles exactly(final double value) {
|
| 211 | return new MinMaxBounds.Doubles(MinMaxBounds.Bounds.exactly(value));
|
| 212 | }
|
| 213 |
|
| 214 | public static MinMaxBounds.Doubles between(final double min, final double max) {
|
| 215 | return new MinMaxBounds.Doubles(MinMaxBounds.Bounds.between(min, max));
|
| 216 | }
|
| 217 |
|
| 218 | public static MinMaxBounds.Doubles atLeast(final double value) {
|
| 219 | return new MinMaxBounds.Doubles(MinMaxBounds.Bounds.atLeast(value));
|
| 220 | }
|
| 221 |
|
| 222 | public static MinMaxBounds.Doubles atMost(final double value) {
|
| 223 | return new MinMaxBounds.Doubles(MinMaxBounds.Bounds.atMost(value));
|
| 224 | }
|
| 225 |
|
| 226 | public boolean matches(final double value) {
|
| 227 | return this.bounds.min.isPresent() && this.bounds.min.get() > value ? false : this.bounds.max.isEmpty() || !(this.bounds.max.get() < value);
|
| 228 | }
|
| 229 |
|
| 230 | public boolean matchesSqr(final double valueSqr) {
|
| 231 | return this.boundsSqr.min.isPresent() && this.boundsSqr.min.get() > valueSqr
|
| 232 | ? false
|
| 233 | : this.boundsSqr.max.isEmpty() || !(this.boundsSqr.max.get() < valueSqr);
|
| 234 | }
|
| 235 |
|
| 236 | public static MinMaxBounds.Doubles fromReader(final StringReader reader) throws CommandSyntaxException {
|
| 237 | int start = reader.getCursor();
|
| 238 | MinMaxBounds.Bounds<Double> bounds = MinMaxBounds.Bounds.fromReader(
|
| 239 | reader, Double::parseDouble, CommandSyntaxException.BUILT_IN_EXCEPTIONS::readerInvalidDouble
|
| 240 | );
|
| 241 | if (bounds.areSwapped()) {
|
| 242 | reader.setCursor(start);
|
| 243 | throw ERROR_SWAPPED.createWithContext(reader);
|
| 244 | } else {
|
| 245 | return new MinMaxBounds.Doubles(bounds);
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | record FloatDegrees(MinMaxBounds.Bounds<Float> bounds) implements MinMaxBounds<Float> {
|
| 251 | public static final MinMaxBounds.FloatDegrees ANY = new MinMaxBounds.FloatDegrees(MinMaxBounds.Bounds.any());
|
| 252 | public static final Codec<MinMaxBounds.FloatDegrees> CODEC = MinMaxBounds.Bounds.createCodec(Codec.FLOAT)
|
| 253 | .xmap(MinMaxBounds.FloatDegrees::new, MinMaxBounds.FloatDegrees::bounds);
|
| 254 | public static final StreamCodec<ByteBuf, MinMaxBounds.FloatDegrees> STREAM_CODEC = MinMaxBounds.Bounds.createStreamCodec(ByteBufCodecs.FLOAT)
|
| 255 | .map(MinMaxBounds.FloatDegrees::new, MinMaxBounds.FloatDegrees::bounds);
|
| 256 |
|
| 257 | public static MinMaxBounds.FloatDegrees fromReader(final StringReader reader) throws CommandSyntaxException {
|
| 258 | MinMaxBounds.Bounds<Float> bounds = MinMaxBounds.Bounds.fromReader(reader, Float::parseFloat, CommandSyntaxException.BUILT_IN_EXCEPTIONS::readerInvalidFloat);
|
| 259 | return new MinMaxBounds.FloatDegrees(bounds);
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | record Ints(MinMaxBounds.Bounds<Integer> bounds, MinMaxBounds.Bounds<Long> boundsSqr) implements MinMaxBounds<Integer> {
|
| 264 | public static final MinMaxBounds.Ints ANY = new MinMaxBounds.Ints(MinMaxBounds.Bounds.any());
|
| 265 | public static final Codec<MinMaxBounds.Ints> CODEC = MinMaxBounds.Bounds.createCodec(Codec.INT)
|
| 266 | .validate(MinMaxBounds.Bounds::validateSwappedBoundsInCodec)
|
| 267 | .xmap(MinMaxBounds.Ints::new, MinMaxBounds.Ints::bounds);
|
| 268 | public static final StreamCodec<ByteBuf, MinMaxBounds.Ints> STREAM_CODEC = MinMaxBounds.Bounds.createStreamCodec(ByteBufCodecs.INT)
|
| 269 | .map(MinMaxBounds.Ints::new, MinMaxBounds.Ints::bounds);
|
| 270 |
|
| 271 | private Ints(final MinMaxBounds.Bounds<Integer> bounds) {
|
| 272 | this(bounds, bounds.map(i -> Mth.square(i.longValue())));
|
| 273 | }
|
| 274 |
|
| 275 | public static MinMaxBounds.Ints exactly(final int value) {
|
| 276 | return new MinMaxBounds.Ints(MinMaxBounds.Bounds.exactly(value));
|
| 277 | }
|
| 278 |
|
| 279 | public static MinMaxBounds.Ints between(final int min, final int max) {
|
| 280 | return new MinMaxBounds.Ints(MinMaxBounds.Bounds.between(min, max));
|
| 281 | }
|
| 282 |
|
| 283 | public static MinMaxBounds.Ints atLeast(final int value) {
|
| 284 | return new MinMaxBounds.Ints(MinMaxBounds.Bounds.atLeast(value));
|
| 285 | }
|
| 286 |
|
| 287 | public static MinMaxBounds.Ints atMost(final int value) {
|
| 288 | return new MinMaxBounds.Ints(MinMaxBounds.Bounds.atMost(value));
|
| 289 | }
|
| 290 |
|
| 291 | public boolean matches(final int value) {
|
| 292 | return this.bounds.min.isPresent() && this.bounds.min.get() > value ? false : this.bounds.max.isEmpty() || this.bounds.max.get() >= value;
|
| 293 | }
|
| 294 |
|
| 295 | public boolean matchesSqr(final long valueSqr) {
|
| 296 | return this.boundsSqr.min.isPresent() && this.boundsSqr.min.get() > valueSqr ? false : this.boundsSqr.max.isEmpty() || this.boundsSqr.max.get() >= valueSqr;
|
| 297 | }
|
| 298 |
|
| 299 | public static MinMaxBounds.Ints fromReader(final StringReader reader) throws CommandSyntaxException {
|
| 300 | int start = reader.getCursor();
|
| 301 | MinMaxBounds.Bounds<Integer> bounds = MinMaxBounds.Bounds.fromReader(reader, Integer::parseInt, CommandSyntaxException.BUILT_IN_EXCEPTIONS::readerInvalidInt);
|
| 302 | if (bounds.areSwapped()) {
|
| 303 | reader.setCursor(start);
|
| 304 | throw ERROR_SWAPPED.createWithContext(reader);
|
| 305 | } else {
|
| 306 | return new MinMaxBounds.Ints(bounds);
|
| 307 | }
|
| 308 | }
|
| 309 | }
|
| 310 | }
|
| 311 |
|