Make some placeholder-related classes not static

This commit is contained in:
filoghost 2021-03-15 23:19:13 +01:00
parent 6cc28cc957
commit 9a56a032de
12 changed files with 129 additions and 89 deletions

View File

@ -24,10 +24,12 @@ public class DefaultBackendAPI extends BackendAPI {
private final APIHologramManager apiHologramManager; private final APIHologramManager apiHologramManager;
private final NMSManager nmsManager; private final NMSManager nmsManager;
private final PlaceholdersRegistry placeholderRegistry;
public DefaultBackendAPI(APIHologramManager apiHologramManager, NMSManager nmsManager) { public DefaultBackendAPI(APIHologramManager apiHologramManager, NMSManager nmsManager, PlaceholdersRegistry placeholderRegistry) {
this.apiHologramManager = apiHologramManager; this.apiHologramManager = apiHologramManager;
this.nmsManager = nmsManager; this.nmsManager = nmsManager;
this.placeholderRegistry = placeholderRegistry;
} }
@Override @Override
@ -46,7 +48,7 @@ public class DefaultBackendAPI extends BackendAPI {
Preconditions.checkArgument(refreshRate >= 0, "refreshRate should be positive"); Preconditions.checkArgument(refreshRate >= 0, "refreshRate should be positive");
Preconditions.notNull(replacer, "replacer"); Preconditions.notNull(replacer, "replacer");
return PlaceholdersRegistry.register(new Placeholder(plugin, textPlaceholder, refreshRate, replacer)); return placeholderRegistry.register(new Placeholder(plugin, textPlaceholder, refreshRate, replacer));
} }
@Override @Override
@ -64,14 +66,14 @@ public class DefaultBackendAPI extends BackendAPI {
@Override @Override
public Collection<String> getRegisteredPlaceholders(Plugin plugin) { public Collection<String> getRegisteredPlaceholders(Plugin plugin) {
Preconditions.notNull(plugin, "plugin"); Preconditions.notNull(plugin, "plugin");
return PlaceholdersRegistry.getTextPlaceholdersByPlugin(plugin); return placeholderRegistry.getTextPlaceholdersByPlugin(plugin);
} }
@Override @Override
public boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) { public boolean unregisterPlaceholder(Plugin plugin, String textPlaceholder) {
Preconditions.notNull(plugin, "plugin"); Preconditions.notNull(plugin, "plugin");
Preconditions.notNull(textPlaceholder, "textPlaceholder"); Preconditions.notNull(textPlaceholder, "textPlaceholder");
return PlaceholdersRegistry.unregister(plugin, textPlaceholder); return placeholderRegistry.unregister(plugin, textPlaceholder);
} }
@Override @Override

View File

