From 10bfead9e4ff8345087f77cc33033a4c9db7581c Mon Sep 17 00:00:00 2001 From: filoghost Date: Mon, 11 Aug 2014 22:57:44 +0200 Subject: [PATCH] Add {world: worldName} placeholder. --- HolographicDisplays/plugin.yml | 2 +- .../holograms/HolographicDisplays.java | 4 ++- .../holograms/bungee/BungeeChannel.java | 1 + .../holograms/object/HologramLineData.java | 17 +++++++++ .../placeholders/PlaceholderManager.java | 36 +++++++++++++++++-- .../{utils => tasks}/BungeeCleanupTask.java | 2 +- .../tasks/WorldPlayerCounterTask.java | 27 ++++++++++++++ 7 files changed, 83 insertions(+), 6 deletions(-) rename HolographicDisplays/src/com/gmail/filoghost/holograms/{utils => tasks}/BungeeCleanupTask.java (92%) create mode 100644 HolographicDisplays/src/com/gmail/filoghost/holograms/tasks/WorldPlayerCounterTask.java diff --git a/HolographicDisplays/plugin.yml b/HolographicDisplays/plugin.yml index 92193392..cf378968 100644 --- a/HolographicDisplays/plugin.yml +++ b/HolographicDisplays/plugin.yml @@ -1,6 +1,6 @@ name: HolographicDisplays main: com.gmail.filoghost.holograms.HolographicDisplays -version: 1.8.5 +version: 1.8.6 softdepend: [Multiverse-Core, MultiWorld, My Worlds, My_Worlds, ProtocolLib] diff --git a/HolographicDisplays/src/com/gmail/filoghost/holograms/HolographicDisplays.java b/HolographicDisplays/src/com/gmail/filoghost/holograms/HolographicDisplays.java index c5f34dac..0c623c54 100644 --- a/HolographicDisplays/src/com/gmail/filoghost/holograms/HolographicDisplays.java +++ b/HolographicDisplays/src/com/gmail/filoghost/holograms/HolographicDisplays.java @@ -26,7 +26,8 @@ import com.gmail.filoghost.holograms.placeholders.AnimationManager; import com.gmail.filoghost.holograms.placeholders.PlaceholderManager; import com.gmail.filoghost.holograms.placeholders.StaticPlaceholders; import com.gmail.filoghost.holograms.protocol.ProtocolLibHook; -import com.gmail.filoghost.holograms.utils.BungeeCleanupTask; +import com.gmail.filoghost.holograms.tasks.BungeeCleanupTask; +import com.gmail.filoghost.holograms.tasks.WorldPlayerCounterTask; import com.gmail.filoghost.holograms.utils.StringUtils; import com.gmail.filoghost.holograms.utils.VersionUtils; import com.gmail.filoghost.holograms.utils.ConfigNode; @@ -195,6 +196,7 @@ public class HolographicDisplays extends JavaPlugin { ServerInfoTimer.setRefreshSeconds(Configuration.bungeeRefreshSeconds); ServerInfoTimer.startTask(); BungeeCleanupTask.start(); + Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new WorldPlayerCounterTask(), 3 * 20L, 3 * 20L); Set savedHolograms = HologramDatabase.getHolograms(); if (savedHolograms != null && savedHolograms.size() > 0) { diff --git a/HolographicDisplays/src/com/gmail/filoghost/holograms/bungee/BungeeChannel.java b/HolographicDisplays/src/com/gmail/filoghost/holograms/bungee/BungeeChannel.java index faf36844..dd63ac7f 100644 --- a/HolographicDisplays/src/com/gmail/filoghost/holograms/bungee/BungeeChannel.java +++ b/HolographicDisplays/src/com/gmail/filoghost/holograms/bungee/BungeeChannel.java @@ -73,6 +73,7 @@ public class BungeeChannel implements PluginMessageListener { out.writeUTF(server); } catch (IOException e) { // It should not happen. + e.printStackTrace(); HolographicDisplays.getInstance().getLogger().warning("I/O Exception while asking for player count on server '" + server + "'."); } diff --git a/HolographicDisplays/src/com/gmail/filoghost/holograms/object/HologramLineData.java b/HolographicDisplays/src/com/gmail/filoghost/holograms/object/HologramLineData.java index 8034f109..3ec66684 100644 --- a/HolographicDisplays/src/com/gmail/filoghost/holograms/object/HologramLineData.java +++ b/HolographicDisplays/src/com/gmail/filoghost/holograms/object/HologramLineData.java @@ -14,6 +14,7 @@ public class HologramLineData { private String[] bungeeOnlinePlayers; private String[] bungeeStatuses; + private String[] worldsPlayerCount; public HologramLineData(HologramHorse horse, String originalName) { this.horse = horse; @@ -34,6 +35,11 @@ public class HologramLineData { bungeeStatuses = new String[list.size()]; bungeeStatuses = list.toArray(bungeeStatuses); } + + public void setWorldsCountToCheck(List list) { + worldsPlayerCount = new String[list.size()]; + worldsPlayerCount = list.toArray(worldsPlayerCount); + } public HologramHorse getHorse() { return horse; @@ -55,6 +61,10 @@ public class HologramLineData { return bungeeStatuses != null; } + public boolean hasWorldsCountToCheck() { + return worldsPlayerCount != null; + } + /** * Can be null. */ @@ -75,4 +85,11 @@ public class HologramLineData { public String[] getBungeeStatusesToCheck() { return bungeeStatuses; } + + /** + * Can be null. + */ + public String[] getWorldsPlayersCountToCheck() { + return worldsPlayerCount; + } } diff --git a/HolographicDisplays/src/com/gmail/filoghost/holograms/placeholders/PlaceholderManager.java b/HolographicDisplays/src/com/gmail/filoghost/holograms/placeholders/PlaceholderManager.java index 1a2cf26d..2553c085 100644 --- a/HolographicDisplays/src/com/gmail/filoghost/holograms/placeholders/PlaceholderManager.java +++ b/HolographicDisplays/src/com/gmail/filoghost/holograms/placeholders/PlaceholderManager.java @@ -13,6 +13,7 @@ import com.gmail.filoghost.holograms.HolographicDisplays; import com.gmail.filoghost.holograms.bungee.ServerInfoTimer; import com.gmail.filoghost.holograms.nms.interfaces.HologramHorse; import com.gmail.filoghost.holograms.object.HologramLineData; +import com.gmail.filoghost.holograms.tasks.WorldPlayerCounterTask; public class PlaceholderManager { @@ -23,6 +24,7 @@ public class PlaceholderManager { private static final Pattern BUNGEE_ONLINE_PATTERN = Pattern.compile("(\\{online:)([^}]+)(\\})"); private static final Pattern BUNGEE_STATUS_PATTERN = Pattern.compile("(\\{status:)([^}]+)(\\})"); private static final Pattern ANIMATION_PATTERN = Pattern.compile("(\\{animation:)([^}]+)(\\})"); + private static final Pattern WORLD_PATTERN = Pattern.compile("(\\{world:)([^}]+)(\\})"); public PlaceholderManager() { horsesToRefresh = new ArrayList(); @@ -48,6 +50,7 @@ public class PlaceholderManager { List containedPlaceholders = null; List bungeeServersOnlinePlayers = null; List bungeeServersStatuses = null; + List worldsPlayerCount = null; Matcher matcher; for (Placeholder placeholder : PlaceholdersList.getDefaults()) { @@ -68,6 +71,25 @@ public class PlaceholderManager { } + + // Players in a world count pattern. + matcher = WORLD_PATTERN.matcher(customName); + while (matcher.find()) { + + if (worldsPlayerCount == null) { + worldsPlayerCount = new ArrayList(); + } + + String worldName = matcher.group(2).trim().toLowerCase(); + + // Shorter placeholder without spaces. + customName = customName.replace(matcher.group(), "{world:" + worldName + "}"); + + // Add it to tracked worlds. + worldsPlayerCount.add(worldName); + } + + // BungeeCord online pattern. matcher = BUNGEE_ONLINE_PATTERN.matcher(customName); while (matcher.find()) { @@ -105,8 +127,6 @@ public class PlaceholderManager { } - - // Animation pattern. matcher = ANIMATION_PATTERN.matcher(customName); boolean updateName = false; @@ -132,7 +152,7 @@ public class PlaceholderManager { } } - if (containedPlaceholders != null || bungeeServersOnlinePlayers != null || bungeeServersStatuses != null) { + if (containedPlaceholders != null || bungeeServersOnlinePlayers != null || bungeeServersStatuses != null || worldsPlayerCount != null) { HologramLineData data = new HologramLineData(horse, customName); if (containedPlaceholders != null) { @@ -147,6 +167,10 @@ public class PlaceholderManager { data.setBungeeStatusesToCheck(bungeeServersStatuses); } + if (worldsPlayerCount != null) { + data.setWorldsCountToCheck(worldsPlayerCount); + } + horsesToRefresh.add(data); updatePlaceholders(data); } else { @@ -223,6 +247,12 @@ public class PlaceholderManager { } } + if (data.hasWorldsCountToCheck()) { + for (String world : data.getWorldsPlayersCountToCheck()) { + newCustomName = newCustomName.replace("{world:" + world + "}", WorldPlayerCounterTask.getCount(world)); + } + } + // Update only if needed, don't send useless packets. if (!oldCustomName.equals(newCustomName)) { data.getHorse().forceSetCustomName(newCustomName); diff --git a/HolographicDisplays/src/com/gmail/filoghost/holograms/utils/BungeeCleanupTask.java b/HolographicDisplays/src/com/gmail/filoghost/holograms/tasks/BungeeCleanupTask.java similarity index 92% rename from HolographicDisplays/src/com/gmail/filoghost/holograms/utils/BungeeCleanupTask.java rename to HolographicDisplays/src/com/gmail/filoghost/holograms/tasks/BungeeCleanupTask.java index c73cf8ba..99996222 100644 --- a/HolographicDisplays/src/com/gmail/filoghost/holograms/utils/BungeeCleanupTask.java +++ b/HolographicDisplays/src/com/gmail/filoghost/holograms/tasks/BungeeCleanupTask.java @@ -1,4 +1,4 @@ -package com.gmail.filoghost.holograms.utils; +package com.gmail.filoghost.holograms.tasks; import java.util.Iterator; import java.util.Map.Entry; diff --git a/HolographicDisplays/src/com/gmail/filoghost/holograms/tasks/WorldPlayerCounterTask.java b/HolographicDisplays/src/com/gmail/filoghost/holograms/tasks/WorldPlayerCounterTask.java new file mode 100644 index 00000000..ad958bfb --- /dev/null +++ b/HolographicDisplays/src/com/gmail/filoghost/holograms/tasks/WorldPlayerCounterTask.java @@ -0,0 +1,27 @@ +package com.gmail.filoghost.holograms.tasks; + +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.World; + +import com.google.common.collect.Maps; + +public class WorldPlayerCounterTask implements Runnable { + + private static Map worlds = Maps.newHashMap(); + + @Override + public void run() { + worlds.clear(); + + for (World world : Bukkit.getWorlds()) { + worlds.put(world.getName().toLowerCase(), world.getPlayers().size()); + } + } + + public static String getCount(String world) { + Integer count = worlds.get(world); + return count != null ? count.toString() : "world not found"; + } +}