diff --git a/src/main/java/com/extendedclip/papi/bungeeexpansion/BungeeExpansion.java b/src/main/java/com/extendedclip/papi/bungeeexpansion/BungeeExpansion.java index 1b0fa55..bce336b 100644 --- a/src/main/java/com/extendedclip/papi/bungeeexpansion/BungeeExpansion.java +++ b/src/main/java/com/extendedclip/papi/bungeeexpansion/BungeeExpansion.java @@ -14,11 +14,13 @@ import org.bukkit.plugin.messaging.PluginMessageListener; import org.bukkit.scheduler.BukkitTask; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; +import java.util.stream.Stream; public final class BungeeExpansion extends PlaceholderExpansion implements PluginMessageListener, Taskable, Configurable { @@ -26,14 +28,11 @@ public final class BungeeExpansion extends PlaceholderExpansion implements Plugi private static final String SERVERS_CHANNEL = "GetServers"; private static final String PLAYERS_CHANNEL = "PlayerCount"; private static final String CONFIG_INTERVAL = "check_interval"; - private static final Splitter SPLITTER = Splitter.on(",").trimResults(); - - - private final Map counts = new HashMap<>(); + private int totalPlayerCount = 0; + private final Map counts = new HashMap<>(); private final AtomicReference cached = new AtomicReference<>(); - @Override public String getIdentifier() { return "bungee"; @@ -46,7 +45,7 @@ public String getAuthor() { @Override public String getVersion() { - return "2.2"; + return "2.3"; } @Override @@ -54,39 +53,31 @@ public Map getDefaults() { return Collections.singletonMap(CONFIG_INTERVAL, 30); } - @Override - public String onRequest(final OfflinePlayer player, String identifier) { - final int value; - - switch (identifier.toLowerCase()) { - case "all": - case "total": - value = counts.values().stream().mapToInt(Integer::intValue).sum(); - break; - default: - value = counts.getOrDefault(identifier.toLowerCase(), 0); - break; + public String onRequest(OfflinePlayer player, String identifier) { + identifier = identifier.toLowerCase(); + if (identifier.equals("all") || identifier.equals("total")) { + return String.valueOf(totalPlayerCount); + } else { + if (identifier.contains(",")) { + Stream servers = Arrays.stream(identifier.split(",")); + int value = servers.mapToInt(server -> counts.computeIfAbsent(server, count -> 0)).sum(); + return String.valueOf(value); + } else { + int value = counts.computeIfAbsent(identifier, count -> 0); + return String.valueOf(value); + } } - - return String.valueOf(value); } @Override public void start() { - final BukkitTask task = Bukkit.getScheduler().runTaskTimer(getPlaceholderAPI(), () -> { - - if (counts.isEmpty()) { - sendServersChannelMessage(); - } - else { - counts.keySet().forEach(this::sendPlayersChannelMessage); - } - - }, 20L * 2L, 20L * getLong(CONFIG_INTERVAL, 30)); + BukkitTask task = Bukkit.getScheduler().runTaskTimer(getPlaceholderAPI(), () -> { + sendPlayersChannelMessage("ALL"); + counts.keySet().forEach(this::sendPlayersChannelMessage); + }, 40L, 20L * getLong(CONFIG_INTERVAL, 30)); - - final BukkitTask prev = cached.getAndSet(task); + BukkitTask prev = cached.getAndSet(task); if (prev != null) { prev.cancel(); } else { @@ -97,7 +88,7 @@ public void start() { @Override public void stop() { - final BukkitTask prev = cached.getAndSet(null); + BukkitTask prev = cached.getAndSet(null); if (prev == null) { return; } @@ -109,18 +100,23 @@ public void stop() { Bukkit.getMessenger().unregisterIncomingPluginChannel(getPlaceholderAPI(), MESSAGE_CHANNEL, this); } - @Override - public void onPluginMessageReceived(final String channel, final Player player, final byte[] message) { + public void onPluginMessageReceived(String channel, Player player, byte[] message) { if (!MESSAGE_CHANNEL.equals(channel)) { return; } - //noinspection UnstableApiUsage - final ByteArrayDataInput in = ByteStreams.newDataInput(message); - switch (in.readUTF()) { + ByteArrayDataInput in = ByteStreams.newDataInput(message); + String messageType = in.readUTF(); + switch (messageType) { case PLAYERS_CHANNEL: - counts.put(in.readUTF().toLowerCase(), in.readInt()); + String server = in.readUTF(); + int count = in.readInt(); + if (server.equalsIgnoreCase("ALL")) { + totalPlayerCount = count; + } else { + counts.put(server.toLowerCase(), count); + } break; case SERVERS_CHANNEL: SPLITTER.split(in.readUTF()).forEach(serverName -> counts.putIfAbsent(serverName.toLowerCase(), 0)); @@ -128,28 +124,26 @@ public void onPluginMessageReceived(final String channel, final Player player, f } } - private void sendServersChannelMessage() { - sendMessage(SERVERS_CHANNEL, out -> { }); + sendMessage(SERVERS_CHANNEL, out -> { + }); } - private void sendPlayersChannelMessage(final String serverName) { + private void sendPlayersChannelMessage(String serverName) { sendMessage(PLAYERS_CHANNEL, out -> out.writeUTF(serverName)); } - private void sendMessage(final String channel, final Consumer consumer) { - final Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null); + private void sendMessage(String channel, Consumer consumer) { + Player player = Iterables.getFirst(Bukkit.getOnlinePlayers(), null); if (player == null) { return; } - //noinspection UnstableApiUsage - final ByteArrayDataOutput out = ByteStreams.newDataOutput(); + ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.writeUTF(channel); consumer.accept(out); player.sendPluginMessage(getPlaceholderAPI(), MESSAGE_CHANNEL, out.toByteArray()); } - } \ No newline at end of file