2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launchserver.manangers;
|
2018-12-26 15:40:53 +03:00
|
|
|
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launchserver.Reconfigurable;
|
2019-07-06 15:54:10 +03:00
|
|
|
import pro.gravit.utils.command.Command;
|
|
|
|
import pro.gravit.utils.command.CommandException;
|
|
|
|
import pro.gravit.utils.command.basic.HelpCommand;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.utils.helper.VerifyHelper;
|
2018-12-26 15:40:53 +03:00
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2018-12-26 15:40:53 +03:00
|
|
|
public class ReconfigurableManager {
|
2019-07-06 15:54:10 +03:00
|
|
|
private final HashMap<String, Command> RECONFIGURABLE = new HashMap<>();
|
2019-01-15 06:35:39 +03:00
|
|
|
|
|
|
|
public void registerReconfigurable(String name, Reconfigurable reconfigurable) {
|
2019-07-06 15:54:10 +03:00
|
|
|
VerifyHelper.putIfAbsent(RECONFIGURABLE, name.toLowerCase(), new ReconfigurableVirtualCommand(reconfigurable.getCommands()),
|
2019-08-25 09:07:09 +03:00
|
|
|
String.format("Reconfigurable has been already registered: '%s'", name));
|
2018-12-26 15:40:53 +03:00
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
2019-07-06 15:54:10 +03:00
|
|
|
public void unregisterReconfigurable(String name) {
|
2019-08-25 09:07:09 +03:00
|
|
|
RECONFIGURABLE.remove(name.toLowerCase());
|
2019-04-27 13:17:10 +03:00
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
public void call(String name, String action, String[] args) throws Exception {
|
2019-07-06 15:54:10 +03:00
|
|
|
Command commands = RECONFIGURABLE.get(name);
|
2019-10-19 19:46:04 +03:00
|
|
|
if (commands == null) throw new CommandException(String.format("Reconfigurable %s not found", name));
|
2019-07-06 15:54:10 +03:00
|
|
|
Command command = commands.childCommands.get(action);
|
2019-10-19 19:46:04 +03:00
|
|
|
if (command == null) throw new CommandException(String.format("Action %s.%s not found", name, action));
|
2019-07-06 15:54:10 +03:00
|
|
|
command.invoke(args);
|
2018-12-26 15:40:53 +03:00
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
public void printHelp(String name) throws CommandException {
|
2019-07-06 15:54:10 +03:00
|
|
|
Command commands = RECONFIGURABLE.get(name);
|
2019-10-19 19:46:04 +03:00
|
|
|
if (commands == null) throw new CommandException(String.format("Reconfigurable %s not found", name));
|
2019-07-06 15:54:10 +03:00
|
|
|
HelpCommand.printSubCommandsHelp(name, commands);
|
2018-12-26 15:40:53 +03:00
|
|
|
}
|
2019-10-19 19:46:04 +03:00
|
|
|
|
|
|
|
public Map<String, Command> getCommands() {
|
2019-07-06 15:54:10 +03:00
|
|
|
return RECONFIGURABLE;
|
2018-12-26 15:40:53 +03:00
|
|
|
}
|
2020-04-05 10:27:04 +03:00
|
|
|
|
|
|
|
private static class ReconfigurableVirtualCommand extends Command {
|
|
|
|
public ReconfigurableVirtualCommand(Map<String, Command> childs) {
|
|
|
|
super(childs);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getArgsDescription() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUsageDescription() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void invoke(String... args) throws Exception {
|
|
|
|
invokeSubcommands(args);
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 15:40:53 +03:00
|
|
|
}
|