fix plugin-specific hooks

This commit is contained in:
jascotty2 2019-08-26 16:30:32 -05:00
parent 97cdcb5937
commit 93ce4b60f3
3 changed files with 28 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package com.songoda.core.hooks;
import com.songoda.core.hooks.holograms.Holograms;
import org.bukkit.Location;
import java.util.List;
import org.bukkit.plugin.Plugin;
/**
* A convenience class for static access to a Holograms HookManager
@ -12,11 +13,13 @@ public class HologramManager {
private static final HookManager<Holograms> manager = new HookManager(Holograms.class);
/**
* Load all supported economy plugins. <br />
* Note: This method should be called in your plugin's onEnable() section
* Load all supported economy plugins.<br/>
* Note: This method should be called in your plugin's onEnable() section
*
* @param plugin plugin that will be using the holograms
*/
public static void load() {
manager.load();
public static void load(Plugin plugin) {
manager.load(plugin);
}
public static HookManager getManager() {

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.bukkit.plugin.Plugin;
public class HookManager<T extends Hook> {
@ -23,7 +24,22 @@ public class HookManager<T extends Hook> {
*/
public void load() {
if (!loaded) {
registeredHooks.putAll(PluginHook.loadHooks(typeClass).entrySet().stream()
registeredHooks.putAll(PluginHook.loadHooks(typeClass, null).entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> (T) e.getValue())));
if (!registeredHooks.isEmpty()) {
defaultHook = (T) registeredHooks.values().iterator().next();
}
loaded = true;
}
}
/**
* Load all supported plugins.
* @param hookingPlugin plugin to pass to the hook handler
*/
public void load(Plugin hookingPlugin) {
if (!loaded) {
registeredHooks.putAll(PluginHook.loadHooks(typeClass, hookingPlugin).entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> (T) e.getValue())));
if (!registeredHooks.isEmpty()) {
defaultHook = (T) registeredHooks.values().iterator().next();
@ -127,8 +143,8 @@ public class HookManager<T extends Hook> {
* @return an immutable collection of plugin names that can be used.
*/
public List<String> getPossiblePlugins() {
return PluginHook.loadHooks(typeClass).values().stream()
.map(v -> v.getName())
return PluginHook.getHooks(typeClass).stream()
.map(v -> v.plugin)
.collect(Collectors.toList());
}

View File

@ -61,13 +61,13 @@ public final class PluginHook <T extends Class> {
}
}
protected static Map<PluginHook, Hook> loadHooks(Class type) {
protected static Map<PluginHook, Hook> loadHooks(Class type, Plugin plugin) {
Map<PluginHook, Hook> loaded = new LinkedHashMap<>();
PluginManager pluginManager = Bukkit.getPluginManager();
for (PluginHook hook : getHooks(type)) {
if (pluginManager.isPluginEnabled(hook.plugin)) {
Hook handler = (Hook) hook.load();
Hook handler = (Hook) (plugin != null ? hook.load(plugin) : hook.load());
if (handler != null && handler.isEnabled()) {
loaded.put(hook, handler);
}