Mineplex2018-withcommit/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerCount/PlayerCount.java

51 lines
1.7 KiB
Java
Raw Normal View History

package mineplex.bungee.playerCount;
2013-09-05 11:12:12 +02:00
import java.util.concurrent.TimeUnit;
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;
public class PlayerCount implements Listener, Runnable
2013-09-05 11:12:12 +02:00
{
private Plugin _plugin;
private PlayerCountRepository _repository;
private int _totalPlayers = -1;
2013-09-05 11:12:12 +02:00
private int _totalMaxPlayers;
public PlayerCount(Plugin plugin)
2013-09-05 11:12:12 +02:00
{
_plugin = plugin;
2013-09-05 11:12:12 +02:00
_plugin.getProxy().getScheduler().schedule(_plugin, this, 3L, 3L, TimeUnit.SECONDS);
_plugin.getProxy().getPluginManager().registerListener(_plugin, this);
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()
{
_repository.updatePlayerCountInDatabase(_plugin.getProxy().getOnlineCount());
2013-09-05 11:12:12 +02:00
PlayerTotalData playerTotalData = _repository.retrievePlayerCount();
2013-09-05 11:12:12 +02:00
_totalPlayers = playerTotalData.CurrentPlayers;
_totalMaxPlayers = playerTotalData.MaxPlayers;
2013-09-05 11:12:12 +02:00
}
@EventHandler
public void ServerPing(ProxyPingEvent event)
{
net.md_5.bungee.api.ServerPing serverPing = event.getResponse();
event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), new Players(_totalMaxPlayers, _totalPlayers, null), serverPing.getDescription(), serverPing.getFaviconObject()));
2013-09-05 11:12:12 +02:00
}
}