Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust AddSpellToBar #4131

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions Source/ACE.Database/Models/Shard/CharacterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,37 +530,53 @@ public static List<CharacterPropertiesSpellBar> GetSpellsInBar(this Character ch
/// <summary>
/// This will incrememt all existing spells on the same barNumber at or after indexInBar by one before adding the new spell.
/// </summary>
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();
}
}

Expand Down
6 changes: 4 additions & 2 deletions Source/ACE.Server/WorldObjects/Player_Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,11 @@ public List<SpellBarPositions> GetSpellsInSpellBar(int barId)
/// </summary>
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;
}

/// <summary>
Expand Down