From b50aa92f76790d75ac92c27ffd63e8fd708bf1fd Mon Sep 17 00:00:00 2001 From: Joel Date: Sat, 20 Feb 2021 03:46:33 +0100 Subject: [PATCH] Made a method HubBalancer#removeServer since the code was duplicated. --- .../themcgamer/proxy/hub/HubBalancer.java | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/proxy/src/main/java/zone/themcgamer/proxy/hub/HubBalancer.java b/proxy/src/main/java/zone/themcgamer/proxy/hub/HubBalancer.java index 48c9037..470243b 100644 --- a/proxy/src/main/java/zone/themcgamer/proxy/hub/HubBalancer.java +++ b/proxy/src/main/java/zone/themcgamer/proxy/hub/HubBalancer.java @@ -49,17 +49,24 @@ public class HubBalancer implements Runnable, Listener { JedisCommandHandler.getInstance().addListener(jedisCommand -> { if (jedisCommand instanceof ServerRestartCommand) { ServerRestartCommand serverRestartCommand = (ServerRestartCommand) jedisCommand; + removeServer(serverRestartCommand.getServerId()); ServerInfo serverInfo = proxy.getProxy().getServerInfo(serverRestartCommand.getServerId()); - if (serverInfo == null) - return; - for (ProxiedPlayer player : proxy.getProxy().getPlayers()) { - if (player.getServer().getInfo().equals(serverInfo)) { - if (hubs.isEmpty() || hubs.size() == 1) + if (serverInfo == null) { + System.out.println("ServerInfo is null"); + //We do this check due sometimes players stay in a server which is deleted. But were send to it before it was deleted. + for (ProxiedPlayer player : proxy.getProxy().getPlayers()) { + if (player.getServer() == null || player.getServer().getInfo().getName().equals(serverRestartCommand.getServerId())) kickPlayer(player, NO_AVAILABLE_HUB); } + return; + } else { + for (ProxiedPlayer player : proxy.getProxy().getPlayers()) { + if (player.getServer().getInfo().equals(serverInfo)) { + if (hubs.isEmpty() || hubs.size() <= 1) + kickPlayer(player, NO_AVAILABLE_HUB); + } + } } - if (hubs.containsValue(serverInfo)) - hubs.remove(serverRestartCommand.getServerId()); } if (jedisCommand instanceof ServerStateChangeCommand) { ServerStateChangeCommand serverStateChangeCommand = (ServerStateChangeCommand) jedisCommand; @@ -69,17 +76,7 @@ public class HubBalancer implements Runnable, Listener { if (!serverStateChangeCommand.getNewState().isShuttingDownState()) return; MinecraftServer server = serverStateChangeCommand.getServer(); - proxy.getProxy().getServers().entrySet().removeIf(entry -> { - String key = entry.getKey(); - if (!key.equals(server.getId())) - return false; - if (key.equals(group.getName())) - return false; - if (entry.getValue().getMotd().equals("STATIC")) - return false; - hubs.remove(server.getId()); - return true; - }); + removeServer(server.getId()); } }); } @@ -225,4 +222,18 @@ public class HubBalancer implements Runnable, Listener { "&b" + reason + "\n" + "&7"))); } + + private void removeServer(String serverId) { + proxy.getProxy().getServers().entrySet().removeIf(entry -> { + String key = entry.getKey(); + if (!key.equals(serverId)) + return false; + if (key.equals(group.getName())) + return false; + if (entry.getValue().getMotd().equals("STATIC")) + return false; + hubs.remove(serverId); + return true; + }); + } } \ No newline at end of file