51 lines
1.7 KiB
Java
51 lines
1.7 KiB
Java
package mineplex.bungee.playerCount;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import net.md_5.bungee.api.ServerPing.Players;
|
|
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
|
|
{
|
|
private Plugin _plugin;
|
|
private PlayerCountRepository _repository;
|
|
|
|
private int _totalPlayers = -1;
|
|
private int _totalMaxPlayers;
|
|
|
|
public PlayerCount(Plugin plugin)
|
|
{
|
|
_plugin = plugin;
|
|
|
|
_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();
|
|
}
|
|
|
|
public void run()
|
|
{
|
|
_repository.updatePlayerCountInDatabase(_plugin.getProxy().getOnlineCount());
|
|
|
|
PlayerTotalData playerTotalData = _repository.retrievePlayerCount();
|
|
|
|
_totalPlayers = playerTotalData.CurrentPlayers;
|
|
_totalMaxPlayers = playerTotalData.MaxPlayers;
|
|
}
|
|
|
|
@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), serverPing.getMotd(), serverPing.getFavicon()));
|
|
}
|
|
}
|