diff --git a/Source/ACE.Database/Models/Shard/CharacterExtensions.cs b/Source/ACE.Database/Models/Shard/CharacterExtensions.cs index 60569d7218..a37d40fc20 100644 --- a/Source/ACE.Database/Models/Shard/CharacterExtensions.cs +++ b/Source/ACE.Database/Models/Shard/CharacterExtensions.cs @@ -530,37 +530,53 @@ public static List GetSpellsInBar(this Character ch /// /// This will incrememt all existing spells on the same barNumber at or after indexInBar by one before adding the new spell. /// - public static void AddSpellToBar(this Character character, uint barNumber, uint indexInBar, uint spell, ReaderWriterLockSlim rwLock) + public static bool AddSpellToBar(this Character character, uint barNumber, uint indexInBar, uint spell, ReaderWriterLockSlim rwLock) { - rwLock.EnterWriteLock(); + rwLock.EnterUpgradeableReadLock(); try { - var spellCountInThisBar = character.CharacterPropertiesSpellBar.Count(x => x.SpellBarNumber == barNumber + 1); + var entity = character.CharacterPropertiesSpellBar.FirstOrDefault(x => x.SpellBarNumber == barNumber + 1 && x.SpellId == spell); + if (entity == null) + { + rwLock.EnterWriteLock(); + try + { + var spellCountInThisBar = character.CharacterPropertiesSpellBar.Count(x => x.SpellBarNumber == barNumber + 1); - //Console.WriteLine($"Character.AddSpellToBar.Entry: barNumber = {barNumber} ({barNumber + 1}) | indexInBar = {indexInBar} ({indexInBar + 1}) | spell = {spell} | spellCountInThisBar = {spellCountInThisBar}"); + //Console.WriteLine($"Character.AddSpellToBar.Entry: barNumber = {barNumber} ({barNumber + 1}) | indexInBar = {indexInBar} ({indexInBar + 1}) | spell = {spell} | spellCountInThisBar = {spellCountInThisBar}"); - if (indexInBar > spellCountInThisBar) - indexInBar = (uint)(spellCountInThisBar); + if (indexInBar > spellCountInThisBar) + indexInBar = (uint)(spellCountInThisBar); - // We must increment the position of existing spells in the bar that exist on or after this position - foreach (var property in character.CharacterPropertiesSpellBar.OrderBy(x => x.SpellBarIndex)) - { - if (property.SpellBarNumber == barNumber + 1 && property.SpellBarIndex >= indexInBar + 1) + // We must increment the position of existing spells in the bar that exist on or after this position + foreach (var property in character.CharacterPropertiesSpellBar.OrderBy(x => x.SpellBarIndex)) + { + if (property.SpellBarNumber == barNumber + 1 && property.SpellBarIndex >= indexInBar + 1) + { + property.SpellBarIndex++; + //Console.WriteLine($"Character.AddSpellToBar.Adjust: SpellBarNumber = {property.SpellBarNumber} | SpellBarIndex = {property.SpellBarIndex} ({property.SpellBarIndex - 1}) | SpellId = {property.SpellId}"); + } + } + + entity = new CharacterPropertiesSpellBar { CharacterId = character.Id, SpellBarNumber = barNumber + 1, SpellBarIndex = indexInBar + 1, SpellId = spell, Character = character }; + + character.CharacterPropertiesSpellBar.Add(entity); + + //Console.WriteLine($"Character.AddSpellToBar.Add: barNumber = {barNumber + 1} | indexInBar = {indexInBar + 1} | spell = {spell}"); + + return true; + } + finally { - property.SpellBarIndex++; - //Console.WriteLine($"Character.AddSpellToBar.Adjust: SpellBarNumber = {property.SpellBarNumber} | SpellBarIndex = {property.SpellBarIndex} ({property.SpellBarIndex - 1}) | SpellId = {property.SpellId}"); + rwLock.ExitWriteLock(); } } - var entity = new CharacterPropertiesSpellBar { CharacterId = character.Id, SpellBarNumber = barNumber + 1, SpellBarIndex = indexInBar + 1, SpellId = spell, Character = character }; - - character.CharacterPropertiesSpellBar.Add(entity); - - //Console.WriteLine($"Character.AddSpellToBar.Add: barNumber = {barNumber + 1} | indexInBar = {indexInBar + 1} | spell = {spell}"); + return false; } finally { - rwLock.ExitWriteLock(); + rwLock.ExitUpgradeableReadLock(); } } diff --git a/Source/ACE.Server/WorldObjects/Player_Character.cs b/Source/ACE.Server/WorldObjects/Player_Character.cs index 388a55bfd6..9af5be8d2f 100644 --- a/Source/ACE.Server/WorldObjects/Player_Character.cs +++ b/Source/ACE.Server/WorldObjects/Player_Character.cs @@ -296,9 +296,11 @@ public List GetSpellsInSpellBar(int barId) /// public void HandleActionAddSpellFavorite(uint spellId, uint spellBarPositionId, uint spellBarId) { - Character.AddSpellToBar(spellBarId, spellBarPositionId, spellId, CharacterDatabaseLock); + if (spellBarId > 7 || spellBarPositionId > (uint)SpellId.NumSpells || !SpellIsKnown(spellId)) + return; - CharacterChangesDetected = true; + if (Character.AddSpellToBar(spellBarId, spellBarPositionId, spellId, CharacterDatabaseLock)) + CharacterChangesDetected = true; } ///