| 1 | package net.minecraft.util.parsing.packrat.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import net.minecraft.util.parsing.packrat.DelayedException;
|
| 6 | import net.minecraft.util.parsing.packrat.ParseState;
|
| 7 | import net.minecraft.util.parsing.packrat.Rule;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public abstract class NumberRunParseRule implements Rule<StringReader, String> {
|
| 11 | private final DelayedException<CommandSyntaxException> noValueError;
|
| 12 | private final DelayedException<CommandSyntaxException> underscoreNotAllowedError;
|
| 13 |
|
| 14 | public NumberRunParseRule(
|
| 15 | final DelayedException<CommandSyntaxException> noValueError, final DelayedException<CommandSyntaxException> underscoreNotAllowedError
|
| 16 | ) {
|
| 17 | this.noValueError = noValueError;
|
| 18 | this.underscoreNotAllowedError = underscoreNotAllowedError;
|
| 19 | }
|
| 20 |
|
| 21 | public @Nullable String parse(final ParseState<StringReader> state) {
|
| 22 | StringReader input = state.input();
|
| 23 | input.skipWhitespace();
|
| 24 | String fullString = input.getString();
|
| 25 | int start = input.getCursor();
|
| 26 | int pos = start;
|
| 27 |
|
| 28 | while (pos < fullString.length() && this.isAccepted(fullString.charAt(pos))) {
|
| 29 | pos++;
|
| 30 | }
|
| 31 |
|
| 32 | int length = pos - start;
|
| 33 | if (length == 0) {
|
| 34 | state.errorCollector().store(state.mark(), this.noValueError);
|
| 35 | return null;
|
| 36 | } else if (fullString.charAt(start) != '_' && fullString.charAt(pos - 1) != '_') {
|
| 37 | input.setCursor(pos);
|
| 38 | return fullString.substring(start, pos);
|
| 39 | } else {
|
| 40 | state.errorCollector().store(state.mark(), this.underscoreNotAllowedError);
|
| 41 | return null;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | protected abstract boolean isAccepted(char c);
|
| 46 | }
|
| 47 |
|