mirror of
https://github.com/Anvilcraft/modpacktools
synced 2024-11-17 23:41:55 +01: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.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 {
|
||||||
|
@ -28,6 +29,10 @@ 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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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;
|
||||||
|
|
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 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,14 +35,31 @@ 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 {
|
}
|
||||||
|
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
|
//copy from resources
|
||||||
try {
|
try {
|
||||||
InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG_NAME);
|
InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG_NAME);
|
||||||
|
@ -52,11 +69,9 @@ public class Config {
|
||||||
out.write(buff);
|
out.write(buff);
|
||||||
in.close();
|
in.close();
|
||||||
out.close();
|
out.close();
|
||||||
return readConfig();
|
CONFIG = readConfig();
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue