From 64a14bb4d9aeeacf2fd1b8fa1d20594aef079ef6 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 26 Jan 2026 10:51:18 -0500 Subject: [PATCH] Fixing item stacks --- Game/Dungeon/DungeonHandler.cs | 61 ++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index 249ecd7..2ce8ac4 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -3690,22 +3690,52 @@ public sealed partial class DungeonHandler : IClientMessageHandler using var scope = _scopeFactory.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - // Get both items (one or both might be null) + // Get both items with their definitions and crafted data var sourceItem = await db.Set() + .Include(i => i.ItemDefinition) + .Include(i => i.CraftedItem) .FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.SlotIndex == request.SourceSlot && !i.IsEquipped, ct); var targetItem = await db.Set() + .Include(i => i.ItemDefinition) + .Include(i => i.CraftedItem) .FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.SlotIndex == request.TargetSlot && !i.IsEquipped, ct); if (sourceItem == null) return; // Nothing to move - // Simple swap of slot indices - if (targetItem != null) + // Check if items can be stacked (same item definition, both stackable, and identical crafted items if any) + if (targetItem != null && + sourceItem.ItemDefinition != null && + targetItem.ItemDefinition != null && + sourceItem.ItemDefinitionId == targetItem.ItemDefinitionId && + sourceItem.ItemDefinition.Stackable && + CanStackItems(sourceItem, targetItem)) { - // Swap the slots - targetItem.SlotIndex = request.SourceSlot; + // Combine stacks + var maxStack = sourceItem.ItemDefinition.MaxStack; + var spaceAvailable = maxStack - targetItem.Quantity; + var amountToMove = Math.Min(sourceItem.Quantity, spaceAvailable); + + if (amountToMove > 0) + { + targetItem.Quantity += amountToMove; + sourceItem.Quantity -= amountToMove; + + if (sourceItem.Quantity <= 0) + { + db.Set().Remove(sourceItem); + } + } + } + else + { + // Simple swap of slot indices + if (targetItem != null) + { + targetItem.SlotIndex = request.SourceSlot; + } + sourceItem.SlotIndex = request.TargetSlot; } - sourceItem.SlotIndex = request.TargetSlot; await db.SaveChangesAsync(ct); @@ -3713,6 +3743,25 @@ public sealed partial class DungeonHandler : IClientMessageHandler await HandleInventoryRequest(connection, session, ct); } + /// + /// Checks if two items can be stacked together. + /// Items without crafted data can always stack (if same definition and stackable). + /// Items with crafted data can only stack if they have the same CraftedItem.Id. + /// + private static bool CanStackItems(DungeonCharacterItem source, DungeonCharacterItem target) + { + // If neither has crafted data, they can stack + if (source.CraftedItem == null && target.CraftedItem == null) + return true; + + // If one has crafted data and the other doesn't, they can't stack + if (source.CraftedItem == null || target.CraftedItem == null) + return false; + + // Both have crafted data - they can only stack if it's the same crafted item + return source.CraftedItem.Id == target.CraftedItem.Id; + } + private async Task HandleAutoStackInventory(IConnection connection, DungeonPlayerSession session, CancellationToken ct) { using var scope = _scopeFactory.CreateScope();