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;
import static me.clip.placeholderapi.util.Msg.color;
import com.google.common.collect.ImmutableMap;
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.ExpansionUnregisterEvent;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
@ -32,22 +42,14 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
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 {
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
@ -56,20 +58,26 @@ public class PlaceholderAPI {
* @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;
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
* @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) {
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;
if (isRegistered(identifier)) {
return false;
}
placeholders.put(identifier.toLowerCase(), placeholderHook);
return true;
}
@ -78,7 +86,8 @@ public class PlaceholderAPI {
* 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
* @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");
@ -104,7 +113,9 @@ public class PlaceholderAPI {
}
public static Set<PlaceholderExpansion> getExpansions() {
Set<PlaceholderExpansion> set = getPlaceholders().values().stream().filter(PlaceholderExpansion.class::isInstance).map(PlaceholderExpansion.class::cast).collect(Collectors.toCollection(HashSet::new));
Set<PlaceholderExpansion> set = getPlaceholders().values().stream()
.filter(PlaceholderExpansion.class::isInstance).map(PlaceholderExpansion.class::cast)
.collect(Collectors.toCollection(HashSet::new));
return ImmutableSet.copyOf(set);
}
@ -129,8 +140,8 @@ public class PlaceholderAPI {
}
/**
* set placeholders in the list<String> text provided
* placeholders are matched with the pattern {<placeholder>} when set with this method
* 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
@ -141,8 +152,8 @@ public class PlaceholderAPI {
}
/**
* set placeholders in the list<String> text provided
* placeholders are matched with the pattern %(identifier)_(params)>% when set with this method
* 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
@ -153,21 +164,24 @@ public class PlaceholderAPI {
}
/**
* set placeholders in the list<String> text provided
* placeholders are matched with the pattern %(identifier)_(params)>% when set with this method
* 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());
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
* 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
@ -178,8 +192,8 @@ public class PlaceholderAPI {
}
/**
* set placeholders in the text specified
* placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method
* 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
@ -190,29 +204,37 @@ public class PlaceholderAPI {
}
/**
* set placeholders in the text specified
* placeholders are matched with the pattern %<(identifier)_(params)>% when set with this method
* 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
*/
public static String setPlaceholders(OfflinePlayer player, String text, Pattern placeholderPattern) {
if (text == null) return null;
if (placeholders.isEmpty()) return color(text);
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;
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);
Object value = hooks.get(identifier).onPlaceholderRequest(player, params);
if (value != null) {
text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(value));
text = text.replaceAll(Pattern.quote(m.group()), Matcher.quoteReplacement(String.valueOf(value)));
}
}
}
@ -220,8 +242,8 @@ public class PlaceholderAPI {
}
/**
* 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 placeholders are matched with the pattern
* %<rel_(identifier)_(params)>% when set with this method
*
* @param one Player to compare
* @param two Player to compare
@ -229,13 +251,16 @@ public class PlaceholderAPI {
* @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;
return text.stream().map(line -> setRelationalPlaceholders(one, two, line)).collect(Collectors.toList());
if (text == null) {
return null;
}
return text.stream().map(line -> setRelationalPlaceholders(one, two, line))
.collect(Collectors.toList());
}
/**
* 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 placeholders are matched with the pattern
* %<rel_(identifier)_(params)>% when set with this method
*
* @param one Player to compare
* @param two Player to compare
@ -243,14 +268,20 @@ public class PlaceholderAPI {
* @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;
if (placeholders.isEmpty()) return color(text);
if (text == null) {
return null;
}
if (placeholders.isEmpty()) {
return color(text);
}
Matcher m = RELATIONAL_PLACEHOLDER_PATTERN.matcher(text);
Map<String, PlaceholderHook> hooks = getPlaceholders();
while (m.find()) {
String format = m.group(2);
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 params = format.substring(index + 1);
if (hooks.containsKey(identifier)) {
@ -279,7 +310,9 @@ public class PlaceholderAPI {
* unregister all expansions provided by PlaceholderAPI
*/
public static void unregisterAllProvidedExpansions() {
if (placeholders.isEmpty()) return;
if (placeholders.isEmpty()) {
return;
}
getPlaceholders().forEach((key, value) -> {
if (value instanceof PlaceholderExpansion) {
PlaceholderExpansion ex = (PlaceholderExpansion) value;

View File

@ -31,7 +31,7 @@ public abstract class PlaceholderHook {
* @param params String passed to the hook to determine what value to return
* @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()) {
return onPlaceholderRequest((Player) p, params);
}