Only unregister expansions if they are provided by PlaceholderAPI

This commit is contained in:
extendedclip 2018-03-24 11:49:31 -04:00
parent 19de55c7c5
commit 05b752baa2

View File

@ -32,6 +32,7 @@ import org.bukkit.plugin.Plugin;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class PlaceholderAPI { public class PlaceholderAPI {
@ -43,27 +44,6 @@ public class PlaceholderAPI {
private final static Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern.compile("[%](rel_)([^%]+)[%]"); private final static Pattern RELATIONAL_PLACEHOLDER_PATTERN = Pattern.compile("[%](rel_)([^%]+)[%]");
private final static Map<String, PlaceholderHook> placeholders = new HashMap<>(); private final static Map<String, PlaceholderHook> placeholders = new HashMap<>();
/**
* unregister ALL placeholder hooks that are currently registered
*/
protected static void unregisterAll() {
unregisterAllExpansions();
placeholders.clear();
}
/**
* unregister all expansions
*/
public static void unregisterAllExpansions() {
if (placeholders.isEmpty()) return;
getPlaceholders().forEach((key, value) -> {
if (value instanceof PlaceholderExpansion) {
Bukkit.getPluginManager().callEvent(new PlaceholderHookUnloadEvent(key, value));
unregisterPlaceholderHook(key);
}
});
}
/** /**
* 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
@ -84,7 +64,6 @@ public class PlaceholderAPI {
Validate.notNull(identifier, "Identifier can not be null"); Validate.notNull(identifier, "Identifier can not be null");
Validate.notNull(placeholderHook, "Placeholderhook can not be null"); Validate.notNull(placeholderHook, "Placeholderhook can not be null");
if (isRegistered(identifier)) return false; if (isRegistered(identifier)) return false;
//test
placeholders.put(identifier.toLowerCase(), placeholderHook); placeholders.put(identifier.toLowerCase(), placeholderHook);
return true; return true;
} }
@ -116,6 +95,10 @@ public class PlaceholderAPI {
return new HashMap<>(placeholders); 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));
}
/** /**
* 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
@ -220,7 +203,7 @@ public class PlaceholderAPI {
} }
} }
} }
return ChatColor.translateAlternateColorCodes('&', text); return colorize(text);
} }
/** /**
@ -259,24 +242,44 @@ public class PlaceholderAPI {
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;
} }
Relational rel = (Relational) hooks.get(identifier); Relational rel = (Relational) hooks.get(identifier);
String value = rel.onPlaceholderRequest(one, two, params); String value = rel.onPlaceholderRequest(one, two, params);
if (value != null) { if (value != null) {
text = text.replaceAll(m.group(), Matcher.quoteReplacement(value)); text = text.replaceAll(m.group(), Matcher.quoteReplacement(value));
} }
} }
} }
return ChatColor.translateAlternateColorCodes('&', text); return colorize(text);
} }
/**
* 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()) {
Bukkit.getPluginManager().callEvent(new PlaceholderHookUnloadEvent(key, value));
unregisterPlaceholderHook(key);
}
}
});
}
public static Pattern getPlaceholderPattern() { public static Pattern getPlaceholderPattern() {
return PLACEHOLDER_PATTERN; return PLACEHOLDER_PATTERN;
} }