Skip to content

Commit

Permalink
Add exception handling/messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Badbird5907 committed Feb 1, 2023
1 parent c7ce8b9 commit aa99896
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public void handleCommandException(CommandInfo info, CoreCommandSender sender, C
} else sender.sendMessage(ChatColor.RED + e.getMessage());
}

@Override
public boolean handleExecutionException(CommandContext ctx, Exception e, CoreCommandSender sender) {
sender.sendMessage(ChatColor.RED + commander.getResponseHandler().getMessage("command.error", ctx.getCommandInfo().getName()));
return true;
}

@Override
public void runAsync(Runnable runnable) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,14 @@ private void executeInternally(CoreCommandSender sender, String label, String[]
LocalizedCommandException.checkResponseHandlerNull(e, getResponseHandler());
e.printStackTrace();
} catch (InvocationTargetException e) {
if (e.getCause() != null && e.getCause() instanceof CommandException) {
LocalizedCommandException.checkResponseHandlerNull((Exception) e.getCause(), getResponseHandler());
platform.handleCommandException(context, (CommandException) e.getCause());
if (e.getCause() != null) {
if (e.getCause() instanceof CommandException) {
LocalizedCommandException.checkResponseHandlerNull((Exception) e.getCause(), getResponseHandler());
platform.handleCommandException(context, (CommandException) e.getCause());
} else {
platform.handleExecutionException(context, e, sender);
throw e;
}
}
}
for (BiConsumer<CommandContext, Object> postProcessor : postProcessors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public interface CommanderPlatform {

void handleCommandException(CommandInfo info, CoreCommandSender sender, CommandException e);

/**
* Handle an exception that occurred during command execution
*
* @param ctx
* @param e
* @param sender
* @return true if the exception should be propagated
*/
default boolean handleExecutionException(CommandContext ctx, Exception e, CoreCommandSender sender) {
return true;
}

default boolean hasPermission(CoreCommandSender sender, String permission) {
return sender.hasPermission(permission);
}
Expand Down
1 change: 1 addition & 0 deletions Commander-Core/src/main/resources/commander_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ no-permission=You do not have permission to perform this command.
suggestion.completer-must-return-string=Completer method must return a collection of strings.
suggestion.failed-to-invoke=Failed to invoke completer method.
command.not-found=Command not found!
command.error=An error occurred while executing this command.

0 comments on commit aa99896

Please sign in to comment.