Fixing item stacks

This commit is contained in:
dingo
2026-01-26 10:51:18 -05:00
parent 199c7bb918
commit 64a14bb4d9
+55 -6
View File
@@ -3690,22 +3690,52 @@ public sealed partial class DungeonHandler : IClientMessageHandler
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
// Get both items (one or both might be null)
// Get both items with their definitions and crafted data
var sourceItem = await db.Set<DungeonCharacterItem>()
.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<DungeonCharacterItem>()
.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<DungeonCharacterItem>().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);
}
/// <summary>
/// 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.
/// </summary>
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();