Finished Hooks API

This commit is contained in:
Florian CUNY 2018-10-30 14:53:19 +01:00
parent 66bbed48c6
commit 8fe4b16805
3 changed files with 61 additions and 11 deletions

View File

@ -20,6 +20,7 @@ import world.bentobox.bentobox.listeners.PanelListenerManager;
import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.HooksManager;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager; 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.managers.SchemsManager;
import world.bentobox.bentobox.util.heads.HeadGetter; import world.bentobox.bentobox.util.heads.HeadGetter;
import java.util.Optional;
/** /**
* Main BentoBox class * Main BentoBox class
* @author tastybento, Poslovitch * @author tastybento, Poslovitch
@ -47,6 +50,7 @@ public class BentoBox extends JavaPlugin {
private IslandWorldManager islandWorldManager; private IslandWorldManager islandWorldManager;
private RanksManager ranksManager; private RanksManager ranksManager;
private SchemsManager schemsManager; private SchemsManager schemsManager;
private HooksManager hooksManager;
// Settings // Settings
private Settings settings; private Settings settings;
@ -120,24 +124,28 @@ public class BentoBox extends JavaPlugin {
playersManager.save(true); playersManager.save(true);
islandsManager.save(true); islandsManager.save(true);
}, getSettings().getDatabaseBackupPeriod() * 20 * 60L, getSettings().getDatabaseBackupPeriod() * 20 * 60L); }, getSettings().getDatabaseBackupPeriod() * 20 * 60L, getSettings().getDatabaseBackupPeriod() * 20 * 60L);
isLoaded = true;
flagsManager.registerListeners(); 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.log(instance.getDescription().getFullName() + " has been fully enabled."); instance.log(instance.getDescription().getFullName() + " has been fully enabled.");
instance.log("It took: " + (System.currentTimeMillis() - startMillis + "ms")); instance.log("It took: " + (System.currentTimeMillis() - startMillis + "ms"));
instance.log("Thanks for using our plugin !"); instance.log("Thanks for using our plugin !");
instance.log("- Tastybento and Poslovitch, 2017-2018"); instance.log("- Tastybento and Poslovitch, 2017-2018");
instance.log("#############################################"); 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() { public boolean isLoaded() {
return isLoaded; return isLoaded;
} }
/**
* @return the HooksManager
*/
public HooksManager getHooks() {
return hooksManager;
}
} }

View File

@ -11,9 +11,21 @@ public abstract class Hook {
private String pluginName; private String pluginName;
public Hook(String pluginName) { public Hook(String pluginName) {
if (pluginName == null || pluginName.isEmpty()) {
throw new IllegalArgumentException("Plugin name cannot be null nor empty.");
}
this.pluginName = pluginName; 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. * 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. * @return the Plugin instance of the plugin this Hook hooks into.

View File

@ -1,17 +1,39 @@
package world.bentobox.bentobox.managers; package world.bentobox.bentobox.managers;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.hooks.Hook; import world.bentobox.bentobox.api.hooks.Hook;
import java.util.List; import java.util.List;
import java.util.Optional;
/** /**
* @author Poslovitch * @author Poslovitch
*/ */
public class HooksManager { public class HooksManager {
private BentoBox plugin;
private List<Hook> hooks; private List<Hook> 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<Hook> getHooks() { public List<Hook> getHooks() {
return hooks; return hooks;
} }
public Optional<Hook> getHook(String pluginName) {
return hooks.stream().filter(hook -> hook.getPluginName().equals(pluginName)).findFirst();
}
} }