Added methods to unregister placeholders in PlaceholdersManager

#592
This commit is contained in:
Florian CUNY 2019-03-03 16:29:46 +01:00
parent 4f7de44b67
commit c5dc7fcfad
3 changed files with 123 additions and 17 deletions

View File

@ -7,11 +7,16 @@ import java.util.Map;
import org.bukkit.entity.Player;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.api.user.User;
/**
* @author Poslovitch
*/
abstract class BasicPlaceholderExpansion extends PlaceholderExpansion {
private Map<String, PlaceholderReplacer> placeholders;
@NonNull
private Map<@NonNull String, @NonNull PlaceholderReplacer> placeholders;
BasicPlaceholderExpansion() {
this.placeholders = new HashMap<>();
@ -22,10 +27,19 @@ abstract class BasicPlaceholderExpansion extends PlaceholderExpansion {
return getName().toLowerCase(Locale.ENGLISH);
}
public void registerPlaceholder(String placeholder, PlaceholderReplacer replacer) {
public void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
placeholders.putIfAbsent(placeholder, replacer);
}
/**
* Unregisters a placeholder from the expansion.
* @param placeholder the placeholder to unregister.
* @since 1.4.0
*/
public void unregisterPlaceholder(@NonNull String placeholder) {
placeholders.remove(placeholder);
}
@Override
public String onPlaceholderRequest(Player p, String placeholder) {
User user = User.getInstance(p);

View File

@ -3,6 +3,8 @@ package world.bentobox.bentobox.hooks;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.hooks.Hook;
@ -41,16 +43,33 @@ public class PlaceholderAPIHook extends Hook {
return "could not register BentoBox's expansion";
}
/**
* @deprecated As of 1.4.0, renamed to {@link #registerPlaceholder(String, PlaceholderReplacer)}.
*/
@Deprecated
public void registerBentoBoxPlaceholder(String placeholder, PlaceholderReplacer replacer) {
registerPlaceholder(placeholder, replacer);
}
/**
* Registers this placeholder into BentoBox's PlaceholderAPI expansion.
* @param placeholder the placeholder to register, not null
* @param replacer its replacement, not null
* @since 1.4.0
*/
public void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
bentoboxExpansion.registerPlaceholder(placeholder, replacer);
}
public void registerAddonPlaceholder(Addon addon, String placeholder, PlaceholderReplacer replacer) {
// If addon is null, then register the placeholder in BentoBox's expansion.
if (addon == null) {
registerBentoBoxPlaceholder(placeholder, replacer);
}
/**
* Registers this placeholder into this addon's PlaceholderAPI expansion.
* It will register the expansion if it previously did not exist.
* @param addon the addon, not null
* @param placeholder the placeholder to register, not null
* @param replacer its replacement, not null
* @since 1.4.0
*/
public void registerPlaceholder(@NonNull Addon addon, @NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
// Check if the addon expansion does not exist
if (!addonsExpansions.containsKey(addon)) {
AddonPlaceholderExpansion addonPlaceholderExpansion = new AddonPlaceholderExpansion(addon);
@ -60,4 +79,33 @@ public class PlaceholderAPIHook extends Hook {
addonsExpansions.get(addon).registerPlaceholder(placeholder, replacer);
}
/**
* Unregisters this placeholder from the BentoBox PlaceholderAPI expansion.
* @param placeholder the placeholder to unregister, not null
* @since 1.4.0
*/
public void unregisterPlaceholder(@NonNull String placeholder) {
bentoboxExpansion.unregisterPlaceholder(placeholder);
}
/**
* Unregister this placeholder from this addon's PlaceholderAPI expansion.
* @param addon the addon, not null
* @param placeholder the placeholder to unregister, not null
* @since 1.4.0
*/
public void unregisterPlaceholder(@NonNull Addon addon, @NonNull String placeholder) {
if (addonsExpansions.containsKey(addon)) {
addonsExpansions.get(addon).unregisterPlaceholder(placeholder);
}
}
/**
* @deprecated As of 1.4.0, renamed to {@link #registerPlaceholder(Addon, String, PlaceholderReplacer)}.
*/
@Deprecated
public void registerAddonPlaceholder(Addon addon, String placeholder, PlaceholderReplacer replacer) {
registerPlaceholder(addon, placeholder, replacer);
}
}

View File

@ -1,10 +1,14 @@
package world.bentobox.bentobox.managers;
import com.mongodb.lang.NonNull;
import com.mongodb.lang.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer;
import world.bentobox.bentobox.hooks.PlaceholderAPIHook;
import java.util.Optional;
/**
* Manages placeholder integration.
*
@ -19,26 +23,66 @@ public class PlaceholdersManager {
}
/**
* Registers the placeholder on the behalf on BentoBox.
* @param placeholder the placeholder to register. It will be appended with {@code "bentobox_"} by the placeholder plugin.
* @param replacer the expression that will return a {@code String} when executed, which will replace the placeholder.
* Registers this placeholder on the behalf of BentoBox.
* @param placeholder the placeholder to register, not null.
* It will be appended with {@code "bentobox_"} by the placeholder plugin.
* @param replacer the expression that will return a {@code String} when executed, which will be this placeholder's replacement.
*/
public void registerPlaceholder(String placeholder, PlaceholderReplacer replacer) {
public void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
// Register it in PlaceholderAPI
plugin.getHooks().getHook("PlaceholderAPI").ifPresent(hook -> ((PlaceholderAPIHook) hook).registerBentoBoxPlaceholder(placeholder, replacer));
getPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(placeholder, replacer));
}
/**
* Registers the placeholder on the behalf of the specified addon.
* @param placeholder the placeholder to register. It will be appended with the addon's name by the placeholder plugin.
* Registers this placeholder on the behalf of the specified addon.
* @param addon the addon to register this placeholder on its behalf.
* If null, the placeholder will be registered using {@link #registerPlaceholder(String, PlaceholderReplacer)}.
* @param placeholder the placeholder to register, not null.
* It will be appended with the addon's name by the placeholder plugin.
* @param replacer the expression that will return a {@code String} when executed, which will replace the placeholder.
*/
public void registerPlaceholder(Addon addon, String placeholder, PlaceholderReplacer replacer) {
public void registerPlaceholder(@Nullable Addon addon, @NonNull String placeholder, @NonNull PlaceholderReplacer replacer) {
if (addon == null) {
registerPlaceholder(placeholder, replacer);
return;
}
// Register it in PlaceholderAPI
plugin.getHooks().getHook("PlaceholderAPI").ifPresent(hook -> ((PlaceholderAPIHook) hook).registerAddonPlaceholder(addon, placeholder, replacer));
getPlaceholderAPIHook().ifPresent(hook -> hook.registerPlaceholder(addon, placeholder, replacer));
}
/**
* Unregisters this placeholder on the behalf of BentoBox.
* Note that if the placeholder you are trying to unregister has been registered by an addon, you should use {@link #unregisterPlaceholder(Addon, String)} instead.
* @param placeholder the placeholder to unregister, not null.
* @since 1.4.0
*/
public void unregisterPlaceholder(@NonNull String placeholder) {
// Unregister it from PlaceholderAPI
getPlaceholderAPIHook().ifPresent(hook -> hook.unregisterPlaceholder(placeholder));
}
/**
* Unregisters this placeholder on the behalf of the specified addon.
* @param addon the addon that originally registered this placeholder.
* If null, this placeholder will be unregistered using {@link #unregisterPlaceholder(String)}.
* @param placeholder the placeholder to unregister, not null.
* @since 1.4.0
*/
public void unregisterPlaceholder(@Nullable Addon addon, @NonNull String placeholder) {
if (addon == null) {
unregisterPlaceholder(placeholder);
return;
}
// Unregister it from PlaceholderAPI
getPlaceholderAPIHook().ifPresent(hook -> hook.unregisterPlaceholder(addon, placeholder));
}
/**
* Returns an Optional containing the PlaceholderAPIHook instance, or an empty Optional otherwise.
* @return Optional containing the PlaceholderAPIHook instance or an empty Optional otherwise.
* @since 1.4.0
*/
private Optional<PlaceholderAPIHook> getPlaceholderAPIHook() {
return plugin.getHooks().getHook("PlaceholderAPI").map(hook -> (PlaceholderAPIHook) hook);
}
}