rewrite MinosoftCommandLineArguments, improvement

This commit is contained in:
Bixilon 2020-11-20 17:25:31 +01:00
parent 2802f5506f
commit 050ff874a2
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 41 additions and 55 deletions

View File

@ -14,17 +14,14 @@
package de.bixilon.minosoft.data.entities; package de.bixilon.minosoft.data.entities;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
public class EntityInformation { public class EntityInformation extends ModIdentifier {
private final String mod;
private final String identifier;
private final float width; private final float width;
private final float height; private final float height;
public EntityInformation(String mod, String identifier, float width, float height) { public EntityInformation(String mod, String identifier, float width, float height) {
this.mod = mod; super(mod, identifier);
this.identifier = identifier;
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
@ -33,14 +30,6 @@ public class EntityInformation {
return new EntityInformation(mod, identifier, data.get("width").getAsFloat(), data.get("height").getAsFloat()); return new EntityInformation(mod, identifier, data.get("width").getAsFloat(), data.get("height").getAsFloat());
} }
public String getMod() {
return mod;
}
public String getIdentifier() {
return identifier;
}
public float getWidth() { public float getWidth() {
return width; return width;
} }

View File

@ -16,57 +16,54 @@ package de.bixilon.minosoft.util;
import de.bixilon.minosoft.config.StaticConfiguration; import de.bixilon.minosoft.config.StaticConfiguration;
import org.apache.commons.cli.*; import org.apache.commons.cli.*;
import javax.annotation.Nullable;
import java.util.HashMap;
public class MinosoftCommandLineArguments { public class MinosoftCommandLineArguments {
private static final HashMap<Option, CommandLineArgumentHandler> optionHashMap = new HashMap<>();
private static final Options options = new Options();
private static final HelpFormatter formatter = new HelpFormatter();
static {
registerDefaultArguments();
}
public static void parseCommandLineArguments(String[] args) { public static void parseCommandLineArguments(String[] args) {
Options options = new Options(); MinosoftCommandLineArguments.optionHashMap.forEach((option, commandLineArgumentHandler) -> options.addOption(option));
Option help = new Option("?", "help", false, "Displays this help");
options.addOption(help);
Option homeFolder = new Option("home_folder", true, "Home of Minosoft");
options.addOption(homeFolder);
Option coloredLog = new Option("colored_log", true, "Should the log be colored");
options.addOption(coloredLog);
Option verboseEntityLogLevel = new Option("verbose_entity_logging", true, "Should entity meta data be printed");
options.addOption(verboseEntityLogLevel);
Option relativeTimeLogging = new Option("log_time_relativ", true, "Should time in log timestamp be relative");
options.addOption(relativeTimeLogging);
HelpFormatter formatter = new HelpFormatter();
CommandLine commandLine;
try { try {
commandLine = new DefaultParser().parse(options, args); CommandLine commandLine = new DefaultParser().parse(options, args);
for (Option option : commandLine.getOptions()) {
if (!MinosoftCommandLineArguments.optionHashMap.containsKey(option)) {
continue;
}
MinosoftCommandLineArguments.optionHashMap.get(option).handle(option.getValue());
}
} catch (ParseException e) { } catch (ParseException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
formatter.printHelp("java -jar Minosoft.jar", options); formatter.printHelp("java -jar Minosoft.jar", options);
System.exit(1); System.exit(1);
return;
} }
if (commandLine.hasOption(help.getOpt())) { }
public static void registerCommandLineOption(Option option, CommandLineArgumentHandler handler) {
optionHashMap.put(option, handler);
}
private static void registerDefaultArguments() {
registerCommandLineOption(new Option("?", "help", false, "Displays this help"), (value -> {
formatter.printHelp("java -jar Minosoft.jar", options); formatter.printHelp("java -jar Minosoft.jar", options);
System.exit(1); System.exit(1);
return; }));
} registerCommandLineOption(new Option("home_folder", true, "Home of Minosoft"), (value -> StaticConfiguration.HOME_DIRECTORY = value + "/"));
if (commandLine.hasOption(homeFolder.getOpt())) { registerCommandLineOption(new Option("colored_log", true, "Should the log be colored"), (value -> StaticConfiguration.COLORED_LOG = Boolean.parseBoolean(value)));
StaticConfiguration.HOME_DIRECTORY = commandLine.getOptionValue(homeFolder.getOpt()); registerCommandLineOption(new Option("verbose_entity_logging", true, "Should entity meta data be printed"), (value -> StaticConfiguration.VERBOSE_ENTITY_META_DATA_LOGGING = Boolean.parseBoolean(value)));
} registerCommandLineOption(new Option("log_time_relativ", true, "Should time in log timestamp be relative"), (value -> StaticConfiguration.LOG_RELATIVE_TIME = Boolean.parseBoolean(value)));
if (commandLine.hasOption(coloredLog.getOpt())) {
StaticConfiguration.COLORED_LOG = Boolean.parseBoolean(commandLine.getOptionValue(coloredLog.getOpt()));
}
if (commandLine.hasOption(verboseEntityLogLevel.getOpt())) {
StaticConfiguration.VERBOSE_ENTITY_META_DATA_LOGGING = Boolean.parseBoolean(commandLine.getOptionValue(verboseEntityLogLevel.getOpt()));
}
if (commandLine.hasOption(relativeTimeLogging.getOpt())) {
StaticConfiguration.LOG_RELATIVE_TIME = Boolean.parseBoolean(commandLine.getOptionValue(relativeTimeLogging.getOpt()));
} }
public interface CommandLineArgumentHandler {
void handle(@Nullable String value);
} }
} }

View File

@ -227,15 +227,15 @@ public final class Util {
public static void downloadFile(String url, String destination) throws IOException { public static void downloadFile(String url, String destination) throws IOException {
createParentFolderIfNotExist(destination); createParentFolderIfNotExist(destination);
copyFile(getInputStreamByURL(url), new FileOutputStream(destination)); copyStream(getInputStreamByURL(url), new FileOutputStream(destination));
} }
public static void downloadFileAsGz(String url, String destination) throws IOException { public static void downloadFileAsGz(String url, String destination) throws IOException {
createParentFolderIfNotExist(destination); createParentFolderIfNotExist(destination);
copyFile(getInputStreamByURL(url), new GZIPOutputStream(new FileOutputStream(destination))); copyStream(getInputStreamByURL(url), new GZIPOutputStream(new FileOutputStream(destination)));
} }
public static void copyFile(InputStream inputStream, OutputStream output) throws IOException { public static void copyStream(InputStream inputStream, OutputStream output) throws IOException {
byte[] buffer = new byte[ProtocolDefinition.DEFAULT_BUFFER_SIZE]; byte[] buffer = new byte[ProtocolDefinition.DEFAULT_BUFFER_SIZE];
int length; int length;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) { while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {