| 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.ServerLevel;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 |
|
| 22 | public class BlockPosArgument implements ArgumentType<Coordinates> {
|
| 23 | private static final Collection<String> EXAMPLES = Arrays.asList("0 0 0", "~ ~ ~", "^ ^ ^", "^1 ^ ^-5", "~0.5 ~1 ~-5");
|
| 24 | public static final SimpleCommandExceptionType ERROR_NOT_LOADED = new SimpleCommandExceptionType(Component.translatable("argument.pos.unloaded"));
|
| 25 | public static final SimpleCommandExceptionType ERROR_OUT_OF_WORLD = new SimpleCommandExceptionType(Component.translatable("argument.pos.outofworld"));
|
| 26 | public static final SimpleCommandExceptionType ERROR_OUT_OF_BOUNDS = new SimpleCommandExceptionType(Component.translatable("argument.pos.outofbounds"));
|
| 27 |
|
| 28 | public static BlockPosArgument blockPos() {
|
| 29 | return new BlockPosArgument();
|
| 30 | }
|
| 31 |
|
| 32 | public static BlockPos getLoadedBlockPos(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 33 | ServerLevel level = context.getSource().getLevel();
|
| 34 | return getLoadedBlockPos(context, level, name);
|
| 35 | }
|
| 36 |
|
| 37 | public static BlockPos getLoadedBlockPos(final CommandContext<CommandSourceStack> context, final ServerLevel level, final String name) throws CommandSyntaxException {
|
| 38 | BlockPos pos = getBlockPos(context, name);
|
| 39 | if (!level.hasChunkAt(pos)) {
|
| 40 | throw ERROR_NOT_LOADED.create();
|
| 41 | } else if (!level.isInWorldBounds(pos)) {
|
| 42 | throw ERROR_OUT_OF_WORLD.create();
|
| 43 | } else {
|
| 44 | return pos;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public static BlockPos getBlockPos(final CommandContext<CommandSourceStack> context, final String name) {
|
| 49 | return context.getArgument(name, Coordinates.class).getBlockPos(context.getSource());
|
| 50 | }
|
| 51 |
|
| 52 | public static BlockPos getSpawnablePos(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 53 | BlockPos pos = getBlockPos(context, name);
|
| 54 | if (!Level.isInSpawnableBounds(pos)) {
|
| 55 | throw ERROR_OUT_OF_BOUNDS.create();
|
| 56 | } else {
|
| 57 | return pos;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | public Coordinates parse(final StringReader reader) throws CommandSyntaxException {
|
| 62 | return reader.canRead() && reader.peek() == '^' ? LocalCoordinates.parse(reader) : WorldCoordinates.parseInt(reader);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 67 | if (!(context.getSource() instanceof SharedSuggestionProvider)) {
|
| 68 | return Suggestions.empty();
|
| 69 | }
|
| 70 |
|
| 71 | String remainder = builder.getRemaining();
|
| 72 | Collection<SharedSuggestionProvider.TextCoordinates> suggestedCoordinates;
|
| 73 | if (!remainder.isEmpty() && remainder.charAt(0) == '^') {
|
| 74 | suggestedCoordinates = Collections.singleton(SharedSuggestionProvider.TextCoordinates.DEFAULT_LOCAL);
|
| 75 | } else {
|
| 76 | suggestedCoordinates = ((SharedSuggestionProvider)context.getSource()).getRelevantCoordinates();
|
| 77 | }
|
| 78 |
|
| 79 | return SharedSuggestionProvider.suggestCoordinates(remainder, suggestedCoordinates, builder, Commands.createValidator(this::parse));
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public Collection<String> getExamples() {
|
| 84 | return EXAMPLES;
|
| 85 | }
|
| 86 | }
|
| 87 |
|