Made things configurable

This commit is contained in:
Norbi Peti 2018-06-12 23:50:36 +02:00
parent 7b500f35b8
commit c83339ce42
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 112 additions and 46 deletions

16
.idea/compiler.xml Normal file
View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="ButtonServerRunner" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="ButtonServerRunner" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

View file

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: jline:jline:2.12">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/jline/jline/2.12/jline-2.12.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/jline/jline/2.12/jline-2.12-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/jline/jline/2.12/jline-2.12-sources.jar!/" />
</SOURCES>
</library>
</component>

11
.idea/misc.xml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

19
pom.xml
View file

@ -18,6 +18,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration> <configuration>
<archive> <archive>
<manifest> <manifest>
@ -28,6 +29,18 @@
</archive> </archive>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
@ -36,5 +49,11 @@
<artifactId>jline</artifactId> <artifactId>jline</artifactId>
<version>2.12</version> <version>2.12</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.21</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -0,0 +1,18 @@
package buttondevteam.serverrunner;
public class Config {
public String serverVersion;
public String serverParams;
public int restartAt;
public Config(String serverVersion, String serverParams, int restartAt) {
this.serverVersion = serverVersion;
this.serverParams = serverParams;
this.restartAt = restartAt;
}
public Config() {
this.serverVersion = "1.12.2";
this.serverParams = "-Djline.terminal=jline.UnixTerminal -Xms4G -Xmx6G";
this.restartAt = 12;
}
}

View file

@ -1,29 +1,24 @@
package buttondevteam.serverrunner; package buttondevteam.serverrunner;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.regex.Pattern;
import jline.console.ConsoleReader; import jline.console.ConsoleReader;
import jline.console.CursorBuffer; import jline.console.CursorBuffer;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.Collections;
import java.util.TimeZone;
import java.util.regex.Pattern;
public class ServerRunner { public class ServerRunner {
private static final int RESTART_HOUR = 12;
private static final int RESTART_MESSAGE_COUNT = 60; private static final int RESTART_MESSAGE_COUNT = 60;
private static final int interval = 24; // hours private static final int interval = 24; // hours
private static volatile String server_version; // SET VERSION IN RUN.SH
private static volatile boolean stop = false; private static volatile boolean stop = false;
private static volatile int restartcounter = RESTART_MESSAGE_COUNT; private static int restartcounter = RESTART_MESSAGE_COUNT;
private static volatile Process serverprocess; private static volatile Process serverprocess;
private static volatile PrintWriter serveroutput; private static volatile PrintWriter serveroutput;
private static volatile Thread rt; private static volatile Thread rt;
@ -32,37 +27,24 @@ public class ServerRunner {
private static volatile boolean customrestartfailed = false; private static volatile boolean customrestartfailed = false;
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException {
String minmem; Yaml yaml = new Yaml();
String maxmem; File f=new File("plugins/ServerRunner/config.yml");
if (args.length == 3) { f.getParentFile().mkdirs();
if ((!args[0].contains("G") && !args[0].contains("M")) final Config config;
|| (!args[1].contains("G") && !args[0].contains("M"))) { if(!f.exists())
System.out.println("Error: Invalid arguments."); Files.write(f.toPath(), Collections.singleton(yaml.dump(config = new Config())));
System.out.println("Usage: java -jar ServerRunner.jar <minmem> <maxmem> <version>"); else
System.out.println("Example: java -jar ServerRunner.jar 1G 2G"); config=yaml.load(new FileInputStream(f));
return; if (!new File("spigot-" + config.serverVersion + ".jar").exists()) {
} System.out.println("The server JAR for " + config.serverVersion + " cannot be found!");
minmem = args[0];
maxmem = args[1];
server_version = args[2];
} else {
System.out.println("Error: Wrong number of arguments.");
System.out.println("Usage: java -jar ServerRunner.jar <minmem> <maxmem> <version>");
System.out.println("Example: java -jar ServerRunner.jar 1G 2G 1.9.2");
return; return;
} }
if (!new File("spigot-" + server_version + ".jar").exists()) {
System.out.println("The server JAR for " + server_version + " cannot be found!");
return;
}
final String fminmem = minmem;
final String fmaxmem = maxmem;
reader = new ConsoleReader(); reader = new ConsoleReader();
reader.setPrompt("Runner>"); reader.setPrompt("Runner>");
runnerout = new PrintWriter(reader.getOutput()); runnerout = new PrintWriter(reader.getOutput());
writeToScreen("Starting server..."); writeToScreen("Starting server...");
serverprocess = startServer(minmem, maxmem); serverprocess = startServer(config);
serveroutput = new PrintWriter(serverprocess.getOutputStream()); serveroutput = new PrintWriter(serverprocess.getOutputStream());
rt = Thread.currentThread(); rt = Thread.currentThread();
final Thread it = new Thread() { final Thread it = new Thread() {
@ -100,20 +82,22 @@ public class ServerRunner {
writeToScreen("A server is already running!"); writeToScreen("A server is already running!");
} }
if (Pattern.matches( if (Pattern.matches(
"[\\d\\d:\\d\\d:\\d\\d INFO]: Unknown command. Type \"/help\" for help.\\s+", line)) "\\[\\d\\d:\\d\\d:\\d\\d INFO]: Unknown command. Type \"/help\" for help.\\s+", line))
customrestartfailed = true; customrestartfailed = true;
} else if (!stop) { } else if (!stop) {
try { try {
serverinput.close(); serverinput.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} }
try { try {
serveroutput.close(); serveroutput.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} }
writeToScreen("Server stopped! Restarting..."); writeToScreen("Server stopped! Restarting...");
serverprocess = startServer(fminmem, fmaxmem); serverprocess = startServer(config);
serverinput = new BufferedReader(new InputStreamReader(serverprocess.getInputStream())); serverinput = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
serveroutput = new PrintWriter(serverprocess.getOutputStream()); serveroutput = new PrintWriter(serverprocess.getOutputStream());
restartcounter = RESTART_MESSAGE_COUNT; restartcounter = RESTART_MESSAGE_COUNT;
@ -131,7 +115,7 @@ public class ServerRunner {
ot.setName("OutputThread"); ot.setName("OutputThread");
ot.start(); ot.start();
Thread.currentThread().setName("RestarterThread"); Thread.currentThread().setName("RestarterThread");
long starttime = syncStart(RESTART_HOUR); long starttime = syncStart(config.restartAt);
System.out.println("Restart scheduled in " + starttime / 3600000f); System.out.println("Restart scheduled in " + starttime / 3600000f);
boolean firstrun = true; boolean firstrun = true;
while (!stop) { while (!stop) {
@ -175,9 +159,8 @@ public class ServerRunner {
writeToScreen("Stopped " + Thread.currentThread().getName()); writeToScreen("Stopped " + Thread.currentThread().getName());
} }
private static Process startServer(String minmem, String maxmem) throws IOException { private static Process startServer(Config config) throws IOException {
return Runtime.getRuntime().exec(new String[] { "java", "-Djline.terminal=jline.UnixTerminal", "-Xms" + minmem, return Runtime.getRuntime().exec(("java "+config.serverParams+" -jar spigot-" + config.serverVersion + ".jar").split(" "));
"-Xmx" + maxmem, "-XX:MaxPermSize=128M", "-jar", "spigot-" + server_version + ".jar" });
} }
private static void sendMessage(PrintWriter output, String color, String text) { private static void sendMessage(PrintWriter output, String color, String text) {