Fix inventory replace bug

master
est31 2015-08-19 02:28:37 +02:00
parent c2d23ff9cc
commit 1fadf7f21e
3 changed files with 12 additions and 7 deletions

View File

@ -163,7 +163,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
std::getline(is, tmp, ' '); std::getline(is, tmp, ' ');
if(!tmp.empty()) if(!tmp.empty())
throw SerializationError("Unexpected text after item name"); throw SerializationError("Unexpected text after item name");
if(name == "MaterialItem") if(name == "MaterialItem")
{ {
// Obsoleted on 2011-07-30 // Obsoleted on 2011-07-30
@ -478,7 +478,7 @@ void InventoryList::setName(const std::string &name)
void InventoryList::serialize(std::ostream &os) const void InventoryList::serialize(std::ostream &os) const
{ {
//os.imbue(std::locale("C")); //os.imbue(std::locale("C"));
os<<"Width "<<m_width<<"\n"; os<<"Width "<<m_width<<"\n";
for(u32 i=0; i<m_items.size(); i++) for(u32 i=0; i<m_items.size(); i++)
@ -653,7 +653,7 @@ ItemStack InventoryList::addItem(const ItemStack &newitem_)
if(newitem.empty()) if(newitem.empty())
return newitem; return newitem;
/* /*
First try to find if it could be added to some existing items First try to find if it could be added to some existing items
*/ */
@ -818,7 +818,7 @@ void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
} }
u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
u32 count, bool swap_if_needed) u32 count, bool swap_if_needed, bool *did_swap)
{ {
if(this == dest && i == dest_i) if(this == dest && i == dest_i)
return count; return count;
@ -850,6 +850,10 @@ u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
// If olditem is returned, nothing was added. // If olditem is returned, nothing was added.
// Swap the items // Swap the items
if (nothing_added && swap_if_needed) { if (nothing_added && swap_if_needed) {
// Tell that we swapped
if (did_swap != NULL) {
*did_swap = true;
}
// Take item from source list // Take item from source list
item1 = changeItem(i, ItemStack()); item1 = changeItem(i, ItemStack());
// Adding was not possible, swap the items. // Adding was not possible, swap the items.

View File

@ -246,7 +246,7 @@ public:
// count is the maximum number of items to move (0 for everything) // count is the maximum number of items to move (0 for everything)
// returns number of moved items // returns number of moved items
u32 moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 moveItem(u32 i, InventoryList *dest, u32 dest_i,
u32 count = 0, bool swap_if_needed = true); u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
// like moveItem, but without a fixed destination index // like moveItem, but without a fixed destination index
// also with optional rollback recording // also with optional rollback recording

View File

@ -375,8 +375,9 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
If something is wrong (source item is empty, destination is the If something is wrong (source item is empty, destination is the
same as source), nothing happens same as source), nothing happens
*/ */
bool did_swap = false;
move_count = list_from->moveItem(from_i, move_count = list_from->moveItem(from_i,
list_to, to_i, count, !caused_by_move_somewhere); list_to, to_i, count, !caused_by_move_somewhere, &did_swap);
// If source is infinite, reset it's stack // If source is infinite, reset it's stack
if (src_can_take_count == -1) { if (src_can_take_count == -1) {
@ -397,7 +398,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
} }
} }
} }
if (move_count > 0) { if (move_count > 0 || did_swap) {
list_from->deleteItem(from_i); list_from->deleteItem(from_i);
list_from->addItem(from_i, from_stack_was); list_from->addItem(from_i, from_stack_was);
} }