diff --git a/src/main/java/ley/anvil/modpacktools/Main.java b/src/main/java/ley/anvil/modpacktools/Main.java index 33fbeeb..5c9bd3e 100644 --- a/src/main/java/ley/anvil/modpacktools/Main.java +++ b/src/main/java/ley/anvil/modpacktools/Main.java @@ -4,6 +4,7 @@ import ley.anvil.modpacktools.command.CommandLoader; import ley.anvil.modpacktools.command.CommandReturn; import ley.anvil.modpacktools.util.Config; +import java.util.Map; import java.util.NoSuchElementException; public class Main { @@ -11,10 +12,10 @@ public class Main { public static final CommandLoader LOADER = new CommandLoader("ley.anvil.modpacktools.commands"); public static void main(String[] args) { - if(args.length <= 0) { - printHelp(); - return; - } + if(args.length <= 0) { + printHelp(); + return; + } try { CommandReturn ret = LOADER.runCommand(args[0], args); @@ -27,7 +28,11 @@ public class Main { } private static void printHelp() { - System.out.println("Commands:"); - LOADER.getCommands().forEach((k, v) -> System.out.println(k + ": " + v.getHelpMessage())); + System.out.println("Commands:"); + LOADER.getCommands() + .entrySet() + .stream() + .sorted(Map.Entry.comparingByKey()) + .forEach(e -> System.out.println(e.getKey() + ": " + e.getValue().getHelpMessage())); } } diff --git a/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java b/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java index 07aecb0..ad48d8d 100644 --- a/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java +++ b/src/main/java/ley/anvil/modpacktools/command/CommandLoader.java @@ -1,5 +1,6 @@ package ley.anvil.modpacktools.command; +import ley.anvil.modpacktools.Main; import org.reflections.Reflections; import org.reflections.scanners.SubTypesScanner; @@ -41,9 +42,16 @@ public class CommandLoader { * @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 -> { + ICommand cmd = commands.computeIfAbsent(name.toLowerCase(), x -> { throw new NoSuchElementException("Command " + x + " Not Found"); - }).execute(args); + }); + return runStatic(cmd, args); + } + + public static CommandReturn runStatic(ICommand cmd, String[] args) { + if(cmd.needsConfig() && !Main.CONFIG.configExists()) + return CommandReturn.fail("Config is needed for this command yet it is not present. Run \'init\' to generate"); + return cmd.execute(args); } /** diff --git a/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java b/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java index 1249e1f..539d7da 100644 --- a/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java +++ b/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java @@ -15,13 +15,13 @@ import java.util.ArrayList; public class ModInfo { - private String name; - private JsonArray authors; + private final String name; + private final JsonArray authors; @SerializedName("websiteUrl") - private String link; + private final String link; @SerializedName("downloadCount") - private int downloads; - private int id; + private final int downloads; + private final int id; private ModInfo(String name, JsonArray authors, String link, int downloads, int id) { this.name = name; diff --git a/src/main/java/ley/anvil/modpacktools/commands/Init.java b/src/main/java/ley/anvil/modpacktools/commands/Init.java new file mode 100644 index 0000000..1c81c88 --- /dev/null +++ b/src/main/java/ley/anvil/modpacktools/commands/Init.java @@ -0,0 +1,37 @@ +package ley.anvil.modpacktools.commands; + +import ley.anvil.modpacktools.Main; +import ley.anvil.modpacktools.command.CommandReturn; +import ley.anvil.modpacktools.command.ICommand; +import ley.anvil.modpacktools.command.LoadCommand; + +import javax.annotation.Nonnull; + +@LoadCommand +public class Init implements ICommand { + @Override + public CommandReturn execute(String[] args) { + if(Main.CONFIG.configExists()) + return CommandReturn.fail("Config already exists"); + Main.CONFIG.copyConfig(); + System.out.println("Config Created"); + return CommandReturn.success(); + } + + @Nonnull + @Override + public String getName() { + return "init"; + } + + @Override + public boolean needsConfig() { + return false; + } + + @Nonnull + @Override + public String getHelpMessage() { + return "initializes the MPT dev environment (currently only creates config file)"; + } +} diff --git a/src/main/java/ley/anvil/modpacktools/util/Config.java b/src/main/java/ley/anvil/modpacktools/util/Config.java index 1da3b15..667f827 100644 --- a/src/main/java/ley/anvil/modpacktools/util/Config.java +++ b/src/main/java/ley/anvil/modpacktools/util/Config.java @@ -8,7 +8,7 @@ import java.net.URISyntaxException; public class Config { public final File JAR_LOCATION; public final File CONFIG_LOCATION; - public final Toml CONFIG; + public Toml CONFIG; private final String CONFIG_NAME = "modpacktoolsconfig.toml"; public Config() { @@ -35,28 +35,43 @@ public class Config { } /** - * reads the config it it exists and otherwise copies it + * reads the config it it exists + * * @return the Toml object of the config file */ private Toml readConfig() { - if(CONFIG_LOCATION.exists()) { - //parse file to json + if(configExists()) { + //parse file to toml return new Toml().read(CONFIG_LOCATION); - } else { - //copy from resources - try { - InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG_NAME); - byte[] buff = new byte[in.available()]; - in.read(buff); - OutputStream out = new FileOutputStream(CONFIG_LOCATION); - out.write(buff); - in.close(); - out.close(); - return readConfig(); - } catch(IOException e) { - e.printStackTrace(); - } } return null; } + + /** + * Checks if the config file exists + * + * @return true if the config file exists + */ + public boolean configExists() { + return CONFIG_LOCATION.exists(); + } + + /** + * Copes the Config file from the resources into the tool's folder + */ + public void copyConfig() { + //copy from resources + try { + InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG_NAME); + byte[] buff = new byte[in.available()]; + in.read(buff); + OutputStream out = new FileOutputStream(CONFIG_LOCATION); + out.write(buff); + in.close(); + out.close(); + CONFIG = readConfig(); + } catch(IOException e) { + e.printStackTrace(); + } + } }