From ce4c534598d9b35fe1799d5cf3b26583c86d5748 Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Mon, 10 May 2021 18:47:05 -0700 Subject: [PATCH] Fix crash in NPCLeaveMapHandler when there's no character matching player ID --- EOLib/PacketHandlers/NPCLeaveMapHandler.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/EOLib/PacketHandlers/NPCLeaveMapHandler.cs b/EOLib/PacketHandlers/NPCLeaveMapHandler.cs index 4ed348174..475604ed8 100644 --- a/EOLib/PacketHandlers/NPCLeaveMapHandler.cs +++ b/EOLib/PacketHandlers/NPCLeaveMapHandler.cs @@ -6,6 +6,7 @@ using EOLib.Domain.Login; using EOLib.Domain.Map; using EOLib.Domain.Notifiers; +using EOLib.Extensions; using EOLib.Net; using EOLib.Net.Handlers; @@ -109,12 +110,16 @@ private void UpdatePlayerDirection(short playerID, EODirection playerDirection) } else { - var character = _currentMapStateRepository.Characters.Single(x => x.ID == playerID); - var updatedRenderProps = character.RenderProperties.WithDirection(playerDirection); - var updatedCharacter = character.WithRenderProperties(updatedRenderProps); + var character = _currentMapStateRepository.Characters.OptionalSingle(x => x.ID == playerID); - _currentMapStateRepository.Characters.Remove(character); - _currentMapStateRepository.Characters.Add(updatedCharacter); + if (character.HasValue) + { + var updatedRenderProps = character.Value.RenderProperties.WithDirection(playerDirection); + var updatedCharacter = character.Value.WithRenderProperties(updatedRenderProps); + + _currentMapStateRepository.Characters.Remove(character.Value); + _currentMapStateRepository.Characters.Add(updatedCharacter); + } } }