Skip to content

Commit 7bbb9b0

Browse files
committedJul 19, 2015
MoveItemSomewhere double bugfix
-> Fix bug where MoveSomewhere from an infinite source would fill the destination inventory with copies of itself. -> Fix bug where MoveSomewhere would needlessly call callbacks. -> Remove trailing whitespaces
1 parent 4046f3e commit 7bbb9b0

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed
 

Diff for: ‎src/inventorymanager.cpp

+39-25
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
171171
{
172172
Inventory *inv_from = mgr->getInventory(from_inv);
173173
Inventory *inv_to = mgr->getInventory(to_inv);
174-
174+
175175
if (!inv_from) {
176176
infostream << "IMoveAction::apply(): FAIL: source inventory not found: "
177177
<< "from_inv=\""<<from_inv.dump() << "\""
@@ -271,7 +271,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
271271

272272
int src_can_take_count = 0xffff;
273273
int dst_can_put_count = 0xffff;
274-
274+
275275
/* Query detached inventories */
276276

277277
// Move occurs in the same detached inventory
@@ -338,7 +338,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
338338
}
339339

340340
int old_count = count;
341-
341+
342342
/* Modify count according to collected data */
343343
count = try_take_count;
344344
if(src_can_take_count != -1 && count > src_can_take_count)
@@ -348,7 +348,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
348348
/* Limit according to source item count */
349349
if(count > list_from->getItem(from_i).count)
350350
count = list_from->getItem(from_i).count;
351-
351+
352352
/* If no items will be moved, don't go further */
353353
if(count == 0)
354354
{
@@ -379,21 +379,28 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
379379
list_to, to_i, count, !caused_by_move_somewhere);
380380

381381
// If source is infinite, reset it's stack
382-
if(src_can_take_count == -1){
383-
// If destination stack is of different type and there are leftover
384-
// items, attempt to put the leftover items to a different place in the
385-
// destination inventory.
386-
// The client-side GUI will try to guess if this happens.
387-
if(from_stack_was.name != to_stack_was.name){
388-
for(u32 i=0; i<list_to->getSize(); i++){
389-
if(list_to->getItem(i).empty()){
390-
list_to->changeItem(i, to_stack_was);
391-
break;
382+
if (src_can_take_count == -1) {
383+
// For the caused_by_move_somewhere == true case we didn't force-put the item,
384+
// which guarantees there is no leftover, and code below would duplicate the
385+
// (not replaced) to_stack_was item.
386+
if (!caused_by_move_somewhere) {
387+
// If destination stack is of different type and there are leftover
388+
// items, attempt to put the leftover items to a different place in the
389+
// destination inventory.
390+
// The client-side GUI will try to guess if this happens.
391+
if (from_stack_was.name != to_stack_was.name) {
Has conversations. Original line has conversations.
392+
for (u32 i = 0; i < list_to->getSize(); i++) {
Has a conversation. Original line has a conversation.
393+
if (list_to->getItem(i).empty()) {
394+
list_to->changeItem(i, to_stack_was);
395+
break;
396+
}
392397
}
393398
}
394399
}
395-
list_from->deleteItem(from_i);
396-
list_from->addItem(from_i, from_stack_was);
400+
if (move_count > 0) {
401+
list_from->deleteItem(from_i);
402+
list_from->addItem(from_i, from_stack_was);
403+
}
397404
}
398405
// If destination is infinite, reset it's stack and take count from source
399406
if(dst_can_put_count == -1){
@@ -416,6 +423,13 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
416423
<< " i=" << to_i
417424
<< std::endl;
418425

426+
// If we are inside the move somewhere loop, we don't need to report
427+
// anything if nothing happened (perhaps we don't need to report
428+
// anything for caused_by_move_somewhere == true, but this way its safer)
429+
if (caused_by_move_somewhere && move_count == 0) {
430+
return;
431+
}
432+
419433
/*
420434
Record rollback information
421435
*/
@@ -454,7 +468,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
454468
/*
455469
Report move to endpoints
456470
*/
457-
471+
458472
/* Detached inventories */
459473

460474
// Both endpoints are same detached
@@ -507,7 +521,7 @@ void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
507521
from_inv.p, from_list, from_i, src_item, player);
508522
}
509523
}
510-
524+
511525
mgr->setInventoryModified(from_inv, false);
512526
if(inv_from != inv_to)
513527
mgr->setInventoryModified(to_inv, false);
@@ -567,7 +581,7 @@ IDropAction::IDropAction(std::istream &is)
567581
void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
568582
{
569583
Inventory *inv_from = mgr->getInventory(from_inv);
570-
584+
571585
if(!inv_from){
572586
infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
573587
<<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
@@ -627,7 +641,7 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
627641

628642
if(src_can_take_count != -1 && src_can_take_count < take_count)
629643
take_count = src_can_take_count;
630-
644+
631645
int actually_dropped_count = 0;
632646

633647
ItemStack src_item = list_from->getItem(from_i);
@@ -644,7 +658,7 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
644658
infostream<<"Actually dropped no items"<<std::endl;
645659
return;
646660
}
647-
661+
648662
// If source isn't infinite
649663
if(src_can_take_count != -1){
650664
// Take item from source list
@@ -662,13 +676,13 @@ void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGame
662676
<<" list=\""<<from_list<<"\""
663677
<<" i="<<from_i
664678
<<std::endl;
665-
679+
666680
src_item.count = actually_dropped_count;
667681

668682
/*
669683
Report drop to endpoints
670684
*/
671-
685+
672686
// Source is detached
673687
if(from_inv.type == InventoryLocation::DETACHED)
674688
{
@@ -752,7 +766,7 @@ void ICraftAction::apply(InventoryManager *mgr,
752766
ServerActiveObject *player, IGameDef *gamedef)
753767
{
754768
Inventory *inv_craft = mgr->getInventory(craft_inv);
755-
769+
756770
if (!inv_craft) {
757771
infostream << "ICraftAction::apply(): FAIL: inventory not found: "
758772
<< "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
@@ -872,7 +886,7 @@ bool getCraftingResult(Inventory *inv, ItemStack& result,
872886
bool decrementInput, IGameDef *gamedef)
873887
{
874888
DSTACK(__FUNCTION_NAME);
875-
889+
876890
result.clear();
877891

878892
// Get the InventoryList in which we will operate

Diff for: ‎src/inventorymanager.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class InventoryManager
108108
public:
109109
InventoryManager(){}
110110
virtual ~InventoryManager(){}
111-
111+
112112
// Get an inventory (server and client)
113113
virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
114114
// Set modified (will be saved and sent over network; only on server)
@@ -124,7 +124,7 @@ class InventoryManager
124124
struct InventoryAction
125125
{
126126
static InventoryAction * deSerialize(std::istream &is);
127-
127+
128128
virtual u16 getType() const = 0;
129129
virtual void serialize(std::ostream &os) const = 0;
130130
virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
@@ -149,7 +149,7 @@ struct IMoveAction : public InventoryAction
149149
// related to movement to somewhere
150150
bool caused_by_move_somewhere;
151151
u32 move_count;
152-
152+
153153
IMoveAction()
154154
{
155155
count = 0;
@@ -159,7 +159,7 @@ struct IMoveAction : public InventoryAction
159159
caused_by_move_somewhere = false;
160160
move_count = 0;
161161
}
162-
162+
163163
IMoveAction(std::istream &is, bool somewhere);
164164

165165
u16 getType() const
@@ -195,13 +195,13 @@ struct IDropAction : public InventoryAction
195195
InventoryLocation from_inv;
196196
std::string from_list;
197197
s16 from_i;
198-
198+
199199
IDropAction()
200200
{
201201
count = 0;
202202
from_i = -1;
203203
}
204-
204+
205205
IDropAction(std::istream &is);
206206

207207
u16 getType() const
@@ -228,12 +228,12 @@ struct ICraftAction : public InventoryAction
228228
// count=0 means "everything"
229229
u16 count;
230230
InventoryLocation craft_inv;
231-
231+
232232
ICraftAction()
233233
{
234234
count = 0;
235235
}
236-
236+
237237
ICraftAction(std::istream &is);
238238

239239
u16 getType() const

0 commit comments

Comments
 (0)
Please sign in to comment.