Return any Object with the new onPlaceholderRequest method.

This commit is contained in:
extendedclip 2018-05-31 15:14:25 -04:00
parent a50212e395
commit 25758ee653
2 changed files with 311 additions and 278 deletions

View File

@ -20,8 +20,18 @@
*/ */
package me.clip.placeholderapi; package me.clip.placeholderapi;
import static me.clip.placeholderapi.util.Msg.color;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import me.clip.placeholderapi.events.ExpansionRegisterEvent; import me.clip.placeholderapi.events.ExpansionRegisterEvent;
import me.clip.placeholderapi.events.ExpansionUnregisterEvent; import me.clip.placeholderapi.events.ExpansionUnregisterEvent;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
@ -32,309 +42,332 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
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<>();
private PlaceholderAPI() {
}
/**
* 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 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;
}
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]"); /**
private static final Pattern BRACKET_PLACEHOLDER_PATTERN = Pattern.compile("[{]([^{}]+)[}]"); * unregister a placeholder hook by identifier
private static final Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern.compile("[%](rel_)([^%]+)[%]"); *
private static final Map<String, PlaceholderHook> placeholders = new HashMap<>(); * @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;
}
/** /**
* check if a specific placeholder identifier is currently registered * Get all registered placeholder identifiers
* *
* @param identifier to check * @return all registered placeholder identifiers
* @return true if identifier is already registered */
*/ public static Set<String> getRegisteredIdentifiers() {
public static boolean isRegistered(String identifier) { return ImmutableSet.copyOf(placeholders.keySet());
return getRegisteredIdentifiers().stream().filter(id -> id.equalsIgnoreCase(identifier)).findFirst().orElse(null) != null; }
/**
* 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(OfflinePlayer p, List<String> text) {
return setPlaceholders(p, text, BRACKET_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
* @return modified list with all placeholders set to the corresponding values
*/
public static List<String> setPlaceholders(OfflinePlayer 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
* @return modified list with all placeholders set to the corresponding values
*/
public static List<String> setPlaceholders(OfflinePlayer p, List<String> text, Pattern pattern) {
if (text == null) {
return null;
} }
return text.stream().map(line -> setPlaceholders(p, line, pattern))
.collect(Collectors.toList());
}
/** /**
* Register a new placeholder hook * set placeholders in the text specified placeholders are matched with the pattern
* * {<placeholder>} when set with this method
* @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 * @param player Player to parse the placeholders for
* @return true if the hook was successfully registered, false if there is already a hook registered for the specified identifier * @param text text to parse the placeholder values to
*/ * @return modified text with all placeholders set to the corresponding values
public static boolean registerPlaceholderHook(String identifier, PlaceholderHook placeholderHook) { */
Validate.notNull(identifier, "Identifier can not be null"); public static String setBracketPlaceholders(OfflinePlayer player, String text) {
Validate.notNull(placeholderHook, "Placeholderhook can not be null"); return setPlaceholders(player, text, BRACKET_PLACEHOLDER_PATTERN);
if (isRegistered(identifier)) return false; }
placeholders.put(identifier.toLowerCase(), placeholderHook);
return true; /**
* 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(OfflinePlayer 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
* @return text with all placeholders set to the corresponding values
*/
public static String setPlaceholders(OfflinePlayer player, String text,
Pattern placeholderPattern) {
if (text == null) {
return null;
} }
if (placeholders.isEmpty()) {
/** return color(text);
* 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;
} }
Matcher m = placeholderPattern.matcher(text);
/** Map<String, PlaceholderHook> hooks = getPlaceholders();
* Get all registered placeholder identifiers while (m.find()) {
* String format = m.group(1);
* @return all registered placeholder identifiers int index = format.indexOf("_");
*/ if (index <= 0 || index >= format.length()) {
public static Set<String> getRegisteredIdentifiers() { continue;
return ImmutableSet.copyOf(placeholders.keySet()); }
} String identifier = format.substring(0, index).toLowerCase();
String params = format.substring(index + 1);
/** if (hooks.containsKey(identifier)) {
* Get map of registered placeholders Object value = hooks.get(identifier).onPlaceholderRequest(player, params);
* if (value != null) {
* @return copy of the internal placeholder map text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(String.valueOf(value)));
*/
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(OfflinePlayer p, List<String> text) {
return setPlaceholders(p, text, BRACKET_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
* @return modified list with all placeholders set to the corresponding values
*/
public static List<String> setPlaceholders(OfflinePlayer 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
* @return modified list with all placeholders set to the corresponding values
*/
public static List<String> setPlaceholders(OfflinePlayer p, List<String> text, Pattern pattern) {
if (text == null) return null;
return text.stream().map(line -> setPlaceholders(p, line, pattern)).collect(Collectors.toList());
}
/**
* set placeholders in the text specified
* placeholders are matched with the pattern {<placeholder>} when set with this method
*
* @param player Player to parse the placeholders for
* @param text text to parse the placeholder values to
* @return modified text with all placeholders set to the corresponding values
*/
public static String setBracketPlaceholders(OfflinePlayer player, String text) {
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(OfflinePlayer 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
* @return text with all placeholders set to the corresponding values
*/
public static String setPlaceholders(OfflinePlayer player, String text, Pattern placeholderPattern) {
if (text == null) return null;
if (placeholders.isEmpty()) return color(text);
Matcher m = placeholderPattern.matcher(text);
Map<String, PlaceholderHook> hooks = getPlaceholders();
while (m.find()) {
String format = m.group(1);
int index = format.indexOf("_");
if (index <= 0 || index >= format.length()) continue;
String identifier = format.substring(0, index).toLowerCase();
String params = format.substring(index + 1);
if (hooks.containsKey(identifier)) {
String value = hooks.get(identifier).onPlaceholderRequest(player, params);
if (value != null) {
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
* placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method * %<rel_(identifier)_(params)>% when set with this method
* *
* @param one Player to compare * @param one Player to compare
* @param two Player to compare * @param two Player to compare
* @param text text to parse the placeholder values to * @param text text to parse the placeholder values to
* @return text with all relational placeholders set to the corresponding values * @return text with all relational placeholders set to the corresponding values
*/ */
public static List<String> setRelationalPlaceholders(Player one, Player two, List<String> text) { public static List<String> setRelationalPlaceholders(Player one, Player two, List<String> text) {
if (text == null) return null; if (text == null) {
return text.stream().map(line -> setRelationalPlaceholders(one, two, line)).collect(Collectors.toList()); return null;
} }
return text.stream().map(line -> setRelationalPlaceholders(one, two, line))
.collect(Collectors.toList());
}
/** /**
* set relational placeholders in the text specified * set relational placeholders in the text specified placeholders are matched with the pattern
* placeholders are matched with the pattern %<rel_(identifier)_(params)>% when set with this method * %<rel_(identifier)_(params)>% when set with this method
* *
* @param one Player to compare * @param one Player to compare
* @param two Player to compare * @param two Player to compare
* @param text text to parse the placeholder values to * @param text text to parse the placeholder values to
* @return text with all relational placeholders set to the corresponding values * @return text with all relational placeholders set to the corresponding values
*/ */
public static String setRelationalPlaceholders(Player one, Player two, String text) { public static String setRelationalPlaceholders(Player one, Player two, String text) {
if (text == null) return null; if (text == null) {
if (placeholders.isEmpty()) return color(text); return null;
Matcher m = RELATIONAL_PLACEHOLDER_PATTERN.matcher(text); }
Map<String, PlaceholderHook> hooks = getPlaceholders(); if (placeholders.isEmpty()) {
while (m.find()) { return color(text);
String format = m.group(2); }
int index = format.indexOf("_"); Matcher m = RELATIONAL_PLACEHOLDER_PATTERN.matcher(text);
if (index <= 0 || index >= format.length()) continue; Map<String, PlaceholderHook> hooks = getPlaceholders();
String identifier = format.substring(0, index).toLowerCase(); while (m.find()) {
String params = format.substring(index + 1); String format = m.group(2);
if (hooks.containsKey(identifier)) { int index = format.indexOf("_");
if (!(hooks.get(identifier) instanceof Relational)) { if (index <= 0 || index >= format.length()) {
continue; continue;
} }
Relational rel = (Relational) hooks.get(identifier); String identifier = format.substring(0, index).toLowerCase();
String value = rel.onPlaceholderRequest(one, two, params); String params = format.substring(index + 1);
if (value != null) { if (hooks.containsKey(identifier)) {
text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value)); if (!(hooks.get(identifier) instanceof Relational)) {
} continue;
}
} }
return color(text); Relational rel = (Relational) hooks.get(identifier);
} String value = rel.onPlaceholderRequest(one, two, params);
if (value != null) {
/** text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value));
* unregister ALL placeholder hooks that are currently registered
*/
protected static void unregisterAll() {
unregisterAllProvidedExpansions();
placeholders.clear();
}
/**
* unregister all expansions provided by PlaceholderAPI
*/
public static void unregisterAllProvidedExpansions() {
if (placeholders.isEmpty()) return;
getPlaceholders().forEach((key, value) -> {
if (value instanceof PlaceholderExpansion) {
PlaceholderExpansion ex = (PlaceholderExpansion) value;
if (!ex.persist()) {
unregisterExpansion(ex);
}
}
});
}
public static boolean registerExpansion(PlaceholderExpansion ex) {
if (registerPlaceholderHook(ex.getIdentifier(), ex)) {
Bukkit.getPluginManager().callEvent(new ExpansionRegisterEvent(ex));
return true;
} }
return false; }
} }
return color(text);
}
public static boolean unregisterExpansion(PlaceholderExpansion ex) { /**
if (unregisterPlaceholderHook(ex.getIdentifier())) { * unregister ALL placeholder hooks that are currently registered
Bukkit.getPluginManager().callEvent(new ExpansionUnregisterEvent(ex)); */
return true; protected static void unregisterAll() {
unregisterAllProvidedExpansions();
placeholders.clear();
}
/**
* unregister all expansions provided by PlaceholderAPI
*/
public static void unregisterAllProvidedExpansions() {
if (placeholders.isEmpty()) {
return;
}
getPlaceholders().forEach((key, value) -> {
if (value instanceof PlaceholderExpansion) {
PlaceholderExpansion ex = (PlaceholderExpansion) value;
if (!ex.persist()) {
unregisterExpansion(ex);
} }
return false; }
} });
}
public static Pattern getPlaceholderPattern() { public static boolean registerExpansion(PlaceholderExpansion ex) {
return PLACEHOLDER_PATTERN; if (registerPlaceholderHook(ex.getIdentifier(), ex)) {
Bukkit.getPluginManager().callEvent(new ExpansionRegisterEvent(ex));
return true;
} }
return false;
}
public static Pattern getBracketPlaceholderPattern() { public static boolean unregisterExpansion(PlaceholderExpansion ex) {
return BRACKET_PLACEHOLDER_PATTERN; if (unregisterPlaceholderHook(ex.getIdentifier())) {
Bukkit.getPluginManager().callEvent(new ExpansionUnregisterEvent(ex));
return true;
} }
return false;
}
public static Pattern getRelationalPlaceholderPattern() { public static Pattern getPlaceholderPattern() {
return RELATIONAL_PLACEHOLDER_PATTERN; return PLACEHOLDER_PATTERN;
} }
@Deprecated public static Pattern getBracketPlaceholderPattern() {
public static Set<String> getRegisteredPlaceholderPlugins() { return BRACKET_PLACEHOLDER_PATTERN;
return getRegisteredIdentifiers(); }
}
@Deprecated public static Pattern getRelationalPlaceholderPattern() {
public static Set<String> getExternalPlaceholderPlugins() { return RELATIONAL_PLACEHOLDER_PATTERN;
return null; }
}
@Deprecated @Deprecated
public static boolean registerPlaceholderHook(Plugin plugin, PlaceholderHook placeholderHook) { public static Set<String> getRegisteredPlaceholderPlugins() {
return plugin != null && registerPlaceholderHook(plugin.getName(), placeholderHook); return getRegisteredIdentifiers();
} }
@Deprecated @Deprecated
public static boolean unregisterPlaceholderHook(Plugin plugin) { public static Set<String> getExternalPlaceholderPlugins() {
return plugin != null && unregisterPlaceholderHook(plugin.getName()); 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());
}
} }

View File

@ -31,7 +31,7 @@ public abstract class PlaceholderHook {
* @param params String passed to the hook to determine what value to return * @param params String passed to the hook to determine what value to return
* @return value for the requested player and params * @return value for the requested player and params
*/ */
public String onPlaceholderRequest(OfflinePlayer p, String params) { public Object onPlaceholderRequest(OfflinePlayer p, String params) {
if (p != null && p.isOnline()) { if (p != null && p.isOnline()) {
return onPlaceholderRequest((Player) p, params); return onPlaceholderRequest((Player) p, params);
} }