From 2a3ac0bd3b37c2d3bdf8643808243224f266f946 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 10 Sep 2020 18:36:41 -0700 Subject: [PATCH] Handles placeholder reloading. (#1503) https://github.com/BentoBoxWorld/BentoBox/issues/1502 --- .../commands/BentoBoxReloadCommand.java | 7 +++++++ .../placeholders/PlaceholderAPIHook.java | 20 ++++++++++++++++++- .../hooks/placeholders/PlaceholderHook.java | 6 ++++++ .../managers/PlaceholdersManager.java | 15 +++++++++----- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java index ab6899191..34dfe8649 100644 --- a/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java +++ b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java @@ -39,6 +39,10 @@ public class BentoBoxReloadCommand extends ConfirmableCommand { public boolean execute(User user, String label, List args) { if (args.isEmpty()) { this.askConfirmation(user, user.getTranslation("commands.bentobox.reload.warning"), () -> { + + // Unregister all placeholders + getPlugin().getPlaceholdersManager().unregisterAll(); + // Close all open panels PanelListenerManager.closeAllPanels(); @@ -53,6 +57,9 @@ public class BentoBoxReloadCommand extends ConfirmableCommand { // Reload locales getPlugin().getLocalesManager().reloadLanguages(); user.sendMessage("commands.bentobox.reload.locales-reloaded"); + + // Register new default gamemode placeholders + getPlugin().getAddonsManager().getGameModeAddons().forEach(getPlugin().getPlaceholdersManager()::registerDefaultPlaceholders); // Fire ready event Bukkit.getPluginManager().callEvent(new BentoBoxReadyEvent()); diff --git a/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java b/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java index 8071dd548..bd563a99d 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderAPIHook.java @@ -1,7 +1,9 @@ package world.bentobox.bentobox.hooks.placeholders; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import org.bukkit.entity.Player; import org.eclipse.jdt.annotation.NonNull; @@ -22,10 +24,15 @@ public class PlaceholderAPIHook extends PlaceholderHook { private BentoBoxPlaceholderExpansion bentoboxExpansion; private Map addonsExpansions; + private final Set bentoBoxPlaceholders; + private final Map> addonPlaceholders; + public PlaceholderAPIHook() { super("PlaceholderAPI"); this.addonsExpansions = new HashMap<>(); + this.bentoBoxPlaceholders = new HashSet<>(); + this.addonPlaceholders = new HashMap<>(); } @Override @@ -50,6 +57,7 @@ public class PlaceholderAPIHook extends PlaceholderHook { @Override public void registerPlaceholder(@NonNull String placeholder, @NonNull PlaceholderReplacer replacer) { bentoboxExpansion.registerPlaceholder(placeholder, replacer); + this.bentoBoxPlaceholders.add(placeholder); } /** @@ -62,6 +70,7 @@ public class PlaceholderAPIHook extends PlaceholderHook { AddonPlaceholderExpansion addonPlaceholderExpansion = new AddonPlaceholderExpansion(addon); addonPlaceholderExpansion.register(); addonsExpansions.put(addon, addonPlaceholderExpansion); + this.addonPlaceholders.computeIfAbsent(addon, k -> new HashSet<>()).add(placeholder); } addonsExpansions.get(addon).registerPlaceholder(placeholder, replacer); @@ -94,11 +103,20 @@ public class PlaceholderAPIHook extends PlaceholderHook { } /** - * {@inheritDoc} + * */ @Override @NonNull public String replacePlaceholders(@NonNull Player player, @NonNull String string) { return PlaceholderAPI.setPlaceholders(player, string); } + + /** + * {@inheritDoc} + */ + @Override + public void unregisterAll() { + this.bentoBoxPlaceholders.forEach(this::unregisterPlaceholder); + this.addonPlaceholders.forEach((addon,list) -> list.forEach(placeholder -> this.unregisterPlaceholder(addon, placeholder))); + } } diff --git a/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderHook.java b/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderHook.java index 6f9dddebf..b742c3d9a 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/placeholders/PlaceholderHook.java @@ -68,4 +68,10 @@ public abstract class PlaceholderHook extends Hook { */ @NonNull public abstract String replacePlaceholders(@NonNull Player player, @NonNull String string); + + /** + * Unregister all previously registered placeholders + * @since 1.15.0 + */ + public abstract void unregisterAll(); } diff --git a/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java b/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java index e5527b49f..4bd8bd537 100644 --- a/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/PlaceholdersManager.java @@ -122,10 +122,15 @@ public class PlaceholdersManager { * @since 1.5.0 */ public String replacePlaceholders(@NonNull Player player, @NonNull String string) { - Optional papi = getPlaceholderAPIHook(); - if (papi.isPresent()) { - string = papi.get().replacePlaceholders(player, string); - } - return string; + return getPlaceholderAPIHook().map(papi -> papi.replacePlaceholders(player, string)).orElse(string); + } + + /** + * Unregisters all the placeholders. + * @since 1.15.0 + */ + public void unregisterAll() { + getPlaceholderAPIHook().ifPresent(hook -> hook.unregisterAll()); + } }