package mineplex.hub.server; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.messaging.PluginMessageListener; import mineplex.core.MiniPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.common.Rank; import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.UtilPlayer; import mineplex.core.common.util.UtilTime; import mineplex.core.common.util.UtilTime.TimeUnit; import mineplex.core.donation.DonationManager; import mineplex.core.portal.Portal; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.hub.party.Party; import mineplex.hub.party.PartyManager; import mineplex.hub.server.command.ServerNpcCommand; import mineplex.hub.server.ui.ServerNpcShop; public class ServerManager extends MiniPlugin implements PluginMessageListener { private CoreClientManager _clientManager; private DonationManager _donationManager; private Portal _portal; private PartyManager _partyManager; private NautHashMap> _serverNpcMap = new NautHashMap>(); private NautHashMap _serverNpcShopMap = new NautHashMap(); private NautHashMap _serverInfoMap = new NautHashMap(); private NautHashMap _serverUpdate = new NautHashMap(); private boolean _update = true; private boolean _loading = false; public ServerManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager, Portal portal, PartyManager partyManager) { super("Server Manager", plugin); _clientManager = clientManager; _donationManager = donationManager; _portal = portal; _partyManager = partyManager; plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord"); plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeSigns"); plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeSigns", this); LoadServers(); } public void AddCommands() { AddCommand(new ServerNpcCommand(this)); } public void AddServer(String serverNpcName, String serverName) { ServerInfo serverInfo = new ServerInfo(); serverInfo.Name = serverName; _serverNpcMap.get(serverNpcName).add(serverInfo); if (!_serverInfoMap.containsKey(serverName)) { _serverInfoMap.put(serverName, serverInfo); _serverUpdate.put(serverName, System.currentTimeMillis()); } SaveServers(); } public void RemoveServer(String serverName) { for (String key : _serverNpcMap.keySet()) { _serverNpcMap.get(key).remove(serverName); } _serverInfoMap.remove(serverName); } public void AddServerNpc(String serverNpcName) { _serverNpcMap.put(serverNpcName, new ArrayList()); _serverNpcShopMap.put(serverNpcName, new ServerNpcShop(this, _clientManager, _donationManager, serverNpcName)); } public void RemoveServerNpc(String serverNpcName) { List mappedServers = _serverNpcMap.remove(serverNpcName); _serverNpcShopMap.remove(serverNpcName); if (mappedServers != null) { for (ServerInfo mappedServer : mappedServers) { boolean isMappedElseWhere = false; for (String key : _serverNpcMap.keySet()) { for (ServerInfo value : _serverNpcMap.get(key)) { if (value.Name.equalsIgnoreCase(mappedServer.Name)) { isMappedElseWhere = true; break; } } if (isMappedElseWhere) break; } if (!isMappedElseWhere) _serverInfoMap.remove(mappedServer.Name); } } } public List GetServerList(String serverNpcName) { return _serverNpcMap.get(serverNpcName); } public Set GetAllServers() { return _serverInfoMap.keySet(); } public ServerInfo GetServerInfo(String serverName) { return _serverInfoMap.get(serverName); } public boolean HasServerNpc(String serverNpcName) { return _serverNpcMap.containsKey(serverNpcName); } @Override public void onPluginMessageReceived(String channel, Player player, byte[] message) { if (!channel.equalsIgnoreCase("BungeeSigns")) return; DataInputStream in = null; String serverName = null; String motd = null; int players = 0; int maxPlayers = 0; try { in = new DataInputStream(new ByteArrayInputStream(message)); serverName = in.readUTF(); motd = in.readUTF(); players = in.readInt(); maxPlayers = in.readInt(); if (_serverInfoMap.containsKey(serverName)) { String[] args = motd.split("\\|"); ServerInfo serverInfo = _serverInfoMap.get(serverName); serverInfo.MOTD = args.length > 0 ? args[0] : motd; serverInfo.CurrentPlayers = players; serverInfo.MaxPlayers = maxPlayers; if (args.length > 1) serverInfo.ServerType = args[1]; if (args.length > 2) serverInfo.Game = args[2]; if (args.length > 3) serverInfo.Map = args[3]; _serverUpdate.put(serverName, System.currentTimeMillis()); } } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } @EventHandler public void UpdateServers(UpdateEvent event) { if (event.getType() != UpdateType.SEC) return; _update = !_update; if (!_update) { for (ServerNpcShop shop : _serverNpcShopMap.values()) { shop.UpdatePages(); } return; } if (Bukkit.getServer().getOnlinePlayers().length > 0) { for (String serverName : _serverInfoMap.keySet()) { ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(b); try { out.writeUTF(serverName); } catch (IOException e) { e.printStackTrace(); } Bukkit.getServer().getOnlinePlayers()[0].sendPluginMessage(GetPlugin(), "BungeeSigns", b.toByteArray()); if (_serverUpdate.get(serverName) != -1L && System.currentTimeMillis() - _serverUpdate.get(serverName) > 5000) { ServerInfo serverInfo = _serverInfoMap.get(serverName); serverInfo.MOTD = ChatColor.DARK_RED + "OFFLINE"; serverInfo.CurrentPlayers = 0; serverInfo.MaxPlayers = 0; _serverUpdate.put(serverName, -1L); } } } } public void Help(Player caller, String message) { UtilPlayer.message(caller, F.main(_moduleName, "Commands List:")); UtilPlayer.message(caller, F.help("/servernpc create ", " is name of npc.", Rank.OWNER)); UtilPlayer.message(caller, F.help("/servernpc delete ", " is name of npc.", Rank.OWNER)); UtilPlayer.message(caller, F.help("/servernpc addserver | ", "Adds server.", Rank.OWNER)); UtilPlayer.message(caller, F.help("/servernpc removeserver ", "Removes server.", Rank.OWNER)); UtilPlayer.message(caller, F.help("/servernpc listnpcs", "Lists all server npcs.", Rank.OWNER)); UtilPlayer.message(caller, F.help("/servernpc listservers ", "Lists all servers.", Rank.OWNER)); UtilPlayer.message(caller, F.help("/servernpc listoffline", "Shows all servers offline.", Rank.OWNER)); if (message != null) UtilPlayer.message(caller, F.main(_moduleName, ChatColor.RED + message)); } public void Help(Player caller) { Help(caller, null); } public PartyManager getPartyManager() { return _partyManager; } public void SelectServer(org.bukkit.entity.Player player, ServerInfo serverInfo) { Party party = _partyManager.GetParty(player); if (party != null) { if (player.getName().equals(party.GetLeader())) { for (String name : party.GetPlayers()) { Player partyPlayer = UtilPlayer.searchExact(name); if (partyPlayer == null) continue; if (_clientManager.Get(partyPlayer).GetRank().Has(Rank.ULTRA) || _donationManager.Get(partyPlayer.getName()).OwnsUnknownPackage(serverInfo.ServerType + " ULTRA")) continue; partyPlayer.leaveVehicle(); partyPlayer.eject(); _portal.SendPlayerToServer(partyPlayer, serverInfo.Name); } for (String name : party.GetPlayers()) { Player partyPlayer = UtilPlayer.searchExact(name); if (partyPlayer == null) continue; if (_clientManager.Get(partyPlayer).GetRank().Has(Rank.ULTRA) || _donationManager.Get(partyPlayer.getName()).OwnsUnknownPackage(serverInfo.ServerType + " ULTRA")) { partyPlayer.leaveVehicle(); partyPlayer.eject(); _portal.SendPlayerToServer(partyPlayer, serverInfo.Name); } } } } else { player.leaveVehicle(); player.eject(); _portal.SendPlayerToServer(player, serverInfo.Name); } } public void ListServerNpcs(Player caller) { UtilPlayer.message(caller, F.main(GetName(), "Listing Server Npcs:")); for (String serverNpc : _serverNpcMap.keySet()) { UtilPlayer.message(caller, F.main(GetName(), C.cYellow + serverNpc)); } } public void ListServers(Player caller, String serverNpcName) { UtilPlayer.message(caller, F.main(GetName(), "Listing Servers for '" + serverNpcName + "':")); for (ServerInfo serverNpc : _serverNpcMap.get(serverNpcName)) { UtilPlayer.message(caller, F.main(GetName(), C.cYellow + serverNpc.Name + C.cWhite + " - " + serverNpc.MOTD + " " + serverNpc.CurrentPlayers + "/" + serverNpc.MaxPlayers)); } } public void ListOfflineServers(Player caller) { UtilPlayer.message(caller, F.main(GetName(), "Listing Offline Servers:")); for (ServerInfo serverNpc : _serverInfoMap.values()) { if (serverNpc.MOTD.equalsIgnoreCase(ChatColor.DARK_RED + "OFFLINE")) { UtilPlayer.message(caller, F.main(GetName(), C.cYellow + serverNpc.Name + C.cWhite + " - " + F.time(UtilTime.convertString(System.currentTimeMillis() - _serverUpdate.get(serverNpc.Name), 0, TimeUnit.FIT)))); } } } public void SaveServers() { if (_loading) return; FileWriter fstream = null; BufferedWriter out = null; try { fstream = new FileWriter("ServerManager.dat"); out = new BufferedWriter(fstream); for (String key : _serverNpcMap.keySet()) { System.out.println("Saving serverinfos for " + key); for (ServerInfo serverInfo : _serverNpcMap.get(key)) { out.write(key + " | " + serverInfo.Name); out.newLine(); } } out.close(); } catch (Exception e) { System.err.println("ServerManager Save Error: " + e.getMessage()); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (fstream != null) { try { fstream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void LoadServers() { _loading = true; FileInputStream fstream = null; BufferedReader br = null; try { File npcFile = new File("ServerManager.dat"); if (npcFile.exists()) { fstream = new FileInputStream(npcFile); br = new BufferedReader(new InputStreamReader(fstream)); String line = br.readLine(); while (line != null) { String serverNpcName = line.substring(0, line.indexOf('|')).trim(); String server = line.substring(line.indexOf('|') + 1).trim(); if (!HasServerNpc(serverNpcName)) { AddServerNpc(serverNpcName); } AddServer(serverNpcName, server); line = br.readLine(); } } } catch (Exception e) { System.out.println("ServerManager - Error parsing servers file : " + e.getMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (fstream != null) { try { fstream.close(); } catch (IOException e) { e.printStackTrace(); } } _loading = false; } } public int GetRequiredSlots(Player player, String serverType) { int slots = 0; Party party = _partyManager.GetParty(player); if (party != null) { if (player.getName().equals(party.GetLeader())) { for (String name : party.GetPlayers()) { Player partyPlayer = UtilPlayer.searchExact(name); if (partyPlayer == null) continue; if (_clientManager.Get(partyPlayer).GetRank().Has(Rank.ULTRA) || _donationManager.Get(partyPlayer.getName()).OwnsUnknownPackage(serverType + " ULTRA")) continue; slots++; } } } else { if (!_clientManager.Get(player).GetRank().Has(Rank.ULTRA) && !_donationManager.Get(player.getName()).OwnsUnknownPackage(serverType + " ULTRA")) slots++; } return slots; } }