Skip to content

Commit 91f3c39

Browse files
committed
Use config file instead of command line args
1 parent d5236f2 commit 91f3c39

File tree

3 files changed

+111
-34
lines changed

3 files changed

+111
-34
lines changed

src/main/java/net/lenni0451/multilaunch/JarLauncher.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,34 @@
55
import java.io.File;
66
import java.io.IOException;
77
import java.io.OutputStream;
8+
import java.util.ArrayList;
9+
import java.util.List;
810
import java.util.concurrent.TimeUnit;
911

1012
public class JarLauncher {
1113

12-
private final String jarPath;
1314
private Thread watchDogThread;
1415
private Process process;
1516
private OutputStream outputStream;
1617

17-
public JarLauncher(final String jarPath) {
18-
this.jarPath = jarPath;
19-
}
20-
2118
public void launch() throws IOException {
22-
File serverJarFile = new File(this.jarPath);
19+
File serverJarFile = new File(MultiLaunchConfig.serverJar);
20+
if (!serverJarFile.exists()) {
21+
Logger.LOGGER.info("---------- MultiLaunch ----------");
22+
Logger.LOGGER.error("Server jar file does not exist!");
23+
Logger.LOGGER.info("Please make sure you have set the correct path in the configuration file!");
24+
Logger.LOGGER.info("If this is your first time using the plugin, please edit the configuration file and restart the server!");
25+
System.exit(0);
26+
}
2327

24-
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home") + "/bin/java", "-jar", serverJarFile.getAbsolutePath());
28+
List<String> arguments = new ArrayList<>();
29+
arguments.add(System.getProperty("java.home") + "/bin/java");
30+
arguments.addAll(MultiLaunchConfig.jvmArguments);
31+
arguments.add("-jar");
32+
arguments.add(serverJarFile.getAbsolutePath());
33+
arguments.addAll(MultiLaunchConfig.serverArguments);
34+
35+
ProcessBuilder pb = new ProcessBuilder(arguments.toArray(new String[0]));
2536
pb.directory(serverJarFile.getParentFile());
2637
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
2738
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
@@ -35,21 +46,27 @@ public void launch() throws IOException {
3546
return;
3647
}
3748
Logger.LOGGER.info("Server process stopped!");
38-
System.exit(0);
49+
if (MultiLaunchConfig.stopViaProxyOnServerStop) {
50+
Logger.LOGGER.info("Stopping ViaProxy...");
51+
System.exit(0);
52+
}
3953
}, "ServerWatchDog");
4054
this.watchDogThread.setDaemon(true);
4155
this.watchDogThread.start();
4256
}
4357

4458
public void stop() throws IOException {
4559
this.watchDogThread.interrupt();
46-
Logger.LOGGER.info("Stopping server (force shutdown after 1 minute)...");
47-
this.outputStream.write("stop\n".getBytes());
60+
Logger.LOGGER.info("Stopping server (force shutdown after {} seconds)...", MultiLaunchConfig.shutdownTimeout);
61+
this.outputStream.write(MultiLaunchConfig.stopCommand.getBytes());
62+
this.outputStream.write('\n');
4863
this.outputStream.flush();
4964
try {
50-
this.process.waitFor(1, TimeUnit.MINUTES);
65+
if (!this.process.waitFor(MultiLaunchConfig.shutdownTimeout, TimeUnit.SECONDS)) {
66+
throw new InterruptedException("Server did not stop in time!");
67+
}
5168
} catch (InterruptedException e) {
52-
Logger.LOGGER.warn("Server stop was interrupted after 1 minute!");
69+
Logger.LOGGER.warn("Server stop was interrupted after {} seconds!", MultiLaunchConfig.shutdownTimeout);
5370
this.process.destroy();
5471
}
5572
}
Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
package net.lenni0451.multilaunch;
22

3-
import joptsimple.OptionSpec;
43
import net.lenni0451.lambdaevents.EventHandler;
54
import net.raphimc.viaproxy.ViaProxy;
65
import net.raphimc.viaproxy.plugins.ViaProxyPlugin;
76
import net.raphimc.viaproxy.plugins.events.ConsoleCommandEvent;
8-
import net.raphimc.viaproxy.plugins.events.PostOptionsParseEvent;
9-
import net.raphimc.viaproxy.plugins.events.PreOptionsParseEvent;
7+
import net.raphimc.viaproxy.util.logging.Logger;
108

119
import java.io.IOException;
1210
import java.io.OutputStream;
1311
import java.nio.charset.StandardCharsets;
1412

