diff --git a/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java b/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java index 042b132..c87d763 100644 --- a/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java +++ b/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java @@ -12,6 +12,11 @@ public class CommandLoader { private final String commandsPkg; private final Map commands = new HashMap<>(); + /** + * The command loader will scan the given package for {@link ICommand} classes and add them to the command list + * + * @param commandsPkg The package to scan for commands + */ public CommandLoader(String commandsPkg) { this.commandsPkg = commandsPkg; loadCommands(); @@ -19,24 +24,54 @@ public class CommandLoader { private void loadCommands() { Reflections reflections = new Reflections(commandsPkg, new SubTypesScanner(false)); + //Get ICommands in package reflections.getSubTypesOf(ICommand.class).stream() + //Only use ones with @LoadCommand annotation .filter(t -> t.isAnnotationPresent(LoadCommand.class)) + //Add to HashMap .forEach(t -> { try { - commands.put(t.getAnnotation(LoadCommand.class).value(), t.newInstance()); + addCommand(t.getAnnotation(LoadCommand.class).value(), t.newInstance()); }catch(InstantiationException | IllegalAccessException e) { e.printStackTrace(); } }); } + /** + * Runs the given command + * + * @param name the name of the command to be run + * @param args the arguments passed into the command + * @return the return of the command + * @throws NoSuchElementException if there's no command with the given name + */ public CommandReturn runCommand(String name, String[] args) throws NoSuchElementException { return commands.computeIfAbsent(name.toLowerCase(), x -> { throw new NoSuchElementException("Command " + x + " Not Found"); }).execute(args); } + /** + * Gets all commands + * + * @return the commands + */ public Map getCommands() { return Collections.unmodifiableMap(commands); } + + /** + * Adds a command to the command list + * + * @param name the name of the command to add + * @param command the command to add + * @return if it was successful + */ + public boolean addCommand(String name, ICommand command) { + if(commands.containsKey(name)) + return false; + commands.put(name, command); + return true; + } } diff --git a/src/main/java/ley/anvil/modpacktools/command/CommandReturn.java b/src/main/java/ley/anvil/modpacktools/command/CommandReturn.java index f3c1f7d..3099b5c 100644 --- a/src/main/java/ley/anvil/modpacktools/command/CommandReturn.java +++ b/src/main/java/ley/anvil/modpacktools/command/CommandReturn.java @@ -9,22 +9,47 @@ public class CommandReturn { this.success = success; } + /** + * Get a failed {@link CommandReturn}. This should be used if something went wrong + * @param ret the error message + * @return the {@link CommandReturn} + */ public static CommandReturn fail(String ret) { return new CommandReturn(ret, false); } + /** + * Get a successful {@link CommandReturn} Without a message. Use this if the command was executed successfully + * + * @return the {@link CommandReturn} + */ public static CommandReturn success() { return success(""); } + /** + * Get a successful {@link CommandReturn} With a message. Use this if the command was executed successfully + * + * @return the {@link CommandReturn} + */ public static CommandReturn success(String ret) { return new CommandReturn(ret, true); } + /** + * Returns true if the command was executed successfully + * + * @return if the command ran successfully + */ public boolean hadSuccess() { return success; } + /** + * Get the Return Message of the command (may be empty String) + * + * @return the return message + */ public String getRet() { return ret; } diff --git a/src/main/java/ley/anvil/modpacktools/command/ICommand.java b/src/main/java/ley/anvil/modpacktools/command/ICommand.java index 417e973..60a1bdf 100644 --- a/src/main/java/ley/anvil/modpacktools/command/ICommand.java +++ b/src/main/java/ley/anvil/modpacktools/command/ICommand.java @@ -1,7 +1,10 @@ package ley.anvil.modpacktools.command; +/** + * This must be implemented by all commands + */ +@FunctionalInterface public interface ICommand { - /** * Executes this Command * @param args Arguments for the Command @@ -9,6 +12,11 @@ public interface ICommand { */ CommandReturn execute(String[] args); + /** + * This should return the name of the command (this usually doesn't need to be overwritten) + * + * @return command name + */ default String getName() { return this.getClass().getAnnotation(LoadCommand.class).value(); } diff --git a/src/main/java/ley/anvil/modpacktools/command/LoadCommand.java b/src/main/java/ley/anvil/modpacktools/command/LoadCommand.java index 2c27dae..ad18fc0 100644 --- a/src/main/java/ley/anvil/modpacktools/command/LoadCommand.java +++ b/src/main/java/ley/anvil/modpacktools/command/LoadCommand.java @@ -3,7 +3,7 @@ package ley.anvil.modpacktools.command; import java.lang.annotation.*; /** - * Tells The {@code CommandLoader} to load this command + * Tells The {@link CommandLoader} to load this command */ @Documented @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/resources/modpacktoolsconfig.toml b/src/main/resources/modpacktoolsconfig.toml index 1048787..cee4a1b 100644 --- a/src/main/resources/modpacktoolsconfig.toml +++ b/src/main/resources/modpacktoolsconfig.toml @@ -1,4 +1,5 @@ [Locations] + #The location of the twitch manifest file (will be replaced with addonscript in later versions) manifestFile = "src/twitch/manifest.json" [Local] selectedVersion = -1 \ No newline at end of file