Skip to content

Commit

Permalink
catch & prevent potential issues related to dead NPCs
Browse files Browse the repository at this point in the history
fixes #339
  • Loading branch information
mcmonkey4eva committed May 11, 2020
1 parent 39e1de8 commit a401008
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/mcmonkey/sentinel/SentinelEventHandler.java
Expand Up @@ -117,10 +117,14 @@ public void whenAttacksAreHappening(EntityDamageByEntityEvent event) {
sentinel.whenSomethingMightDie(victimUuid);
}
SentinelTrait victim = SentinelUtilities.tryGetSentinel(event.getEntity());
SentinelTrait attacker = SentinelUtilities.tryGetSentinel(event.getDamager());
if (victim != null) {
if (attacker != null && victim.getNPC().getId() == attacker.getNPC().getId()) {
event.setCancelled(true);
return;
}
victim.whenAttacksAreHappeningToMe(event);
}
SentinelTrait attacker = SentinelUtilities.tryGetSentinel(event.getDamager());
if (attacker != null) {
attacker.whenAttacksAreHappeningFromMe(event);
}
Expand Down Expand Up @@ -167,10 +171,14 @@ public void whenAttacksHappened(EntityDamageByEntityEvent event) {
}
}
SentinelTrait victim = SentinelUtilities.tryGetSentinel(event.getEntity());
SentinelTrait attacker = SentinelUtilities.tryGetSentinel(damager);
if (victim != null) {
if (attacker != null && victim.getNPC().getId() == attacker.getNPC().getId()) {
event.setCancelled(true);
return;
}
victim.whenAttacksHappened(event);
}
SentinelTrait attacker = SentinelUtilities.tryGetSentinel(damager);
if (attacker != null) {
attacker.whenAttacksHappened(event);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/mcmonkey/sentinel/SentinelTrait.java
Expand Up @@ -659,6 +659,9 @@ public void whenAttacksAreHappeningToMe(EntityDamageByEntityEvent event) {
if (event.isCancelled()) {
return;
}
if (!getNPC().isSpawned()) {
return;
}
double armorLevel = getArmor(getLivingEntity());
if (hitIsBlocked(event.getDamager())) {
armorLevel = (1.0 - armorLevel) * 0.5 + armorLevel;
Expand All @@ -683,6 +686,9 @@ public void whenAttacksAreHappeningFromMe(EntityDamageByEntityEvent event) {
if (event.isCancelled()) {
return;
}
if (!npc.isSpawned()) {
return;
}
if (SentinelPlugin.instance.alternateDamage) {
if (canEnforce) {
canEnforce = false;
Expand Down Expand Up @@ -716,6 +722,9 @@ public void whenAttacksAreHappeningFromMyArrow(EntityDamageByEntityEvent event)
if (event.isCancelled()) {
return;
}
if (!npc.isSpawned()) {
return;
}
if (SentinelPlugin.instance.alternateDamage) {
if (canEnforce) {
canEnforce = false;
Expand Down
Expand Up @@ -46,6 +46,9 @@ public SentinelTargetList duplicate() {
return result;
}

/**
* Initialize the targets list after loading it.
*/
public void init() {
recalculateTargetsCache();
for (String str : new ArrayList<>(byEvent)) {
Expand Down

0 comments on commit a401008

Please sign in to comment.