Setting Equipped item to be swapped in htbar when equipinging via hotbar

This commit is contained in:
dingo
2026-01-26 12:59:22 -05:00
parent 16fb38f606
commit 39920c81a2
2 changed files with 41 additions and 2 deletions
+40 -2
View File
@@ -1494,7 +1494,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
// Send updated inventory state (includes hotbar)
await HandleInventoryRequest(connection, session, ct);
_log.LogError("Player {Name} assigned inventory slot {InvSlot} to hotbar slot {HotbarSlot}",
_log.LogDebug("Player {Name} assigned inventory slot {InvSlot} to hotbar slot {HotbarSlot}",
session.Character.Name, request.InventorySlot, request.HotbarSlot);
}
@@ -3435,6 +3435,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
// Unequip any existing item in that slot
var existingEquipped = await db.Set<DungeonCharacterItem>()
.FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.IsEquipped && i.EquipSlot == equipSlot, ct);
// We'll need this for hotbar remapping
int? movedOldEquipToInventorySlot = null;
if (existingEquipped != null)
{
@@ -3445,7 +3447,43 @@ public sealed partial class DungeonHandler : IClientMessageHandler
.Where(i => i.CharacterId == session.CharacterId && !i.IsEquipped)
.Select(i => i.SlotIndex)
.ToListAsync(ct);
existingEquipped.SlotIndex = Enumerable.Range(0, 30).FirstOrDefault(s => !usedSlots.Contains(s));
var emptySlot = Enumerable.Range(0, 30)
.Where(s => !usedSlots.Contains(s))
.DefaultIfEmpty(-1)
.First();
if (emptySlot == -1)
{
// Inventory full: decide your policy (swap into the source slot, reject equip, etc.)
// Safest is to reject equip rather than corrupting inventory/hotbar state.
_log.LogWarning("Cannot equip item {ItemName} - inventory full, nowhere to place unequipped item", item.ItemDefinition.Name);
return;
}
existingEquipped.SlotIndex = emptySlot;
movedOldEquipToInventorySlot = emptySlot;
// existingEquipped.SlotIndex = Enumerable.Range(0, 30).FirstOrDefault(s => !usedSlots.Contains(s));
}
// Any hotbar entries pointing at the *inventory slot being equipped from* should now point at
// the slot where the previous equipped item landed (if any), otherwise remove them.
var affectedHotbarEntries = await db.Set<DungeonBeltItem>()
.Where(b => b.CharacterId == session.CharacterId &&
b.InventorySlotIndex == request.SlotIndex)
.ToListAsync(ct);
if (affectedHotbarEntries.Count > 0)
{
if (movedOldEquipToInventorySlot.HasValue)
{
foreach (var belt in affectedHotbarEntries)
belt.InventorySlotIndex = movedOldEquipToInventorySlot.Value;
}
else
{
// No item replaced; the inventory slot becomes empty, so clear hotbar mapping
db.Set<DungeonBeltItem>().RemoveRange(affectedHotbarEntries);
}
}
// Equip the new item