|
| 1 | +package org.hydev.mcpm; |
| 2 | + |
| 3 | +import net.sourceforge.argparse4j.inf.ArgumentParserException; |
| 4 | +import org.hydev.mcpm.client.injector.*; |
| 5 | + |
| 6 | +import java.util.function.Consumer; |
| 7 | + |
| 8 | +import static java.lang.String.format; |
| 9 | +import static org.hydev.mcpm.utils.Sugar.sub; |
| 10 | +import static org.hydev.mcpm.utils.Sugar.subFrom; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Controller of the program |
| 15 | + * |
| 16 | + * @author Azalea (https://github.com/hykilpikonna) |
| 17 | + * @since 2022-10-30 |
| 18 | + */ |
| 19 | +public class Controller |
| 20 | +{ |
| 21 | + private final LoadBoundary loader; |
| 22 | + private final UnloadBoundary unloader; |
| 23 | + private final ReloadBoundary reloader; |
| 24 | + |
| 25 | + public Controller() |
| 26 | + { |
| 27 | + var pl = new PluginLoader(); |
| 28 | + loader = pl; |
| 29 | + unloader = pl; |
| 30 | + reloader = pl; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Run a command |
| 35 | + * |
| 36 | + * @param args Command-line arguments (not including the command name/path) |
| 37 | + * @param log Callback logger |
| 38 | + */ |
| 39 | + public void runCommand(String[] args, Consumer<String> log) |
| 40 | + { |
| 41 | + if (args.length == 0) return; |
| 42 | + |
| 43 | + var cmd = args[0].toLowerCase(); |
| 44 | + switch (cmd) |
| 45 | + { |
| 46 | + case "load", "unload", "reload" -> |
| 47 | + { |
| 48 | + try |
| 49 | + { |
| 50 | + var a = ArgsParser.LOADER.parseArgs(subFrom(args, 1)); |
| 51 | + var name = a.getString("name"); |
| 52 | + |
| 53 | + try |
| 54 | + { |
| 55 | + switch (cmd) |
| 56 | + { |
| 57 | + case "load" -> loader.loadPlugin(name); |
| 58 | + case "unload" -> unloader.unloadPlugin(name); |
| 59 | + case "reload" -> reloader.reloadPlugin(name); |
| 60 | + } |
| 61 | + log.accept(format("Plugin %s %sed", name, cmd)); |
| 62 | + } |
| 63 | + catch (PluginNotFoundException e) |
| 64 | + { |
| 65 | + log.accept(format("Plugin %s not found", name)); |
| 66 | + } |
| 67 | + } |
| 68 | + catch (ArgumentParserException e) |
| 69 | + { |
| 70 | + log.accept("Error: " + e.getMessage()); |
| 71 | + log.accept(ArgsParser.LOADER.formatHelp()); |
| 72 | + } |
| 73 | + } |
| 74 | + default -> |
| 75 | + { |
| 76 | + log.accept("Usage: /mcpm <install/uninstall/update/search/load/unload/reload/help>"); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments