diff --git a/Plugins/Mineplex.Core/src/mineplex/core/message/Commands/SendCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/message/Commands/SendCommand.java deleted file mode 100644 index 94ab3b73b..000000000 --- a/Plugins/Mineplex.Core/src/mineplex/core/message/Commands/SendCommand.java +++ /dev/null @@ -1,62 +0,0 @@ -package mineplex.core.message.Commands; - -import org.bukkit.Sound; -import org.bukkit.entity.Player; - -import mineplex.core.command.CommandBase; -import mineplex.core.common.Rank; -import mineplex.core.common.util.C; -import mineplex.core.common.util.F; -import mineplex.core.common.util.UtilPlayer; -import mineplex.core.message.MessageManager; -import mineplex.core.portal.Portal; - -public class SendCommand extends CommandBase -{ - private Portal _portal; - - public SendCommand(MessageManager plugin) - { - super(plugin, Rank.ADMIN, "send"); - - _portal = new Portal(Plugin.GetPlugin()); - } - - private void SendPlayerToServerWithMessage(Player player, String serverName) - { - _portal.AddTransferRecord(player.getName(), serverName); - - player.playSound(player.getLocation(), Sound.CHEST_OPEN, 0.5f, 2f); - - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cYellow + "You are being sent to server: " + C.cGold + serverName + C.cYellow + "!")); - } - - @Override - public void Execute(Player player, String[] args) - { - Rank playerRank = Plugin.GetClientManager().Get(player).GetRank(); - - if(args == null || args.length == 0) - { - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cDPurple + "Your arguments are inappropriate for this command!")); - return; - } - else if(args.length == 2) - { - if((playerRank == Rank.OWNER) || (playerRank == Rank.DEVELOPER) || (playerRank == Rank.ADMIN)) - { - Player sendPlayer = Plugin.GetClientManager().Get(args[0]).GetPlayer(); - SendPlayerToServerWithMessage(sendPlayer, args[1]); - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cYellow + "You have sent player: " + C.cGold + args[0] + " " + C.cYellow + "to server: " + C.cGold + args[1] + C.cYellow + "!")); - return; - } - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cDPurple + "Your arguments are not allowed for this command!")); - return; - } - else - { - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cDPurple + "Your arguments are inappropriate for this command!")); - return; - } - } -} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java b/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java index e8c3d7b59..269962a36 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/message/MessageManager.java @@ -86,9 +86,6 @@ public class MessageManager extends MiniClientPlugin AddCommand(new AdminCommand(this)); - - AddCommand(new ServerCommand(this)); - AddCommand(new SendCommand(this)); } public void Help(Player caller, String message) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/portal/Commands/SendCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/portal/Commands/SendCommand.java new file mode 100644 index 000000000..7795f5a2c --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/portal/Commands/SendCommand.java @@ -0,0 +1,80 @@ +package mineplex.core.portal.Commands; + +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +import mineplex.core.command.*; +import mineplex.core.common.Rank; +import mineplex.core.common.util.C; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilPlayer; +import mineplex.core.portal.Portal; + +public class SendCommand extends CommandBase +{ + private Portal _portal; + + public SendCommand(Portal plugin) + { + super(plugin, Rank.ADMIN, "snd","send"); + + _portal = plugin; + } + + private void SendPlayerToServerWithMessage(Player player, String serverName) + { + _portal.AddTransferRecord(player.getName(), serverName); + + player.playSound(player.getLocation(), Sound.PORTAL, 0.5f, 2f); + + UtilPlayer.message(player, F.main("SEND", C.cYellow + "You are being sent to server: " + C.cGold + serverName + C.cYellow + "!")); + } + + @Override + public void Execute(Player player, String[] args) + { + Rank playerRank = CommandCenter.GetClientManager().Get(player).GetRank(); + + if(args == null || args.length == 0) + { + UtilPlayer.message(player, F.main("SEND", C.cPurple + "Your arguments are inappropriate for this command!")); + return; + } + else if(args.length == 2) + { + if((playerRank == Rank.OWNER) || (playerRank == Rank.DEVELOPER) || (playerRank == Rank.ADMIN)) + { + Player sendPlayer; + + try + { + sendPlayer = CommandCenter.GetClientManager().Get(args[0]).GetPlayer(); + } + catch(NullPointerException npe) + { + UtilPlayer.message(player, F.main("SEND", C.cPurple + "Your specified player, " + C.cGold + args[0] + C.cPurple + ", does not exist!")); + if(!_portal.DoesServerExist(args[1])) + { + UtilPlayer.message(player, F.main("SEND", C.cPurple + "Your specified server, " + C.cGold + args[1] + C.cPurple + ", does not exist!")); + } + return; + } + if(!_portal.DoesServerExist(args[1])) + { + UtilPlayer.message(player, F.main("SEND", C.cPurple + "Your specified server, " + C.cGold + args[1] + C.cPurple + ", does not exist!")); + return; + } + SendPlayerToServerWithMessage(sendPlayer, args[1]); + UtilPlayer.message(player, F.main("SEND", C.cYellow + "You have sent player: " + C.cGold + args[0] + C.cYellow + " to server: " + C.cGold + args[1] + C.cYellow + "!")); + return; + } + UtilPlayer.message(player, F.main("SEND", C.cPurple + "Your arguments are not allowed for this command!")); + return; + } + else + { + UtilPlayer.message(player, F.main("SEND", C.cPurple + "Your arguments are inappropriate for this command!")); + return; + } + } +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/message/Commands/ServerCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/portal/Commands/ServerCommand.java similarity index 62% rename from Plugins/Mineplex.Core/src/mineplex/core/message/Commands/ServerCommand.java rename to Plugins/Mineplex.Core/src/mineplex/core/portal/Commands/ServerCommand.java index c5e276636..7fa44e6a1 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/message/Commands/ServerCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/portal/Commands/ServerCommand.java @@ -1,48 +1,58 @@ -package mineplex.core.message.Commands; +package mineplex.core.portal.Commands; import org.bukkit.Sound; import org.bukkit.entity.Player; -import mineplex.core.command.CommandBase; +import mineplex.core.command.*; import mineplex.core.common.Rank; import mineplex.core.common.util.C; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; -import mineplex.core.message.MessageManager; import mineplex.core.portal.Portal; -public class ServerCommand extends CommandBase +public class ServerCommand extends CommandBase { private Portal _portal; - public ServerCommand(MessageManager plugin) + public ServerCommand(Portal plugin) { - super(plugin, Rank.ALL, "server"); + super(plugin, Rank.ALL, "srv","server"); - _portal = new Portal(Plugin.GetPlugin()); + _portal = plugin; } private void SendPlayerToServerWithMessage(Player player, String serverName) { _portal.SendPlayerToServer(player, serverName); - player.playSound(player.getLocation(), Sound.CHEST_OPEN, 0.5f, 2f); + player.playSound(player.getLocation(), Sound.PORTAL, 0.5f, 2f); - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cYellow + "You have been sent to server: " + C.cGold + serverName + C.cYellow + "!")); + UtilPlayer.message(player, F.main("SERVER", C.cYellow + "You have been sent to server: " + C.cGold + serverName + C.cYellow + "!")); } @Override public void Execute(Player player, String[] args) { - Rank playerRank = Plugin.GetClientManager().Get(player).GetRank(); + Rank playerRank = CommandCenter.GetClientManager().Get(player).GetRank(); + String serverName = Plugin.GetPlugin().getConfig().getString("serverstatus.name"); if(args == null || args.length == 0) { - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cYellow + "You are currently on server: " + C.cGold + Plugin.GetPlugin().getConfig().getString("serverstatus.name"))); + UtilPlayer.message(player, F.main("SERVER", C.cYellow + "You are currently on server: " + C.cGold + serverName)); return; } else if(args.length == 1) { + if(serverName.equalsIgnoreCase(args[0])) + { + UtilPlayer.message(player, F.main("SERVER", C.cPurple + "You are already on server: " + C.cGold + serverName + C.cPurple + "!")); + return; + } + if(!_portal.DoesServerExist(args[0])) + { + UtilPlayer.message(player, F.main("SERVER", C.cPurple + "Your specified server, " + C.cGold + args[0] + C.cPurple + ", does not exist!")); + return; + } String servUp = args[0].toUpperCase(); if(!(servUp.contains("HERO") || servUp.contains("ULTRA") || servUp.contains("BETA") || servUp.contains("STAFF") || servUp.contains("TEST"))) @@ -79,12 +89,12 @@ public class ServerCommand extends CommandBase SendPlayerToServerWithMessage(player, args[0]); return; } - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cDPurple + "Your arguments are not allowed for this command!")); + UtilPlayer.message(player, F.main("SERVER", C.cPurple + "Your arguments are not allowed for this command!")); return; } else { - UtilPlayer.message(player, F.main(Plugin.GetName(), C.cDPurple + "Your arguments are inappropriate for this command!")); + UtilPlayer.message(player, F.main("SERVER", C.cPurple + "Your arguments are inappropriate for this command!")); return; } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java b/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java index bd1b830d4..a641b7dfb 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/portal/Portal.java @@ -16,6 +16,7 @@ import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.MiniPlugin; import mineplex.core.common.util.NautHashMap; +import mineplex.core.portal.Commands.*; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; @@ -28,6 +29,7 @@ public class Portal extends MiniPlugin private List _transfers = new ArrayList(); private boolean _retrieve = true; + private volatile boolean _serverExists = false; public Portal(JavaPlugin plugin) { @@ -35,7 +37,7 @@ public class Portal extends MiniPlugin Bukkit.getMessenger().registerOutgoingPluginChannel(GetPlugin(), "BungeeCord"); - _repository.initialize(plugin.getConfig().getBoolean("serverstatus.us")); + _repository.initialize(plugin.getConfig().getBoolean("serverstatus.us"), plugin.getConfig().getString("serverstatus.connectionurl")); } public void SendAllPlayers(String serverName) @@ -101,6 +103,27 @@ public class Portal extends MiniPlugin } } + public boolean DoesServerExist(final String serverName) + { + synchronized (_transferLock) + { + Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable() + { + public void run() + { + _serverExists = _repository.doesServerExist(serverName); + } + }); + } + return _serverExists; + } + + public void AddCommands() + { + AddCommand(new ServerCommand(this)); + AddCommand(new SendCommand(this)); + } + @EventHandler public void addTransferDelete(final PlayerQuitEvent event) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/portal/PortalRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/portal/PortalRepository.java index acb9b8ec3..185de9348 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/portal/PortalRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/portal/PortalRepository.java @@ -5,8 +5,12 @@ import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; import mineplex.core.common.util.NautHashMap; +import mineplex.core.status.ServerStatusData; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -18,6 +22,7 @@ public class PortalRepository private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Queue?autoReconnect=true&failOverReadOnly=false&maxReconnects=10"; private String _userName = "root"; private String _password = "tAbechAk3wR7tuTh"; + private String _altConnectString = ""; private boolean _us = true; @@ -25,12 +30,15 @@ public class PortalRepository private static String RETRIEVE_TRANSFER_RECORDS = "SELECT playerName, serverName FROM playerServerTransfer WHERE playerName IN "; private static String DELETE_TRANSFER_RECORDS = "DELETE FROM playerServerTransfer WHERE playerName = ?;"; private static String ADD_TRANSFER_RECORD = "INSERT INTO playerServerTransfer (playerName,serverName) VALUES(?,?);"; + private static String RETRIEVE_SERVER_STATUSES = "SELECT ServerStatus.serverName, motd, players, maxPlayers, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = ? AND ServerStatus.serverName = ?;"; private Connection _connection = null; + private Connection _altConnection = null; - public void initialize(boolean us) + public void initialize(boolean us, String altConnectString) { _us = us; + _altConnectString = altConnectString; PreparedStatement preparedStatement = null; @@ -213,4 +221,73 @@ public class PortalRepository } } } + + public boolean doesServerExist(String serverName) + { + ResultSet resultSet = null; + PreparedStatement preparedStatement = null; + List serverData = new ArrayList(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + try + { + if (_altConnection == null) + { + _altConnection = DriverManager.getConnection(_altConnectString, _userName, _password); + } + + preparedStatement = _altConnection.prepareStatement(RETRIEVE_SERVER_STATUSES); + preparedStatement.setBoolean(1, _us); + preparedStatement.setString(2, serverName); + + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) + { + ServerStatusData serverStatusData = new ServerStatusData(); + + serverStatusData.Name = resultSet.getString(1); + serverStatusData.Motd = resultSet.getString(2); + serverStatusData.Players = resultSet.getInt(3); + serverStatusData.MaxPlayers = resultSet.getInt(4); + long current = dateFormat.parse(resultSet.getString(5)).getTime(); + long updated = dateFormat.parse(resultSet.getString(6)).getTime(); + + if (current - updated < 15000) + serverData.add(serverStatusData); + } + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (preparedStatement != null) + { + try + { + preparedStatement.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + + if (resultSet != null) + { + try + { + resultSet.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + + return !serverData.isEmpty(); + } }