Fix double count for kill-NPC objectives

This commit is contained in:
PikaMug 2021-06-14 02:10:46 -04:00
parent 6a2c5eabe2
commit 2237e58960
2 changed files with 49 additions and 71 deletions

View File

@ -14,8 +14,10 @@ package me.blackvein.quests.listeners;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
@ -37,7 +39,6 @@ import me.blackvein.quests.Quests;
import me.blackvein.quests.enums.ObjectiveType; import me.blackvein.quests.enums.ObjectiveType;
import me.blackvein.quests.util.ItemUtil; import me.blackvein.quests.util.ItemUtil;
import me.blackvein.quests.util.Lang; import me.blackvein.quests.util.Lang;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCDeathEvent; import net.citizensnpcs.api.event.NPCDeathEvent;
import net.citizensnpcs.api.event.NPCLeftClickEvent; import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.event.NPCRightClickEvent;
@ -282,40 +283,37 @@ public class NpcListener implements Listener {
= (EntityDamageByEntityEvent) evt.getNPC().getEntity().getLastDamageCause(); = (EntityDamageByEntityEvent) evt.getNPC().getEntity().getLastDamageCause();
final Entity damager = damageEvent.getDamager(); final Entity damager = damageEvent.getDamager();
if (damager != null) { if (damager != null) {
final ObjectiveType type = ObjectiveType.KILL_NPC;
if (damager instanceof Projectile) {
if (evt.getNPC().getEntity().getLastDamageCause().getEntity() instanceof Player) {
final Player player = (Player) evt.getNPC().getEntity().getLastDamageCause().getEntity();
boolean okay = true;
if (plugin.getDependencies().getCitizens() != null) {
if (CitizensAPI.getNPCRegistry().isNPC(player)) {
okay = false;
}
}
if (okay) {
final Quester quester = plugin.getQuester(player.getUniqueId());
for (final Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.getCurrentStage(quest).containsObjective(type)) {
quester.killNPC(quest, evt.getNPC());
}
}
}
}
} else if (damager instanceof Player) {
boolean okay = true;
if (plugin.getDependencies().getCitizens() != null) {
if (plugin.getDependencies().getCitizens().getNPCRegistry().isNPC(damager)) { if (plugin.getDependencies().getCitizens().getNPCRegistry().isNPC(damager)) {
okay = false; return;
} }
final ObjectiveType type = ObjectiveType.KILL_NPC;
final Set<String> dispatchedQuestIDs = new HashSet<String>();
Player player = null;
if (damager instanceof Projectile
&& evt.getNPC().getEntity().getLastDamageCause().getEntity() instanceof Player) {
player = (Player) evt.getNPC().getEntity().getLastDamageCause().getEntity();
} else if (damager instanceof Player) {
player = (Player) damager;
} }
if (okay) { if (player != null) {
final Player player = (Player) damager;
final Quester quester = plugin.getQuester(player.getUniqueId()); final Quester quester = plugin.getQuester(player.getUniqueId());
for (final Quest quest : quester.getCurrentQuests().keySet()) { for (final Quest quest : quester.getCurrentQuests().keySet()) {
if (quester.getCurrentStage(quest).containsObjective(type)) { if (!quester.meetsCondition(quest, true)) {
continue;
}
if (quester.getCurrentQuests().containsKey(quest)
&& quester.getCurrentStage(quest).containsObjective(type)) {
quester.killNPC(quest, evt.getNPC()); quester.killNPC(quest, evt.getNPC());
} }
dispatchedQuestIDs.addAll(quester.dispatchMultiplayerEverything(quest, type,
(final Quester q, final Quest cq) -> {
if (!dispatchedQuestIDs.contains(cq.getId())) {
q.killNPC(cq, evt.getNPC());
} }
return null;
}));
} }
} }
} }

View File

@ -632,29 +632,10 @@ public class PlayerListener implements Listener {
return; return;
} }
if (damager instanceof Player) { if (damager instanceof Player) {
final Quester quester = plugin.getQuester(damager.getUniqueId());
if (plugin.getDependencies().getCitizens() != null && CitizensAPI.getNPCRegistry().isNPC(target)) { if (plugin.getDependencies().getCitizens() != null && CitizensAPI.getNPCRegistry().isNPC(target)) {
final ObjectiveType type = ObjectiveType.KILL_NPC; return;
final Set<String> dispatchedQuestIDs = new HashSet<String>();
for (final Quest quest : plugin.getLoadedQuests()) {
if (!quester.meetsCondition(quest, true)) {
continue;
} }
final Quester quester = plugin.getQuester(damager.getUniqueId());
if (quester.getCurrentQuests().containsKey(quest)
&& quester.getCurrentStage(quest).containsObjective(type)) {
quester.killNPC(quest, CitizensAPI.getNPCRegistry().getNPC(target));
}
dispatchedQuestIDs.addAll(quester.dispatchMultiplayerEverything(quest, type,
(final Quester q, final Quest cq) -> {
if (!dispatchedQuestIDs.contains(cq.getId())) {
q.killNPC(cq, CitizensAPI.getNPCRegistry().getNPC(target));
}
return null;
}));
}
} else {
final ObjectiveType type = ObjectiveType.KILL_MOB; final ObjectiveType type = ObjectiveType.KILL_MOB;
final Set<String> dispatchedQuestIDs = new HashSet<String>(); final Set<String> dispatchedQuestIDs = new HashSet<String>();
for (final Quest quest : plugin.getLoadedQuests()) { for (final Quest quest : plugin.getLoadedQuests()) {
@ -677,7 +658,6 @@ public class PlayerListener implements Listener {
} }
} }
} }
}
@EventHandler @EventHandler
public void onPlayerDeath(final PlayerDeathEvent evt) { public void onPlayerDeath(final PlayerDeathEvent evt) {