From 8fe4b16805b482a4f4da6f6acb6e909ab7c620ac Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Tue, 30 Oct 2018 14:53:19 +0100 Subject: [PATCH] Finished Hooks API --- .../world/bentobox/bentobox/BentoBox.java | 38 +++++++++++++------ .../bentobox/bentobox/api/hooks/Hook.java | 12 ++++++ .../bentobox/managers/HooksManager.java | 22 +++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 5079e0559..3da5bbd58 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -20,6 +20,7 @@ import world.bentobox.bentobox.listeners.PanelListenerManager; import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.FlagsManager; +import world.bentobox.bentobox.managers.HooksManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; @@ -28,6 +29,8 @@ import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.SchemsManager; import world.bentobox.bentobox.util.heads.HeadGetter; +import java.util.Optional; + /** * Main BentoBox class * @author tastybento, Poslovitch @@ -47,6 +50,7 @@ public class BentoBox extends JavaPlugin { private IslandWorldManager islandWorldManager; private RanksManager ranksManager; private SchemsManager schemsManager; + private HooksManager hooksManager; // Settings private Settings settings; @@ -120,24 +124,28 @@ public class BentoBox extends JavaPlugin { playersManager.save(true); islandsManager.save(true); }, getSettings().getDatabaseBackupPeriod() * 20 * 60L, getSettings().getDatabaseBackupPeriod() * 20 * 60L); - isLoaded = true; + flagsManager.registerListeners(); + + // Load metrics + if (settings.isMetrics()) { + BStats bStats = new BStats(this); + bStats.registerMetrics(); + } + + // Load hooks + hooksManager = new HooksManager(this); + + // Fire plugin ready event + isLoaded = true; + Bukkit.getServer().getPluginManager().callEvent(new BentoBoxReadyEvent()); + instance.log("#############################################"); instance.log(instance.getDescription().getFullName() + " has been fully enabled."); instance.log("It took: " + (System.currentTimeMillis() - startMillis + "ms")); instance.log("Thanks for using our plugin !"); instance.log("- Tastybento and Poslovitch, 2017-2018"); instance.log("#############################################"); - - // Load metrics - - if (settings.isMetrics()) { - BStats bStats = new BStats(this); - bStats.registerMetrics(); - } - - // Fire plugin ready event - Bukkit.getServer().getPluginManager().callEvent(new BentoBoxReadyEvent()); }); } @@ -306,4 +314,12 @@ public class BentoBox extends JavaPlugin { public boolean isLoaded() { return isLoaded; } + + /** + * @return the HooksManager + */ + public HooksManager getHooks() { + return hooksManager; + } + } diff --git a/src/main/java/world/bentobox/bentobox/api/hooks/Hook.java b/src/main/java/world/bentobox/bentobox/api/hooks/Hook.java index 54912f3fb..1b624f6e5 100644 --- a/src/main/java/world/bentobox/bentobox/api/hooks/Hook.java +++ b/src/main/java/world/bentobox/bentobox/api/hooks/Hook.java @@ -11,9 +11,21 @@ public abstract class Hook { private String pluginName; public Hook(String pluginName) { + if (pluginName == null || pluginName.isEmpty()) { + throw new IllegalArgumentException("Plugin name cannot be null nor empty."); + } this.pluginName = pluginName; } + /** + * Returns the name of the plugin related to this Hook. + * Cannot be null. + * @return the plugin name. + */ + public String getPluginName() { + return pluginName; + } + /** * Returns the Plugin instance related to this Hook or null if it could not be found. * @return the Plugin instance of the plugin this Hook hooks into. diff --git a/src/main/java/world/bentobox/bentobox/managers/HooksManager.java b/src/main/java/world/bentobox/bentobox/managers/HooksManager.java index 18eb33019..4cb40f2f5 100644 --- a/src/main/java/world/bentobox/bentobox/managers/HooksManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/HooksManager.java @@ -1,17 +1,39 @@ package world.bentobox.bentobox.managers; +import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.hooks.Hook; import java.util.List; +import java.util.Optional; /** * @author Poslovitch */ public class HooksManager { + private BentoBox plugin; private List hooks; + public HooksManager(BentoBox plugin) { + this.plugin = plugin; + } + + public void registerHook(Hook hook) { + if (hook.isPluginAvailable()) { + plugin.log("Hooking with " + hook.getPluginName() + "..."); + if (hook.hook()) { + hooks.add(hook); + } else { + plugin.log("Could not hook with " + hook.getPluginName() + ". Skipping..."); + } + } + } + public List getHooks() { return hooks; } + + public Optional getHook(String pluginName) { + return hooks.stream().filter(hook -> hook.getPluginName().equals(pluginName)).findFirst(); + } }