4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-09-30 04:59:05 +02:00

small improvements and config changes

help message sorts commands now
init command added
config is no longer created on program start
CommandLoader now checks if a config file exists if commands require it
an ICommand can now be executed without a CommandLoader instance
minor cleanups
This commit is contained in:
LordMZTE 2020-06-18 18:53:39 +02:00
parent 74887d8fa6
commit 4099a02bfb
5 changed files with 96 additions and 31 deletions

View file

@ -4,6 +4,7 @@ import ley.anvil.modpacktools.command.CommandLoader;
import ley.anvil.modpacktools.command.CommandReturn; import ley.anvil.modpacktools.command.CommandReturn;
import ley.anvil.modpacktools.util.Config; import ley.anvil.modpacktools.util.Config;
import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
public class Main { public class Main {
@ -11,10 +12,10 @@ public class Main {
public static final CommandLoader LOADER = new CommandLoader("ley.anvil.modpacktools.commands"); public static final CommandLoader LOADER = new CommandLoader("ley.anvil.modpacktools.commands");
public static void main(String[] args) { public static void main(String[] args) {
if(args.length <= 0) { if(args.length <= 0) {
printHelp(); printHelp();
return; return;
} }
try { try {
CommandReturn ret = LOADER.runCommand(args[0], args); CommandReturn ret = LOADER.runCommand(args[0], args);
@ -27,7 +28,11 @@ public class Main {
} }
private static void printHelp() { private static void printHelp() {
System.out.println("Commands:"); System.out.println("Commands:");
LOADER.getCommands().forEach((k, v) -> System.out.println(k + ": " + v.getHelpMessage())); LOADER.getCommands()
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.forEach(e -> System.out.println(e.getKey() + ": " + e.getValue().getHelpMessage()));
} }
} }

View file

@ -1,5 +1,6 @@
package ley.anvil.modpacktools.command; package ley.anvil.modpacktools.command;
import ley.anvil.modpacktools.Main;
import org.reflections.Reflections; import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner; import org.reflections.scanners.SubTypesScanner;
@ -41,9 +42,16 @@ public class CommandLoader {
* @throws NoSuchElementException if there's no command with the given name * @throws NoSuchElementException if there's no command with the given name
*/ */
public CommandReturn runCommand(String name, String[] args) throws NoSuchElementException { 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"); 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);
} }
/** /**

View file

@ -15,13 +15,13 @@ import java.util.ArrayList;
public class ModInfo { public class ModInfo {
private String name; private final String name;
private JsonArray authors; private final JsonArray authors;
@SerializedName("websiteUrl") @SerializedName("websiteUrl")
private String link; private final String link;
@SerializedName("downloadCount") @SerializedName("downloadCount")
private int downloads; private final int downloads;
private int id; private final int id;
private ModInfo(String name, JsonArray authors, String link, int downloads, int id) { private ModInfo(String name, JsonArray authors, String link, int downloads, int id) {
this.name = name; this.name = name;

View file

@ -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)";
}
}

View file

@ -8,7 +8,7 @@ import java.net.URISyntaxException;
public class Config { public class Config {
public final File JAR_LOCATION; public final File JAR_LOCATION;
public final File CONFIG_LOCATION; public final File CONFIG_LOCATION;
public final Toml CONFIG; public Toml CONFIG;
private final String CONFIG_NAME = "modpacktoolsconfig.toml"; private final String CONFIG_NAME = "modpacktoolsconfig.toml";
public Config() { 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 * @return the Toml object of the config file
*/ */
private Toml readConfig() { private Toml readConfig() {
if(CONFIG_LOCATION.exists()) { if(configExists()) {
//parse file to json //parse file to toml
return new Toml().read(CONFIG_LOCATION); 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; 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();
}
}
} }