Using separate process - Did a while ago

This commit is contained in:
Norbi Peti 2016-08-06 21:07:30 +02:00
parent d7deeb35b5
commit 97217cb0c4
2 changed files with 30 additions and 57 deletions

View file

@ -18,10 +18,5 @@
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
<dependency>
<groupId>org.xeustechnologies</groupId>
<artifactId>jcl-core</artifactId>
<version>2.6</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -1,61 +1,39 @@
package buttondevteam.serverrunner; package buttondevteam.serverrunner;
import java.util.logging.ConsoleHandler; import java.io.BufferedReader;
import java.util.logging.Level; import java.io.BufferedWriter;
import java.util.logging.Logger; import java.io.IOException;
import java.util.logging.SimpleFormatter; import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.Main;
import org.bukkit.craftbukkit.v1_9_R1.CraftServer;
import org.xeustechnologies.jcl.ClasspathResources;
import org.xeustechnologies.jcl.Configuration;
import org.xeustechnologies.jcl.JarClassLoader;
import org.xeustechnologies.jcl.JclObjectFactory;
import org.xeustechnologies.jcl.proxy.CglibProxyProvider;
import org.xeustechnologies.jcl.proxy.ProxyProviderFactory;
public class ServerRunner { public class ServerRunner {
private static final String SERVER_VERSION = "1.9.2"; private static final String SERVER_VERSION = "1.9.2";
@SuppressWarnings("static-access") public static void main(String[] args) throws IOException {
public static void main(String[] args) {
try {
System.out.println("Loading server classes...");
JarClassLoader jcl = new JarClassLoader();
jcl.getSystemLoader().setEnabled(true);
jcl.getCurrentLoader().setEnabled(false);
jcl.getParentLoader().setEnabled(false);
jcl.getOsgiBootLoader().setEnabled(false);
jcl.getThreadLoader().setEnabled(false);
jcl.getLocalLoader().setEnabled(true);
jcl.add("spigot-" + SERVER_VERSION + ".jar");
jcl.add("craftbukkit-" + SERVER_VERSION + ".jar");
System.out.println("Starting server..."); System.out.println("Starting server...");
// Set default to cglib (from version 2.2.1) Process p = Runtime.getRuntime().exec(
ProxyProviderFactory.setDefaultProxyProvider(new CglibProxyProvider()); new String[] { "java", "-Xms512M", "-Xmx1024M", "-XX:MaxPermSize=128M", "-jar", "spigot-1.9.2.jar" });
Thread t = new Thread() {
// Create a factory of castable objects/proxies @Override
JclObjectFactory factory = JclObjectFactory.getInstance(true); public void run() {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
// Create and cast object of loaded class BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Main serverinstance = (Main) factory.create(jcl, "buttondevteam.serverrunner.ServerMain"); String outline;
serverinstance.main(new String[0]); try {
Thread.sleep(5000); // Wait for primaryThread to be set while ((outline = br.readLine()) != null)
while (Bukkit.getServer() == null || ((CraftServer) Bukkit.getServer()).getServer() == null output.write(outline + "\n");
|| ((CraftServer) Bukkit.getServer()).getServer().primaryThread == null) } catch (IOException e) {
;
((CraftServer) Bukkit.getServer()).getServer().primaryThread.join();
/*
* for (Thread t : Thread.getAllStackTraces().keySet()) if
* (t.getName().equals("Server Infinisleeper")) t.join();
*/ // TODO: After stopping the server it stops
System.out.println("Unloading classes...");
for (String entry : jcl.getLoadedClasses().keySet())
jcl.unloadClass(entry);
System.out.println("Done!");
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
};
t.setName("InputThread");
t.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
} }