Allow animated motd to be enabled or disabled through database

This commit is contained in:
Shaun Bennett 2014-12-22 16:39:34 -06:00
parent 3efc80ec58
commit 9a9936bc8c
2 changed files with 78 additions and 1 deletions

View File

@ -3,17 +3,20 @@ package mineplex.bungee.motd;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; 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.Listener;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.connection.CustomMotd; import net.md_5.bungee.connection.CustomMotd;
import net.md_5.bungee.connection.CustomMotdFactory; import net.md_5.bungee.connection.CustomMotdFactory;
import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.event.EventHandler;
public class MotdManager implements Listener, Runnable, CustomMotdFactory public class MotdManager implements Listener, Runnable, CustomMotdFactory
{ {
private Plugin _plugin; private Plugin _plugin;
private MotdRepository _repository; 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<String> _motdLines; private List<String> _motdLines;
public MotdManager(Plugin plugin) public MotdManager(Plugin plugin)
@ -25,14 +28,33 @@ public class MotdManager implements Listener, Runnable, CustomMotdFactory
_repository = new MotdRepository(); _repository = new MotdRepository();
_repository.initialize(); _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 @Override
public void run() public void run()
{ {
_motdLines = _repository.retrieveMotd(); _motdLines = _repository.retrieveMotd();
if (_repository.retrieveMotdAnimated())
{
InitialHandler.setCustomMotdFactory(this);
}
else
{
InitialHandler.setCustomMotdFactory(null);
}
} }
public List<String> getMotdLines() public List<String> getMotdLines()

View File

@ -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 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_MOTD = "SELECT motd FROM BungeeMotd;";
private static String RETRIEVE_ANIMATED = "SELECT value FROM bungeeSettings WHERE id='animatedMotd'";
public void initialize() public void initialize()
{ {
@ -113,4 +114,58 @@ public class MotdRepository
return lines; 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;
}
} }