diff --git a/api/src/main/java/me/blackvein/quests/dependencies/IDependencies.java b/api/src/main/java/me/blackvein/quests/dependencies/IDependencies.java index 1cd7b25ca..594d6d465 100644 --- a/api/src/main/java/me/blackvein/quests/dependencies/IDependencies.java +++ b/api/src/main/java/me/blackvein/quests/dependencies/IDependencies.java @@ -61,6 +61,10 @@ public interface IDependencies { ZNPCsPlus getZnpcsPlus(); + void linkZnpcsPlus(); + + void unlinkZnpcsPlus(); + PartiesAPI getPartiesApi(); boolean isPluginAvailable(final String pluginName); diff --git a/core/src/main/java/me/blackvein/quests/Dependencies.java b/core/src/main/java/me/blackvein/quests/Dependencies.java index bd78b2e25..782ddeb27 100644 --- a/core/src/main/java/me/blackvein/quests/Dependencies.java +++ b/core/src/main/java/me/blackvein/quests/Dependencies.java @@ -24,6 +24,7 @@ import io.github.znetworkw.znpcservers.npc.NPC; import lol.pyr.znpcsplus.ZNPCsPlus; import me.blackvein.quests.dependencies.IDependencies; import me.blackvein.quests.listeners.CitizensListener; +import me.blackvein.quests.listeners.ZnpcsListener; import me.blackvein.quests.player.IQuester; import me.blackvein.quests.reflect.denizen.DenizenAPI; import me.blackvein.quests.reflect.worldguard.WorldGuardAPI; @@ -165,8 +166,8 @@ public class Dependencies implements IDependencies { plugin.getLogger().info("Successfully linked Quests with Citizens " + citizens.getDescription().getVersion()); } - } catch (final Exception e) { - plugin.getLogger().warning("Legacy version of Citizens found. Citizens in Quests not enabled."); + } catch (final Exception | NoClassDefFoundError e) { + plugin.getLogger().severe("Unsupported version of Citizens found. Citizens in Quests not enabled."); } } } @@ -196,20 +197,45 @@ public class Dependencies implements IDependencies { public ZNPCsPlus getZnpcsPlus() { if (znpcs == null) { - znpcs = (ZNPCsPlus) plugin.getServer().getPluginManager().getPlugin("ZNPCsPlus"); - startNpcEffectThread(); + linkZnpcsPlus(); } return znpcs; } public Set getZnpcsPlusUuids() { - if (znpcs != null && isPluginAvailable("ZNPCsPlus")) { + if (getZnpcsPlus() != null) { return io.github.znetworkw.znpcservers.npc.NPC.all().stream() .map(io.github.znetworkw.znpcservers.npc.NPC::getUUID).collect(Collectors.toSet()); } return Collections.emptySet(); } + public void linkZnpcsPlus() { + if (isPluginAvailable("ZNPCsPlus")) { + try { + znpcs = (ZNPCsPlus) plugin.getServer().getPluginManager().getPlugin("ZNPCsPlus"); + boolean found = false; + for (final RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) { + if (listener.getListener() instanceof ZnpcsListener) { + found = true; + } + } + if (!found) { + plugin.getServer().getPluginManager().registerEvents(plugin.getZnpcsListener(), plugin); + startNpcEffectThread(); + plugin.getLogger().info("Successfully linked Quests with ZNPCsPlus " + + znpcs.getDescription().getVersion()); + } + } catch (final Exception | NoClassDefFoundError e) { + plugin.getLogger().severe("Unsupported version of ZNPCsPlus found. ZNPCsPlus in Quests not enabled."); + } + } + } + + public void unlinkZnpcsPlus() { + znpcs = null; + } + public PartiesAPI getPartiesApi() { if (parties == null && isPluginAvailable("Parties")) { try { @@ -223,7 +249,7 @@ public class Dependencies implements IDependencies { } public boolean isPluginAvailable(final String pluginName) { - if (plugin.getServer().getPluginManager().getPlugin(pluginName) != null ) { + if (plugin.getServer().getPluginManager().getPlugin(pluginName) != null) { try { if (!Objects.requireNonNull(plugin.getServer().getPluginManager().getPlugin(pluginName)).isEnabled()) { plugin.getLogger().warning(pluginName diff --git a/core/src/main/java/me/blackvein/quests/Quests.java b/core/src/main/java/me/blackvein/quests/Quests.java index c60a053c7..770006106 100644 --- a/core/src/main/java/me/blackvein/quests/Quests.java +++ b/core/src/main/java/me/blackvein/quests/Quests.java @@ -865,6 +865,9 @@ public class Quests extends JavaPlugin implements QuestsAPI { depends.unlinkCitizens(); } } + if (depends.getZnpcsPlus() != null) { + depends.unlinkZnpcsPlus(); + } loadModules(); importQuests(); if (getSettings().canDisableCommandFeedback()) {