mirror of
https://github.com/Anvilcraft/modpacktools
synced 2024-11-17 23:41:55 +01:00
Add Docs to new command loading system
This commit is contained in:
parent
b78a95fa8e
commit
8461f0f931
5 changed files with 72 additions and 3 deletions
|
@ -12,6 +12,11 @@ public class CommandLoader {
|
|||
private final String commandsPkg;
|
||||
private final Map<String, ICommand> 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<String, ICommand> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue