Better display of BadCommandUsage message on paper server

This commit is contained in:
Marc Baloup 2024-02-17 20:02:16 +01:00
parent 7795d94dfb
commit 2393352770
2 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package fr.pandacube.lib.commands;
import java.util.logging.Logger;
/**
* Throw an instance of this exception to indicate to the plugin command handler that the user has missused the command.
* The message, if provided, must indicate the reason of the mussusage of the command. It will be displayed on the
* screen with eventual indications of how to use the command (help command for example).
* If a {@link Throwable} cause is provided, it will be relayed to the plugin {@link Logger}.
*
*/
public class BadCommandUsage extends RuntimeException {
public BadCommandUsage() {
super();
}
public BadCommandUsage(Throwable cause) {
super(cause);
}
public BadCommandUsage(String message) {
super(message);
}
public BadCommandUsage(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -10,6 +10,7 @@ import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import fr.pandacube.lib.chat.Chat;
import fr.pandacube.lib.commands.BadCommandUsage;
import fr.pandacube.lib.commands.BrigadierCommand;
import fr.pandacube.lib.commands.SuggestionsSupplier;
import fr.pandacube.lib.paper.permissions.PandalibPaperPermissions;
@ -461,6 +462,9 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<BukkitBriga
return cmd.run(context);
} catch(CommandSyntaxException e) {
throw e;
} catch (BadCommandUsage e) {
getCommandSender(context).sendMessage(Chat.failureText("Error while using the command: " + e.getMessage()));
return 0;
} catch (Throwable t) {
Log.severe(t);
getCommandSender(context).sendMessage(Chat.failureText("Error while executing the command: " + t));