| 1 | package net.minecraft.commands.arguments.coordinates;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 6 | import net.minecraft.network.chat.Component;
|
| 7 |
|
| 8 | public record WorldCoordinate(boolean relative, double value) {
|
| 9 | private static final char PREFIX_RELATIVE = '~';
|
| 10 | public static final SimpleCommandExceptionType ERROR_EXPECTED_DOUBLE = new SimpleCommandExceptionType(Component.translatable("argument.pos.missing.double"));
|
| 11 | public static final SimpleCommandExceptionType ERROR_EXPECTED_INT = new SimpleCommandExceptionType(Component.translatable("argument.pos.missing.int"));
|
| 12 |
|
| 13 | public double get(final double original) {
|
| 14 | return this.relative ? this.value + original : this.value;
|
| 15 | }
|
| 16 |
|
| 17 | public static WorldCoordinate parseDouble(final StringReader reader, final boolean center) throws CommandSyntaxException {
|
| 18 | if (reader.canRead() && reader.peek() == '^') {
|
| 19 | throw Vec3Argument.ERROR_MIXED_TYPE.createWithContext(reader);
|
| 20 | }
|
| 21 |
|
| 22 | if (!reader.canRead()) {
|
| 23 | throw ERROR_EXPECTED_DOUBLE.createWithContext(reader);
|
| 24 | }
|
| 25 |
|
| 26 | boolean relative = isRelative(reader);
|
| 27 | int start = reader.getCursor();
|
| 28 | double value = reader.canRead() && reader.peek() != ' ' ? reader.readDouble() : 0.0;
|
| 29 | String number = reader.getString().substring(start, reader.getCursor());
|
| 30 | if (relative && number.isEmpty()) {
|
| 31 | return new WorldCoordinate(true, 0.0);
|
| 32 | }
|
| 33 |
|
| 34 | if (!number.contains(".") && !relative && center) {
|
| 35 | value += 0.5;
|
| 36 | }
|
| 37 |
|
| 38 | return new WorldCoordinate(relative, value);
|
| 39 | }
|
| 40 |
|
| 41 | public static WorldCoordinate parseInt(final StringReader reader) throws CommandSyntaxException {
|
| 42 | if (reader.canRead() && reader.peek() == '^') {
|
| 43 | throw Vec3Argument.ERROR_MIXED_TYPE.createWithContext(reader);
|
| 44 | }
|
| 45 |
|
| 46 | if (!reader.canRead()) {
|
| 47 | throw ERROR_EXPECTED_INT.createWithContext(reader);
|
| 48 | }
|
| 49 |
|
| 50 | boolean relative = isRelative(reader);
|
| 51 | double value;
|
| 52 | if (reader.canRead() && reader.peek() != ' ') {
|
| 53 | value = relative ? reader.readDouble() : reader.readInt();
|
| 54 | } else {
|
| 55 | value = 0.0;
|
| 56 | }
|
| 57 |
|
| 58 | return new WorldCoordinate(relative, value);
|
| 59 | }
|
| 60 |
|
| 61 | public static boolean isRelative(final StringReader reader) {
|
| 62 | boolean relative;
|
| 63 | if (reader.peek() == '~') {
|
| 64 | relative = true;
|
| 65 | reader.skip();
|
| 66 | } else {
|
| 67 | relative = false;
|
| 68 | }
|
| 69 |
|
| 70 | return relative;
|
| 71 | }
|
| 72 |
|
| 73 | public boolean isRelative() {
|
| 74 | return this.relative;
|
| 75 | }
|
| 76 | }
|
| 77 |
|