2013-09-11 22:53:24 +02:00
|
|
|
package mineplex.bungee.playerCount;
|
2013-09-05 11:12:12 +02:00
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2013-10-28 07:37:23 +01:00
|
|
|
import net.md_5.bungee.api.ServerPing.Players;
|
2013-09-05 11:12:12 +02:00
|
|
|
import net.md_5.bungee.api.config.ListenerInfo;
|
|
|
|
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;
|
|
|
|
|
2013-09-11 22:53:24 +02:00
|
|
|
public class PlayerCount implements Listener, Runnable
|
2013-09-05 11:12:12 +02:00
|
|
|
{
|
2013-09-11 22:53:24 +02:00
|
|
|
private Plugin _plugin;
|
2013-09-18 00:50:23 +02:00
|
|
|
private PlayerCountRepository _repository;
|
2013-09-11 22:53:24 +02:00
|
|
|
|
2013-09-13 07:33:03 +02:00
|
|
|
private int _totalPlayers = -1;
|
2013-09-05 11:12:12 +02:00
|
|
|
private int _totalMaxPlayers;
|
|
|
|
|
2013-09-11 22:53:24 +02:00
|
|
|
public PlayerCount(Plugin plugin)
|
2013-09-05 11:12:12 +02:00
|
|
|
{
|
2013-09-11 22:53:24 +02:00
|
|
|
_plugin = plugin;
|
2013-09-05 11:12:12 +02:00
|
|
|
|
2013-09-18 00:50:23 +02:00
|
|
|
_plugin.getProxy().getScheduler().schedule(_plugin, this, 3L, 3L, TimeUnit.SECONDS);
|
2013-09-11 22:53:24 +02:00
|
|
|
_plugin.getProxy().getPluginManager().registerListener(_plugin, this);
|
2013-09-18 00:50:23 +02:00
|
|
|
|
|
|
|
ListenerInfo listenerInfo = _plugin.getProxy().getConfigurationAdapter().getListeners().iterator().next();
|
|
|
|
|
|
|
|
_repository = new PlayerCountRepository(listenerInfo.getHost().getAddress().getHostAddress() + ":" + listenerInfo.getHost().getPort(), listenerInfo.getMaxPlayers());
|
|
|
|
_repository.initialize();
|
2013-09-05 11:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run()
|
2013-09-18 00:50:23 +02:00
|
|
|
{
|
|
|
|
_repository.updatePlayerCountInDatabase(_plugin.getProxy().getOnlineCount());
|
2013-09-05 11:12:12 +02:00
|
|
|
|
2013-09-18 00:50:23 +02:00
|
|
|
PlayerTotalData playerTotalData = _repository.retrievePlayerCount();
|
2013-09-05 11:12:12 +02:00
|
|
|
|
2013-09-18 00:50:23 +02:00
|
|
|
_totalPlayers = playerTotalData.CurrentPlayers;
|
|
|
|
_totalMaxPlayers = playerTotalData.MaxPlayers;
|
2013-09-05 11:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void ServerPing(ProxyPingEvent event)
|
|
|
|
{
|
2013-09-18 00:50:23 +02:00
|
|
|
net.md_5.bungee.api.ServerPing serverPing = event.getResponse();
|
2013-10-28 07:37:23 +01:00
|
|
|
|
2014-04-11 02:15:44 +02:00
|
|
|
event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), new Players(_totalMaxPlayers, _totalPlayers, null), serverPing.getDescription(), serverPing.getFavicon()));
|
2013-09-05 11:12:12 +02:00
|
|
|
}
|
|
|
|
}
|