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>
</build>
<dependencies>
<dependency>
<groupId>org.xeustechnologies</groupId>
<artifactId>jcl-core</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>

View file

@ -1,61 +1,39 @@
package buttondevteam.serverrunner;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
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;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ServerRunner {
private static final String SERVER_VERSION = "1.9.2";
@SuppressWarnings("static-access")
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");
public static void main(String[] args) throws IOException {
System.out.println("Starting server...");
// Set default to cglib (from version 2.2.1)
ProxyProviderFactory.setDefaultProxyProvider(new CglibProxyProvider());
// Create a factory of castable objects/proxies
JclObjectFactory factory = JclObjectFactory.getInstance(true);
// Create and cast object of loaded class
Main serverinstance = (Main) factory.create(jcl, "buttondevteam.serverrunner.ServerMain");
serverinstance.main(new String[0]);
Thread.sleep(5000); // Wait for primaryThread to be set
while (Bukkit.getServer() == null || ((CraftServer) Bukkit.getServer()).getServer() == null
|| ((CraftServer) Bukkit.getServer()).getServer().primaryThread == null)
;
((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) {
Process p = Runtime.getRuntime().exec(
new String[] { "java", "-Xms512M", "-Xmx1024M", "-XX:MaxPermSize=128M", "-jar", "spigot-1.9.2.jar" });
Thread t = new Thread() {
@Override
public void run() {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String outline;
try {
while ((outline = br.readLine()) != null)
output.write(outline + "\n");
} catch (IOException e) {
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();
}
}