Add {world: worldName} placeholder.

This commit is contained in:
filoghost 2014-08-11 22:57:44 +02:00
parent 9ba424e749
commit 10bfead9e4
7 changed files with 83 additions and 6 deletions

View File

@ -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]

View File

@ -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<String> savedHolograms = HologramDatabase.getHolograms();
if (savedHolograms != null && savedHolograms.size() > 0) {

View File

@ -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 + "'.");
}

View File

@ -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<String> 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;
}
}

View File

@ -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<HologramLineData>();
@ -48,6 +50,7 @@ public class PlaceholderManager {
List<Placeholder> containedPlaceholders = null;
List<String> bungeeServersOnlinePlayers = null;
List<String> bungeeServersStatuses = null;
List<String> 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>();
}
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);

View File

@ -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;

View File

@ -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<String, Integer> 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";
}
}