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:
parent
74887d8fa6
commit
4099a02bfb
5 changed files with 96 additions and 31 deletions
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
|
37
src/main/java/ley/anvil/modpacktools/commands/Init.java
Normal file
37
src/main/java/ley/anvil/modpacktools/commands/Init.java
Normal 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)";
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue