Avoid double registration of NPC event listener

This commit is contained in:
PikaMug 2021-02-04 22:33:52 -05:00
parent 335360b640
commit df564644cf
2 changed files with 22 additions and 11 deletions

View File

@ -16,6 +16,8 @@ import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.RegisteredServiceProvider;
import com.alessiodp.parties.api.Parties;
@ -29,6 +31,7 @@ import com.herocraftonline.heroes.Heroes;
import com.herocraftonline.heroes.characters.Hero;
import de.erethon.dungeonsxl.DungeonsXL;
import me.blackvein.quests.listeners.NpcListener;
import me.blackvein.quests.reflect.denizen.DenizenAPI;
import me.blackvein.quests.reflect.worldguard.WorldGuardAPI;
import me.blackvein.quests.util.Lang;
@ -125,15 +128,22 @@ public class Dependencies {
public CitizensPlugin getCitizens() {
if (citizens == null) {
enableCitizens();
linkCitizens();
}
return citizens;
}
public void enableCitizens() {
public void linkCitizens() {
if (isPluginAvailable("Citizens")) {
try {
citizens = (CitizensPlugin) plugin.getServer().getPluginManager().getPlugin("Citizens");
boolean found = false;
for (final RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) {
if (listener.getListener() instanceof NpcListener) {
found = true;
}
}
if (!found) {
plugin.getServer().getPluginManager().registerEvents(plugin.getNpcListener(), plugin);
if (plugin.getSettings().canNpcEffects()) {
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, plugin.getNpcEffectThread(),
@ -141,13 +151,14 @@ public class Dependencies {
}
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.");
}
}
}
public void disableCitizens() {
public void unlinkCitizens() {
citizens = null;
}

View File

@ -217,7 +217,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
// 10 - Register listeners
getServer().getPluginManager().registerEvents(blockListener, this);
getServer().getPluginManager().registerEvents(itemListener, this);
depends.enableCitizens();
depends.linkCitizens();
getServer().getPluginManager().registerEvents(playerListener, this);
if (settings.getStrictPlayerMovement() > 0) {
final long ticks = settings.getStrictPlayerMovement() * 20;
@ -650,7 +650,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
if (depends.getCitizens().getNPCRegistry() == null) {
getLogger().log(Level.SEVERE,
"Citizens was enabled but NPCRegistry was null. Disabling linkage.");
depends.disableCitizens();
depends.unlinkCitizens();
}
}
loadModules();