| 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.core.BlockPos;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.server.level.ColumnPos;
|
| 20 |
|
| 21 | public class ColumnPosArgument implements ArgumentType<Coordinates> {
|
| 22 | private static final Collection<String> EXAMPLES = Arrays.asList("0 0", "~ ~", "~1 ~-2", "^ ^", "^-1 ^0");
|
| 23 | public static final SimpleCommandExceptionType ERROR_NOT_COMPLETE = new SimpleCommandExceptionType(Component.translatable("argument.pos2d.incomplete"));
|
| 24 |
|
| 25 | public static ColumnPosArgument columnPos() {
|
| 26 | return new ColumnPosArgument();
|
| 27 | }
|
| 28 |
|
| 29 | public static ColumnPos getColumnPos(final CommandContext<CommandSourceStack> context, final String name) {
|
| 30 | BlockPos pos = context.getArgument(name, Coordinates.class).getBlockPos(context.getSource());
|
| 31 | return new ColumnPos(pos.getX(), pos.getZ());
|
| 32 | }
|
| 33 |
|
| 34 | public Coordinates parse(final StringReader reader) throws CommandSyntaxException {
|
| 35 | int start = reader.getCursor();
|
| 36 | if (!reader.canRead()) {
|
| 37 | throw ERROR_NOT_COMPLETE.createWithContext(reader);
|
| 38 | } else {
|
| 39 | WorldCoordinate x = WorldCoordinate.parseInt(reader);
|
| 40 | if (reader.canRead() && reader.peek() == ' ') {
|
| 41 | reader.skip();
|
| 42 | WorldCoordinate z = WorldCoordinate.parseInt(reader);
|
| 43 | return new WorldCoordinates(x, new WorldCoordinate(true, 0.0), z);
|
| 44 | } else {
|
| 45 | reader.setCursor(start);
|
| 46 | throw ERROR_NOT_COMPLETE.createWithContext(reader);
|
| 47 | }
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 53 | if (!(context.getSource() instanceof SharedSuggestionProvider)) {
|
| 54 | return Suggestions.empty();
|
| 55 | }
|
| 56 |
|
| 57 | String remainder = builder.getRemaining();
|
| 58 | Collection<SharedSuggestionProvider.TextCoordinates> suggestedCoordinates;
|
| 59 | if (!remainder.isEmpty() && remainder.charAt(0) == '^') {
|
| 60 | suggestedCoordinates = Collections.singleton(SharedSuggestionProvider.TextCoordinates.DEFAULT_LOCAL);
|
| 61 | } else {
|
| 62 | suggestedCoordinates = ((SharedSuggestionProvider)context.getSource()).getRelevantCoordinates();
|
| 63 | }
|
| 64 |
|
| 65 | return SharedSuggestionProvider.suggest2DCoordinates(remainder, suggestedCoordinates, builder, Commands.createValidator(this::parse));
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public Collection<String> getExamples() {
|
| 70 | return EXAMPLES;
|
| 71 | }
|
| 72 | }
|
| 73 |
|