|
1 | 1 | package com.wizardlybump17.wlib.command; |
2 | 2 |
|
| 3 | +import com.wizardlybump17.wlib.command.exception.CommandExecutionException; |
3 | 4 | import com.wizardlybump17.wlib.command.exception.SuggesterException; |
4 | 5 | import com.wizardlybump17.wlib.command.manager.CommandManager; |
5 | 6 | import com.wizardlybump17.wlib.command.result.CommandResult; |
6 | | -import com.wizardlybump17.wlib.command.result.error.*; |
7 | | -import com.wizardlybump17.wlib.command.result.success.SuccessResult; |
8 | 7 | import com.wizardlybump17.wlib.command.sender.BukkitCommandSender; |
9 | 8 | import org.bukkit.command.Command; |
10 | 9 | import org.bukkit.command.CommandExecutor; |
@@ -41,25 +40,35 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command |
41 | 40 |
|
42 | 41 | String wlibArgs = command.getName() + " " + String.join(" ", args); |
43 | 42 |
|
44 | | - CommandResult<?> result = commandManager.execute(wlibSender, wlibArgs); |
45 | | - switch (result) { |
46 | | - case SuccessResult<?> successResult -> {} |
47 | | - case ExceptionResult<?> exceptionResult -> { |
48 | | - sender.sendMessage("§cAn internal error occurred while executing this command: " + exceptionResult.exception() + "."); |
49 | | - logger.log(Level.SEVERE, "Error while " + sender + " tried to execute " + wlibArgs, exceptionResult.exception()); |
| 43 | + CommandResult<?> result; |
| 44 | + try { |
| 45 | + result = commandManager.execute(wlibSender, wlibArgs); |
| 46 | + } catch (CommandExecutionException e) { |
| 47 | + switch (e.getReason()) { |
| 48 | + case EMPTY_INPUT -> sender.sendMessage("§cHow did you manage to send an empty string?"); |
| 49 | + case PARSING_ERROR -> sender.sendMessage("§cError while parsing input at index " + e.getLastInputIndex() + "."); |
| 50 | + case INVALID_INPUT -> sender.sendMessage("§cThe input at index " + e.getLastInputIndex() + " is invalid."); |
| 51 | + case EXTRA_INPUT -> sender.sendMessage("§cReceived extra input after index " + e.getLastInputIndex() + "."); |
| 52 | + case NO_COMMAND_EXECUTOR -> { |
| 53 | + sender.sendMessage("§cThere are no executors for the node at index " + e.getLastInputIndex() + "."); |
| 54 | + getLogger().log(Level.SEVERE, sender.getName() + " tried to execute \"" + wlibArgs + "\", but the node " + e.getLastNode().getName() + " does not have an executor"); |
| 55 | + } |
| 56 | + case COMMAND_NOT_FOUND -> sender.sendMessage("§cCommand not found."); |
| 57 | + case INVALID_COMMAND_RESULT -> { |
| 58 | + sender.sendMessage("§cThe command returned an invalid CommandResult."); |
| 59 | + getLogger().log(Level.SEVERE, sender.getName() + " tried to execute \"" + wlibArgs + "\", but it returned an invalid CommandResult (probably null)"); |
| 60 | + } |
| 61 | + case GENERIC -> { |
| 62 | + sender.sendMessage("§cAn internal error occurred while executing this command."); |
| 63 | + getLogger().log(Level.SEVERE, "Error while " + sender.getName() + " tried to execute \"" + wlibArgs + "\"", e); |
| 64 | + } |
50 | 65 | } |
51 | | - case OutOfRangeInputResult<?> outOfRangeInputResult -> sender.sendMessage("§cInvalid input at index " + outOfRangeInputResult.lastInputIndex() + "."); |
52 | | - case ExtraArgumentsResult<?> extraArgumentsResult -> sender.sendMessage("§cExtra arguments provided at index " + extraArgumentsResult.lastInputIndex() + "."); |
53 | | - case InsufficientArgumentsResult<?> insufficientArgumentsResult -> sender.sendMessage("§cInsufficient arguments provided."); |
54 | | - case ParseInputExceptionResult<?> parseInputExceptionResult -> sender.sendMessage("§cInvalid input at index " + parseInputExceptionResult.lastInputIndex() + ": " + parseInputExceptionResult.exception().getMessage()); |
55 | | - case CommandNodeExecutorNotFoundResult<?> notFoundResult -> sender.sendMessage("§cNo executor found for this command."); |
56 | | - case GenericErrorResult<?> genericErrorResult -> sender.sendMessage("§cAn error occurred while executing the command: " + genericErrorResult.message() + "."); |
57 | | - case NoPermissionResult<?> noPermissionResult -> sender.sendMessage("§cYou do not have permission to execute this command."); |
58 | | - case CommandNotFoundResult<?> notFoundResult -> sender.sendMessage("§cCommand not found."); |
59 | | - case InvalidSenderResult<?> invalidSenderResult -> sender.sendMessage("§cYou can not execute this command."); |
60 | | - default -> {} |
| 66 | + return false; |
61 | 67 | } |
62 | 68 |
|
| 69 | + if (!result.success()) |
| 70 | + sender.sendMessage("§c" + result.errorDetails().message()); |
| 71 | + |
63 | 72 | return false; |
64 | 73 | } |
65 | 74 |
|
|
0 commit comments