Skip to content

Commit 529dcfb

Browse files
improved the CommandResult handling
1 parent f17636e commit 529dcfb

3 files changed

Lines changed: 41 additions & 21 deletions

File tree

commands/src/main/java/com/wizardlybump17/wlib/command/exception/CommandExecutionException.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public CommandExecutionException(@NotNull Throwable cause, int lastInputIndex, @
3737
this.reason = reason;
3838
}
3939

40+
public @NotNull Reason getReason() {
41+
return reason;
42+
}
43+
44+
public @Nullable CommandNode<?> getLastNode() {
45+
return lastNode;
46+
}
47+
48+
public int getLastInputIndex() {
49+
return lastInputIndex;
50+
}
51+
4052
public enum Reason {
4153

4254
EMPTY_INPUT,
@@ -45,7 +57,6 @@ public enum Reason {
4557
EXTRA_INPUT,
4658
NO_COMMAND_EXECUTOR,
4759
COMMAND_NOT_FOUND,
48-
NULL_INPUT_NOT_ACCEPTED,
4960
INVALID_COMMAND_RESULT,
5061
GENERIC
5162
}

commands/src/main/java/com/wizardlybump17/wlib/command/manager/CommandManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ protected void addCommand(@NotNull String identifier, @NotNull String name, @Not
174174
}
175175
}
176176

177-
public @NotNull CommandResult<?> execute(@NotNull CommandSender<?> sender, @NotNull String input) {
177+
public @NotNull CommandResult<?> execute(@NotNull CommandSender<?> sender, @NotNull String input) throws CommandExecutionException {
178178
return execute(sender, StringUtil.parseQuotedStrings(input));
179179
}
180180

181-
public @NotNull CommandResult<?> execute(@NotNull CommandSender<?> sender, @NotNull String @NotNull [] input) {
181+
public @NotNull CommandResult<?> execute(@NotNull CommandSender<?> sender, @NotNull String @NotNull [] input) throws CommandExecutionException {
182182
return execute(sender, String.join(" ", input));
183183
}
184184

core/src/main/java/com/wizardlybump17/wlib/command/WLibCommandExecutor.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.wizardlybump17.wlib.command;
22

3+
import com.wizardlybump17.wlib.command.exception.CommandExecutionException;
34
import com.wizardlybump17.wlib.command.exception.SuggesterException;
45
import com.wizardlybump17.wlib.command.manager.CommandManager;
56
import com.wizardlybump17.wlib.command.result.CommandResult;
6-
import com.wizardlybump17.wlib.command.result.error.*;
7-
import com.wizardlybump17.wlib.command.result.success.SuccessResult;
87
import com.wizardlybump17.wlib.command.sender.BukkitCommandSender;
98
import org.bukkit.command.Command;
109
import org.bukkit.command.CommandExecutor;
@@ -41,25 +40,35 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
4140

4241
String wlibArgs = command.getName() + " " + String.join(" ", args);
4342

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+
}
5065
}
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;
6167
}
6268

69+
if (!result.success())
70+
sender.sendMessage("§c" + result.errorDetails().message());
71+
6372
return false;
6473
}
6574

0 commit comments

Comments
 (0)