Fix wrong placeholder warning

This commit is contained in:
filoghost 2022-04-25 17:02:20 +02:00
parent da25d050ab
commit c6a14285a0

View File

@ -120,31 +120,36 @@ public class PlaceholderRegistry {
public void registerLegacyPlaceholder( public void registerLegacyPlaceholder(
Plugin plugin, Plugin plugin,
String textPlaceholder, String legacyTextPlaceholder,
int refreshIntervalTicks, int refreshIntervalTicks,
GlobalPlaceholderReplaceFunction replaceFunction) { GlobalPlaceholderReplaceFunction replaceFunction) {
String identifier = convertToNewFormat(textPlaceholder); String newIdentifier = convertLegacyPlaceholderToNewIdentifier(legacyTextPlaceholder);
if (!identifier.equals(textPlaceholder)) {
Log.warning("The plugin " + plugin.getName() + " registered the placeholder " + textPlaceholder // If the whole legacy placeholder is used as identifier, it was not compliant with the new format.
+ " with the old v2 API, but it doesn't comply with the new format. In order to display it," // If it was compliant, the curly braces surrounding the legacy placeholder would be removed for
+ " you must use {" + textPlaceholder + "} instead."); // determining the new identifier, which would be different from the legacy placeholder.
if (newIdentifier.equals(legacyTextPlaceholder)) {
Log.warning("The plugin " + plugin.getName() + " registered the placeholder " + legacyTextPlaceholder
+ " with the old v2 API, but is not compliant with the new format. In order to display it,"
+ " you must use {" + newIdentifier + "} instead.");
} }
GlobalPlaceholder placeholder = new SimpleGlobalPlaceholder(refreshIntervalTicks, replaceFunction); GlobalPlaceholder placeholder = new SimpleGlobalPlaceholder(refreshIntervalTicks, replaceFunction);
GlobalPlaceholderFactory placeholderFactory = (String argument) -> placeholder; GlobalPlaceholderFactory placeholderFactory = (String argument) -> placeholder;
LegacyGlobalPlaceholderExpansion expansion = new LegacyGlobalPlaceholderExpansion( LegacyGlobalPlaceholderExpansion expansion = new LegacyGlobalPlaceholderExpansion(
plugin, plugin,
identifier, newIdentifier,
placeholderFactory, placeholderFactory,
textPlaceholder); legacyTextPlaceholder);
legacyPlaceholderExpansions.put(new CaseInsensitiveString(identifier), new PluginName(plugin), expansion); legacyPlaceholderExpansions.put(new CaseInsensitiveString(newIdentifier), new PluginName(plugin), expansion);
version.incrementAndGet(); version.incrementAndGet();
} }
public void unregisterLegacyPlaceholder(Plugin plugin, String textPlaceholder) { public void unregisterLegacyPlaceholder(Plugin plugin, String legacyTextPlaceholder) {
String identifier = convertToNewFormat(textPlaceholder); String newIdentifier = convertLegacyPlaceholderToNewIdentifier(legacyTextPlaceholder);
legacyPlaceholderExpansions.remove(new CaseInsensitiveString(identifier), new PluginName(plugin)); legacyPlaceholderExpansions.remove(new CaseInsensitiveString(newIdentifier), new PluginName(plugin));
version.incrementAndGet(); version.incrementAndGet();
} }
@ -155,25 +160,24 @@ public class PlaceholderRegistry {
version.incrementAndGet(); version.incrementAndGet();
} }
public boolean isRegisteredLegacyPlaceholder(Plugin plugin, String textPlaceholder) { public boolean isRegisteredLegacyPlaceholder(Plugin plugin, String legacyTextPlaceholder) {
String identifier = convertToNewFormat(textPlaceholder); String newIdentifier = convertLegacyPlaceholderToNewIdentifier(legacyTextPlaceholder);
return legacyPlaceholderExpansions.contains(new CaseInsensitiveString(identifier), new PluginName(plugin)); return legacyPlaceholderExpansions.contains(new CaseInsensitiveString(newIdentifier), new PluginName(plugin));
} }
public Collection<LegacyGlobalPlaceholderExpansion> getRegisteredLegacyPlaceholders(Plugin plugin) { public Collection<LegacyGlobalPlaceholderExpansion> getRegisteredLegacyPlaceholders(Plugin plugin) {
return legacyPlaceholderExpansions.column(new PluginName(plugin)).values(); return legacyPlaceholderExpansions.column(new PluginName(plugin)).values();
} }
private String convertToNewFormat(String textPlaceholder) { private String convertLegacyPlaceholderToNewIdentifier(String legacyTextPlaceholder) {
String identifier; if (legacyTextPlaceholder.startsWith("{") && legacyTextPlaceholder.endsWith("}")) {
if (textPlaceholder.startsWith("{") && textPlaceholder.endsWith("}")) { // The legacy placeholder has correct format, remove the curly braces to get the identifier
// The placeholder already had the correct format, remove the curly braces return legacyTextPlaceholder.substring(1, legacyTextPlaceholder.length() - 1);
identifier = textPlaceholder.substring(1, textPlaceholder.length() - 1);
} else { } else {
// The placeholder will be wrapped with curly braces to partially maintain compatibility // Partial compatibility: the whole legacy placeholder will be used as identifier,
identifier = textPlaceholder; // thus will be replaced only when surrounded by curly braces
return legacyTextPlaceholder;
} }
return identifier;
} }
} }