Added registerExpansion method. Placeholder map interaction methods now return immutable objects

This commit is contained in:
extendedclip 2018-04-20 22:11:54 -04:00
parent 796136982a
commit 1a905f88a9

View File

@ -20,13 +20,14 @@
*/ */
package me.clip.placeholderapi; package me.clip.placeholderapi;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.clip.placeholderapi.events.ExpansionRegisterEvent;
import me.clip.placeholderapi.events.ExpansionUnregisterEvent; import me.clip.placeholderapi.events.ExpansionUnregisterEvent;
import me.clip.placeholderapi.events.PlaceholderHookUnloadEvent;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Relational; import me.clip.placeholderapi.expansion.Relational;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -38,226 +39,232 @@ import java.util.stream.Collectors;
import static me.clip.placeholderapi.util.Msg.color; import static me.clip.placeholderapi.util.Msg.color;
public class PlaceholderAPI { public class PlaceholderAPI {
private PlaceholderAPI() {
}
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
private static final Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]");
private static final Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern.compile("[%](rel_)([^%]+)[%]");
private static final Map<String, PlaceholderHook> placeholders = new HashMap<>();
/**
* check if a specific placeholder identifier is currently registered
* @param identifier to check
* @return true if identifier is already registered
*/
public static boolean isRegistered(String identifier) {
return !placeholders.isEmpty() && getRegisteredIdentifiers().stream().filter(id -> id.equalsIgnoreCase(identifier)).findFirst().orElse(null) != null;
}
/**
* Register a new placeholder hook
* @param identifier Identifier of the placeholder -> "%(identifier)_(args...)%
*
* @param placeholderHook implementing class that contains the onPlaceholderRequest method which is called when a value is needed for the specific placeholder
* @return true if the hook was successfully registered, false if there is already a hook registered for the specified identifier
*/
public static boolean registerPlaceholderHook(String identifier, PlaceholderHook placeholderHook) {
Validate.notNull(identifier, "Identifier can not be null");
Validate.notNull(placeholderHook, "Placeholderhook can not be null");
if (isRegistered(identifier)) return false;
placeholders.put(identifier.toLowerCase(), placeholderHook);
return true;
}
/**
* unregister a placeholder hook by identifier
* @param identifier the identifier for the placeholder hook to unregister
* @return true if the placeholder hook was successfully unregistered, false if there was no placeholder hook registered for the identifier specified
*/
public static boolean unregisterPlaceholderHook(String identifier) {
Validate.notNull(identifier, "Identifier can not be null");
return placeholders.remove(identifier.toLowerCase()) != null;
}
/**
* Get all registered placeholder identifiers
* @return all registered placeholder identifiers
*/
public static Set<String> getRegisteredIdentifiers() {
if (placeholders.isEmpty()) return new HashSet<>();
return new HashSet<>(placeholders.keySet());
}
/** private PlaceholderAPI() {
* Get map of registered placeholders
* @return copy of the internal placeholder map
*/
public static Map<String, PlaceholderHook> getPlaceholders() {
return new HashMap<>(placeholders);
}
public static Set<PlaceholderExpansion> getExpansions() {
return getPlaceholders().values().stream().filter(PlaceholderExpansion.class::isInstance).map(PlaceholderExpansion.class::cast).collect(Collectors.toCollection(HashSet::new));
} }
/** private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
* check if a String contains any PlaceholderAPI placeholders private static final Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]");
* @param text String to check private static final Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern.compile("[%](rel_)([^%]+)[%]");
* @return true if String contains any registered placeholder identifiers, false otherwise private static final Map<String, PlaceholderHook> placeholders = new HashMap<>();
*/
public static boolean containsPlaceholders(String text) {
return text != null && PLACEHOLDER_PATTERN.matcher(text).find();
}
/**
* check if a String contains any PlaceholderAPI bracket placeholders
* @param text String to check
* @return true if String contains any registered placeholder identifiers, false otherwise
*/
public static boolean containsBracketPlaceholders(String text) {
return text != null && BRACKET_PLACEHOLDER_PATTERN.matcher(text).find();
}
/**
* set placeholders in the list<String> text provided
* placeholders are matched with the pattern {<placeholder>} when set with this method
* @param p Player to parse the placeholders for
* @param text text to set the placeholder values in
* @return modified list with all placeholders set to the corresponding values
*/
public static List<String> setBracketPlaceholders(Player p, List<String> text) {
return setPlaceholders(p, text, BRACKET_PLACEHOLDER_PATTERN);
}
/** /**
* set placeholders in the list<String> text provided * check if a specific placeholder identifier is currently registered
* placeholders are matched with the pattern %(identifier)_(params)>% when set with this method *
* @param p Player to parse the placeholders for * @param identifier to check
* @param text text to parse the placeholder values in * @return true if identifier is already registered
* @return modified list with all placeholders set to the corresponding values */
*/ public static boolean isRegistered(String identifier) {
public static List<String> setPlaceholders(Player p, List<String> text) { return getRegisteredIdentifiers().stream().filter(id -> id.equalsIgnoreCase(identifier)).findFirst().orElse(null) != null;
return setPlaceholders(p, text, PLACEHOLDER_PATTERN); }
}
/**
* Register a new placeholder hook
*
* @param identifier Identifier of the placeholder -> "%(identifier)_(args...)%
* @param placeholderHook implementing class that contains the onPlaceholderRequest method which is called when a value is needed for the specific placeholder
* @return true if the hook was successfully registered, false if there is already a hook registered for the specified identifier
*/
public static boolean registerPlaceholderHook(String identifier, PlaceholderHook placeholderHook) {
Validate.notNull(identifier, "Identifier can not be null");
Validate.notNull(placeholderHook, "Placeholderhook can not be null");
if (isRegistered(identifier)) return false;
placeholders.put(identifier.toLowerCase(), placeholderHook);
return true;
}
/**
* unregister a placeholder hook by identifier
*
* @param identifier the identifier for the placeholder hook to unregister
* @return true if the placeholder hook was successfully unregistered, false if there was no placeholder hook registered for the identifier specified
*/
public static boolean unregisterPlaceholderHook(String identifier) {
Validate.notNull(identifier, "Identifier can not be null");
return placeholders.remove(identifier.toLowerCase()) != null;
}
/**
* Get all registered placeholder identifiers
*
* @return all registered placeholder identifiers
*/
public static Set<String> getRegisteredIdentifiers() {
return ImmutableSet.copyOf(placeholders.keySet());
}
/**
* Get map of registered placeholders
*
* @return copy of the internal placeholder map
*/
public static Map<String, PlaceholderHook> getPlaceholders() {
return ImmutableMap.copyOf(placeholders);
}
public static Set<PlaceholderExpansion> getExpansions() {
Set<PlaceholderExpansion> set = getPlaceholders().values().stream().filter(PlaceholderExpansion.class::isInstance).map(PlaceholderExpansion.class::cast).collect(Collectors.toCollection(HashSet::new));
return ImmutableSet.copyOf(set);
}
/**
* check if a String contains any PlaceholderAPI placeholders
*
* @param text String to check
* @return true if String contains any registered placeholder identifiers, false otherwise
*/
public static boolean containsPlaceholders(String text) {
return text != null && PLACEHOLDER_PATTERN.matcher(text).find();
}
/**
* check if a String contains any PlaceholderAPI bracket placeholders
*
* @param text String to check
* @return true if String contains any registered placeholder identifiers, false otherwise
*/
public static boolean containsBracketPlaceholders(String text) {
return text != null && BRACKET_PLACEHOLDER_PATTERN.matcher(text).find();
}
/**
* set placeholders in the list<String> text provided
* placeholders are matched with the pattern {<placeholder>} when set with this method
*
* @param p Player to parse the placeholders for
* @param text text to set the placeholder values in
* @return modified list with all placeholders set to the corresponding values
*/
public static List<String> setBracketPlaceholders(Player p, List<String> text) {
return setPlaceholders(p, text, BRACKET_PLACEHOLDER_PATTERN);
}
/** /**
* set placeholders in the list<String> text provided * set placeholders in the list<String> text provided
* placeholders are matched with the pattern %(identifier)_(params)>% when set with this method * placeholders are matched with the pattern %(identifier)_(params)>% when set with this method
* @param p Player to parse the placeholders for *
* @param p Player to parse the placeholders for
* @param text text to parse the placeholder values in
* @return modified list with all placeholders set to the corresponding values
*/
public static List<String> setPlaceholders(Player p, List<String> text) {
return setPlaceholders(p, text, PLACEHOLDER_PATTERN);
}
/**
* set placeholders in the list<String> text provided
* placeholders are matched with the pattern %(identifier)_(params)>% when set with this method
*
* @param p Player to parse the placeholders for
* @param text text to parse the placeholder values in * @param text text to parse the placeholder values in
* @return modified list with all placeholders set to the corresponding values * @return modified list with all placeholders set to the corresponding values
*/ */
public static List<String> setPlaceholders(Player p, List<String> text, Pattern pattern) { public static List<String> setPlaceholders(Player p, List<String> text, Pattern pattern) {
if (text == null) return null; if (text == null) return null;
List<String> temp = new ArrayList<>(); return text.stream().map(line -> setPlaceholders(p, line, pattern)).collect(Collectors.toList());
text.forEach(line -> {
temp.add(setPlaceholders(p, line, pattern));
});
return temp;
} }
/** /**
* set placeholders in the text specified * set placeholders in the text specified
* placeholders are matched with the pattern {<placeholder>} when set with this method * placeholders are matched with the pattern {<placeholder>} when set with this method
*
* @param player Player to parse the placeholders for * @param player Player to parse the placeholders for
* @param text text to parse the placeholder values to * @param text text to parse the placeholder values to
* @return modified text with all placeholders set to the corresponding values * @return modified text with all placeholders set to the corresponding values
*/ */
public static String setBracketPlaceholders(Player player, String text) { public static String setBracketPlaceholders(Player player, String text) {
return setPlaceholders(player, text, BRACKET_PLACEHOLDER_PATTERN); return setPlaceholders(player, text, BRACKET_PLACEHOLDER_PATTERN);
} }
/**
* set placeholders in the text specified
* placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method
* @param player Player to parse the placeholders for
* @param text text to parse the placeholder values to
* @return text with all placeholders set to the corresponding values
*/
public static String setPlaceholders(Player player, String text) {
return setPlaceholders(player, text, PLACEHOLDER_PATTERN);
}
/** /**
* set placeholders in the text specified * set placeholders in the text specified
* placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method * placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method
*
* @param player Player to parse the placeholders for * @param player Player to parse the placeholders for
* @param text text to parse the placeholder values to * @param text text to parse the placeholder values to
* @return text with all placeholders set to the corresponding values
*/
public static String setPlaceholders(Player player, String text) {
return setPlaceholders(player, text, PLACEHOLDER_PATTERN);
}
/**
* set placeholders in the text specified
* placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method
*
* @param player Player to parse the placeholders for
* @param text text to parse the placeholder values to
* @param placeholderPattern the pattern to match placeholders to. Capture group 1 must contain an underscore separating the identifier from the params * @param placeholderPattern the pattern to match placeholders to. Capture group 1 must contain an underscore separating the identifier from the params
* @return text with all placeholders set to the corresponding values * @return text with all placeholders set to the corresponding values
*/ */
public static String setPlaceholders(Player player, String text, Pattern placeholderPattern) { public static String setPlaceholders(Player player, String text, Pattern placeholderPattern) {
if (text == null) return null; if (text == null) return null;
if (placeholders.isEmpty()) return color(text); if (placeholders.isEmpty()) return color(text);
Matcher m = placeholderPattern.matcher(text); Matcher m = placeholderPattern.matcher(text);
Map<String, PlaceholderHook> hooks = getPlaceholders(); Map<String, PlaceholderHook> hooks = getPlaceholders();
while (m.find()) { while (m.find()) {
String format = m.group(1); String format = m.group(1);
int index = format.indexOf("_"); int index = format.indexOf("_");
if (index <= 0 || index >= format.length()) continue; if (index <= 0 || index >= format.length()) continue;
String identifier = format.substring(0, index).toLowerCase(); String identifier = format.substring(0, index).toLowerCase();
String params = format.substring(index+1); String params = format.substring(index + 1);
if (hooks.containsKey(identifier)) { if (hooks.containsKey(identifier)) {
String value = hooks.get(identifier).onPlaceholderRequest(player, params); String value = hooks.get(identifier).onPlaceholderRequest(player, params);
if (value != null) { if (value != null) {
text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value)); text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value));
} }
} }
} }
return color(text); return color(text);
} }
/** /**
* set relational placeholders in the text specified * set relational placeholders in the text specified
* placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method * placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method
* @param one Player to compare *
* @param two Player to compare * @param one Player to compare
* @param text text to parse the placeholder values to * @param two Player to compare
* @return text with all relational placeholders set to the corresponding values * @param text text to parse the placeholder values to
*/ * @return text with all relational placeholders set to the corresponding values
public static List<String> setRelationalPlaceholders(Player one, Player two, List<String> text) { */
if (text == null) return null; public static List<String> setRelationalPlaceholders(Player one, Player two, List<String> text) {
List<String> temp = new ArrayList<>(); if (text == null) return null;
text.forEach(line -> { return text.stream().map(line -> setRelationalPlaceholders(one, two, line)).collect(Collectors.toList());
temp.add(setRelationalPlaceholders(one, two, line)); }
});
return temp; /**
} * set relational placeholders in the text specified
* placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method
/** *
* set relational placeholders in the text specified * @param one Player to compare
* placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method * @param two Player to compare
* @param one Player to compare * @param text text to parse the placeholder values to
* @param two Player to compare * @return text with all relational placeholders set to the corresponding values
* @param text text to parse the placeholder values to */
* @return text with all relational placeholders set to the corresponding values public static String setRelationalPlaceholders(Player one, Player two, String text) {
*/ if (text == null) return null;
public static String setRelationalPlaceholders(Player one, Player two, String text) { if (placeholders.isEmpty()) return color(text);
if (text == null) return null; Matcher m = RELATIONAL_PLACEHOLDER_PATTERN.matcher(text);
if (placeholders.isEmpty()) return color(text); Map<String, PlaceholderHook> hooks = getPlaceholders();
Matcher m = RELATIONAL_PLACEHOLDER_PATTERN.matcher(text); while (m.find()) {
Map<String, PlaceholderHook> hooks = getPlaceholders(); String format = m.group(2);
while (m.find()) { int index = format.indexOf("_");
String format = m.group(2); if (index <= 0 || index >= format.length()) continue;
int index = format.indexOf("_"); String identifier = format.substring(0, index).toLowerCase();
if (index <= 0 || index >= format.length()) continue; String params = format.substring(index + 1);
String identifier = format.substring(0, index).toLowerCase(); if (hooks.containsKey(identifier)) {
String params = format.substring(index+1); if (!(hooks.get(identifier) instanceof Relational)) {
if (hooks.containsKey(identifier)) { continue;
if (!(hooks.get(identifier) instanceof Relational)) { }
continue; Relational rel = (Relational) hooks.get(identifier);
} String value = rel.onPlaceholderRequest(one, two, params);
Relational rel = (Relational) hooks.get(identifier); if (value != null) {
String value = rel.onPlaceholderRequest(one, two, params); text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value));
if (value != null) { }
text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value)); }
} }
} return color(text);
} }
return color(text);
}
/** /**
* unregister ALL placeholder hooks that are currently registered * unregister ALL placeholder hooks that are currently registered
@ -274,7 +281,7 @@ public class PlaceholderAPI {
if (placeholders.isEmpty()) return; if (placeholders.isEmpty()) return;
getPlaceholders().forEach((key, value) -> { getPlaceholders().forEach((key, value) -> {
if (value instanceof PlaceholderExpansion) { if (value instanceof PlaceholderExpansion) {
PlaceholderExpansion ex = (PlaceholderExpansion) value; PlaceholderExpansion ex = (PlaceholderExpansion) value;
if (!ex.persist()) { if (!ex.persist()) {
unregisterExpansion(ex); unregisterExpansion(ex);
} }
@ -282,6 +289,14 @@ public class PlaceholderAPI {
}); });
} }
public static boolean registerExpansion(PlaceholderExpansion ex) {
if (registerPlaceholderHook(ex.getIdentifier(), ex)) {
Bukkit.getPluginManager().callEvent(new ExpansionRegisterEvent(ex));
return true;
}
return false;
}
public static boolean unregisterExpansion(PlaceholderExpansion ex) { public static boolean unregisterExpansion(PlaceholderExpansion ex) {
if (unregisterPlaceholderHook(ex.getIdentifier())) { if (unregisterPlaceholderHook(ex.getIdentifier())) {
Bukkit.getPluginManager().callEvent(new ExpansionUnregisterEvent(ex)); Bukkit.getPluginManager().callEvent(new ExpansionUnregisterEvent(ex));
@ -289,33 +304,36 @@ public class PlaceholderAPI {
} }
return false; return false;
} }
public static Pattern getPlaceholderPattern() {
return PLACEHOLDER_PATTERN;
}
public static Pattern getBracketPlaceholderPattern() {
return BRACKET_PLACEHOLDER_PATTERN;
}
public static Pattern getRelationalPlaceholderPattern() {
return RELATIONAL_PLACEHOLDER_PATTERN;
}
@Deprecated public static Pattern getPlaceholderPattern() {
public static Set<String> getRegisteredPlaceholderPlugins() { return PLACEHOLDER_PATTERN;
return getRegisteredIdentifiers(); }
}
@Deprecated public static Pattern getBracketPlaceholderPattern() {
public static Set<String> getExternalPlaceholderPlugins() { return BRACKET_PLACEHOLDER_PATTERN;
return null; }
}
@Deprecated public static Pattern getRelationalPlaceholderPattern() {
public static boolean registerPlaceholderHook(Plugin plugin, PlaceholderHook placeholderHook) { return RELATIONAL_PLACEHOLDER_PATTERN;
return plugin != null && registerPlaceholderHook(plugin.getName(), placeholderHook); }
}
@Deprecated @Deprecated
public static boolean unregisterPlaceholderHook(Plugin plugin) { public static Set<String> getRegisteredPlaceholderPlugins() {
return plugin != null && unregisterPlaceholderHook(plugin.getName()); return getRegisteredIdentifiers();
} }
@Deprecated
public static Set<String> getExternalPlaceholderPlugins() {
return null;
}
@Deprecated
public static boolean registerPlaceholderHook(Plugin plugin, PlaceholderHook placeholderHook) {
return plugin != null && registerPlaceholderHook(plugin.getName(), placeholderHook);
}
@Deprecated
public static boolean unregisterPlaceholderHook(Plugin plugin) {
return plugin != null && unregisterPlaceholderHook(plugin.getName());
}
} }