|
| 1 | +package de.pascalpex.pexnpc.commands; |
| 2 | + |
| 3 | +import com.mojang.brigadier.Message; |
| 4 | +import com.mojang.brigadier.arguments.ArgumentType; |
| 5 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 6 | +import com.mojang.brigadier.context.CommandContext; |
| 7 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 8 | +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; |
| 9 | +import com.mojang.brigadier.suggestion.Suggestions; |
| 10 | +import com.mojang.brigadier.suggestion.SuggestionsBuilder; |
| 11 | +import de.pascalpex.pexnpc.npc.NPCPose; |
| 12 | +import de.pascalpex.pexnpc.util.MessageHandler; |
| 13 | +import io.papermc.paper.command.brigadier.MessageComponentSerializer; |
| 14 | +import io.papermc.paper.command.brigadier.argument.CustomArgumentType; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | + |
| 19 | +public class NPCPoseArgument implements CustomArgumentType.Converted<NPCPose, String> { |
| 20 | + @Override |
| 21 | + public @NotNull NPCPose convert(@NotNull String nativeType) throws CommandSyntaxException { |
| 22 | + for (NPCPose pose : NPCPose.values()) { |
| 23 | + if (pose.name().equalsIgnoreCase(nativeType)) { |
| 24 | + return pose; |
| 25 | + } |
| 26 | + } |
| 27 | + final Message exceptionMessage = MessageComponentSerializer.message().serialize(MessageHandler.errorMessage("The NPC pose you entered is invalid. Valid options are STANDING, CROUCHING, SLEEPING and SWIMMING")); |
| 28 | + throw new CommandSyntaxException(new SimpleCommandExceptionType(exceptionMessage), exceptionMessage); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public @NotNull ArgumentType<String> getNativeType() { |
| 33 | + return StringArgumentType.word(); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public <S> @NotNull CompletableFuture<Suggestions> listSuggestions(@NotNull CommandContext<S> context, @NotNull SuggestionsBuilder builder) { |
| 38 | + String currentInput = ""; |
| 39 | + try { |
| 40 | + currentInput = context.getInput().split(" ")[3].toUpperCase(); |
| 41 | + } catch (ArrayIndexOutOfBoundsException ignored) { |
| 42 | + } // Command does not contain the argument yet |
| 43 | + for (NPCPose pose : NPCPose.values()) { |
| 44 | + if (pose.name().startsWith(currentInput.toUpperCase())) { |
| 45 | + builder.suggest(pose.name()); |
| 46 | + } |
| 47 | + } |
| 48 | + return builder.buildFuture(); |
| 49 | + } |
| 50 | +} |
0 commit comments