@ -47,6 +47,8 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
private InternalHologramManager internalHologramManager; private InternalHologramManager internalHologramManager;
private APIHologramManager apiHologramManager; private APIHologramManager apiHologramManager;
private BungeeServerTracker bungeeServerTracker; private BungeeServerTracker bungeeServerTracker;
private AnimationsRegistry animationRegistry;
private PlaceholdersManager placeholderManager;
@Override @Override
public void onCheckedEnable() throws PluginEnableException { public void onCheckedEnable() throws PluginEnableException {
@ -87,11 +89,13 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
} catch (Exception e) { } catch (Exception e) {
throw new PluginEnableException(e, "Couldn't initialize the NMS manager."); throw new PluginEnableException(e, "Couldn't initialize the NMS manager.");
} }
bungeeServerTracker = new BungeeServerTracker(this); bungeeServerTracker = new BungeeServerTracker(this);
animationRegistry = new AnimationsRegistry();
placeholderManager = new PlaceholdersManager(bungeeServerTracker, animationRegistry);
configManager = new ConfigManager(getDataFolder().toPath()); configManager = new ConfigManager(getDataFolder().toPath());
internalHologramManager = new InternalHologramManager(nmsManager); internalHologramManager = new InternalHologramManager(nmsManager, placeholderManager);
apiHologramManager = new APIHologramManager(nmsManager); apiHologramManager = new APIHologramManager(nmsManager, placeholderManager);
PrintableErrorCollector errorCollector = new PrintableErrorCollector(); PrintableErrorCollector errorCollector = new PrintableErrorCollector();
@ -107,7 +111,7 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
ProtocolLibHook.setup(this, nmsManager, this, errorCollector); ProtocolLibHook.setup(this, nmsManager, this, errorCollector);
// Start repeating tasks. // Start repeating tasks.
PlaceholdersManager.startRefreshTask(this, bungeeServerTracker); placeholderManager.startRefreshTask(this);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BungeeCleanupTask(bungeeServerTracker), 5 * 60 * 20, 5 * 60 * 20); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new BungeeCleanupTask(bungeeServerTracker), 5 * 60 * 20, 5 * 60 * 20);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new WorldPlayerCounterTask(), 0L, 3 * 20); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new WorldPlayerCounterTask(), 0L, 3 * 20);
@ -121,7 +125,7 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
registerListener(updateNotificationListener); registerListener(updateNotificationListener);
// Enable the API. // Enable the API.
BackendAPI.setImplementation(new DefaultBackendAPI(apiHologramManager, nmsManager)); BackendAPI.setImplementation(new DefaultBackendAPI(apiHologramManager, nmsManager, placeholderManager.getRegistry()));
// Register bStats metrics // Register bStats metrics
int pluginID = 3123; int pluginID = 3123;
@ -136,7 +140,7 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
} }
public void load(boolean deferHologramsCreation, ErrorCollector errorCollector) { public void load(boolean deferHologramsCreation, ErrorCollector errorCollector) {
PlaceholdersManager.untrackAll(); placeholderManager.untrackAll();
internalHologramManager.clearAll(); internalHologramManager.clearAll();
bungeeServerTracker.resetTrackedServers(); bungeeServerTracker.resetTrackedServers();
@ -144,7 +148,7 @@ public class HolographicDisplays extends FCommonsPlugin implements ProtocolPacke
configManager.reloadMainConfig(errorCollector); configManager.reloadMainConfig(errorCollector);
HologramDatabase hologramDatabase = configManager.loadHologramDatabase(errorCollector); HologramDatabase hologramDatabase = configManager.loadHologramDatabase(errorCollector);
try { try {
AnimationsRegistry.loadAnimations(configManager, errorCollector); animationRegistry.loadAnimations(configManager, errorCollector);
} catch (IOException | ConfigException e) { } catch (IOException | ConfigException e) {
errorCollector.add(e, "failed to load animation files"); errorCollector.add(e, "failed to load animation files");
} }

View File

@ -14,6 +14,7 @@ import me.filoghost.holographicdisplays.api.line.TextLine;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.Configuration;
import me.filoghost.holographicdisplays.object.base.BaseHologram; import me.filoghost.holographicdisplays.object.base.BaseHologram;
import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -30,8 +31,8 @@ public class APIHologram extends BaseHologram<APIHologramLine> implements Hologr
private boolean allowPlaceholders; private boolean allowPlaceholders;
protected APIHologram(Location source, Plugin plugin, NMSManager nmsManager, APIHologramManager apiHologramManager) { protected APIHologram(Location source, Plugin plugin, NMSManager nmsManager, APIHologramManager apiHologramManager, PlaceholdersManager placeholderManager) {
super(source, nmsManager); super(source, nmsManager, placeholderManager);
Preconditions.notNull(plugin, "plugin"); Preconditions.notNull(plugin, "plugin");
this.plugin = plugin; this.plugin = plugin;
this.apiHologramManager = apiHologramManager; this.apiHologramManager = apiHologramManager;

View File

@ -8,6 +8,7 @@ package me.filoghost.holographicdisplays.object.api;
import me.filoghost.holographicdisplays.api.Hologram; import me.filoghost.holographicdisplays.api.Hologram;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.object.base.BaseHologramManager; import me.filoghost.holographicdisplays.object.base.BaseHologramManager;
import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -19,13 +20,15 @@ import java.util.List;
public class APIHologramManager extends BaseHologramManager<APIHologram> { public class APIHologramManager extends BaseHologramManager<APIHologram> {
private final NMSManager nmsManager; private final NMSManager nmsManager;
private final PlaceholdersManager placeholderManager;
public APIHologramManager(NMSManager nmsManager) { public APIHologramManager(NMSManager nmsManager, PlaceholdersManager placeholderManager) {
this.nmsManager = nmsManager; this.nmsManager = nmsManager;
this.placeholderManager = placeholderManager;
} }
public Hologram createHologram(Location source, Plugin plugin) { public Hologram createHologram(Location source, Plugin plugin) {
APIHologram hologram = new APIHologram(source, plugin, nmsManager, this); APIHologram hologram = new APIHologram(source, plugin, nmsManager, this, placeholderManager);
super.addHologram(hologram); super.addHologram(hologram);
return hologram; return hologram;
} }

View File

@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.core.hologram.StandardHologram;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine; import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.disk.Configuration; import me.filoghost.holographicdisplays.disk.Configuration;
import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
@ -21,12 +22,14 @@ import java.util.List;
public abstract class BaseHologram<T extends StandardHologramLine> extends BaseHologramComponent implements StandardHologram { public abstract class BaseHologram<T extends StandardHologramLine> extends BaseHologramComponent implements StandardHologram {
private final NMSManager nmsManager; private final NMSManager nmsManager;
private final PlaceholdersManager placeholderManager;
private final List<T> lines; private final List<T> lines;
private final List<T> unmodifiableLinesView; private final List<T> unmodifiableLinesView;
private boolean deleted; private boolean deleted;
public BaseHologram(Location location, NMSManager nmsManager) { public BaseHologram(Location location, NMSManager nmsManager, PlaceholdersManager placeholderManager) {
this.placeholderManager = placeholderManager;
Preconditions.notNull(location, "location"); Preconditions.notNull(location, "location");
this.setLocation(location); this.setLocation(location);
this.nmsManager = nmsManager; this.nmsManager = nmsManager;
@ -38,6 +41,10 @@ public abstract class BaseHologram<T extends StandardHologramLine> extends BaseH
return nmsManager; return nmsManager;
} }
protected final PlaceholdersManager getPlaceholderManager() {
return placeholderManager;
}
@Override @Override
public boolean isDeleted() { public boolean isDeleted() {
return deleted; return deleted;

View File

@ -11,6 +11,7 @@ import me.filoghost.holographicdisplays.core.hologram.StandardHologram;
import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine; import me.filoghost.holographicdisplays.core.hologram.StandardHologramLine;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException; import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager;
import org.bukkit.World; import org.bukkit.World;
public abstract class BaseHologramLine extends BaseHologramComponent implements StandardHologramLine { public abstract class BaseHologramLine extends BaseHologramComponent implements StandardHologramLine {
@ -33,6 +34,10 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
return hologram.getNMSManager(); return hologram.getNMSManager();
} }
protected final PlaceholdersManager getPlaceholderManager() {
return hologram.getPlaceholderManager();
}
@Override @Override
public final void respawn(World world, double x, double y, double z) { public final void respawn(World world, double x, double y, double z) {
Preconditions.notNull(world, "world"); Preconditions.notNull(world, "world");

View File

@ -9,7 +9,6 @@ import me.filoghost.holographicdisplays.core.hologram.StandardTextLine;
import me.filoghost.holographicdisplays.core.nms.SpawnFailedException; import me.filoghost.holographicdisplays.core.nms.SpawnFailedException;
import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand; import me.filoghost.holographicdisplays.core.nms.entity.NMSArmorStand;
import me.filoghost.holographicdisplays.core.placeholder.RelativePlaceholder; import me.filoghost.holographicdisplays.core.placeholder.RelativePlaceholder;
import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager;
import org.bukkit.World; import org.bukkit.World;
import java.util.ArrayList; import java.util.ArrayList;
@ -41,12 +40,12 @@ public abstract class BaseTextLine extends BaseTouchableLine implements Standard
if (text != null && !text.isEmpty()) { if (text != null && !text.isEmpty()) {
textEntity.setCustomNameNMS(text); textEntity.setCustomNameNMS(text);
if (isAllowPlaceholders()) { if (isAllowPlaceholders()) {
PlaceholdersManager.trackIfNecessary(this); getPlaceholderManager().trackIfNecessary(this);
} }
} else { } else {
textEntity.setCustomNameNMS(""); // It will not appear textEntity.setCustomNameNMS(""); // It will not appear
if (isAllowPlaceholders()) { if (isAllowPlaceholders()) {
PlaceholdersManager.untrack(this); getPlaceholderManager().untrack(this);
} }
} }
} }
@ -72,7 +71,7 @@ public abstract class BaseTextLine extends BaseTouchableLine implements Standard
} }
if (isAllowPlaceholders()) { if (isAllowPlaceholders()) {
PlaceholdersManager.trackIfNecessary(this); getPlaceholderManager().trackIfNecessary(this);
} }
} }

View File

@ -8,6 +8,7 @@ package me.filoghost.holographicdisplays.object.internal;
import me.filoghost.holographicdisplays.HolographicDisplays; import me.filoghost.holographicdisplays.HolographicDisplays;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.object.base.BaseHologram; import me.filoghost.holographicdisplays.object.base.BaseHologram;
import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -17,8 +18,8 @@ public class InternalHologram extends BaseHologram<InternalHologramLine> {
private final String name; private final String name;
protected InternalHologram(Location source, String name, NMSManager nmsManager) { protected InternalHologram(Location source, String name, NMSManager nmsManager, PlaceholdersManager placeholderManager) {
super(source, nmsManager); super(source, nmsManager, placeholderManager);
this.name = name; this.name = name;
} }

View File

@ -7,18 +7,21 @@ package me.filoghost.holographicdisplays.object.internal;
import me.filoghost.holographicdisplays.core.nms.NMSManager; import me.filoghost.holographicdisplays.core.nms.NMSManager;
import me.filoghost.holographicdisplays.object.base.BaseHologramManager; import me.filoghost.holographicdisplays.object.base.BaseHologramManager;
import me.filoghost.holographicdisplays.placeholder.PlaceholdersManager;
import org.bukkit.Location; import org.bukkit.Location;
public class InternalHologramManager extends BaseHologramManager<InternalHologram> { public class InternalHologramManager extends BaseHologramManager<InternalHologram> {
private final NMSManager nmsManager; private final NMSManager nmsManager;
private final PlaceholdersManager placeholderManager;
public InternalHologramManager(NMSManager nmsManager) { public InternalHologramManager(NMSManager nmsManager, PlaceholdersManager placeholderManager) {
this.nmsManager = nmsManager; this.nmsManager = nmsManager;
this.placeholderManager = placeholderManager;
} }
public InternalHologram createHologram(Location source, String name) { public InternalHologram createHologram(Location source, String name) {
InternalHologram hologram = new InternalHologram(source, name, nmsManager); InternalHologram hologram = new InternalHologram(source, name, nmsManager, placeholderManager);
super.addHologram(hologram); super.addHologram(hologram);
return hologram; return hologram;
} }

View File

@ -24,9 +24,9 @@ public class AnimationsRegistry {
private static final String SPEED_PREFIX = "speed:"; private static final String SPEED_PREFIX = "speed:";
private static final Map<String, Placeholder> animationsByFilename = new HashMap<>(); private final Map<String, Placeholder> animationsByFilename = new HashMap<>();
public static void loadAnimations(ConfigManager configManager, ErrorCollector errorCollector) throws IOException, ConfigSaveException { public void loadAnimations(ConfigManager configManager, ErrorCollector errorCollector) throws IOException, ConfigSaveException {
animationsByFilename.clear(); animationsByFilename.clear();
Path animationFolder = configManager.getAnimationsFolder(); Path animationFolder = configManager.getAnimationsFolder();
@ -41,7 +41,7 @@ public class AnimationsRegistry {
} }
} }
private static void readAnimationFile(Path file, ErrorCollector errorCollector) { private void readAnimationFile(Path file, ErrorCollector errorCollector) {
String fileName = file.getFileName().toString(); String fileName = file.getFileName().toString();
try { try {
@ -89,11 +89,11 @@ public class AnimationsRegistry {
} }
public static Map<String, Placeholder> getAnimationsByFilename() { public Map<String, Placeholder> getAnimationsByFilename() {
return animationsByFilename; return animationsByFilename;
} }
public static Placeholder getAnimation(String name) { public Placeholder getAnimation(String name) {
return animationsByFilename.get(name); return animationsByFilename.get(name);
} }

View File

@ -26,9 +26,6 @@ import java.util.regex.Pattern;
public class PlaceholdersManager { public class PlaceholdersManager {
private static long elapsedTenthsOfSecond;
protected static final Set<DynamicLineData> linesToUpdate = new HashSet<>();
private static final Pattern BUNGEE_ONLINE_PATTERN = makePlaceholderWithArgsPattern("online"); private static final Pattern BUNGEE_ONLINE_PATTERN = makePlaceholderWithArgsPattern("online");
private static final Pattern BUNGEE_MAX_PATTERN = makePlaceholderWithArgsPattern("max_players"); private static final Pattern BUNGEE_MAX_PATTERN = makePlaceholderWithArgsPattern("max_players");
private static final Pattern BUNGEE_MOTD_PATTERN = makePlaceholderWithArgsPattern("motd"); private static final Pattern BUNGEE_MOTD_PATTERN = makePlaceholderWithArgsPattern("motd");
@ -36,24 +33,26 @@ public class PlaceholdersManager {
private static final Pattern BUNGEE_STATUS_PATTERN = makePlaceholderWithArgsPattern("status"); private static final Pattern BUNGEE_STATUS_PATTERN = makePlaceholderWithArgsPattern("status");
private static final Pattern ANIMATION_PATTERN = makePlaceholderWithArgsPattern("animation"); private static final Pattern ANIMATION_PATTERN = makePlaceholderWithArgsPattern("animation");
private static final Pattern WORLD_PATTERN = makePlaceholderWithArgsPattern("world"); private static final Pattern WORLD_PATTERN = makePlaceholderWithArgsPattern("world");
private static BungeeServerTracker bungeeServerTracker;
private static Pattern makePlaceholderWithArgsPattern(String prefix) { private final PlaceholdersRegistry placeholderRegistry;
return Pattern.compile("(\\{" + Pattern.quote(prefix) + ":)(.+?)(\\})"); private final AnimationsRegistry animationRegistry;
} private final BungeeServerTracker bungeeServerTracker;
protected final Set<DynamicLineData> linesToUpdate;
private static String extractArgumentFromPlaceholder(Matcher matcher) { private long elapsedTenthsOfSecond;
return matcher.group(2).trim();
}
public static void startRefreshTask(Plugin plugin, BungeeServerTracker bungeeServerTracker) {
PlaceholdersManager.bungeeServerTracker = bungeeServerTracker;
public PlaceholdersManager(BungeeServerTracker bungeeServerTracker, AnimationsRegistry animationRegistry) {
this.placeholderRegistry = new PlaceholdersRegistry(this);
this.animationRegistry = animationRegistry;
this.bungeeServerTracker = bungeeServerTracker;
this.linesToUpdate = new HashSet<>();
placeholderRegistry.addDefaultPlaceholders();
}
public void startRefreshTask(Plugin plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> { Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
for (Placeholder placeholder : PlaceholdersRegistry.getPlaceholders()) { for (Placeholder placeholder : placeholderRegistry.getPlaceholders()) {
if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) { if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) {
try { try {
placeholder.update(); placeholder.update();
@ -63,7 +62,7 @@ public class PlaceholdersManager {
} }
} }
for (Placeholder placeholder : AnimationsRegistry.getAnimationsByFilename().values()) { for (Placeholder placeholder : animationRegistry.getAnimationsByFilename().values()) {
if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) { if (elapsedTenthsOfSecond % placeholder.getTenthsToRefresh() == 0) {
placeholder.update(); placeholder.update();
} }
@ -88,11 +87,11 @@ public class PlaceholdersManager {
} }
public static void untrackAll() { public void untrackAll() {
linesToUpdate.clear(); linesToUpdate.clear();
} }
public static void untrack(StandardTextLine line) { public void untrack(StandardTextLine line) {
Iterator<DynamicLineData> iter = linesToUpdate.iterator(); Iterator<DynamicLineData> iter = linesToUpdate.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
DynamicLineData data = iter.next(); DynamicLineData data = iter.next();
@ -103,7 +102,7 @@ public class PlaceholdersManager {
} }
} }
public static void trackIfNecessary(StandardTextLine line) { public void trackIfNecessary(StandardTextLine line) {
String text = line.getText(); String text = line.getText();
if (text == null || text.isEmpty()) { if (text == null || text.isEmpty()) {
return; return;
@ -124,7 +123,7 @@ public class PlaceholdersManager {
Matcher matcher; Matcher matcher;
for (Placeholder placeholder : PlaceholdersRegistry.getPlaceholders()) { for (Placeholder placeholder : placeholderRegistry.getPlaceholders()) {
if (text.contains(placeholder.getTextPlaceholder())) { if (text.contains(placeholder.getTextPlaceholder())) {
if (normalPlaceholders == null) { if (normalPlaceholders == null) {
normalPlaceholders = new HashSet<>(); normalPlaceholders = new HashSet<>();
@ -266,7 +265,7 @@ public class PlaceholdersManager {
matcher = ANIMATION_PATTERN.matcher(text); matcher = ANIMATION_PATTERN.matcher(text);
while (matcher.find()) { while (matcher.find()) {
String fileName = extractArgumentFromPlaceholder(matcher); String fileName = extractArgumentFromPlaceholder(matcher);
Placeholder animation = AnimationsRegistry.getAnimation(fileName); Placeholder animation = animationRegistry.getAnimation(fileName);
// If exists... // If exists...
if (animation != null) { if (animation != null) {
@ -319,7 +318,7 @@ public class PlaceholdersManager {
} }
private static void updatePlaceholders(DynamicLineData lineData) { private void updatePlaceholders(DynamicLineData lineData) {
String oldCustomName = lineData.getEntity().getCustomNameStringNMS(); String oldCustomName = lineData.getEntity().getCustomNameStringNMS();
String newCustomName = lineData.getOriginalName(); String newCustomName = lineData.getOriginalName();
@ -347,4 +346,16 @@ public class PlaceholdersManager {
} }
} }
public PlaceholdersRegistry getRegistry() {
return placeholderRegistry;
}
private static Pattern makePlaceholderWithArgsPattern(String prefix) {
return Pattern.compile("(\\{" + Pattern.quote(prefix) + ":)(.+?)(\\})");
}
private static String extractArgumentFromPlaceholder(Matcher matcher) {
return matcher.group(2).trim();
}
} }

View File

@ -18,40 +18,17 @@ import java.util.Iterator;
import java.util.Set; import java.util.Set;
public class PlaceholdersRegistry { public class PlaceholdersRegistry {
private final PlaceholdersManager placeholderManager;
private final Set<Placeholder> placeholders;
private static final Set<Placeholder> placeholders = new HashSet<>(); public PlaceholdersRegistry(PlaceholdersManager placeholderManager) {
this.placeholderManager = placeholderManager;
// Register the default placeholders statically. this.placeholders = new HashSet<>();
static {
register(new Placeholder(HolographicDisplays.getInstance(), "{online}", 1.0, () -> {
return String.valueOf(Bukkit.getOnlinePlayers().size());
}));
register(new Placeholder(HolographicDisplays.getInstance(), "{max_players}", 10.0, () -> {
return String.valueOf(Bukkit.getMaxPlayers());
}));
register(new Placeholder(HolographicDisplays.getInstance(), "{motd}", 60.0, () -> {
return Bukkit.getMotd();
}));
register(new Placeholder(HolographicDisplays.getInstance(), "{time}", 0.9, () -> {
return Configuration.timeFormat.format(Instant.now());
}));
register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.toStringList(
ChatColor.RED,
ChatColor.GOLD,
ChatColor.YELLOW,
ChatColor.GREEN,
ChatColor.AQUA,
ChatColor.LIGHT_PURPLE
))));
} }
public static boolean register(Placeholder placeholder) { public boolean register(Placeholder placeholder) {
if (placeholders.contains(placeholder)) { if (placeholders.contains(placeholder)) {
return false; return false;
} }
@ -60,7 +37,7 @@ public class PlaceholdersRegistry {
return true; return true;
} }
public static Set<String> getTextPlaceholdersByPlugin(Plugin plugin) { public Set<String> getTextPlaceholdersByPlugin(Plugin plugin) {
Set<String> found = new HashSet<>(); Set<String> found = new HashSet<>();
for (Placeholder placeholder : placeholders) { for (Placeholder placeholder : placeholders) {
@ -72,7 +49,7 @@ public class PlaceholdersRegistry {
return found; return found;
} }
public static boolean unregister(Plugin plugin, String textPlaceholder) { public boolean unregister(Plugin plugin, String textPlaceholder) {
Iterator<Placeholder> iter = placeholders.iterator(); Iterator<Placeholder> iter = placeholders.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
@ -81,7 +58,7 @@ public class PlaceholdersRegistry {
if (placeholder.getOwner().equals(plugin) && placeholder.getTextPlaceholder().equals(textPlaceholder)) { if (placeholder.getOwner().equals(plugin) && placeholder.getTextPlaceholder().equals(textPlaceholder)) {
iter.remove(); iter.remove();
for (DynamicLineData data : PlaceholdersManager.linesToUpdate) { for (DynamicLineData data : placeholderManager.linesToUpdate) {
data.getPlaceholders().remove(placeholder); data.getPlaceholders().remove(placeholder);
} }
@ -92,8 +69,35 @@ public class PlaceholdersRegistry {
return false; return false;
} }
protected static Set<Placeholder> getPlaceholders() { protected Set<Placeholder> getPlaceholders() {
return placeholders; return placeholders;
} }
public void addDefaultPlaceholders() {
register(new Placeholder(HolographicDisplays.getInstance(), "{online}", 1.0, () -> {
return String.valueOf(Bukkit.getOnlinePlayers().size());
}));
register(new Placeholder(HolographicDisplays.getInstance(), "{max_players}", 10.0, () -> {
return String.valueOf(Bukkit.getMaxPlayers());
}));
register(new Placeholder(HolographicDisplays.getInstance(), "{motd}", 60.0, () -> {
return Bukkit.getMotd();
}));
register(new Placeholder(HolographicDisplays.getInstance(), "{time}", 0.9, () -> {
return Configuration.timeFormat.format(Instant.now());
}));
register(new Placeholder(HolographicDisplays.getInstance(), "&u", 0.2, new CyclicPlaceholderReplacer(Utils.toStringList(
ChatColor.RED,
ChatColor.GOLD,
ChatColor.YELLOW,
ChatColor.GREEN,
ChatColor.AQUA,
ChatColor.LIGHT_PURPLE
))));
}
} }