From d0e6e10031cc737b934d6d8b43c86b6aa9c19470 Mon Sep 17 00:00:00 2001 From: Revxrsal Date: Thu, 10 Oct 2024 13:19:44 +0300 Subject: [PATCH] map exceptions to ArgumentSyntaxException --- .../minestom/hooks/MinestomCommandHooks.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/minestom/src/main/java/revxrsal/commands/minestom/hooks/MinestomCommandHooks.java b/minestom/src/main/java/revxrsal/commands/minestom/hooks/MinestomCommandHooks.java index b182c28b..7085da66 100644 --- a/minestom/src/main/java/revxrsal/commands/minestom/hooks/MinestomCommandHooks.java +++ b/minestom/src/main/java/revxrsal/commands/minestom/hooks/MinestomCommandHooks.java @@ -31,12 +31,15 @@ import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.arguments.ArgumentLiteral; import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.command.builder.suggestion.SuggestionCallback; import net.minestom.server.command.builder.suggestion.SuggestionEntry; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import revxrsal.commands.autocomplete.SuggestionProvider; import revxrsal.commands.command.ExecutableCommand; +import revxrsal.commands.exception.ThrowableFromCommand; import revxrsal.commands.exception.context.ErrorContext; import revxrsal.commands.hook.CancelHandle; import revxrsal.commands.hook.CommandRegisteredHook; @@ -280,18 +283,28 @@ private ArgumentColl ofFlag(ParameterNode parameter) { .map((sender, strings) -> { A actor = actorFactory.create(sender, command.lamp()); MutableStringStream input = StringStream.createMutable(String.join(" ", strings)); - ExecutionContext context = ExecutionContext.create(command, actor, input); - return parameter.parse(input, context); + return parseOrThrow(command, parameter, actor, input); }); return ArgumentType.String(parameter.name()) .map((sender, s) -> { A actor = actorFactory.create(sender, command.lamp()); MutableStringStream input = StringStream.createMutable(s); - ExecutionContext context = ExecutionContext.create(command, actor, input); - return parameter.parse(input, context); + return parseOrThrow(command, parameter, actor, input); }); } + @Nullable + private T parseOrThrow(@NotNull ExecutableCommand command, @NotNull ParameterNode parameter, A actor, MutableStringStream input) { + ExecutionContext context = ExecutionContext.create(command, actor, input); + try { + return parameter.parse(input, context); + } catch (Throwable t) { + if (t.getClass().isAnnotationPresent(ThrowableFromCommand.class)) + throw new ArgumentSyntaxException(t.getMessage(), context.input().source(), 0); + throw t; + } + } + /** * Creates a {@link SuggestionCallback} that wraps the parameter's {@link SuggestionProvider}. *