package mineplex.bungee.motd; import java.awt.Color; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.TimeUnit; import mineplex.serverdata.Region; import mineplex.serverdata.data.BungeeServer; import mineplex.serverdata.data.DataRepository; import mineplex.serverdata.redis.RedisDataRepository; import mineplex.serverdata.servers.ConnectionData; import mineplex.serverdata.servers.ServerManager; import mineplex.serverdata.servers.ConnectionData.ConnectionType; import net.md_5.bungee.api.ChatColor; 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.event.EventHandler; public class MotdManager implements Listener, Runnable { private Plugin _plugin; private DataRepository _repository; private DataRepository _secondRepository; private Region _region; private Random _random = new Random(); 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) { _plugin = plugin; _region = !new File("eu.dat").exists() ? Region.US : Region.EU; _plugin.getProxy().getScheduler().schedule(_plugin, this, 5L, 30L, TimeUnit.SECONDS); _plugin.getProxy().getPluginManager().registerListener(_plugin, this); _repository = new RedisDataRepository(ServerManager.getConnection(true, ServerManager.SERVER_STATUS_LABEL), ServerManager.getConnection(false, ServerManager.SERVER_STATUS_LABEL), Region.ALL, GlobalMotd.class, "globalMotd"); run(); if (new File("updateMOTD.dat").exists()) { if (_region == Region.US) _secondRepository = new RedisDataRepository(new ConnectionData("10.81.1.156", 6379, ConnectionType.MASTER, "ServerStatus"), new ConnectionData("10.81.1.156", 6377, ConnectionType.SLAVE, "ServerStatus"), Region.ALL, GlobalMotd.class, "globalMotd"); else _secondRepository = new RedisDataRepository(new ConnectionData("10.33.53.16", 6379, ConnectionType.MASTER, "ServerStatus"), new ConnectionData("10.33.53.16", 6377, ConnectionType.SLAVE, "ServerStatus"), Region.ALL, GlobalMotd.class, "globalMotd"); //String motdLine = "§f§l◄ §c§lMaintenance§f§l ►"; //String motdLine = "§f§l◄ §a§lCarl the Creeper§f§l ►"; // String motdLine = " §2§l§n M O N S T E R M A Z E B E T A §f"; String motdLine = " §f§l◄ §b§lComing Soon§f§l ▬ §3§lChampions CTF§f§l ►"; //String motdLine = " §d§lRank Sale §a§l40% Off"); //String motdLine = " §f§l◄§c§lMAINTENANCE§f§l►"); updateMainMotd(" §f§l§m §8§l§m[ §r §6§lMineplex§r §f§lGames§r §8§l§m ]§f§l§m §r", motdLine); System.out.println("Updated Bungee 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(_random.nextInt(_motdLines.size())); } event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), serverPing.getPlayers(), motd, serverPing.getFaviconObject())); } @Override public void run() { GlobalMotd motd = _repository.getElement("MainMotd"); if (motd != null) { _motdLines = motd.getMotd(); _firstLine = motd.getHeadline(); } } /** * Update the main {@link GlobalMotd} determining the MOTD for Bungee instances. * @param motdLines - the lines to update the MOTD to. */ public void updateMainMotd(String headline, String motdLine) { List motdLines = new ArrayList(); motdLines.add(motdLine); _repository.addElement(new GlobalMotd("MainMotd", headline, motdLines)); _secondRepository.addElement(new GlobalMotd("MainMotd", headline, motdLines)); } public List getMotdLines() { return _motdLines; } }