| 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.Vec3;
|
| 19 |
|
| 20 | public class Vec3Argument implements ArgumentType<Coordinates> {
|
| 21 | private static final Collection<String> EXAMPLES = Arrays.asList("0 0 0", "~ ~ ~", "^ ^ ^", "^1 ^ ^-5", "0.1 -0.5 .9", "~0.5 ~1 ~-5");
|
| 22 | public static final SimpleCommandExceptionType ERROR_NOT_COMPLETE = new SimpleCommandExceptionType(Component.translatable("argument.pos3d.incomplete"));
|
| 23 | public static final SimpleCommandExceptionType ERROR_MIXED_TYPE = new SimpleCommandExceptionType(Component.translatable("argument.pos.mixed"));
|
| 24 | private final boolean centerCorrect;
|
| 25 |
|
| 26 | public Vec3Argument(final boolean centerCorrect) {
|
| 27 | this.centerCorrect = centerCorrect;
|
| 28 | }
|
| 29 |
|
| 30 | public static Vec3Argument vec3() {
|
| 31 | return new Vec3Argument(true);
|
| 32 | }
|
| 33 |
|
| 34 | public static Vec3Argument vec3(final boolean centerCorrect) {
|
| 35 | return new Vec3Argument(centerCorrect);
|
| 36 | }
|
| 37 |
|
| 38 | public static Vec3 getVec3(final CommandContext<CommandSourceStack> context, final String name) {
|
| 39 | return context.getArgument(name, Coordinates.class).getPosition(context.getSource());
|
| 40 | }
|
| 41 |
|
| 42 | public static Coordinates getCoordinates(final CommandContext<CommandSourceStack> context, final String name) {
|
| 43 | return context.getArgument(name, Coordinates.class);
|
| 44 | }
|
| 45 |
|
| 46 | public Coordinates parse(final StringReader reader) throws CommandSyntaxException {
|
| 47 | return reader.canRead() && reader.peek() == '^' ? LocalCoordinates.parse(reader) : WorldCoordinates.parseDouble(reader, this.centerCorrect);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 52 | if (!(context.getSource() instanceof SharedSuggestionProvider)) {
|
| 53 | return Suggestions.empty();
|
| 54 | }
|
| 55 |
|
| 56 | String remainder = builder.getRemaining();
|
| 57 | Collection<SharedSuggestionProvider.TextCoordinates> suggestedCoordinates;
|
| 58 | if (!remainder.isEmpty() && remainder.charAt(0) == '^') {
|
| 59 | suggestedCoordinates = Collections.singleton(SharedSuggestionProvider.TextCoordinates.DEFAULT_LOCAL);
|
| 60 | } else {
|
| 61 | suggestedCoordinates = ((SharedSuggestionProvider)context.getSource()).getAbsoluteCoordinates();
|
| 62 | }
|
| 63 |
|
| 64 | return SharedSuggestionProvider.suggestCoordinates(remainder, suggestedCoordinates, builder, Commands.createValidator(this::parse));
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public Collection<String> getExamples() {
|
| 69 | return EXAMPLES;
|
| 70 | }
|
| 71 | }
|
| 72 |
|