Registers a subcommand, the subcommand may occur only once with the same path!
+ *Example: 'registerSubCommand(new AboutSub(ExecutorLevel.CONSOLE_PLAYER, ""), "about");'
+ * + * @param command The subcommand to be executed + * @param args the complete path of the command including name + */ + public void registerSubCommand(@NotNull SmartSubCommand command, @NotNull String... args) { + args = ArrayUtil.toLowerCase(args); + if (isValid(args)) { + command.setName(ArrayUtil.buildString(args)); + addAnnotations(command); + subCommandMap.put(args, command); + } else { + throw new CommandNotValidException(command.getClass()); + } + } + + private void addAnnotations(@NotNull SmartSubCommand command) { + for (var ann : command.getClass().getAnnotations()) { + if (ann instanceof Description description) { + command.setDescription(description.value()); + } + } + } + + protected boolean isValid(@NotNull String[] args) { + return (args.length != 0) && (!subCommandMap.containsKey(args)); + } + + protected @NotNull OptionalThe new arguments are the arguments without the path and the name of the command.
+ *Example: subcommand path : "arg0 name"
+ *path: "arg0 name value" becomes Arguments: "value"
+ * @return The new Arguments of the command. + */ + public @NotNull String[] getArgs() { + return args; + } + + /*** + * @return The SubCommand + */ + public @NotNull SmartSubCommand getCommand() { + return command; + } + } +} diff --git a/src/main/java/de/goldmensch/commanddispatcher/exceptions/CommandNotValidException.java b/src/main/java/de/goldmensch/commanddispatcher/exceptions/CommandNotValidException.java new file mode 100644 index 00000000..c651ebe8 --- /dev/null +++ b/src/main/java/de/goldmensch/commanddispatcher/exceptions/CommandNotValidException.java @@ -0,0 +1,14 @@ +package de.goldmensch.commanddispatcher.exceptions; + +import de.goldmensch.commanddispatcher.subcommand.SmartSubCommand; + +import org.jetbrains.annotations.NotNull; + +public class CommandNotValidException extends RuntimeException { + + @java.io.Serial private static final long serialVersionUID = 17441953375440988L; + + public CommandNotValidException(@NotNull Class extends SmartSubCommand> sub) { + super("Command not valid, class: " + sub.getName()); + } +} diff --git a/src/main/java/de/goldmensch/commanddispatcher/subcommand/SmartSubCommand.java b/src/main/java/de/goldmensch/commanddispatcher/subcommand/SmartSubCommand.java new file mode 100644 index 00000000..601990d7 --- /dev/null +++ b/src/main/java/de/goldmensch/commanddispatcher/subcommand/SmartSubCommand.java @@ -0,0 +1,60 @@ +package de.goldmensch.commanddispatcher.subcommand; + +import de.goldmensch.commanddispatcher.Executor; + +import org.bukkit.command.CommandExecutor; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; + +public abstract class SmartSubCommand implements CommandExecutor { + + private final Executor executor; + private final String permission; + private String name; + private String description; + + public SmartSubCommand(@NotNull Executor executor, @Nullable String permission) { + this.executor = executor; + this.permission = permission; + } + + @ApiStatus.Internal + public void setName(@NotNull String name) { + this.name = name; + } + + @ApiStatus.Internal + public void setDescription(@NotNull String description) { + this.description = description; + } + + /*** + * @return The {@link Executor} of the SubCommand + */ + public @NotNull Executor getExecutor() { + return executor; + } + + /*** + * @return The permission of the SubCommand + */ + public @NotNull OptionalThe name is a string consisting of the path and the name.
+ *Example: The StringArray {"arg0", "arg1", "name"} becomes "arg0 arg1 name"
+ * @return The name of the SubCommand + */ + public @NotNull String getName() { + return name; + } + + public @NotNull Optional