Adding option to use different config file for standalone (#1102)

* Added config option to standalone Geyser

* Cleanup

* Added --gui, --nogui options

* Made new options read default config.yml from correct place internally

* Changed to locale strings rather than hardcoded English

* Using separate options texts

* Changed '-c' to be string parameter so it isn't translated
This commit is contained in:
abeshi-softwire 2020-08-25 22:39:51 +01:00 committed by GitHub
parent 81f58ee9bf
commit 5458a85ed7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -49,6 +49,7 @@ import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.UUID; import java.util.UUID;
public class GeyserStandaloneBootstrap implements GeyserBootstrap { public class GeyserStandaloneBootstrap implements GeyserBootstrap {
@ -62,22 +63,61 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {
@Getter @Getter
private boolean useGui = System.console() == null && !isHeadless(); private boolean useGui = System.console() == null && !isHeadless();
private String configFilename = "config.yml";
private GeyserConnector connector; private GeyserConnector connector;
public static void main(String[] args) { public static void main(String[] args) {
for (String arg : args) { GeyserStandaloneBootstrap bootstrap = new GeyserStandaloneBootstrap();
// Set defaults
boolean useGuiOpts = bootstrap.useGui;
String configFilenameOpt = bootstrap.configFilename;
for (int i = 0; i < args.length; i++) {
// By default, standalone Geyser will check if it should open the GUI based on if the GUI is null // By default, standalone Geyser will check if it should open the GUI based on if the GUI is null
// Optionally, you can force the use of a GUI or no GUI by specifying args // Optionally, you can force the use of a GUI or no GUI by specifying args
if (arg.equals("gui")) { // Allows gui and nogui without options, for backwards compatibility
new GeyserStandaloneBootstrap().onEnable(true); String arg = args[i];
return; switch (arg) {
} else if (arg.equals("nogui")) { case "--gui":
new GeyserStandaloneBootstrap().onEnable(false); case "gui":
return; useGuiOpts = true;
break;
case "--nogui":
case "nogui":
useGuiOpts = false;
break;
case "--config":
case "-c":
if (i >= args.length - 1) {
System.err.println(MessageFormat.format(LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.confignotspecified"), "-c"));
return;
}
configFilenameOpt = args[i+1]; i++;
System.out.println(MessageFormat.format(LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.configspecified"), configFilenameOpt));
break;
case "--help":
case "-h":
System.out.println(MessageFormat.format(LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.usage"), "[java -jar] Geyser.jar [opts]"));
System.out.println(" " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.options"));
System.out.println(" -c, --config [file] " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.config"));
System.out.println(" -h, --help " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.help"));
System.out.println(" --gui, --nogui " + LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.gui"));
return;
default:
String badArgMsg = LanguageUtils.getLocaleStringLog("geyser.bootstrap.args.unrecognised");
System.err.println(MessageFormat.format(badArgMsg, arg));
return;
} }
} }
new GeyserStandaloneBootstrap().onEnable(); bootstrap.onEnable(useGuiOpts, configFilenameOpt);
}
public void onEnable(boolean useGui, String configFilename) {
this.configFilename = configFilename;
this.useGui = useGui;
this.onEnable();
} }
public void onEnable(boolean useGui) { public void onEnable(boolean useGui) {
@ -106,7 +146,7 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {
LoopbackUtil.checkLoopback(geyserLogger); LoopbackUtil.checkLoopback(geyserLogger);
try { try {
File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString())); File configFile = FileUtils.fileOrCopiedFromResource(new File(configFilename), "config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
geyserConfig = FileUtils.loadConfig(configFile, GeyserStandaloneConfiguration.class); geyserConfig = FileUtils.loadConfig(configFile, GeyserStandaloneConfiguration.class);
if (this.geyserConfig.getRemote().getAddress().equalsIgnoreCase("auto")) { if (this.geyserConfig.getRemote().getAddress().equalsIgnoreCase("auto")) {
geyserConfig.setAutoconfiguredRemote(true); // Doesn't really need to be set but /shrug geyserConfig.setAutoconfiguredRemote(true); // Doesn't really need to be set but /shrug