Compare commits

...

6 commits
master ... dev

12 changed files with 390 additions and 278 deletions

2
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: jline:jline:2.12" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" />
</component>
</module>

View file

@ -6,11 +6,12 @@
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="ButtonServerRunner" />
<module name="MCServerRunner" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="ButtonServerRunner" target="1.8" />
<module name="MCServerRunner" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

4
.idea/encodings.xml Normal file
View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>

View file

@ -0,0 +1,11 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="WeakerAccess" enabled="true" level="WARNING" enabled_by_default="true">
<option name="SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS" value="false" />
<option name="SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES" value="false" />
<option name="SUGGEST_PRIVATE_FOR_INNERS" value="false" />
<disabledExtension id="moduleInfo" />
</inspection_tool>
</profile>
</component>

View file

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

View file

@ -0,0 +1,3 @@
<component name="MarkdownNavigator.ProfileManager">
<settings default="" pdf-export="" />
</component>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/MCServerRunner.iml" filepath="$PROJECT_DIR$/MCServerRunner.iml" />
</modules>
</component>
</project>

15
MCServerRunner.iml Normal file
View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: jline:jline:2.12" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" />
</component>
</module>

View file

@ -6,6 +6,7 @@
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<finalName>ServerRunner</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>

View file

@ -4,6 +4,7 @@ public class Config {
public String serverVersion;
public String serverParams;
public int restartAt;
public String customJar;
public Config(String serverVersion, String serverParams, int restartAt) {
this.serverVersion = serverVersion;
@ -14,5 +15,6 @@ public class Config {
this.serverVersion = "1.12.2";
this.serverParams = "-Djline.terminal=jline.UnixTerminal -Xms4G -Xmx6G";
this.restartAt = 12;
this.customJar = "";
}
}

View file

@ -1,5 +1,6 @@
package buttondevteam.serverrunner;
import jline.Terminal;
import jline.console.ConsoleReader;
import jline.console.CursorBuffer;
import org.yaml.snakeyaml.Yaml;
@ -24,19 +25,23 @@ public class ServerRunner {
private static volatile Thread rt;
private static volatile ConsoleReader reader;
private static volatile PrintWriter runnerout;
private static volatile Thread it;
private static volatile boolean customrestartfailed = false;
public static void main(String[] args) throws IOException {
Yaml yaml = new Yaml();
File f=new File("plugins/ServerRunner/config.yml");
File f = new File("plugins/ServerRunner/config.yml");
f.getParentFile().mkdirs();
final Config config;
if(!f.exists())
if (!f.exists())
Files.write(f.toPath(), Collections.singleton(yaml.dump(config = new Config())));
else
config=yaml.load(new FileInputStream(f));
if (!new File("spigot-" + config.serverVersion + ".jar").exists()) {
config = yaml.load(new FileInputStream(f));
final File serverJar = new File(config.customJar == null || config.customJar.length() == 0
? "spigot-" + config.serverVersion + ".jar"
: config.customJar);
if (!serverJar.exists()) {
System.out.println("The server JAR for " + config.serverVersion + " cannot be found!");
return;
}
@ -44,21 +49,28 @@ public class ServerRunner {
reader.setPrompt("Runner>");
runnerout = new PrintWriter(reader.getOutput());
writeToScreen("Starting server...");
serverprocess = startServer(config);
serverprocess = startServer(config, serverJar);
serveroutput = new PrintWriter(serverprocess.getOutputStream());
rt = Thread.currentThread();
final Thread it = new Thread() {
it = new Thread() {
@Override
public void run() {
try {
String readLine;
while (!stop && (readLine = reader.readLine()) != null) {
if (readLine.equalsIgnoreCase("stop"))
ServerRunner.stop();
serveroutput.println(readLine);
serveroutput.flush();
while (!stop || serveroutput.) { //TODO: Store if stream got closed (enum for state)?
try {
if ((readLine = reader.readLine()) == null)
continue; //Keep going until we stop
if (readLine.equalsIgnoreCase("stop"))
ServerRunner.stop();
serveroutput.println(readLine);
serveroutput.flush();
} catch (Exception e) {
e.printStackTrace();
Thread.sleep(100); //Sleep a bit and keep going
}
}
} catch (IOException e) {
} catch (InterruptedException e) {
e.printStackTrace();
}
ServerRunner.stop();
@ -97,7 +109,7 @@ public class ServerRunner {
e.printStackTrace();
}
writeToScreen("Server stopped! Restarting...");
serverprocess = startServer(config);
serverprocess = startServer(config, serverJar);
serverinput = new BufferedReader(new InputStreamReader(serverprocess.getInputStream()));
serveroutput = new PrintWriter(serverprocess.getOutputStream());
restartcounter = RESTART_MESSAGE_COUNT;
@ -114,9 +126,32 @@ public class ServerRunner {
};
ot.setName("OutputThread");
ot.start();
final Thread errt = new Thread() {
@Override
public void run() {
try {
BufferedReader serverinput = new BufferedReader(
new InputStreamReader(serverprocess.getErrorStream(), StandardCharsets.UTF_8));
String line;
while (true) {
if ((line = serverinput.readLine()) != null) {
writeToScreen(line);
} else if (stop)
break;
}
serverinput.close();
} catch (IOException e) {
e.printStackTrace();
}
ServerRunner.stop();
writeToScreen("Stopped " + Thread.currentThread().getName());
}
};
errt.setName("ErrorThread");
errt.start();
Thread.currentThread().setName("RestarterThread");
long starttime = syncStart(config.restartAt);
System.out.println("Restart scheduled in " + starttime / 3600000f);
writeToScreen("Restart scheduled in " + starttime / 3600000f);
boolean firstrun = true;
while (!stop) {
try {
@ -159,8 +194,9 @@ public class ServerRunner {
writeToScreen("Stopped " + Thread.currentThread().getName());
}
private static Process startServer(Config config) throws IOException {
return Runtime.getRuntime().exec(("java "+config.serverParams+" -jar spigot-" + config.serverVersion + ".jar").split(" "));
private static Process startServer(Config config, File serverJar) throws IOException {
ProcessBuilder pb = new ProcessBuilder(("java " + config.serverParams + " -jar " + serverJar.getPath()).split(" ")); //Need to use split() because of the supplied params
return pb.start();
}
private static void sendMessage(PrintWriter output, String color, String text) {
@ -172,6 +208,7 @@ public class ServerRunner {
private static void stop() {
stop = true;
rt.interrupt(); // The restarter thread sleeps for a long time and keeps the program running
it.interrupt(); // The input thread will listen until it's stopped
}
private static void writeToScreen(String line) {