24d1fa6ff470c7873455e7126ce153ff5aa96630
[oweals/minetest.git] / src / inventorymanager.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "inventorymanager.h"
21 #include "debug.h"
22 #include "log.h"
23 #include "serverenvironment.h"
24 #include "scripting_server.h"
25 #include "serverobject.h"
26 #include "settings.h"
27 #include "craftdef.h"
28 #include "rollback_interface.h"
29 #include "util/strfnd.h"
30 #include "util/basic_macros.h"
31
32 #define PLAYER_TO_SA(p)   p->getEnv()->getScriptIface()
33
34 /*
35         InventoryLocation
36 */
37
38 std::string InventoryLocation::dump() const
39 {
40         std::ostringstream os(std::ios::binary);
41         serialize(os);
42         return os.str();
43 }
44
45 void InventoryLocation::serialize(std::ostream &os) const
46 {
47         switch (type) {
48         case InventoryLocation::UNDEFINED:
49                 os<<"undefined";
50                 break;
51         case InventoryLocation::CURRENT_PLAYER:
52                 os<<"current_player";
53                 break;
54         case InventoryLocation::PLAYER:
55                 os<<"player:"<<name;
56                 break;
57         case InventoryLocation::NODEMETA:
58                 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
59                 break;
60         case InventoryLocation::DETACHED:
61                 os<<"detached:"<<name;
62                 break;
63         default:
64                 FATAL_ERROR("Unhandled inventory location type");
65         }
66 }
67
68 void InventoryLocation::deSerialize(std::istream &is)
69 {
70         std::string tname;
71         std::getline(is, tname, ':');
72         if (tname == "undefined") {
73                 type = InventoryLocation::UNDEFINED;
74         } else if (tname == "current_player") {
75                 type = InventoryLocation::CURRENT_PLAYER;
76         } else if (tname == "player") {
77                 type = InventoryLocation::PLAYER;
78                 std::getline(is, name, '\n');
79         } else if (tname == "nodemeta") {
80                 type = InventoryLocation::NODEMETA;
81                 std::string pos;
82                 std::getline(is, pos, '\n');
83                 Strfnd fn(pos);
84                 p.X = stoi(fn.next(","));
85                 p.Y = stoi(fn.next(","));
86                 p.Z = stoi(fn.next(","));
87         } else if (tname == "detached") {
88                 type = InventoryLocation::DETACHED;
89                 std::getline(is, name, '\n');
90         } else {
91                 infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
92                 throw SerializationError("Unknown InventoryLocation type");
93         }
94 }
95
96 void InventoryLocation::deSerialize(const std::string &s)
97 {
98         std::istringstream is(s, std::ios::binary);
99         deSerialize(is);
100 }
101
102 /*
103         InventoryAction
104 */
105
106 InventoryAction *InventoryAction::deSerialize(std::istream &is)
107 {
108         std::string type;
109         std::getline(is, type, ' ');
110
111         InventoryAction *a = nullptr;
112
113         if (type == "Move") {
114                 a = new IMoveAction(is, false);
115         } else if (type == "MoveSomewhere") {
116                 a = new IMoveAction(is, true);
117         } else if (type == "Drop") {
118                 a = new IDropAction(is);
119         } else if (type == "Craft") {
120                 a = new ICraftAction(is);
121         }
122
123         return a;
124 }
125
126 /*
127         IMoveAction
128 */
129
130 IMoveAction::IMoveAction(std::istream &is, bool somewhere) :
131                 move_somewhere(somewhere)
132 {
133         std::string ts;
134
135         std::getline(is, ts, ' ');
136         count = stoi(ts);
137
138         std::getline(is, ts, ' ');
139         from_inv.deSerialize(ts);
140
141         std::getline(is, from_list, ' ');
142
143         std::getline(is, ts, ' ');
144         from_i = stoi(ts);
145
146         std::getline(is, ts, ' ');
147         to_inv.deSerialize(ts);
148
149         std::getline(is, to_list, ' ');
150
151         if (!somewhere) {
152                 std::getline(is, ts, ' ');
153                 to_i = stoi(ts);
154         }
155 }
156
157 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
158 {
159         Inventory *inv_from = mgr->getInventory(from_inv);
160         Inventory *inv_to = mgr->getInventory(to_inv);
161
162         if (!inv_from) {
163                 infostream << "IMoveAction::apply(): FAIL: source inventory not found: "
164                         << "from_inv=\""<<from_inv.dump() << "\""
165                         << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
166                 return;
167         }
168         if (!inv_to) {
169                 infostream << "IMoveAction::apply(): FAIL: destination inventory not found: "
170                         << "from_inv=\"" << from_inv.dump() << "\""
171                         << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
172                 return;
173         }
174
175         InventoryList *list_from = inv_from->getList(from_list);
176         InventoryList *list_to = inv_to->getList(to_list);
177
178         /*
179                 If a list doesn't exist or the source item doesn't exist
180         */
181         if (!list_from) {
182                 infostream << "IMoveAction::apply(): FAIL: source list not found: "
183                         << "from_inv=\"" << from_inv.dump() << "\""
184                         << ", from_list=\"" << from_list << "\"" << std::endl;
185                 return;
186         }
187         if (!list_to) {
188                 infostream << "IMoveAction::apply(): FAIL: destination list not found: "
189                         << "to_inv=\""<<to_inv.dump() << "\""
190                         << ", to_list=\"" << to_list << "\"" << std::endl;
191                 return;
192         }
193
194         if (move_somewhere) {
195                 s16 old_to_i = to_i;
196                 u16 old_count = count;
197                 caused_by_move_somewhere = true;
198                 move_somewhere = false;
199
200                 infostream << "IMoveAction::apply(): moving item somewhere"
201                         << " msom=" << move_somewhere
202                         << " count=" << count
203                         << " from inv=\"" << from_inv.dump() << "\""
204                         << " list=\"" << from_list << "\""
205                         << " i=" << from_i
206                         << " to inv=\"" << to_inv.dump() << "\""
207                         << " list=\"" << to_list << "\""
208                         << std::endl;
209
210                 // Try to add the item to destination list
211                 s16 dest_size = list_to->getSize();
212                 // First try all the non-empty slots
213                 for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
214                         if (!list_to->getItem(dest_i).empty()) {
215                                 to_i = dest_i;
216                                 apply(mgr, player, gamedef);
217                                 count -= move_count;
218                         }
219                 }
220
221                 // Then try all the empty ones
222                 for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
223                         if (list_to->getItem(dest_i).empty()) {
224                                 to_i = dest_i;
225                                 apply(mgr, player, gamedef);
226                                 count -= move_count;
227                         }
228                 }
229
230                 to_i = old_to_i;
231                 count = old_count;
232                 caused_by_move_somewhere = false;
233                 move_somewhere = true;
234                 return;
235         }
236
237         if ((u16)to_i > list_to->getSize()) {
238                 infostream << "IMoveAction::apply(): FAIL: destination index out of bounds: "
239                         << "to_i=" << to_i
240                         << ", size=" << list_to->getSize() << std::endl;
241                 return;
242         }
243         /*
244                 Do not handle rollback if both inventories are that of the same player
245         */
246         bool ignore_rollback = (
247                 from_inv.type == InventoryLocation::PLAYER &&
248                 from_inv == to_inv);
249
250         /*
251                 Collect information of endpoints
252         */
253
254         int try_take_count = count;
255         if (try_take_count == 0)
256                 try_take_count = list_from->getItem(from_i).count;
257
258         int src_can_take_count = 0xffff;
259         int dst_can_put_count = 0xffff;
260
261         /* Query detached inventories */
262
263         // Move occurs in the same detached inventory
264         if (from_inv.type == InventoryLocation::DETACHED &&
265                         from_inv == to_inv) {
266                 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowMove(
267                         *this, try_take_count, player);
268                 dst_can_put_count = src_can_take_count;
269         } else {
270                 // Destination is detached
271                 if (to_inv.type == InventoryLocation::DETACHED) {
272                         ItemStack src_item = list_from->getItem(from_i);
273                         src_item.count = try_take_count;
274                         dst_can_put_count = PLAYER_TO_SA(player)->detached_inventory_AllowPut(
275                                 *this, src_item, player);
276                 }
277                 // Source is detached
278                 if (from_inv.type == InventoryLocation::DETACHED) {
279                         ItemStack src_item = list_from->getItem(from_i);
280                         src_item.count = try_take_count;
281                         src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
282                                 *this, src_item, player);
283                 }
284         }
285
286         /* Query node metadata inventories */
287
288         // Both endpoints are nodemeta
289         // Move occurs in the same nodemeta inventory
290         if (from_inv.type == InventoryLocation::NODEMETA &&
291                         from_inv == to_inv) {
292                 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowMove(
293                         *this, try_take_count, player);
294                 dst_can_put_count = src_can_take_count;
295         } else {
296                 // Destination is nodemeta
297                 if (to_inv.type == InventoryLocation::NODEMETA) {
298                         ItemStack src_item = list_from->getItem(from_i);
299                         src_item.count = try_take_count;
300                         dst_can_put_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowPut(
301                                 *this, src_item, player);
302                 }
303                 // Source is nodemeta
304                 if (from_inv.type == InventoryLocation::NODEMETA) {
305                         ItemStack src_item = list_from->getItem(from_i);
306                         src_item.count = try_take_count;
307                         src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
308                                 *this, src_item, player);
309                 }
310         }
311
312         // Query player inventories
313
314         // Move occurs in the same player inventory
315         if (from_inv.type == InventoryLocation::PLAYER &&
316                         from_inv == to_inv) {
317                 src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowMove(
318                         *this, try_take_count, player);
319                 dst_can_put_count = src_can_take_count;
320         } else {
321                 // Destination is a player
322                 if (to_inv.type == InventoryLocation::PLAYER) {
323                         ItemStack src_item = list_from->getItem(from_i);
324                         src_item.count = try_take_count;
325                         dst_can_put_count = PLAYER_TO_SA(player)->player_inventory_AllowPut(
326                                 *this, src_item, player);
327                 }
328                 // Source is a player
329                 if (from_inv.type == InventoryLocation::PLAYER) {
330                         ItemStack src_item = list_from->getItem(from_i);
331                         src_item.count = try_take_count;
332                         src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowTake(
333                                 *this, src_item, player);
334                 }
335         }
336
337         int old_count = count;
338
339         /* Modify count according to collected data */
340         count = try_take_count;
341         if (src_can_take_count != -1 && count > src_can_take_count)
342                 count = src_can_take_count;
343         if (dst_can_put_count != -1 && count > dst_can_put_count)
344                 count = dst_can_put_count;
345         /* Limit according to source item count */
346         if (count > list_from->getItem(from_i).count)
347                 count = list_from->getItem(from_i).count;
348
349         /* If no items will be moved, don't go further */
350         if (count == 0) {
351                 infostream<<"IMoveAction::apply(): move was completely disallowed:"
352                                 <<" count="<<old_count
353                                 <<" from inv=\""<<from_inv.dump()<<"\""
354                                 <<" list=\""<<from_list<<"\""
355                                 <<" i="<<from_i
356                                 <<" to inv=\""<<to_inv.dump()<<"\""
357                                 <<" list=\""<<to_list<<"\""
358                                 <<" i="<<to_i
359                                 <<std::endl;
360                 return;
361         }
362
363         ItemStack src_item = list_from->getItem(from_i);
364         src_item.count = count;
365         ItemStack from_stack_was = list_from->getItem(from_i);
366         ItemStack to_stack_was = list_to->getItem(to_i);
367
368         /*
369                 Perform actual move
370
371                 If something is wrong (source item is empty, destination is the
372                 same as source), nothing happens
373         */
374         bool did_swap = false;
375         move_count = list_from->moveItem(from_i,
376                 list_to, to_i, count, !caused_by_move_somewhere, &did_swap);
377
378         // If source is infinite, reset it's stack
379         if (src_can_take_count == -1) {
380                 // For the caused_by_move_somewhere == true case we didn't force-put the item,
381                 // which guarantees there is no leftover, and code below would duplicate the
382                 // (not replaced) to_stack_was item.
383                 if (!caused_by_move_somewhere) {
384                         // If destination stack is of different type and there are leftover
385                         // items, attempt to put the leftover items to a different place in the
386                         // destination inventory.
387                         // The client-side GUI will try to guess if this happens.
388                         if (from_stack_was.name != to_stack_was.name) {
389                                 for (u32 i = 0; i < list_to->getSize(); i++) {
390                                         if (list_to->getItem(i).empty()) {
391                                                 list_to->changeItem(i, to_stack_was);
392                                                 break;
393                                         }
394                                 }
395                         }
396                 }
397                 if (move_count > 0 || did_swap) {
398                         list_from->deleteItem(from_i);
399                         list_from->addItem(from_i, from_stack_was);
400                 }
401         }
402         // If destination is infinite, reset it's stack and take count from source
403         if (dst_can_put_count == -1) {
404                 list_to->deleteItem(to_i);
405                 list_to->addItem(to_i, to_stack_was);
406                 list_from->deleteItem(from_i);
407                 list_from->addItem(from_i, from_stack_was);
408                 list_from->takeItem(from_i, count);
409         }
410
411         infostream << "IMoveAction::apply(): moved"
412                         << " msom=" << move_somewhere
413                         << " caused=" << caused_by_move_somewhere
414                         << " count=" << count
415                         << " from inv=\"" << from_inv.dump() << "\""
416                         << " list=\"" << from_list << "\""
417                         << " i=" << from_i
418                         << " to inv=\"" << to_inv.dump() << "\""
419                         << " list=\"" << to_list << "\""
420                         << " i=" << to_i
421                         << std::endl;
422
423         // If we are inside the move somewhere loop, we don't need to report
424         // anything if nothing happened (perhaps we don't need to report
425         // anything for caused_by_move_somewhere == true, but this way its safer)
426         if (caused_by_move_somewhere && move_count == 0)
427                 return;
428
429         /*
430                 Record rollback information
431         */
432         if (!ignore_rollback && gamedef->rollback()) {
433                 IRollbackManager *rollback = gamedef->rollback();
434
435                 // If source is not infinite, record item take
436                 if (src_can_take_count != -1) {
437                         RollbackAction action;
438                         std::string loc;
439                         {
440                                 std::ostringstream os(std::ios::binary);
441                                 from_inv.serialize(os);
442                                 loc = os.str();
443                         }
444                         action.setModifyInventoryStack(loc, from_list, from_i, false,
445                                         src_item);
446                         rollback->reportAction(action);
447                 }
448                 // If destination is not infinite, record item put
449                 if (dst_can_put_count != -1) {
450                         RollbackAction action;
451                         std::string loc;
452                         {
453                                 std::ostringstream os(std::ios::binary);
454                                 to_inv.serialize(os);
455                                 loc = os.str();
456                         }
457                         action.setModifyInventoryStack(loc, to_list, to_i, true,
458                                         src_item);
459                         rollback->reportAction(action);
460                 }
461         }
462
463         /*
464                 Report move to endpoints
465         */
466
467         /* Detached inventories */
468
469         // Both endpoints are same detached
470         if (from_inv.type == InventoryLocation::DETACHED &&
471                         from_inv == to_inv) {
472                 PLAYER_TO_SA(player)->detached_inventory_OnMove(
473                                 *this, count, player);
474         } else {
475                 // Destination is detached
476                 if (to_inv.type == InventoryLocation::DETACHED) {
477                         PLAYER_TO_SA(player)->detached_inventory_OnPut(
478                                 *this, src_item, player);
479                 }
480                 // Source is detached
481                 if (from_inv.type == InventoryLocation::DETACHED) {
482                         PLAYER_TO_SA(player)->detached_inventory_OnTake(
483                                 *this, src_item, player);
484                 }
485         }
486
487         /* Node metadata inventories */
488
489         // Both endpoints are same nodemeta
490         if (from_inv.type == InventoryLocation::NODEMETA &&
491                         from_inv == to_inv) {
492                 PLAYER_TO_SA(player)->nodemeta_inventory_OnMove(
493                         *this, count, player);
494         } else {
495                 // Destination is nodemeta
496                 if (to_inv.type == InventoryLocation::NODEMETA) {
497                         PLAYER_TO_SA(player)->nodemeta_inventory_OnPut(
498                                 *this, src_item, player);
499                 }
500                 // Source is nodemeta
501                 if (from_inv.type == InventoryLocation::NODEMETA) {
502                         PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
503                                 *this, src_item, player);
504                 }
505         }
506
507         // Player inventories
508
509         // Both endpoints are same player inventory
510         if (from_inv.type == InventoryLocation::PLAYER &&
511                         from_inv == to_inv) {
512                 PLAYER_TO_SA(player)->player_inventory_OnMove(
513                         *this, count, player);
514         } else {
515                 // Destination is player inventory
516                 if (to_inv.type == InventoryLocation::PLAYER) {
517                         PLAYER_TO_SA(player)->player_inventory_OnPut(
518                                 *this, src_item, player);
519                 }
520                 // Source is player inventory
521                 if (from_inv.type == InventoryLocation::PLAYER) {
522                         PLAYER_TO_SA(player)->player_inventory_OnTake(
523                                 *this, src_item, player);
524                 }
525         }
526
527         mgr->setInventoryModified(from_inv, false);
528         if (inv_from != inv_to)
529                 mgr->setInventoryModified(to_inv, false);
530 }
531
532 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
533 {
534         // Optional InventoryAction operation that is run on the client
535         // to make lag less apparent.
536
537         Inventory *inv_from = mgr->getInventory(from_inv);
538         Inventory *inv_to = mgr->getInventory(to_inv);
539         if (!inv_from || !inv_to)
540                 return;
541
542         InventoryLocation current_player;
543         current_player.setCurrentPlayer();
544         Inventory *inv_player = mgr->getInventory(current_player);
545         if (inv_from != inv_player || inv_to != inv_player)
546                 return;
547
548         InventoryList *list_from = inv_from->getList(from_list);
549         InventoryList *list_to = inv_to->getList(to_list);
550         if (!list_from || !list_to)
551                 return;
552
553         if (!move_somewhere)
554                 list_from->moveItem(from_i, list_to, to_i, count);
555         else
556                 list_from->moveItemSomewhere(from_i, list_to, count);
557
558         mgr->setInventoryModified(from_inv);
559         if (inv_from != inv_to)
560                 mgr->setInventoryModified(to_inv);
561 }
562
563 /*
564         IDropAction
565 */
566
567 IDropAction::IDropAction(std::istream &is)
568 {
569         std::string ts;
570
571         std::getline(is, ts, ' ');
572         count = stoi(ts);
573
574         std::getline(is, ts, ' ');
575         from_inv.deSerialize(ts);
576
577         std::getline(is, from_list, ' ');
578
579         std::getline(is, ts, ' ');
580         from_i = stoi(ts);
581 }
582
583 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
584 {
585         Inventory *inv_from = mgr->getInventory(from_inv);
586
587         if (!inv_from) {
588                 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
589                                 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
590                 return;
591         }
592
593         InventoryList *list_from = inv_from->getList(from_list);
594
595         /*
596                 If a list doesn't exist or the source item doesn't exist
597         */
598         if (!list_from) {
599                 infostream<<"IDropAction::apply(): FAIL: source list not found: "
600                                 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
601                 return;
602         }
603         if (list_from->getItem(from_i).empty()) {
604                 infostream<<"IDropAction::apply(): FAIL: source item not found: "
605                                 <<"from_inv=\""<<from_inv.dump()<<"\""
606                                 <<", from_list=\""<<from_list<<"\""
607                                 <<" from_i="<<from_i<<std::endl;
608                 return;
609         }
610
611         /*
612                 Do not handle rollback if inventory is player's
613         */
614         bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
615
616         /*
617                 Collect information of endpoints
618         */
619
620         int take_count = list_from->getItem(from_i).count;
621         if (count != 0 && count < take_count)
622                 take_count = count;
623         int src_can_take_count = take_count;
624
625         ItemStack src_item = list_from->getItem(from_i);
626         src_item.count = take_count;
627
628         // Run callbacks depending on source inventory
629         switch (from_inv.type) {
630         case InventoryLocation::DETACHED:
631                 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
632                         *this, src_item, player);
633                 break;
634         case InventoryLocation::NODEMETA:
635                 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
636                         *this, src_item, player);
637                 break;
638         case InventoryLocation::PLAYER:
639                 src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowTake(
640                         *this, src_item, player);
641                 break;
642         default:
643                 break;
644         }
645
646         if (src_can_take_count != -1 && src_can_take_count < take_count)
647                 take_count = src_can_take_count;
648
649         int actually_dropped_count = 0;
650
651         // Update item due executed callbacks
652         src_item = list_from->getItem(from_i);
653
654         // Drop the item
655         ItemStack item1 = list_from->getItem(from_i);
656         item1.count = take_count;
657         if(PLAYER_TO_SA(player)->item_OnDrop(item1, player,
658                                 player->getBasePosition())) {
659                 actually_dropped_count = take_count - item1.count;
660
661                 if (actually_dropped_count == 0) {
662                         infostream<<"Actually dropped no items"<<std::endl;
663                         return;
664                 }
665
666                 // If source isn't infinite
667                 if (src_can_take_count != -1) {
668                         // Take item from source list
669                         ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
670
671                         if (item2.count != actually_dropped_count)
672                                 errorstream<<"Could not take dropped count of items"<<std::endl;
673
674                         mgr->setInventoryModified(from_inv, false);
675                 }
676         }
677
678         infostream<<"IDropAction::apply(): dropped "
679                         <<" from inv=\""<<from_inv.dump()<<"\""
680                         <<" list=\""<<from_list<<"\""
681                         <<" i="<<from_i
682                         <<std::endl;
683
684         src_item.count = actually_dropped_count;
685
686         /*
687                 Report drop to endpoints
688         */
689
690         switch (from_inv.type) {
691         case InventoryLocation::DETACHED:
692                 PLAYER_TO_SA(player)->detached_inventory_OnTake(
693                         *this, src_item, player);
694                 break;
695         case InventoryLocation::NODEMETA:
696                 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
697                         *this, src_item, player);
698                 break;
699         case InventoryLocation::PLAYER:
700                 PLAYER_TO_SA(player)->player_inventory_OnTake(
701                         *this, src_item, player);
702                 break;
703         default:
704                 break;
705         }
706
707         /*
708                 Record rollback information
709         */
710         if (!ignore_src_rollback && gamedef->rollback()) {
711                 IRollbackManager *rollback = gamedef->rollback();
712
713                 // If source is not infinite, record item take
714                 if (src_can_take_count != -1) {
715                         RollbackAction action;
716                         std::string loc;
717                         {
718                                 std::ostringstream os(std::ios::binary);
719                                 from_inv.serialize(os);
720                                 loc = os.str();
721                         }
722                         action.setModifyInventoryStack(loc, from_list, from_i,
723                                         false, src_item);
724                         rollback->reportAction(action);
725                 }
726         }
727 }
728
729 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
730 {
731         // Optional InventoryAction operation that is run on the client
732         // to make lag less apparent.
733
734         Inventory *inv_from = mgr->getInventory(from_inv);
735         if (!inv_from)
736                 return;
737
738         InventoryLocation current_player;
739         current_player.setCurrentPlayer();
740         Inventory *inv_player = mgr->getInventory(current_player);
741         if (inv_from != inv_player)
742                 return;
743
744         InventoryList *list_from = inv_from->getList(from_list);
745         if (!list_from)
746                 return;
747
748         if (count == 0)
749                 list_from->changeItem(from_i, ItemStack());
750         else
751                 list_from->takeItem(from_i, count);
752
753         mgr->setInventoryModified(from_inv);
754 }
755
756 /*
757         ICraftAction
758 */
759
760 ICraftAction::ICraftAction(std::istream &is)
761 {
762         std::string ts;
763
764         std::getline(is, ts, ' ');
765         count = stoi(ts);
766
767         std::getline(is, ts, ' ');
768         craft_inv.deSerialize(ts);
769 }
770
771 void ICraftAction::apply(InventoryManager *mgr,
772         ServerActiveObject *player, IGameDef *gamedef)
773 {
774         Inventory *inv_craft = mgr->getInventory(craft_inv);
775
776         if (!inv_craft) {
777                 infostream << "ICraftAction::apply(): FAIL: inventory not found: "
778                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
779                 return;
780         }
781
782         InventoryList *list_craft = inv_craft->getList("craft");
783         InventoryList *list_craftresult = inv_craft->getList("craftresult");
784         InventoryList *list_main = inv_craft->getList("main");
785
786         /*
787                 If a list doesn't exist or the source item doesn't exist
788         */
789         if (!list_craft) {
790                 infostream << "ICraftAction::apply(): FAIL: craft list not found: "
791                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
792                 return;
793         }
794         if (!list_craftresult) {
795                 infostream << "ICraftAction::apply(): FAIL: craftresult list not found: "
796                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
797                 return;
798         }
799         if (list_craftresult->getSize() < 1) {
800                 infostream << "ICraftAction::apply(): FAIL: craftresult list too short: "
801                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
802                 return;
803         }
804
805         ItemStack crafted;
806         ItemStack craftresultitem;
807         int count_remaining = count;
808         std::vector<ItemStack> output_replacements;
809         getCraftingResult(inv_craft, crafted, output_replacements, false, gamedef);
810         PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
811         bool found = !crafted.empty();
812
813         while (found && list_craftresult->itemFits(0, crafted)) {
814                 InventoryList saved_craft_list = *list_craft;
815
816                 std::vector<ItemStack> temp;
817                 // Decrement input and add crafting output
818                 getCraftingResult(inv_craft, crafted, temp, true, gamedef);
819                 PLAYER_TO_SA(player)->item_OnCraft(crafted, player, &saved_craft_list, craft_inv);
820                 list_craftresult->addItem(0, crafted);
821                 mgr->setInventoryModified(craft_inv);
822
823                 // Add the new replacements to the list
824                 IItemDefManager *itemdef = gamedef->getItemDefManager();
825                 for (auto &itemstack : temp) {
826                         for (auto &output_replacement : output_replacements) {
827                                 if (itemstack.name == output_replacement.name) {
828                                         itemstack = output_replacement.addItem(itemstack, itemdef);
829                                         if (itemstack.empty())
830                                                 continue;
831                                 }
832                         }
833                         output_replacements.push_back(itemstack);
834                 }
835
836                 actionstream << player->getDescription()
837                                 << " crafts "
838                                 << crafted.getItemString()
839                                 << std::endl;
840
841                 // Decrement counter
842                 if (count_remaining == 1)
843                         break;
844
845                 if (count_remaining > 1)
846                         count_remaining--;
847
848                 // Get next crafting result
849                 getCraftingResult(inv_craft, crafted, temp, false, gamedef);
850                 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
851                 found = !crafted.empty();
852         }
853
854         // Put the replacements in the inventory or drop them on the floor, if
855         // the invenotry is full
856         for (auto &output_replacement : output_replacements) {
857                 if (list_main)
858                         output_replacement = list_main->addItem(output_replacement);
859                 if (output_replacement.empty())
860                         continue;
861                 u16 count = output_replacement.count;
862                 do {
863                         PLAYER_TO_SA(player)->item_OnDrop(output_replacement, player,
864                                 player->getBasePosition());
865                         if (count >= output_replacement.count) {
866                                 errorstream << "Couldn't drop replacement stack " <<
867                                         output_replacement.getItemString() << " because drop loop didn't "
868                                         "decrease count." << std::endl;
869
870                                 break;
871                         }
872                 } while (!output_replacement.empty());
873         }
874
875         infostream<<"ICraftAction::apply(): crafted "
876                         <<" craft_inv=\""<<craft_inv.dump()<<"\""
877                         <<std::endl;
878 }
879
880 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
881 {
882         // Optional InventoryAction operation that is run on the client
883         // to make lag less apparent.
884 }
885
886
887 // Crafting helper
888 bool getCraftingResult(Inventory *inv, ItemStack &result,
889                 std::vector<ItemStack> &output_replacements,
890                 bool decrementInput, IGameDef *gamedef)
891 {
892         result.clear();
893
894         // Get the InventoryList in which we will operate
895         InventoryList *clist = inv->getList("craft");
896         if (!clist)
897                 return false;
898
899         // Mangle crafting grid to an another format
900         CraftInput ci;
901         ci.method = CRAFT_METHOD_NORMAL;
902         ci.width = clist->getWidth() ? clist->getWidth() : 3;
903         for (u16 i=0; i < clist->getSize(); i++)
904                 ci.items.push_back(clist->getItem(i));
905
906         // Find out what is crafted and add it to result item slot
907         CraftOutput co;
908         bool found = gamedef->getCraftDefManager()->getCraftResult(
909                         ci, co, output_replacements, decrementInput, gamedef);
910         if (found)
911                 result.deSerialize(co.item, gamedef->getItemDefManager());
912
913         if (found && decrementInput) {
914                 // CraftInput has been changed, apply changes in clist
915                 for (u16 i=0; i < clist->getSize(); i++) {
916                         clist->changeItem(i, ci.items[i]);
917                 }
918         }
919
920         return found;
921 }
922