From 2a48ead90d020d8e1e022b1f89de985d50335708 Mon Sep 17 00:00:00 2001 From: LOOHP Date: Tue, 4 Aug 2020 01:47:43 +0800 Subject: [PATCH] Fixed stop command --- src/com/loohp/limbo/Console.java | 12 +----------- src/com/loohp/limbo/Limbo.java | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/com/loohp/limbo/Console.java b/src/com/loohp/limbo/Console.java index 1d2de01..e1c318d 100644 --- a/src/com/loohp/limbo/Console.java +++ b/src/com/loohp/limbo/Console.java @@ -37,17 +37,7 @@ public class Console { String[] input = CustomStringUtils.splitStringToArgs(reader.readLine()); if (input[0].equalsIgnoreCase("stop")) { - for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) { - client.getSocket().close(); - while (client.getSocket().isConnected()) { - try { - TimeUnit.MILLISECONDS.sleep(500); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - System.exit(0); + Limbo.getInstance().stopServer(); } else if (input[0].equalsIgnoreCase("say")) { if (input.length > 1) { String message = "[Server] " + String.join(" ", Arrays.copyOfRange(input, 1, input.length)); diff --git a/src/com/loohp/limbo/Limbo.java b/src/com/loohp/limbo/Limbo.java index e19a828..4e3091a 100644 --- a/src/com/loohp/limbo/Limbo.java +++ b/src/com/loohp/limbo/Limbo.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -16,6 +17,7 @@ import org.json.simple.parser.ParseException; import com.loohp.limbo.File.ServerProperties; import com.loohp.limbo.Location.Location; +import com.loohp.limbo.Server.ClientConnection; import com.loohp.limbo.Server.ServerConnection; import com.loohp.limbo.Server.Packets.Packet; import com.loohp.limbo.Server.Packets.PacketIn; @@ -194,5 +196,26 @@ public class Limbo { return base; } + + @SuppressWarnings("deprecation") + public void stopServer() { + Limbo.getInstance().getConsole().sendMessage("Stopping Server..."); + for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) { + try { + client.getSocket().close(); + } catch (IOException e) { + e.printStackTrace(); + client.destroy(); + } + } + while (!Limbo.getInstance().getServerConnection().getClients().isEmpty()) { + try { + TimeUnit.MILLISECONDS.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.exit(0); + } }