From 9a9936bc8c5d6215f187b5d445f07a8e87b37068 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Mon, 22 Dec 2014 16:39:34 -0600 Subject: [PATCH] Allow animated motd to be enabled or disabled through database --- .../src/mineplex/bungee/motd/MotdManager.java | 24 +++++++- .../mineplex/bungee/motd/MotdRepository.java | 55 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdManager.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdManager.java index e4b17b2b0..4a05869c9 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdManager.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdManager.java @@ -3,17 +3,20 @@ package mineplex.bungee.motd; import java.util.List; import java.util.concurrent.TimeUnit; +import net.md_5.bungee.api.event.ProxyPingEvent; import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.connection.CustomMotd; import net.md_5.bungee.connection.CustomMotdFactory; import net.md_5.bungee.connection.InitialHandler; +import net.md_5.bungee.event.EventHandler; public class MotdManager implements Listener, Runnable, CustomMotdFactory { private Plugin _plugin; private MotdRepository _repository; + private String firstLine = " §b§l§m §8§l§m[ §r §9§lMineplex§r §f§lGames§r §8§l§m ]§b§l§m §r"; private List _motdLines; public MotdManager(Plugin plugin) @@ -25,14 +28,33 @@ public class MotdManager implements Listener, Runnable, CustomMotdFactory _repository = new MotdRepository(); _repository.initialize(); + } - InitialHandler.setCustomMotdFactory(this); // For animated motd + @EventHandler + public void serverPing(ProxyPingEvent event) + { + net.md_5.bungee.api.ServerPing serverPing = event.getResponse(); + + String motd = firstLine; + if (_motdLines != null && _motdLines.size() > 0) + motd += "\n" + _motdLines.get(0); + + event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), serverPing.getPlayers(), motd, serverPing.getFaviconObject())); } @Override public void run() { _motdLines = _repository.retrieveMotd(); + + if (_repository.retrieveMotdAnimated()) + { + InitialHandler.setCustomMotdFactory(this); + } + else + { + InitialHandler.setCustomMotdFactory(null); + } } public List getMotdLines() diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java index c4a2758d4..23bcbf632 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/motd/MotdRepository.java @@ -19,6 +19,7 @@ public class MotdRepository private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS BungeeMotd (id INT NOT NULL AUTO_INCREMENT, motd VARCHAR(256), PRIMARY KEY (id));"; private static String RETRIEVE_MOTD = "SELECT motd FROM BungeeMotd;"; + private static String RETRIEVE_ANIMATED = "SELECT value FROM bungeeSettings WHERE id='animatedMotd'"; public void initialize() { @@ -113,4 +114,58 @@ public class MotdRepository return lines; } + + public boolean retrieveMotdAnimated() + { + boolean enabled = false; + + ResultSet resultSet = null; + PreparedStatement preparedStatement = null; + + try + { + if (_connection == null || _connection.isClosed()) + _connection = DriverManager.getConnection(_connectionString, _userName, _password); + + preparedStatement = _connection.prepareStatement(RETRIEVE_ANIMATED); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) + { + enabled = resultSet.getBoolean(1); + } + } + 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 enabled; + } }