From 37eba7a6f92cfc24c9ab70abb32caed2862ecaee Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 16 Dec 2019 09:04:59 +0100 Subject: [PATCH] Adding shutdown callback, adapting startup --- pom.xml | 1 + src/de/steamwar/bungeecore/Subserver.java | 31 +++++++++++++---------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index e7cb401..bea02a0 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ + persistentbungeecore diff --git a/src/de/steamwar/bungeecore/Subserver.java b/src/de/steamwar/bungeecore/Subserver.java index 9122a0e..79270c1 100644 --- a/src/de/steamwar/bungeecore/Subserver.java +++ b/src/de/steamwar/bungeecore/Subserver.java @@ -9,7 +9,6 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.InetSocketAddress; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -39,18 +38,24 @@ public class Subserver implements Runnable { } private final String serverName; - private Process p; - private PrintWriter writer; - private ServerInfo server; - private Servertype type; + private final Runnable shutdownCallback; + private final Process p; + private final PrintWriter writer; + private final ServerInfo server; + private final Servertype type; private boolean started; - private final List cachedPlayers = new ArrayList<>(); + private final List cachedPlayers = new LinkedList<>(); public Subserver(Servertype type, String serverName, int port, String... command){ + this(type, serverName, port, () -> {}, command); + } + + public Subserver(Servertype type, String serverName, int port, Runnable shutdownCallback, String... command){ this.started = false; this.serverName = serverName; this.type = type; + this.shutdownCallback = shutdownCallback; ProcessBuilder pb = new ProcessBuilder(command); InetSocketAddress address = new InetSocketAddress("127.0.0.1", port); @@ -58,8 +63,7 @@ public class Subserver implements Runnable { try{ this.p = pb.start(); }catch(IOException e){ - logger.log(Level.SEVERE, "Server could not be started", e); - return; + throw new SecurityException("Server could not be started", e); } this.server = ProxyServer.getInstance().constructServerInfo( @@ -108,12 +112,10 @@ public class Subserver implements Runnable { } private void fatalError(){ - for(ProxiedPlayer cached : cachedPlayers){ + for(ProxiedPlayer cached : cachedPlayers) cached.sendMessage(Persistent.getPrefix() + "§cUnerwarteter Fehler beim Serverstart."); - } - for(ProxiedPlayer player : server.getPlayers()){ + for(ProxiedPlayer player : server.getPlayers()) player.sendMessage(Persistent.getPrefix() + "§cUnerwarteter Fehler im Server."); - } } public void run(){ @@ -123,7 +125,7 @@ public class Subserver implements Runnable { try(BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))){ String line = ""; while (!started && (line = reader.readLine()) != null) { - started = line.contains("Done ("); //Problematisch + started = line.contains("ViaVersion detected server version"); } if(line == null){ @@ -132,7 +134,7 @@ public class Subserver implements Runnable { return; } - Thread.sleep(800); + Thread.sleep(300); for(ProxiedPlayer cachedPlayer : cachedPlayers){ Thread.sleep(200); @@ -151,6 +153,7 @@ public class Subserver implements Runnable { } finally { serverList.remove(this); ProxyServer.getInstance().getServers().remove(serverName); + shutdownCallback.run(); } } }