| 1 | package net.minecraft.commands.arguments.coordinates;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 5 | import com.mojang.brigadier.context.CommandContext;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 9 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 10 | import java.util.Arrays;
|
| 11 | import java.util.Collection;
|
| 12 | import java.util.Collections;
|
| 13 | import java.util.concurrent.CompletableFuture;
|
| 14 | import net.minecraft.commands.CommandSourceStack;
|
| 15 | import net.minecraft.commands.Commands;
|
| 16 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.world.phys.Vec2;
|
| 19 | import net.minecraft.world.phys.Vec3;
|
| 20 |
|
| 21 | public class Vec2Argument implements ArgumentType<Coordinates> {
|
| 22 | private static final Collection<String> EXAMPLES = Arrays.asList("0 0", "~ ~", "0.1 -0.5", "~1 ~-2");
|
| 23 | public static final SimpleCommandExceptionType ERROR_NOT_COMPLETE = new SimpleCommandExceptionType(Component.translatable("argument.pos2d.incomplete"));
|
| 24 | private final boolean centerCorrect;
|
| 25 |
|
| 26 | public Vec2Argument(final boolean centerCorrect) {
|
| 27 | this.centerCorrect = centerCorrect;
|
| 28 | }
|
| 29 |
|
| 30 | public static Vec2Argument vec2() {
|
| 31 | return new Vec2Argument(true);
|
| 32 | }
|
| 33 |
|
| 34 | public static Vec2Argument vec2(final boolean centerCorrect) {
|
| 35 | return new Vec2Argument(centerCorrect);
|
| 36 | }
|
| 37 |
|
| 38 | public static Vec2 getVec2(final CommandContext<CommandSourceStack> context, final String name) {
|
| 39 | Vec3 vec3 = context.getArgument(name, Coordinates.class).getPosition(context.getSource());
|
| 40 | return new Vec2((float)vec3.x, (float)vec3.z);
|
| 41 | }
|
| 42 |
|
| 43 | public Coordinates parse(final StringReader reader) throws CommandSyntaxException {
|
| 44 | int start = reader.getCursor();
|
| 45 | if (!reader.canRead()) {
|
| 46 | throw ERROR_NOT_COMPLETE.createWithContext(reader);
|
| 47 | } else {
|
| 48 | WorldCoordinate x = WorldCoordinate.parseDouble(reader, this.centerCorrect);
|
| 49 | if (reader.canRead() && reader.peek() == ' ') {
|
| 50 | reader.skip();
|
| 51 | WorldCoordinate z = WorldCoordinate.parseDouble(reader, this.centerCorrect);
|
| 52 | return new WorldCoordinates(x, new WorldCoordinate(true, 0.0), z);
|
| 53 | } else {
|
| 54 | reader.setCursor(start);
|
| 55 | throw ERROR_NOT_COMPLETE.createWithContext(reader);
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 62 | if (!(context.getSource() instanceof SharedSuggestionProvider)) {
|
| 63 | return Suggestions.empty();
|
| 64 | }
|
| 65 |
|
| 66 | String remainder = builder.getRemaining();
|
| 67 | Collection<SharedSuggestionProvider.TextCoordinates> suggestedCoordinates;
|
| 68 | if (!remainder.isEmpty() && remainder.charAt(0) == '^') {
|
| 69 | suggestedCoordinates = Collections.singleton(SharedSuggestionProvider.TextCoordinates.DEFAULT_LOCAL);
|
| 70 | } else {
|
| 71 | suggestedCoordinates = ((SharedSuggestionProvider)context.getSource()).getAbsoluteCoordinates();
|
| 72 | }
|
| 73 |
|
| 74 | return SharedSuggestionProvider.suggest2DCoordinates(remainder, suggestedCoordinates, builder, Commands.createValidator(this::parse));
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public Collection<String> getExamples() {
|
| 79 | return EXAMPLES;
|
| 80 | }
|
| 81 | }
|
| 82 |
|