diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 0178d6527..8b41a6c2c 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -147,10 +147,7 @@ public class BentoBox extends JavaPlugin { // Load hooks hooksManager = new HooksManager(this); hooksManager.registerHook(new VaultHook()); - if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI") - || getServer().getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) { - hooksManager.registerHook(new PlaceholderAPIHook()); - } + hooksManager.registerHook(new PlaceholderAPIHook()); // Setup the Placeholders manager placeholdersManager = new PlaceholdersManager(this); diff --git a/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/AddonPlaceholderExpansion.java b/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/AddonPlaceholderExpansion.java new file mode 100644 index 000000000..c066261c9 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/AddonPlaceholderExpansion.java @@ -0,0 +1,26 @@ +package world.bentobox.bentobox.api.placeholders.placeholderapi; + +import world.bentobox.bentobox.api.addons.Addon; + +public class AddonPlaceholderExpansion extends BasicPlaceholderExpansion { + private Addon addon; + + public AddonPlaceholderExpansion(Addon addon) { + this.addon = addon; + } + + @Override + public String getName() { + return addon.getDescription().getName(); + } + + @Override + public String getAuthor() { + return addon.getDescription().getAuthors().get(0); + } + + @Override + public String getVersion() { + return addon.getDescription().getVersion(); + } +} diff --git a/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/BasicPlaceholderExpansion.java b/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/BasicPlaceholderExpansion.java new file mode 100644 index 000000000..8c6c48f0f --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/BasicPlaceholderExpansion.java @@ -0,0 +1,37 @@ +package world.bentobox.bentobox.api.placeholders.placeholderapi; + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.entity.Player; +import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; +import world.bentobox.bentobox.api.user.User; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +abstract class BasicPlaceholderExpansion extends PlaceholderExpansion { + private Map placeholders; + + BasicPlaceholderExpansion() { + this.placeholders = new HashMap<>(); + } + + @Override + public String getIdentifier() { + return getName().toLowerCase(Locale.ENGLISH); + } + + public void registerPlaceholder(String placeholder, PlaceholderReplacer replacer) { + placeholders.putIfAbsent(placeholder, replacer); + } + + @Override + public String onPlaceholderRequest(Player p, String placeholder) { + User user = User.getInstance(p); + + if (placeholders.containsKey(placeholder)) { + return placeholders.get(placeholder).onReplace(user); + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/BentoBoxPlaceholderExpansion.java b/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/BentoBoxPlaceholderExpansion.java new file mode 100644 index 000000000..8f1a999fc --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/placeholders/placeholderapi/BentoBoxPlaceholderExpansion.java @@ -0,0 +1,26 @@ +package world.bentobox.bentobox.api.placeholders.placeholderapi; + +import world.bentobox.bentobox.BentoBox; + +public class BentoBoxPlaceholderExpansion extends BasicPlaceholderExpansion { + private BentoBox plugin; + + public BentoBoxPlaceholderExpansion(BentoBox plugin) { + this.plugin = plugin; + } + + @Override + public String getName() { + return plugin.getName(); + } + + @Override + public String getAuthor() { + return "Tastybento and Poslovitch"; + } + + @Override + public String getVersion() { + return plugin.getDescription().getVersion(); + } +} \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/hooks/PlaceholderAPIHook.java b/src/main/java/world/bentobox/bentobox/hooks/PlaceholderAPIHook.java index f8e425f35..dcb66fc97 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/PlaceholderAPIHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/PlaceholderAPIHook.java @@ -6,10 +6,10 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.hooks.Hook; import world.bentobox.bentobox.api.placeholders.PlaceholderReplacer; -import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.api.placeholders.placeholderapi.AddonPlaceholderExpansion; +import world.bentobox.bentobox.api.placeholders.placeholderapi.BentoBoxPlaceholderExpansion; import java.util.HashMap; -import java.util.Locale; import java.util.Map; /** @@ -24,12 +24,17 @@ public class PlaceholderAPIHook extends Hook { public PlaceholderAPIHook() { super("PlaceholderAPI"); - this.bentoboxExpansion = new BentoBoxPlaceholderExpansion(BentoBox.getInstance()); this.addonsExpansions = new HashMap<>(); } @Override public boolean hook() { + try { + this.bentoboxExpansion = new BentoBoxPlaceholderExpansion(BentoBox.getInstance()); + } catch (Exception | NoClassDefFoundError | NoSuchMethodError e) { + return false; + } + return bentoboxExpansion.canRegister() && bentoboxExpansion.register(); } @@ -43,6 +48,11 @@ public class PlaceholderAPIHook extends Hook { } 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); + } + // Check if the addon expansion does not exist if (!addonsExpansions.containsKey(addon)) { AddonPlaceholderExpansion addonPlaceholderExpansion = new AddonPlaceholderExpansion(addon); @@ -52,77 +62,4 @@ public class PlaceholderAPIHook extends Hook { addonsExpansions.get(addon).registerPlaceholder(placeholder, replacer); } - - abstract class BasicPlaceholderExpansion extends PlaceholderExpansion { - private Map placeholders; - - BasicPlaceholderExpansion() { - this.placeholders = new HashMap<>(); - } - - @Override - public String getIdentifier() { - return getName().toLowerCase(Locale.ENGLISH); - } - - void registerPlaceholder(String placeholder, PlaceholderReplacer replacer) { - placeholders.putIfAbsent(placeholder, replacer); - } - - @Override - public String onPlaceholderRequest(Player p, String placeholder) { - User user = User.getInstance(p); - - if (placeholders.containsKey(placeholder)) { - return placeholders.get(placeholder).onReplace(user); - } - return null; - } - } - - class BentoBoxPlaceholderExpansion extends BasicPlaceholderExpansion { - private BentoBox plugin; - - BentoBoxPlaceholderExpansion(BentoBox plugin) { - this.plugin = plugin; - } - - @Override - public String getName() { - return plugin.getName(); - } - - @Override - public String getAuthor() { - return "Tastybento and Poslovitch"; - } - - @Override - public String getVersion() { - return plugin.getDescription().getVersion(); - } - } - - class AddonPlaceholderExpansion extends BasicPlaceholderExpansion { - private Addon addon; - - AddonPlaceholderExpansion(Addon addon) { - this.addon = addon; - } - - @Override - public String getName() { - return addon.getDescription().getName(); - } - - @Override - public String getAuthor() { - return addon.getDescription().getAuthors().get(0); - } - - @Override - public String getVersion() { - return addon.getDescription().getVersion(); - } - } }