Created
February 27, 2018 19:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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