2013-09-11 22:53:24 +02:00
|
|
|
package mineplex.bungee.playerCount;
|
2013-09-05 11:12:12 +02:00
|
|
|
|
2015-03-14 01:53:36 +01:00
|
|
|
import java.io.File;
|
2015-03-14 06:03:22 +01:00
|
|
|
import java.util.UUID;
|
2013-09-05 11:12:12 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2015-03-14 06:03:22 +01:00
|
|
|
import mineplex.bungee.status.InternetStatus;
|
2015-03-14 01:53:36 +01:00
|
|
|
import mineplex.serverdata.Region;
|
|
|
|
import mineplex.serverdata.data.BungeeServer;
|
2015-03-14 06:03:22 +01:00
|
|
|
import mineplex.serverdata.data.DataRepository;
|
|
|
|
import mineplex.serverdata.redis.RedisDataRepository;
|
|
|
|
import mineplex.serverdata.servers.ServerManager;
|
|
|
|
import mineplex.serverdata.servers.ServerRepository;
|
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
|
|
|
{
|
2015-03-14 06:03:22 +01:00
|
|
|
private DataRepository<BungeeServer> _repository;
|
|
|
|
private UUID _uuid;
|
|
|
|
private Region _region;
|
|
|
|
|
2015-03-14 01:53:36 +01:00
|
|
|
private ListenerInfo _listenerInfo;
|
2015-03-14 06:03:22 +01:00
|
|
|
private Plugin _plugin;
|
2013-09-11 22:53:24 +02:00
|
|
|
|
2013-09-13 07:33:03 +02:00
|
|
|
private int _totalPlayers = -1;
|
2015-01-29 16:05:50 +01:00
|
|
|
|
2013-09-11 22:53:24 +02:00
|
|
|
public PlayerCount(Plugin plugin)
|
2013-09-05 11:12:12 +02:00
|
|
|
{
|
2015-03-14 06:03:22 +01:00
|
|
|
_uuid = UUID.randomUUID();
|
|
|
|
_region = !new File("eu.dat").exists() ? Region.US : Region.EU;
|
2013-09-11 22:53:24 +02:00
|
|
|
_plugin = plugin;
|
2013-09-05 11:12:12 +02:00
|
|
|
|
2015-03-14 01:53:36 +01:00
|
|
|
_plugin.getProxy().getScheduler().schedule(_plugin, this, 500L, 500L, TimeUnit.MILLISECONDS);
|
2013-09-11 22:53:24 +02:00
|
|
|
_plugin.getProxy().getPluginManager().registerListener(_plugin, this);
|
2013-09-18 00:50:23 +02:00
|
|
|
|
2015-03-14 01:53:36 +01:00
|
|
|
_listenerInfo = _plugin.getProxy().getConfigurationAdapter().getListeners().iterator().next();
|
2015-03-14 06:03:22 +01:00
|
|
|
|
|
|
|
_repository = new RedisDataRepository<BungeeServer>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(),
|
|
|
|
Region.ALL, BungeeServer.class, "bungeeServers");
|
2013-09-05 11:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void run()
|
2013-09-18 00:50:23 +02:00
|
|
|
{
|
2015-03-14 01:53:36 +01:00
|
|
|
|
2015-03-14 06:03:22 +01:00
|
|
|
BungeeServer snapshot = generateSnapshot();
|
|
|
|
_repository.addElement(snapshot, 15); // Update with a 15 second expiry on session
|
2013-09-05 11:12:12 +02:00
|
|
|
|
2015-03-14 06:03:22 +01:00
|
|
|
_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())
|
2015-03-14 01:53:36 +01:00
|
|
|
{
|
2015-03-14 02:10:35 +01:00
|
|
|
totalPlayers += server.getPlayerCount();
|
2015-03-14 01:53:36 +01:00
|
|
|
}
|
2015-03-14 02:10:35 +01:00
|
|
|
|
2015-03-14 06:03:22 +01:00
|
|
|
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);
|
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();
|
2014-12-11 16:17:19 +01:00
|
|
|
|
2014-12-12 11:21:23 +01:00
|
|
|
event.setResponse(new net.md_5.bungee.api.ServerPing(serverPing.getVersion(), new Players(_totalPlayers + 1, _totalPlayers, null), serverPing.getDescription(), serverPing.getFaviconObject()));
|
2013-09-05 11:12:12 +02:00
|
|
|
}
|
2015-01-04 06:37:04 +01:00
|
|
|
|
|
|
|
public int getTotalPlayers()
|
|
|
|
{
|
|
|
|
return _totalPlayers;
|
|
|
|
}
|
2013-09-05 11:12:12 +02:00
|
|
|
}
|