Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Created February 27, 2018 19:55
Show Gist options
  • Select an option

  • Save DarkSeraphim/98e72752bbb92c2c4cc078abd7bd0693 to your computer and use it in GitHub Desktop.

Select an option

Save DarkSeraphim/98e72752bbb92c2c4cc078abd7bd0693 to your computer and use it in GitHub Desktop.
Grabs the player count (on request), and allows outside access through a public getter.
public class PlayerCountPoller implements PluginMessageListener {
private int count;
public void requestUpdate() {
List<? extends Player> players = Bukkit.getOnlinePlayers();
if (players.isEmpty()) {
return;
}
Player player = players.get(0); // Just grab the first
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("PlayerCount");
out.writeUTF("ALL");
player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("bungee") {
return;
}
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
if (subchannel.equals("PlayerCount")) {
in.readUTF(); // I could read the server name, if I cared
this.count = in.readInt();
}
}
public int getLastKnownCount() {
return this.count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment