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
1 changed files with 237 additions and 219 deletions

View File

@ -20,13 +20,14 @@
*/
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.PlaceholderHookUnloadEvent;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Relational;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -49,17 +50,18 @@ public class 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 !placeholders.isEmpty() && 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 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
*/
@ -73,6 +75,7 @@ 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
*/
@ -83,27 +86,30 @@ public class PlaceholderAPI {
/**
* 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());
return ImmutableSet.copyOf(placeholders.keySet());
}
/**
* Get map of registered placeholders
*
* @return copy of the internal placeholder map
*/
public static Map<String, PlaceholderHook> getPlaceholders() {
return new HashMap<>(placeholders);
return ImmutableMap.copyOf(placeholders);
}
public static Set<PlaceholderExpansion> getExpansions() {
return 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);
}
/**
* check if a String contains any PlaceholderAPI placeholders
*
* @param text String to check
* @return true if String contains any registered placeholder identifiers, false otherwise
*/
@ -113,6 +119,7 @@ public class PlaceholderAPI {
/**
* 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
*/
@ -123,6 +130,7 @@ public class PlaceholderAPI {
/**
* 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
@ -134,6 +142,7 @@ public class PlaceholderAPI {
/**
* 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
@ -145,22 +154,20 @@ public class PlaceholderAPI {
/**
* 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(Player p, List<String> text, Pattern pattern) {
if (text == null) return null;
List<String> temp = new ArrayList<>();
text.forEach(line -> {
temp.add(setPlaceholders(p, line, pattern));
});
return temp;
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
@ -172,6 +179,7 @@ public class PlaceholderAPI {
/**
* 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
@ -183,6 +191,7 @@ public class PlaceholderAPI {
/**
* 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
@ -212,6 +221,7 @@ public class PlaceholderAPI {
/**
* 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
* @param text text to parse the placeholder values to
@ -219,16 +229,13 @@ public class PlaceholderAPI {
*/
public static List<String> setRelationalPlaceholders(Player one, Player two, List<String> text) {
if (text == null) return null;
List<String> temp = new ArrayList<>();
text.forEach(line -> {
temp.add(setRelationalPlaceholders(one, two, line));
});
return temp;
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
*
* @param one Player to compare
* @param two Player to compare
* @param text text to parse the placeholder values to
@ -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) {
if (unregisterPlaceholderHook(ex.getIdentifier())) {
Bukkit.getPluginManager().callEvent(new ExpansionUnregisterEvent(ex));
@ -306,14 +321,17 @@ public class PlaceholderAPI {
public static Set<String> getRegisteredPlaceholderPlugins() {
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());