From 39920c81a22e08e77c11457931829a1ba0522f99 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 26 Jan 2026 12:59:22 -0500 Subject: [PATCH] Setting Equipped item to be swapped in htbar when equipinging via hotbar --- Game/Dungeon/DungeonHandler.cs | 42 +++++++++++++++++++++++++-- Networking/Messages/DungeonOpCodes.cs | 1 + 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index ea6f264..f5b6c29 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -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() .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() + .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().RemoveRange(affectedHotbarEntries); + } } // Equip the new item diff --git a/Networking/Messages/DungeonOpCodes.cs b/Networking/Messages/DungeonOpCodes.cs index e880018..cc56d9d 100644 --- a/Networking/Messages/DungeonOpCodes.cs +++ b/Networking/Messages/DungeonOpCodes.cs @@ -65,6 +65,7 @@ public static class DungeonOpCodes public const int MOVE_ITEM = 258; // Move/swap item between inventory slots public const int AUTO_STACK_INVENTORY = 259; // Auto-stack all stackable items in inventory + // Chat (260-269) public const int CHAT_SEND = 260; public const int CHAT_MESSAGE = 261;