1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Inventory: Fix order of callbacks when swapping items

This commit is contained in:
SmallJoker 2022-07-26 20:40:27 +02:00 committed by SmallJoker
parent d5d6e36ae0
commit 4245a7604b
6 changed files with 119 additions and 95 deletions

View file

@ -758,54 +758,53 @@ void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
if (!leftover.empty()) {
// Add the remaining part back to the source item
addItem(i, leftover);
ItemStack &source = getItem(i);
source.add(leftover.count); // do NOT use addItem to allow oversized stacks!
}
}
u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
ItemStack InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
u32 count, bool swap_if_needed, bool *did_swap)
{
ItemStack moved;
if (this == dest && i == dest_i)
return count;
return moved;
// Take item from source list
ItemStack item1;
if (count == 0)
item1 = changeItem(i, ItemStack());
moved = changeItem(i, ItemStack());
else
item1 = takeItem(i, count);
if (item1.empty())
return 0;
moved = takeItem(i, count);
// Try to add the item to destination list
count = item1.count;
item1 = dest->addItem(dest_i, item1);
ItemStack leftover = dest->addItem(dest_i, moved);
// If something is returned, the item was not fully added
if (!item1.empty()) {
bool nothing_added = (item1.count == count);
if (!leftover.empty()) {
// Keep track of how many we actually moved
moved.remove(leftover.count);
// Add any leftover stack back to the source stack.
item1.add(getItem(i).count); // leftover + source count
changeItem(i, item1); // do NOT use addItem to allow oversized stacks!
leftover.add(getItem(i).count); // leftover + source count
changeItem(i, leftover); // do NOT use addItem to allow oversized stacks!
leftover.clear();
// Swap if no items could be moved
if (nothing_added && swap_if_needed) {
if (moved.empty() && swap_if_needed) {
// Tell that we swapped
if (did_swap != NULL) {
*did_swap = true;
}
// Take item from source list
item1 = changeItem(i, ItemStack());
moved = changeItem(i, ItemStack());
// Adding was not possible, swap the items.
ItemStack item2 = dest->changeItem(dest_i, item1);
ItemStack item2 = dest->changeItem(dest_i, moved);
// Put item from destination list to the source list
changeItem(i, item2);
item1.clear(); // no leftover
}
}
return (count - item1.count);
return moved;
}
void InventoryList::checkResizeLock()