mirror of
				https://github.com/PlaceholderAPI/PlaceholderAPI
				synced 2025-10-31 06:12:28 +01:00 
			
		
		
		
	Added registerExpansion method. Placeholder map interaction methods now return immutable objects
This commit is contained in:
		| @@ -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; | ||||||
|  |  | ||||||
| @@ -49,17 +50,18 @@ public class PlaceholderAPI { | |||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * check if a specific placeholder identifier is currently registered |      * check if a specific placeholder identifier is currently registered | ||||||
|  |      * | ||||||
|      * @param identifier to check |      * @param identifier to check | ||||||
|      * @return true if identifier is already registered |      * @return true if identifier is already registered | ||||||
|      */ |      */ | ||||||
|     public static boolean isRegistered(String identifier) { |     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 |      * 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 |      * @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 |      * @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 |      * unregister a placeholder hook by identifier | ||||||
|  |      * | ||||||
|      * @param identifier the identifier for the placeholder hook to unregister |      * @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 | ||||||
|      */ |      */ | ||||||
| @@ -83,27 +86,30 @@ public class PlaceholderAPI { | |||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Get all registered placeholder identifiers |      * Get all registered placeholder identifiers | ||||||
|  |      * | ||||||
|      * @return all registered placeholder identifiers |      * @return all registered placeholder identifiers | ||||||
|      */ |      */ | ||||||
|     public static Set<String> getRegisteredIdentifiers() { |     public static Set<String> getRegisteredIdentifiers() { | ||||||
| 		if (placeholders.isEmpty()) return new HashSet<>(); |         return ImmutableSet.copyOf(placeholders.keySet()); | ||||||
| 		return new HashSet<>(placeholders.keySet()); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Get map of registered placeholders |      * Get map of registered placeholders | ||||||
|  |      * | ||||||
|      * @return copy of the internal placeholder map |      * @return copy of the internal placeholder map | ||||||
|      */ |      */ | ||||||
|     public static Map<String, PlaceholderHook> getPlaceholders() { |     public static Map<String, PlaceholderHook> getPlaceholders() { | ||||||
| 		return new HashMap<>(placeholders); |         return ImmutableMap.copyOf(placeholders); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static Set<PlaceholderExpansion> getExpansions() { |     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 |      * check if a String contains any PlaceholderAPI placeholders | ||||||
|  |      * | ||||||
|      * @param text String to check |      * @param text String to check | ||||||
|      * @return true if String contains any registered placeholder identifiers, false otherwise |      * @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 |      * check if a String contains any PlaceholderAPI bracket placeholders | ||||||
|  |      * | ||||||
|      * @param text String to check |      * @param text String to check | ||||||
|      * @return true if String contains any registered placeholder identifiers, false otherwise |      * @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 |      * set placeholders in the list<String> text provided | ||||||
|      * 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 p    Player to parse the placeholders for |      * @param p    Player to parse the placeholders for | ||||||
|      * @param text text to set the placeholder values in |      * @param text text to set 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 | ||||||
| @@ -134,6 +142,7 @@ public class PlaceholderAPI { | |||||||
|     /** |     /** | ||||||
|      * 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 |      * @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 | ||||||
| @@ -145,22 +154,20 @@ public class PlaceholderAPI { | |||||||
|     /** |     /** | ||||||
|      * 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 |      * @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 | ||||||
| @@ -172,6 +179,7 @@ public class PlaceholderAPI { | |||||||
|     /** |     /** | ||||||
|      * 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 |      * @return text with all placeholders set to the corresponding values | ||||||
| @@ -183,6 +191,7 @@ public class PlaceholderAPI { | |||||||
|     /** |     /** | ||||||
|      * 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 | ||||||
|      * @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 | ||||||
| @@ -198,7 +207,7 @@ public class PlaceholderAPI { | |||||||
|             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) { | ||||||
| @@ -212,6 +221,7 @@ public class PlaceholderAPI { | |||||||
|     /** |     /** | ||||||
|      * 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 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 | ||||||
| @@ -219,16 +229,13 @@ public class PlaceholderAPI { | |||||||
|      */ |      */ | ||||||
|     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 null; | ||||||
| 		List<String> temp = new ArrayList<>(); |         return text.stream().map(line -> setRelationalPlaceholders(one, two, line)).collect(Collectors.toList()); | ||||||
| 		text.forEach(line -> { |  | ||||||
| 			temp.add(setRelationalPlaceholders(one, two, line)); |  | ||||||
| 		}); |  | ||||||
| 		return temp; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 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 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 | ||||||
| @@ -244,7 +251,7 @@ public class PlaceholderAPI { | |||||||
|             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)) { | ||||||
|                 if (!(hooks.get(identifier) instanceof Relational)) { |                 if (!(hooks.get(identifier) instanceof Relational)) { | ||||||
|                     continue; |                     continue; | ||||||
| @@ -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)); | ||||||
| @@ -306,14 +321,17 @@ public class PlaceholderAPI { | |||||||
|     public static Set<String> getRegisteredPlaceholderPlugins() { |     public static Set<String> getRegisteredPlaceholderPlugins() { | ||||||
|         return getRegisteredIdentifiers(); |         return getRegisteredIdentifiers(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Deprecated |     @Deprecated | ||||||
|     public static Set<String> getExternalPlaceholderPlugins() { |     public static Set<String> getExternalPlaceholderPlugins() { | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Deprecated |     @Deprecated | ||||||
|     public static boolean registerPlaceholderHook(Plugin plugin, PlaceholderHook placeholderHook) { |     public static boolean registerPlaceholderHook(Plugin plugin, PlaceholderHook placeholderHook) { | ||||||
|         return plugin != null && registerPlaceholderHook(plugin.getName(), placeholderHook); |         return plugin != null && registerPlaceholderHook(plugin.getName(), placeholderHook); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Deprecated |     @Deprecated | ||||||
|     public static boolean unregisterPlaceholderHook(Plugin plugin) { |     public static boolean unregisterPlaceholderHook(Plugin plugin) { | ||||||
|         return plugin != null && unregisterPlaceholderHook(plugin.getName()); |         return plugin != null && unregisterPlaceholderHook(plugin.getName()); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user