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

96 lines
3.0 KiB
Java

package mineplex.bungee.playerCount;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import mineplex.bungee.status.InternetStatus;
import mineplex.serverdata.Region;
import mineplex.serverdata.data.BungeeServer;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ServerManager;
import mineplex.serverdata.servers.ServerRepository;
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 DataRepository<BungeeServer> _repository;
private UUID _uuid;
private Region _region;
private ListenerInfo _listenerInfo;
private Plugin _plugin;
private int _totalPlayers = -1;
public PlayerCount(Plugin plugin)
{
_uuid = UUID.randomUUID();
_region = !new File("eu.dat").exists() ? Region.US : Region.EU;
_plugin = plugin;
_plugin.getProxy().getScheduler().schedule(_plugin, this, 500L, 500L, TimeUnit.MILLISECONDS);
_plugin.getProxy().getPluginManager().registerListener(_plugin, this);
_listenerInfo = _plugin.getProxy().getConfigurationAdapter().getListeners().iterator().next();
_repository = new RedisDataRepository<BungeeServer>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(),
Region.ALL, BungeeServer.class, "bungeeServers");
}
public void run()
{
BungeeServer snapshot = generateSnapshot();
_repository.addElement(snapshot, 15); // Update with a 15 second expiry on session
_totalPlayers = fetchPlayerCount();
}
/**
* @return an up-to-date total player count across all active Bungee Servers.
*/
private int fetchPlayerCount()
{
int totalPlayers = 0;
for (BungeeServer server : _repository.getElements())
{
totalPlayers += server.getPlayerCount();
}
return totalPlayers;
}
/**
* @return a newly instantiated {@link BungeeServer} snapshot of the current state of this server.
*/
private BungeeServer generateSnapshot()
{
String name = _uuid.toString(); // Use random UUID for unique id name.
String host = _listenerInfo.getHost().getAddress().getHostAddress();
int port = _listenerInfo.getHost().getPort();
boolean connected = InternetStatus.isConnected();
int playerCount = _plugin.getProxy().getOnlineCount();
return new BungeeServer(name, _region, host, port, playerCount, connected);
}
@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(_totalPlayers + 1, _totalPlayers, null), serverPing.getDescription(), serverPing.getFaviconObject()));
}
public int getTotalPlayers()
{
return _totalPlayers;
}
}