]> git.lizzy.rs Git - dragonfireclient.git/blob - src/inventorymanager.cpp
Merge branch 'master' of https://github.com/EliasFleckenstein03/dragonfireclient
[dragonfireclient.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 "server/serveractiveobject.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::swapDirections()
158 {
159         std::swap(from_inv, to_inv);
160         std::swap(from_list, to_list);
161         std::swap(from_i, to_i);
162 }
163
164 void IMoveAction::onPutAndOnTake(const ItemStack &src_item, ServerActiveObject *player) const
165 {
166         ServerScripting *sa = PLAYER_TO_SA(player);
167         if (to_inv.type == InventoryLocation::DETACHED)
168                 sa->detached_inventory_OnPut(*this, src_item, player);
169         else if (to_inv.type == InventoryLocation::NODEMETA)
170                 sa->nodemeta_inventory_OnPut(*this, src_item, player);
171         else if (to_inv.type == InventoryLocation::PLAYER)
172                 sa->player_inventory_OnPut(*this, src_item, player);
173         else
174                 assert(false);
175         
176         if (from_inv.type == InventoryLocation::DETACHED)
177                 sa->detached_inventory_OnTake(*this, src_item, player);
178         else if (from_inv.type == InventoryLocation::NODEMETA)
179                 sa->nodemeta_inventory_OnTake(*this, src_item, player);
180         else if (from_inv.type == InventoryLocation::PLAYER)
181                 sa->player_inventory_OnTake(*this, src_item, player);
182         else
183                 assert(false);
184 }
185
186 void IMoveAction::onMove(int count, ServerActiveObject *player) const
187 {
188         ServerScripting *sa = PLAYER_TO_SA(player);
189         if (from_inv.type == InventoryLocation::DETACHED)
190                 sa->detached_inventory_OnMove(*this, count, player);
191         else if (from_inv.type == InventoryLocation::NODEMETA)
192                 sa->nodemeta_inventory_OnMove(*this, count, player);
193         else if (from_inv.type == InventoryLocation::PLAYER)
194                 sa->player_inventory_OnMove(*this, count, player);
195         else
196                 assert(false);
197 }
198
199 int IMoveAction::allowPut(const ItemStack &dst_item, ServerActiveObject *player) const
200 {
201         ServerScripting *sa = PLAYER_TO_SA(player);
202         int dst_can_put_count = 0xffff;
203         if (to_inv.type == InventoryLocation::DETACHED)
204                 dst_can_put_count = sa->detached_inventory_AllowPut(*this, dst_item, player);
205         else if (to_inv.type == InventoryLocation::NODEMETA)
206                 dst_can_put_count = sa->nodemeta_inventory_AllowPut(*this, dst_item, player);
207         else if (to_inv.type == InventoryLocation::PLAYER)
208                 dst_can_put_count = sa->player_inventory_AllowPut(*this, dst_item, player);
209         else
210                 assert(false);
211         return dst_can_put_count;
212 }
213
214 int IMoveAction::allowTake(const ItemStack &src_item, ServerActiveObject *player) const
215 {
216         ServerScripting *sa = PLAYER_TO_SA(player);
217         int src_can_take_count = 0xffff;
218         if (from_inv.type == InventoryLocation::DETACHED)
219                 src_can_take_count = sa->detached_inventory_AllowTake(*this, src_item, player);
220         else if (from_inv.type == InventoryLocation::NODEMETA)
221                 src_can_take_count = sa->nodemeta_inventory_AllowTake(*this, src_item, player);
222         else if (from_inv.type == InventoryLocation::PLAYER)
223                 src_can_take_count = sa->player_inventory_AllowTake(*this, src_item, player);
224         else
225                 assert(false);
226         return src_can_take_count;
227 }
228
229 int IMoveAction::allowMove(int try_take_count, ServerActiveObject *player) const
230 {
231         ServerScripting *sa = PLAYER_TO_SA(player);
232         int src_can_take_count = 0xffff;
233         if (from_inv.type == InventoryLocation::DETACHED)
234                 src_can_take_count = sa->detached_inventory_AllowMove(*this, try_take_count, player);
235         else if (from_inv.type == InventoryLocation::NODEMETA)
236                 src_can_take_count = sa->nodemeta_inventory_AllowMove(*this, try_take_count, player);
237         else if (from_inv.type == InventoryLocation::PLAYER)
238                 src_can_take_count = sa->player_inventory_AllowMove(*this, try_take_count, player);
239         else
240                 assert(false);
241         return src_can_take_count;
242 }
243
244 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
245 {
246         Inventory *inv_from = mgr->getInventory(from_inv);
247         Inventory *inv_to = mgr->getInventory(to_inv);
248
249         if (!inv_from) {
250                 infostream << "IMoveAction::apply(): FAIL: source inventory not found: "
251                         << "from_inv=\""<<from_inv.dump() << "\""
252                         << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
253                 return;
254         }
255         if (!inv_to) {
256                 infostream << "IMoveAction::apply(): FAIL: destination inventory not found: "
257                         << "from_inv=\"" << from_inv.dump() << "\""
258                         << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
259                 return;
260         }
261
262         InventoryList *list_from = inv_from->getList(from_list);
263         InventoryList *list_to = inv_to->getList(to_list);
264
265         /*
266                 If a list doesn't exist or the source item doesn't exist
267         */
268         if (!list_from) {
269                 infostream << "IMoveAction::apply(): FAIL: source list not found: "
270                         << "from_inv=\"" << from_inv.dump() << "\""
271                         << ", from_list=\"" << from_list << "\"" << std::endl;
272                 return;
273         }
274         if (!list_to) {
275                 infostream << "IMoveAction::apply(): FAIL: destination list not found: "
276                         << "to_inv=\""<<to_inv.dump() << "\""
277                         << ", to_list=\"" << to_list << "\"" << std::endl;
278                 return;
279         }
280
281         if (move_somewhere) {
282                 s16 old_to_i = to_i;
283                 u16 old_count = count;
284                 caused_by_move_somewhere = true;
285                 move_somewhere = false;
286
287                 infostream << "IMoveAction::apply(): moving item somewhere"
288                         << " msom=" << move_somewhere
289                         << " count=" << count
290                         << " from inv=\"" << from_inv.dump() << "\""
291                         << " list=\"" << from_list << "\""
292                         << " i=" << from_i
293                         << " to inv=\"" << to_inv.dump() << "\""
294                         << " list=\"" << to_list << "\""
295                         << std::endl;
296
297                 // Try to add the item to destination list
298                 s16 dest_size = list_to->getSize();
299                 // First try all the non-empty slots
300                 for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
301                         if (!list_to->getItem(dest_i).empty()) {
302                                 to_i = dest_i;
303                                 apply(mgr, player, gamedef);
304                                 count -= move_count;
305                         }
306                 }
307
308                 // Then try all the empty ones
309                 for (s16 dest_i = 0; dest_i < dest_size && count > 0; dest_i++) {
310                         if (list_to->getItem(dest_i).empty()) {
311                                 to_i = dest_i;
312                                 apply(mgr, player, gamedef);
313                                 count -= move_count;
314                         }
315                 }
316
317                 to_i = old_to_i;
318                 count = old_count;
319                 caused_by_move_somewhere = false;
320                 move_somewhere = true;
321                 return;
322         }
323
324         if ((u16)to_i > list_to->getSize()) {
325                 infostream << "IMoveAction::apply(): FAIL: destination index out of bounds: "
326                         << "to_i=" << to_i
327                         << ", size=" << list_to->getSize() << std::endl;
328                 return;
329         }
330         /*
331                 Do not handle rollback if both inventories are that of the same player
332         */
333         bool ignore_rollback = (
334                 from_inv.type == InventoryLocation::PLAYER &&
335                 from_inv == to_inv);
336
337         /*
338                 Collect information of endpoints
339         */
340
341         ItemStack src_item = list_from->getItem(from_i);
342         if (count > 0)
343                 src_item.count = count;
344         if (src_item.empty())
345                 return;
346
347         int src_can_take_count = 0xffff;
348         int dst_can_put_count = 0xffff;
349
350         // this is needed for swapping items inside one inventory to work
351         ItemStack restitem;
352         bool allow_swap = !list_to->itemFits(to_i, src_item, &restitem)
353                 && restitem.count == src_item.count
354                 && !caused_by_move_somewhere;
355
356         // Shift-click: Cannot fill this stack, proceed with next slot
357         if (caused_by_move_somewhere && restitem.count == src_item.count)
358                 return;
359
360         if (allow_swap) {
361                 // Swap will affect the entire stack if it can performed.
362                 src_item = list_from->getItem(from_i);
363                 count = src_item.count;
364         }
365
366         if (from_inv == to_inv) {
367                 // Move action within the same inventory
368                 src_can_take_count = allowMove(src_item.count, player);
369
370                 bool swap_expected = allow_swap;
371                 allow_swap = allow_swap
372                         && (src_can_take_count == -1 || src_can_take_count >= src_item.count);
373                 if (allow_swap) {
374                         int try_put_count = list_to->getItem(to_i).count;
375                         swapDirections();
376                         dst_can_put_count = allowMove(try_put_count, player);
377                         allow_swap = allow_swap
378                                 && (dst_can_put_count == -1 || dst_can_put_count >= try_put_count);
379                         swapDirections();
380                 } else {
381                         dst_can_put_count = src_can_take_count;
382                 }
383                 if (swap_expected != allow_swap)
384                         src_can_take_count = dst_can_put_count = 0;
385         } else {
386                 // Take from one inventory, put into another
387                 dst_can_put_count = allowPut(src_item, player);
388                 src_can_take_count = allowTake(src_item, player);
389                 
390                 bool swap_expected = allow_swap;
391                 allow_swap = allow_swap
392                         && (src_can_take_count == -1 || src_can_take_count >= src_item.count)
393                         && (dst_can_put_count == -1 || dst_can_put_count >= src_item.count);
394                 // A swap is expected, which means that we have to
395                 // run the "allow" callbacks a second time with swapped inventories
396                 if (allow_swap) {
397                         ItemStack dst_item = list_to->getItem(to_i);
398                         swapDirections();
399
400                         int src_can_take = allowPut(dst_item, player);
401                         int dst_can_put = allowTake(dst_item, player);
402                         allow_swap = allow_swap
403                                 && (src_can_take == -1 || src_can_take >= dst_item.count)
404                                 && (dst_can_put == -1 || dst_can_put >= dst_item.count);
405                         swapDirections();
406                 }
407                 if (swap_expected != allow_swap)
408                         src_can_take_count = dst_can_put_count = 0;
409         }
410
411         int old_count = count;
412
413         /* Modify count according to collected data */
414         count = src_item.count;
415         if (src_can_take_count != -1 && count > src_can_take_count)
416                 count = src_can_take_count;
417         if (dst_can_put_count != -1 && count > dst_can_put_count)
418                 count = dst_can_put_count;
419         /* Limit according to source item count */
420         if (count > list_from->getItem(from_i).count)
421                 count = list_from->getItem(from_i).count;
422
423         /* If no items will be moved, don't go further */
424         if (count == 0) {
425                 // Undo client prediction. See 'clientApply'
426                 if (from_inv.type == InventoryLocation::PLAYER)
427                         list_from->setModified();
428
429                 if (to_inv.type == InventoryLocation::PLAYER)
430                         list_to->setModified();
431
432                 infostream<<"IMoveAction::apply(): move was completely disallowed:"
433                                 <<" count="<<old_count
434                                 <<" from inv=\""<<from_inv.dump()<<"\""
435                                 <<" list=\""<<from_list<<"\""
436                                 <<" i="<<from_i
437                                 <<" to inv=\""<<to_inv.dump()<<"\""
438                                 <<" list=\""<<to_list<<"\""
439                                 <<" i="<<to_i
440                                 <<std::endl;
441                 return;
442         }
443
444         src_item = list_from->getItem(from_i);
445         src_item.count = count;
446         ItemStack from_stack_was = list_from->getItem(from_i);
447         ItemStack to_stack_was = list_to->getItem(to_i);
448
449         /*
450                 Perform actual move
451
452                 If something is wrong (source item is empty, destination is the
453                 same as source), nothing happens
454         */
455         bool did_swap = false;
456         move_count = list_from->moveItem(from_i,
457                 list_to, to_i, count, allow_swap, &did_swap);
458         assert(allow_swap == did_swap);
459
460         // If source is infinite, reset it's stack
461         if (src_can_take_count == -1) {
462                 // For the caused_by_move_somewhere == true case we didn't force-put the item,
463                 // which guarantees there is no leftover, and code below would duplicate the
464                 // (not replaced) to_stack_was item.
465                 if (!caused_by_move_somewhere) {
466                         // If destination stack is of different type and there are leftover
467                         // items, attempt to put the leftover items to a different place in the
468                         // destination inventory.
469                         // The client-side GUI will try to guess if this happens.
470                         if (from_stack_was.name != to_stack_was.name) {
471                                 for (u32 i = 0; i < list_to->getSize(); i++) {
472                                         if (list_to->getItem(i).empty()) {
473                                                 list_to->changeItem(i, to_stack_was);
474                                                 break;
475                                         }
476                                 }
477                         }
478                 }
479                 if (move_count > 0 || did_swap) {
480                         list_from->deleteItem(from_i);
481                         list_from->addItem(from_i, from_stack_was);
482                 }
483         }
484         // If destination is infinite, reset it's stack and take count from source
485         if (dst_can_put_count == -1) {
486                 list_to->deleteItem(to_i);
487                 list_to->addItem(to_i, to_stack_was);
488                 list_from->deleteItem(from_i);
489                 list_from->addItem(from_i, from_stack_was);
490                 list_from->takeItem(from_i, count);
491         }
492
493         infostream << "IMoveAction::apply(): moved"
494                         << " msom=" << move_somewhere
495                         << " caused=" << caused_by_move_somewhere
496                         << " count=" << count
497                         << " from inv=\"" << from_inv.dump() << "\""
498                         << " list=\"" << from_list << "\""
499                         << " i=" << from_i
500                         << " to inv=\"" << to_inv.dump() << "\""
501                         << " list=\"" << to_list << "\""
502                         << " i=" << to_i
503                         << std::endl;
504
505         // If we are inside the move somewhere loop, we don't need to report
506         // anything if nothing happened (perhaps we don't need to report
507         // anything for caused_by_move_somewhere == true, but this way its safer)
508         if (caused_by_move_somewhere && move_count == 0)
509                 return;
510
511         /*
512                 Record rollback information
513         */
514         if (!ignore_rollback && gamedef->rollback()) {
515                 IRollbackManager *rollback = gamedef->rollback();
516
517                 // If source is not infinite, record item take
518                 if (src_can_take_count != -1) {
519                         RollbackAction action;
520                         std::string loc;
521                         {
522                                 std::ostringstream os(std::ios::binary);
523                                 from_inv.serialize(os);
524                                 loc = os.str();
525                         }
526                         action.setModifyInventoryStack(loc, from_list, from_i, false,
527                                         src_item);
528                         rollback->reportAction(action);
529                 }
530                 // If destination is not infinite, record item put
531                 if (dst_can_put_count != -1) {
532                         RollbackAction action;
533                         std::string loc;
534                         {
535                                 std::ostringstream os(std::ios::binary);
536                                 to_inv.serialize(os);
537                                 loc = os.str();
538                         }
539                         action.setModifyInventoryStack(loc, to_list, to_i, true,
540                                         src_item);
541                         rollback->reportAction(action);
542                 }
543         }
544
545         /*
546                 Report move to endpoints
547         */
548
549         // Source = destination => move
550         if (from_inv == to_inv) {
551                 onMove(count, player);
552                 if (did_swap) {
553                         // Item is now placed in source list
554                         src_item = list_from->getItem(from_i);
555                         swapDirections();
556                         onMove(src_item.count, player);
557                         swapDirections();
558                 }
559                 mgr->setInventoryModified(from_inv);
560         } else {
561                 onPutAndOnTake(src_item, player);
562                 if (did_swap) {
563                         // Item is now placed in source list
564                         src_item = list_from->getItem(from_i);
565                         swapDirections();
566                         onPutAndOnTake(src_item, player);
567                         swapDirections();
568                 }
569                 mgr->setInventoryModified(to_inv);
570                 mgr->setInventoryModified(from_inv);
571         }
572 }
573
574 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
575 {
576         // Optional InventoryAction operation that is run on the client
577         // to make lag less apparent.
578
579         Inventory *inv_from = mgr->getInventory(from_inv);
580         Inventory *inv_to = mgr->getInventory(to_inv);
581         if (!inv_from || !inv_to)
582                 return;
583
584         InventoryLocation current_player;
585         current_player.setCurrentPlayer();
586         Inventory *inv_player = mgr->getInventory(current_player);
587         if (inv_from != inv_player || inv_to != inv_player)
588                 return;
589
590         InventoryList *list_from = inv_from->getList(from_list);
591         InventoryList *list_to = inv_to->getList(to_list);
592         if (!list_from || !list_to)
593                 return;
594
595         if (!move_somewhere)
596                 list_from->moveItem(from_i, list_to, to_i, count);
597         else
598                 list_from->moveItemSomewhere(from_i, list_to, count);
599
600         mgr->setInventoryModified(from_inv);
601         if (inv_from != inv_to)
602                 mgr->setInventoryModified(to_inv);
603 }
604
605 /*
606         IDropAction
607 */
608
609 IDropAction::IDropAction(std::istream &is)
610 {
611         std::string ts;
612
613         std::getline(is, ts, ' ');
614         count = stoi(ts);
615
616         std::getline(is, ts, ' ');
617         from_inv.deSerialize(ts);
618
619         std::getline(is, from_list, ' ');
620
621         std::getline(is, ts, ' ');
622         from_i = stoi(ts);
623 }
624
625 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
626 {
627         Inventory *inv_from = mgr->getInventory(from_inv);
628
629         if (!inv_from) {
630                 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
631                                 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
632                 return;
633         }
634
635         InventoryList *list_from = inv_from->getList(from_list);
636
637         /*
638                 If a list doesn't exist or the source item doesn't exist
639         */
640         if (!list_from) {
641                 infostream<<"IDropAction::apply(): FAIL: source list not found: "
642                                 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
643                 return;
644         }
645         if (list_from->getItem(from_i).empty()) {
646                 infostream<<"IDropAction::apply(): FAIL: source item not found: "
647                                 <<"from_inv=\""<<from_inv.dump()<<"\""
648                                 <<", from_list=\""<<from_list<<"\""
649                                 <<" from_i="<<from_i<<std::endl;
650                 return;
651         }
652
653         /*
654                 Do not handle rollback if inventory is player's
655         */
656         bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
657
658         /*
659                 Collect information of endpoints
660         */
661
662         int take_count = list_from->getItem(from_i).count;
663         if (count != 0 && count < take_count)
664                 take_count = count;
665         int src_can_take_count = take_count;
666
667         ItemStack src_item = list_from->getItem(from_i);
668         src_item.count = take_count;
669
670         // Run callbacks depending on source inventory
671         switch (from_inv.type) {
672         case InventoryLocation::DETACHED:
673                 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
674                         *this, src_item, player);
675                 break;
676         case InventoryLocation::NODEMETA:
677                 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
678                         *this, src_item, player);
679                 break;
680         case InventoryLocation::PLAYER:
681                 src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowTake(
682                         *this, src_item, player);
683                 break;
684         default:
685                 break;
686         }
687
688         if (src_can_take_count != -1 && src_can_take_count < take_count)
689                 take_count = src_can_take_count;
690
691         // Update item due executed callbacks
692         src_item = list_from->getItem(from_i);
693
694         // Drop the item
695         ItemStack item1 = list_from->getItem(from_i);
696         item1.count = take_count;
697         if(PLAYER_TO_SA(player)->item_OnDrop(item1, player,
698                                 player->getBasePosition())) {
699                 int actually_dropped_count = take_count - item1.count;
700
701                 if (actually_dropped_count == 0) {
702                         infostream<<"Actually dropped no items"<<std::endl;
703
704                         // Revert client prediction. See 'clientApply'
705                         if (from_inv.type == InventoryLocation::PLAYER)
706                                 list_from->setModified();
707                         return;
708                 }
709
710                 // If source isn't infinite
711                 if (src_can_take_count != -1) {
712                         // Take item from source list
713                         ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
714
715                         if (item2.count != actually_dropped_count)
716                                 errorstream<<"Could not take dropped count of items"<<std::endl;
717                 }
718
719                 src_item.count = actually_dropped_count;
720                 mgr->setInventoryModified(from_inv);
721         }
722
723         infostream<<"IDropAction::apply(): dropped "
724                         <<" from inv=\""<<from_inv.dump()<<"\""
725                         <<" list=\""<<from_list<<"\""
726                         <<" i="<<from_i
727                         <<std::endl;
728
729
730         /*
731                 Report drop to endpoints
732         */
733
734         switch (from_inv.type) {
735         case InventoryLocation::DETACHED:
736                 PLAYER_TO_SA(player)->detached_inventory_OnTake(
737                         *this, src_item, player);
738                 break;
739         case InventoryLocation::NODEMETA:
740                 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
741                         *this, src_item, player);
742                 break;
743         case InventoryLocation::PLAYER:
744                 PLAYER_TO_SA(player)->player_inventory_OnTake(
745                         *this, src_item, player);
746                 break;
747         default:
748                 break;
749         }
750
751         /*
752                 Record rollback information
753         */
754         if (!ignore_src_rollback && gamedef->rollback()) {
755                 IRollbackManager *rollback = gamedef->rollback();
756
757                 // If source is not infinite, record item take
758                 if (src_can_take_count != -1) {
759                         RollbackAction action;
760                         std::string loc;
761                         {
762                                 std::ostringstream os(std::ios::binary);
763                                 from_inv.serialize(os);
764                                 loc = os.str();
765                         }
766                         action.setModifyInventoryStack(loc, from_list, from_i,
767                                         false, src_item);
768                         rollback->reportAction(action);
769                 }
770         }
771 }
772
773 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
774 {
775         // Optional InventoryAction operation that is run on the client
776         // to make lag less apparent.
777
778         Inventory *inv_from = mgr->getInventory(from_inv);
779         if (!inv_from)
780                 return;
781
782         InventoryLocation current_player;
783         current_player.setCurrentPlayer();
784         Inventory *inv_player = mgr->getInventory(current_player);
785         if (inv_from != inv_player)
786                 return;
787
788         InventoryList *list_from = inv_from->getList(from_list);
789         if (!list_from)
790                 return;
791
792         if (count == 0)
793                 list_from->changeItem(from_i, ItemStack());
794         else
795                 list_from->takeItem(from_i, count);
796
797         mgr->setInventoryModified(from_inv);
798 }
799
800 /*
801         ICraftAction
802 */
803
804 ICraftAction::ICraftAction(std::istream &is)
805 {
806         std::string ts;
807
808         std::getline(is, ts, ' ');
809         count = stoi(ts);
810
811         std::getline(is, ts, ' ');
812         craft_inv.deSerialize(ts);
813 }
814
815 void ICraftAction::apply(InventoryManager *mgr,
816         ServerActiveObject *player, IGameDef *gamedef)
817 {
818         Inventory *inv_craft = mgr->getInventory(craft_inv);
819
820         if (!inv_craft) {
821                 infostream << "ICraftAction::apply(): FAIL: inventory not found: "
822                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
823                 return;
824         }
825
826         InventoryList *list_craft = inv_craft->getList("craft");
827         InventoryList *list_craftresult = inv_craft->getList("craftresult");
828         InventoryList *list_main = inv_craft->getList("main");
829
830         /*
831                 If a list doesn't exist or the source item doesn't exist
832         */
833         if (!list_craft) {
834                 infostream << "ICraftAction::apply(): FAIL: craft list not found: "
835                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
836                 return;
837         }
838         if (!list_craftresult) {
839                 infostream << "ICraftAction::apply(): FAIL: craftresult list not found: "
840                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
841                 return;
842         }
843         if (list_craftresult->getSize() < 1) {
844                 infostream << "ICraftAction::apply(): FAIL: craftresult list too short: "
845                                 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
846                 return;
847         }
848
849         ItemStack crafted;
850         ItemStack craftresultitem;
851         int count_remaining = count;
852         std::vector<ItemStack> output_replacements;
853         getCraftingResult(inv_craft, crafted, output_replacements, false, gamedef);
854         PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
855         bool found = !crafted.empty();
856
857         while (found && list_craftresult->itemFits(0, crafted)) {
858                 InventoryList saved_craft_list = *list_craft;
859
860                 std::vector<ItemStack> temp;
861                 // Decrement input and add crafting output
862                 getCraftingResult(inv_craft, crafted, temp, true, gamedef);
863                 PLAYER_TO_SA(player)->item_OnCraft(crafted, player, &saved_craft_list, craft_inv);
864                 list_craftresult->addItem(0, crafted);
865                 mgr->setInventoryModified(craft_inv);
866
867                 // Add the new replacements to the list
868                 IItemDefManager *itemdef = gamedef->getItemDefManager();
869                 for (auto &itemstack : temp) {
870                         for (auto &output_replacement : output_replacements) {
871                                 if (itemstack.name == output_replacement.name) {
872                                         itemstack = output_replacement.addItem(itemstack, itemdef);
873                                         if (itemstack.empty())
874                                                 continue;
875                                 }
876                         }
877                         output_replacements.push_back(itemstack);
878                 }
879
880                 actionstream << player->getDescription()
881                                 << " crafts "
882                                 << crafted.getItemString()
883                                 << std::endl;
884
885                 // Decrement counter
886                 if (count_remaining == 1)
887                         break;
888
889                 if (count_remaining > 1)
890                         count_remaining--;
891
892                 // Get next crafting result
893                 getCraftingResult(inv_craft, crafted, temp, false, gamedef);
894                 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
895                 found = !crafted.empty();
896         }
897
898         // Put the replacements in the inventory or drop them on the floor, if
899         // the inventory is full
900         for (auto &output_replacement : output_replacements) {
901                 if (list_main)
902                         output_replacement = list_main->addItem(output_replacement);
903                 if (output_replacement.empty())
904                         continue;
905                 u16 count = output_replacement.count;
906                 do {
907                         PLAYER_TO_SA(player)->item_OnDrop(output_replacement, player,
908                                 player->getBasePosition());
909                         if (count >= output_replacement.count) {
910                                 errorstream << "Couldn't drop replacement stack " <<
911                                         output_replacement.getItemString() << " because drop loop didn't "
912                                         "decrease count." << std::endl;
913
914                                 break;
915                         }
916                 } while (!output_replacement.empty());
917         }
918
919         infostream<<"ICraftAction::apply(): crafted "
920                         <<" craft_inv=\""<<craft_inv.dump()<<"\""
921                         <<std::endl;
922 }
923
924 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
925 {
926         // Optional InventoryAction operation that is run on the client
927         // to make lag less apparent.
928 }
929
930
931 // Crafting helper
932 bool getCraftingResult(Inventory *inv, ItemStack &result,
933                 std::vector<ItemStack> &output_replacements,
934                 bool decrementInput, IGameDef *gamedef)
935 {
936         result.clear();
937
938         // Get the InventoryList in which we will operate
939         InventoryList *clist = inv->getList("craft");
940         if (!clist)
941                 return false;
942
943         // Mangle crafting grid to an another format
944         CraftInput ci;
945         ci.method = CRAFT_METHOD_NORMAL;
946         ci.width = clist->getWidth() ? clist->getWidth() : 3;
947         for (u16 i=0; i < clist->getSize(); i++)
948                 ci.items.push_back(clist->getItem(i));
949
950         // Find out what is crafted and add it to result item slot
951         CraftOutput co;
952         bool found = gamedef->getCraftDefManager()->getCraftResult(
953                         ci, co, output_replacements, decrementInput, gamedef);
954         if (found)
955                 result.deSerialize(co.item, gamedef->getItemDefManager());
956
957         if (found && decrementInput) {
958                 // CraftInput has been changed, apply changes in clist
959                 for (u16 i=0; i < clist->getSize(); i++) {
960                         clist->changeItem(i, ci.items[i]);
961                 }
962         }
963
964         return found;
965 }
966