| 1 | package net.minecraft.util.parsing.packrat.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 6 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 7 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 8 | import com.mojang.serialization.Codec;
|
| 9 | import com.mojang.serialization.DataResult;
|
| 10 | import com.mojang.serialization.DynamicOps;
|
| 11 | import java.util.concurrent.CompletableFuture;
|
| 12 | import java.util.function.Function;
|
| 13 |
|
| 14 | public interface CommandArgumentParser<T> {
|
| 15 | T parseForCommands(StringReader reader) throws CommandSyntaxException;
|
| 16 |
|
| 17 | CompletableFuture<Suggestions> parseForSuggestions(SuggestionsBuilder suggestionsBuilder);
|
| 18 |
|
| 19 | default <S> CommandArgumentParser<S> mapResult(final Function<T, S> mapper) {
|
| 20 | return new CommandArgumentParser<S>() {
|
| 21 | @Override
|
| 22 | public S parseForCommands(final StringReader reader) throws CommandSyntaxException {
|
| 23 | return mapper.apply((T)CommandArgumentParser.this.parseForCommands(reader));
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | public CompletableFuture<Suggestions> parseForSuggestions(final SuggestionsBuilder suggestionsBuilder) {
|
| 28 | return CommandArgumentParser.this.parseForSuggestions(suggestionsBuilder);
|
| 29 | }
|
| 30 | };
|
| 31 | }
|
| 32 |
|
| 33 | default <T, O> CommandArgumentParser<T> withCodec(
|
| 34 | final DynamicOps<O> ops, final CommandArgumentParser<O> valueParser, final Codec<T> codec, final DynamicCommandExceptionType exceptionType
|
| 35 | ) {
|
| 36 | return new CommandArgumentParser<T>() {
|
| 37 | @Override
|
| 38 | public T parseForCommands(final StringReader reader) throws CommandSyntaxException {
|
| 39 | int cursor = reader.getCursor();
|
| 40 | O tag = valueParser.parseForCommands(reader);
|
| 41 | DataResult<T> result = codec.parse(ops, tag);
|
| 42 | return result.getOrThrow(message -> {
|
| 43 | reader.setCursor(cursor);
|
| 44 | return exceptionType.createWithContext(reader, message);
|
| 45 | });
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public CompletableFuture<Suggestions> parseForSuggestions(final SuggestionsBuilder suggestionsBuilder) {
|
| 50 | return CommandArgumentParser.this.parseForSuggestions(suggestionsBuilder);
|
| 51 | }
|
| 52 | };
|
| 53 | }
|
| 54 | }
|
| 55 |
|