1513
public class Main extends ViaProxyPlugin {
1614

17-
private OptionSpec<String> serverjarArg;
1815
private JarLauncher launcher;
1916

2017
@Override
2118
public void onEnable() {
19+
MultiLaunchConfig.load();
2220
ViaProxy.EVENT_MANAGER.register(this);
21+
this.launch();
2322
}
2423

25-
@EventHandler
26-
public void onPreOptionsParse(PreOptionsParseEvent event) {
27-
this.serverjarArg = event.getParser().accepts("serverjar", "The path to the server jar to launch").withRequiredArg().ofType(String.class).required();
28-
}
29-
30-
@EventHandler
31-
public void onPostOptionsParse(PostOptionsParseEvent event) {
32-
String serverJar = event.getOptions().valueOf(this.serverjarArg);
24+
private void launch() {
3325
try {
34-
this.launcher = new JarLauncher(serverJar);
26+
this.launcher = new JarLauncher();
3527
this.launcher.launch();
3628
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
3729
try {
@@ -41,22 +33,28 @@ public void onPostOptionsParse(PostOptionsParseEvent event) {
4133
}
4234
}));
4335
} catch (Throwable t) {
44-
t.printStackTrace();
36+
Logger.LOGGER.error("Failed to launch the server jar!", t);
4537
System.exit(-1);
4638
}
4739
}
4840

4941
@EventHandler
5042
public void onConsoleCommand(ConsoleCommandEvent event) throws IOException {
51-
event.setCancelled(true);
52-
OutputStream os = this.launcher.getOutputStream();
53-
os.write(event.getCommand().getBytes(StandardCharsets.UTF_8));
54-
if (event.getArgs().length != 0) {
55-
os.write(' ');
56-
os.write(String.join(" ", event.getArgs()).getBytes(StandardCharsets.UTF_8));
43+
if (!MultiLaunchConfig.forwardConsole) return;
44+
45+
try {
46+
OutputStream os = this.launcher.getOutputStream();
47+
os.write(event.getCommand().getBytes(StandardCharsets.UTF_8));
48+
if (event.getArgs().length != 0) {
49+
os.write(' ');
50+
os.write(String.join(" ", event.getArgs()).getBytes(StandardCharsets.UTF_8));
51+
}
52+
os.write('\n');
53+
os.flush();
54+
event.setCancelled(true);
55+
} catch (Throwable ignored) {
56+
//If the server is not running, the input will be handled by ViaProxy
5757
}
58-
os.write('\n');
59-
os.flush();
6058
}
6159

6260
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.lenni0451.multilaunch;
2+
3+
import net.lenni0451.optconfig.ConfigLoader;
4+
import net.lenni0451.optconfig.annotations.Description;
5+
import net.lenni0451.optconfig.annotations.OptConfig;
6+
import net.lenni0451.optconfig.annotations.Option;
7+
import net.lenni0451.optconfig.provider.ConfigProvider;
8+
import net.raphimc.viaproxy.util.logging.Logger;
9+
10+
import java.io.File;
11+
import java.util.List;
12+
13+
@OptConfig(header = {
14+
"Configuration for the MultiLaunch ViaProxy plugin.",
15+
"Used to launch a server jar alongside ViaProxy.",
16+
"Make sure the server jar is in a separate directory to avoid conflicts!",
17+
"",
18+
"Made by Lenni0451",
19+
"Source: https://github.com/ViaVersionAddons/ViaProxyMultiLaunch"
20+
})
21+
public class MultiLaunchConfig {
22+
23+
@Option("ServerJar")
24+
@Description("The path to the server jar to launch")
25+
public static String serverJar = "other/server.jar";
26+
27+
@Option("JvmArguments")
28+
@Description("The JVM arguments to use when launching the server")
29+
public static List<String> jvmArguments = List.of("-DIReallyKnowWhatIAmDoingISwear");
30+
31+
@Option("ServerArguments")
32+
@Description("The arguments to use when launching the server")
33+
public static List<String> serverArguments = List.of("nogui");
34+
35+
@Option("ForwardConsole")
36+
@Description({"If the console input should be forwarded to the server", "This also means that ViaProxy is not able to read the console input!", "If the server process is not running, the input will be handled by ViaProxy"})
37+
public static boolean forwardConsole = true;
38+
39+
@Option("ShutdownTimeout")
40+
@Description({"The time in seconds to wait for the server to shutdown before forcing it", "A force shutdown may cause data loss!"})
41+
public static int shutdownTimeout = 60;
42+
43+
@Option("StopCommand")
44+
@Description("The command to send to the server when stopping it")
45+
public static String stopCommand = "stop";
46+
47+
@Option("StopViaProxyOnServerStop")
48+
@Description("If ViaProxy should be stopped when the server stops")
49+
public static boolean stopViaProxyOnServerStop = true;
50+
51+
public static void load() {
52+
try {
53+
ConfigLoader<MultiLaunchConfig> configLoader = new ConfigLoader<>(MultiLaunchConfig.class);
54+
configLoader.getConfigOptions().setResetInvalidOptions(true); //Reset invalid options to their default value
55+
configLoader.loadStatic(ConfigProvider.file(new File("multilaunch.yml")));
56+
} catch (Throwable t) {
57+
Logger.LOGGER.error("Failed to load the MultiLaunch configuration!", t);
58+
System.exit(-1);
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)