| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 6 | import com.mojang.serialization.DynamicOps;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.UUID;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import java.util.stream.IntStream;
|
| 14 | import java.util.stream.Stream;
|
| 15 | import net.minecraft.core.UUIDUtil;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.util.parsing.packrat.DelayedException;
|
| 18 | import net.minecraft.util.parsing.packrat.ParseState;
|
| 19 | import net.minecraft.util.parsing.packrat.SuggestionSupplier;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public class SnbtOperations {
|
| 23 | private static final DelayedException<CommandSyntaxException> ERROR_EXPECTED_STRING_UUID = DelayedException.create(
|
| 24 | new SimpleCommandExceptionType(Component.translatable("snbt.parser.expected_string_uuid"))
|
| 25 | );
|
| 26 | private static final DelayedException<CommandSyntaxException> ERROR_EXPECTED_NUMBER_OR_BOOLEAN = DelayedException.create(
|
| 27 | new SimpleCommandExceptionType(Component.translatable("snbt.parser.expected_number_or_boolean"))
|
| 28 | );
|
| 29 | public static final String BUILTIN_TRUE = "true";
|
| 30 | public static final String BUILTIN_FALSE = "false";
|
| 31 | public static final Map<SnbtOperations.BuiltinKey, SnbtOperations.BuiltinOperation> BUILTIN_OPERATIONS = Map.of(
|
| 32 | new SnbtOperations.BuiltinKey("bool", 1), new SnbtOperations.BuiltinOperation() {
|
| 33 | @Override
|
| 34 | public <T> T run(final DynamicOps<T> ops, final List<T> arguments, final ParseState<StringReader> state) {
|
| 35 | Boolean result = convert(ops, arguments.getFirst());
|
| 36 | if (result == null) {
|
| 37 | state.errorCollector().store(state.mark(), SnbtOperations.ERROR_EXPECTED_NUMBER_OR_BOOLEAN);
|
| 38 | return null;
|
| 39 | } else {
|
| 40 | return ops.createBoolean(result);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | private static <T> @Nullable Boolean convert(final DynamicOps<T> ops, final T arg) {
|
| 45 | Optional<Boolean> asBoolean = ops.getBooleanValue(arg).result();
|
| 46 | if (asBoolean.isPresent()) {
|
| 47 | return asBoolean.get();
|
| 48 | }
|
| 49 |
|
| 50 | Optional<Number> asNumber = ops.getNumberValue(arg).result();
|
| 51 | return asNumber.isPresent() ? asNumber.get().doubleValue() != 0.0 : null;
|
| 52 | }
|
| 53 | }, new SnbtOperations.BuiltinKey("uuid", 1), new SnbtOperations.BuiltinOperation() {
|
| 54 | @Override
|
| 55 | public <T> T run(final DynamicOps<T> ops, final List<T> arguments, final ParseState<StringReader> state) {
|
| 56 | Optional<String> arg = ops.getStringValue(arguments.getFirst()).result();
|
| 57 | if (arg.isEmpty()) {
|
| 58 | state.errorCollector().store(state.mark(), SnbtOperations.ERROR_EXPECTED_STRING_UUID);
|
| 59 | return null;
|
| 60 | }
|
| 61 |
|
| 62 | UUID uuid;
|
| 63 | try {
|
| 64 | uuid = UUID.fromString(arg.get());
|
| 65 | } catch (IllegalArgumentException e) {
|
| 66 | state.errorCollector().store(state.mark(), SnbtOperations.ERROR_EXPECTED_STRING_UUID);
|
| 67 | return null;
|
| 68 | }
|
| 69 |
|
| 70 | return ops.createIntList(IntStream.of(UUIDUtil.uuidToIntArray(uuid)));
|
| 71 | }
|
| 72 | }
|
| 73 | );
|
| 74 | public static final SuggestionSupplier<StringReader> BUILTIN_IDS = new SuggestionSupplier<StringReader>() {
|
| 75 | private final Set<String> keys = Stream.concat(
|
| 76 | Stream.of("false", "true"), SnbtOperations.BUILTIN_OPERATIONS.keySet().stream().map(SnbtOperations.BuiltinKey::id)
|
| 77 | )
|
| 78 | .collect(Collectors.toSet());
|
| 79 |
|
| 80 | @Override
|
| 81 | public Stream<String> possibleValues(final ParseState<StringReader> state) {
|
| 82 | return this.keys.stream();
|
| 83 | }
|
| 84 | };
|
| 85 |
|
| 86 | public record BuiltinKey(String id, int argCount) {
|
| 87 | @Override
|
| 88 | public String toString() {
|
| 89 | return this.id + "/" + this.argCount;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | public interface BuiltinOperation {
|
| 94 | <T> @Nullable T run(DynamicOps<T> ops, List<T> arguments, ParseState<StringReader> state);
|
| 95 | }
|
| 96 | }
|
| 97 |
|