Skip to content

Commit

Permalink
Fix issue with attacking NPC with invalid index
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Jan 23, 2021
1 parent 741d035 commit 786e20a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions EOLib/PacketHandlers/NPCTakeDamageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using EOLib.Domain.Notifiers;
using EOLib.Net;
using EOLib.Net.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -69,13 +70,16 @@ public override bool HandlePacket(IPacket packet)
}

// todo: this has the potential to bug out if the opponent ID is never reset and the player dies/leaves
var npc = _currentMapStateRepository.NPCs.Single(x => x.Index == npcIndex);
var newNpc = npc.WithOpponentID(fromPlayerId);
_currentMapStateRepository.NPCs.Remove(npc);
_currentMapStateRepository.NPCs.Add(newNpc);

foreach (var notifier in _npcNotifiers)
notifier.NPCTakeDamage(npcIndex, fromPlayerId, damageToNpc, npcPctHealth, spellId);
try
{
var npc = _currentMapStateRepository.NPCs.Single(x => x.Index == npcIndex);
var newNpc = npc.WithOpponentID(fromPlayerId);
_currentMapStateRepository.NPCs.Remove(npc);
_currentMapStateRepository.NPCs.Add(newNpc);
foreach (var notifier in _npcNotifiers)
notifier.NPCTakeDamage(npcIndex, fromPlayerId, damageToNpc, npcPctHealth, spellId);
}
catch (InvalidOperationException) { return false; }

return true;
}
Expand Down

0 comments on commit 786e20a

Please sign in to comment.