Handles placeholder reloading. (#1503)

https://github.com/BentoBoxWorld/BentoBox/issues/1502
This commit is contained in:
tastybento 2020-09-10 18:36:41 -07:00 committed by GitHub
parent 42622d6e38
commit 2a3ac0bd3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 6 deletions

View File

@ -39,6 +39,10 @@ public class BentoBoxReloadCommand extends ConfirmableCommand {
public boolean execute(User user, String label, List<String> 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());

View File

@ -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<Addon, AddonPlaceholderExpansion> addonsExpansions;
private final Set<String> bentoBoxPlaceholders;
private final Map<Addon, Set<String>> 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)));
}
}

View File

@ -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();
}

View File

@ -122,10 +122,15 @@ public class PlaceholdersManager {
* @since 1.5.0
*/
public String replacePlaceholders(@NonNull Player player, @NonNull String string) {
Optional<PlaceholderAPIHook> 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());
}
}