3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "inventorymanager.h"
23 #include "serverenvironment.h"
24 #include "scripting_server.h"
25 #include "serverobject.h"
28 #include "rollback_interface.h"
29 #include "util/strfnd.h"
30 #include "util/basic_macros.h"
32 #define PLAYER_TO_SA(p) p->getEnv()->getScriptIface()
38 std::string InventoryLocation::dump() const
40 std::ostringstream os(std::ios::binary);
45 void InventoryLocation::serialize(std::ostream &os) const
48 case InventoryLocation::UNDEFINED:
51 case InventoryLocation::CURRENT_PLAYER:
54 case InventoryLocation::PLAYER:
57 case InventoryLocation::NODEMETA:
58 os<<"nodemeta:"<<p.X<<","<<p.Y<<","<<p.Z;
60 case InventoryLocation::DETACHED:
61 os<<"detached:"<<name;
64 FATAL_ERROR("Unhandled inventory location type");
68 void InventoryLocation::deSerialize(std::istream &is)
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;
82 std::getline(is, pos, '\n');
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');
91 infostream<<"Unknown InventoryLocation type=\""<<tname<<"\""<<std::endl;
92 throw SerializationError("Unknown InventoryLocation type");
96 void InventoryLocation::deSerialize(const std::string &s)
98 std::istringstream is(s, std::ios::binary);
106 InventoryAction *InventoryAction::deSerialize(std::istream &is)
109 std::getline(is, type, ' ');
111 InventoryAction *a = nullptr;
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);
130 IMoveAction::IMoveAction(std::istream &is, bool somewhere) :
131 move_somewhere(somewhere)
135 std::getline(is, ts, ' ');
138 std::getline(is, ts, ' ');
139 from_inv.deSerialize(ts);
141 std::getline(is, from_list, ' ');
143 std::getline(is, ts, ' ');
146 std::getline(is, ts, ' ');
147 to_inv.deSerialize(ts);
149 std::getline(is, to_list, ' ');
152 std::getline(is, ts, ' ');
157 void IMoveAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
159 Inventory *inv_from = mgr->getInventory(from_inv);
160 Inventory *inv_to = mgr->getInventory(to_inv);
163 infostream << "IMoveAction::apply(): FAIL: source inventory not found: "
164 << "from_inv=\""<<from_inv.dump() << "\""
165 << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
169 infostream << "IMoveAction::apply(): FAIL: destination inventory not found: "
170 << "from_inv=\"" << from_inv.dump() << "\""
171 << ", to_inv=\"" << to_inv.dump() << "\"" << std::endl;
175 InventoryList *list_from = inv_from->getList(from_list);
176 InventoryList *list_to = inv_to->getList(to_list);
179 If a list doesn't exist or the source item doesn't exist
182 infostream << "IMoveAction::apply(): FAIL: source list not found: "
183 << "from_inv=\"" << from_inv.dump() << "\""
184 << ", from_list=\"" << from_list << "\"" << std::endl;
188 infostream << "IMoveAction::apply(): FAIL: destination list not found: "
189 << "to_inv=\""<<to_inv.dump() << "\""
190 << ", to_list=\"" << to_list << "\"" << std::endl;
194 if (move_somewhere) {
196 u16 old_count = count;
197 caused_by_move_somewhere = true;
198 move_somewhere = false;
200 infostream << "IMoveAction::apply(): moving item somewhere"
201 << " msom=" << move_somewhere
202 << " count=" << count
203 << " from inv=\"" << from_inv.dump() << "\""
204 << " list=\"" << from_list << "\""
206 << " to inv=\"" << to_inv.dump() << "\""
207 << " list=\"" << to_list << "\""
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()) {
216 apply(mgr, player, gamedef);
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()) {
225 apply(mgr, player, gamedef);
232 caused_by_move_somewhere = false;
233 move_somewhere = true;
237 if ((u16)to_i > list_to->getSize()) {
238 infostream << "IMoveAction::apply(): FAIL: destination index out of bounds: "
240 << ", size=" << list_to->getSize() << std::endl;
244 Do not handle rollback if both inventories are that of the same player
246 bool ignore_rollback = (
247 from_inv.type == InventoryLocation::PLAYER &&
251 Collect information of endpoints
254 int try_take_count = count;
255 if (try_take_count == 0)
256 try_take_count = list_from->getItem(from_i).count;
258 int src_can_take_count = 0xffff;
259 int dst_can_put_count = 0xffff;
261 /* Query detached inventories */
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;
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);
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);
286 /* Query node metadata inventories */
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;
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);
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);
312 // Query player inventories
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;
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);
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);
337 int old_count = count;
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;
349 /* If no items will be moved, don't go further */
351 // Undo client prediction. See 'clientApply'
352 if (from_inv.type == InventoryLocation::PLAYER)
353 list_from->setModified();
355 if (to_inv.type == InventoryLocation::PLAYER)
356 list_to->setModified();
358 infostream<<"IMoveAction::apply(): move was completely disallowed:"
359 <<" count="<<old_count
360 <<" from inv=\""<<from_inv.dump()<<"\""
361 <<" list=\""<<from_list<<"\""
363 <<" to inv=\""<<to_inv.dump()<<"\""
364 <<" list=\""<<to_list<<"\""
370 ItemStack src_item = list_from->getItem(from_i);
371 src_item.count = count;
372 ItemStack from_stack_was = list_from->getItem(from_i);
373 ItemStack to_stack_was = list_to->getItem(to_i);
378 If something is wrong (source item is empty, destination is the
379 same as source), nothing happens
381 bool did_swap = false;
382 move_count = list_from->moveItem(from_i,
383 list_to, to_i, count, !caused_by_move_somewhere, &did_swap);
385 // If source is infinite, reset it's stack
386 if (src_can_take_count == -1) {
387 // For the caused_by_move_somewhere == true case we didn't force-put the item,
388 // which guarantees there is no leftover, and code below would duplicate the
389 // (not replaced) to_stack_was item.
390 if (!caused_by_move_somewhere) {
391 // If destination stack is of different type and there are leftover
392 // items, attempt to put the leftover items to a different place in the
393 // destination inventory.
394 // The client-side GUI will try to guess if this happens.
395 if (from_stack_was.name != to_stack_was.name) {
396 for (u32 i = 0; i < list_to->getSize(); i++) {
397 if (list_to->getItem(i).empty()) {
398 list_to->changeItem(i, to_stack_was);
404 if (move_count > 0 || did_swap) {
405 list_from->deleteItem(from_i);
406 list_from->addItem(from_i, from_stack_was);
409 // If destination is infinite, reset it's stack and take count from source
410 if (dst_can_put_count == -1) {
411 list_to->deleteItem(to_i);
412 list_to->addItem(to_i, to_stack_was);
413 list_from->deleteItem(from_i);
414 list_from->addItem(from_i, from_stack_was);
415 list_from->takeItem(from_i, count);
418 infostream << "IMoveAction::apply(): moved"
419 << " msom=" << move_somewhere
420 << " caused=" << caused_by_move_somewhere
421 << " count=" << count
422 << " from inv=\"" << from_inv.dump() << "\""
423 << " list=\"" << from_list << "\""
425 << " to inv=\"" << to_inv.dump() << "\""
426 << " list=\"" << to_list << "\""
430 // If we are inside the move somewhere loop, we don't need to report
431 // anything if nothing happened (perhaps we don't need to report
432 // anything for caused_by_move_somewhere == true, but this way its safer)
433 if (caused_by_move_somewhere && move_count == 0)
437 Record rollback information
439 if (!ignore_rollback && gamedef->rollback()) {
440 IRollbackManager *rollback = gamedef->rollback();
442 // If source is not infinite, record item take
443 if (src_can_take_count != -1) {
444 RollbackAction action;
447 std::ostringstream os(std::ios::binary);
448 from_inv.serialize(os);
451 action.setModifyInventoryStack(loc, from_list, from_i, false,
453 rollback->reportAction(action);
455 // If destination is not infinite, record item put
456 if (dst_can_put_count != -1) {
457 RollbackAction action;
460 std::ostringstream os(std::ios::binary);
461 to_inv.serialize(os);
464 action.setModifyInventoryStack(loc, to_list, to_i, true,
466 rollback->reportAction(action);
471 Report move to endpoints
474 /* Detached inventories */
476 // Both endpoints are same detached
477 if (from_inv.type == InventoryLocation::DETACHED &&
478 from_inv == to_inv) {
479 PLAYER_TO_SA(player)->detached_inventory_OnMove(
480 *this, count, player);
482 // Destination is detached
483 if (to_inv.type == InventoryLocation::DETACHED) {
484 PLAYER_TO_SA(player)->detached_inventory_OnPut(
485 *this, src_item, player);
487 // Source is detached
488 if (from_inv.type == InventoryLocation::DETACHED) {
489 PLAYER_TO_SA(player)->detached_inventory_OnTake(
490 *this, src_item, player);
494 /* Node metadata inventories */
496 // Both endpoints are same nodemeta
497 if (from_inv.type == InventoryLocation::NODEMETA &&
498 from_inv == to_inv) {
499 PLAYER_TO_SA(player)->nodemeta_inventory_OnMove(
500 *this, count, player);
502 // Destination is nodemeta
503 if (to_inv.type == InventoryLocation::NODEMETA) {
504 PLAYER_TO_SA(player)->nodemeta_inventory_OnPut(
505 *this, src_item, player);
507 // Source is nodemeta
508 if (from_inv.type == InventoryLocation::NODEMETA) {
509 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
510 *this, src_item, player);
514 // Player inventories
516 // Both endpoints are same player inventory
517 if (from_inv.type == InventoryLocation::PLAYER &&
518 from_inv == to_inv) {
519 PLAYER_TO_SA(player)->player_inventory_OnMove(
520 *this, count, player);
522 // Destination is player inventory
523 if (to_inv.type == InventoryLocation::PLAYER) {
524 PLAYER_TO_SA(player)->player_inventory_OnPut(
525 *this, src_item, player);
527 // Source is player inventory
528 if (from_inv.type == InventoryLocation::PLAYER) {
529 PLAYER_TO_SA(player)->player_inventory_OnTake(
530 *this, src_item, player);
534 mgr->setInventoryModified(from_inv);
535 if (inv_from != inv_to)
536 mgr->setInventoryModified(to_inv);
539 void IMoveAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
541 // Optional InventoryAction operation that is run on the client
542 // to make lag less apparent.
544 Inventory *inv_from = mgr->getInventory(from_inv);
545 Inventory *inv_to = mgr->getInventory(to_inv);
546 if (!inv_from || !inv_to)
549 InventoryLocation current_player;
550 current_player.setCurrentPlayer();
551 Inventory *inv_player = mgr->getInventory(current_player);
552 if (inv_from != inv_player || inv_to != inv_player)
555 InventoryList *list_from = inv_from->getList(from_list);
556 InventoryList *list_to = inv_to->getList(to_list);
557 if (!list_from || !list_to)
561 list_from->moveItem(from_i, list_to, to_i, count);
563 list_from->moveItemSomewhere(from_i, list_to, count);
565 mgr->setInventoryModified(from_inv);
566 if (inv_from != inv_to)
567 mgr->setInventoryModified(to_inv);
574 IDropAction::IDropAction(std::istream &is)
578 std::getline(is, ts, ' ');
581 std::getline(is, ts, ' ');
582 from_inv.deSerialize(ts);
584 std::getline(is, from_list, ' ');
586 std::getline(is, ts, ' ');
590 void IDropAction::apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef)
592 Inventory *inv_from = mgr->getInventory(from_inv);
595 infostream<<"IDropAction::apply(): FAIL: source inventory not found: "
596 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
600 InventoryList *list_from = inv_from->getList(from_list);
603 If a list doesn't exist or the source item doesn't exist
606 infostream<<"IDropAction::apply(): FAIL: source list not found: "
607 <<"from_inv=\""<<from_inv.dump()<<"\""<<std::endl;
610 if (list_from->getItem(from_i).empty()) {
611 infostream<<"IDropAction::apply(): FAIL: source item not found: "
612 <<"from_inv=\""<<from_inv.dump()<<"\""
613 <<", from_list=\""<<from_list<<"\""
614 <<" from_i="<<from_i<<std::endl;
619 Do not handle rollback if inventory is player's
621 bool ignore_src_rollback = (from_inv.type == InventoryLocation::PLAYER);
624 Collect information of endpoints
627 int take_count = list_from->getItem(from_i).count;
628 if (count != 0 && count < take_count)
630 int src_can_take_count = take_count;
632 ItemStack src_item = list_from->getItem(from_i);
633 src_item.count = take_count;
635 // Run callbacks depending on source inventory
636 switch (from_inv.type) {
637 case InventoryLocation::DETACHED:
638 src_can_take_count = PLAYER_TO_SA(player)->detached_inventory_AllowTake(
639 *this, src_item, player);
641 case InventoryLocation::NODEMETA:
642 src_can_take_count = PLAYER_TO_SA(player)->nodemeta_inventory_AllowTake(
643 *this, src_item, player);
645 case InventoryLocation::PLAYER:
646 src_can_take_count = PLAYER_TO_SA(player)->player_inventory_AllowTake(
647 *this, src_item, player);
653 if (src_can_take_count != -1 && src_can_take_count < take_count)
654 take_count = src_can_take_count;
656 // Update item due executed callbacks
657 src_item = list_from->getItem(from_i);
660 ItemStack item1 = list_from->getItem(from_i);
661 item1.count = take_count;
662 if(PLAYER_TO_SA(player)->item_OnDrop(item1, player,
663 player->getBasePosition())) {
664 int actually_dropped_count = take_count - item1.count;
666 if (actually_dropped_count == 0) {
667 infostream<<"Actually dropped no items"<<std::endl;
669 // Revert client prediction. See 'clientApply'
670 if (from_inv.type == InventoryLocation::PLAYER)
671 list_from->setModified();
675 // If source isn't infinite
676 if (src_can_take_count != -1) {
677 // Take item from source list
678 ItemStack item2 = list_from->takeItem(from_i, actually_dropped_count);
680 if (item2.count != actually_dropped_count)
681 errorstream<<"Could not take dropped count of items"<<std::endl;
684 src_item.count = actually_dropped_count;
685 mgr->setInventoryModified(from_inv);
688 infostream<<"IDropAction::apply(): dropped "
689 <<" from inv=\""<<from_inv.dump()<<"\""
690 <<" list=\""<<from_list<<"\""
696 Report drop to endpoints
699 switch (from_inv.type) {
700 case InventoryLocation::DETACHED:
701 PLAYER_TO_SA(player)->detached_inventory_OnTake(
702 *this, src_item, player);
704 case InventoryLocation::NODEMETA:
705 PLAYER_TO_SA(player)->nodemeta_inventory_OnTake(
706 *this, src_item, player);
708 case InventoryLocation::PLAYER:
709 PLAYER_TO_SA(player)->player_inventory_OnTake(
710 *this, src_item, player);
717 Record rollback information
719 if (!ignore_src_rollback && gamedef->rollback()) {
720 IRollbackManager *rollback = gamedef->rollback();
722 // If source is not infinite, record item take
723 if (src_can_take_count != -1) {
724 RollbackAction action;
727 std::ostringstream os(std::ios::binary);
728 from_inv.serialize(os);
731 action.setModifyInventoryStack(loc, from_list, from_i,
733 rollback->reportAction(action);
738 void IDropAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
740 // Optional InventoryAction operation that is run on the client
741 // to make lag less apparent.
743 Inventory *inv_from = mgr->getInventory(from_inv);
747 InventoryLocation current_player;
748 current_player.setCurrentPlayer();
749 Inventory *inv_player = mgr->getInventory(current_player);
750 if (inv_from != inv_player)
753 InventoryList *list_from = inv_from->getList(from_list);
758 list_from->changeItem(from_i, ItemStack());
760 list_from->takeItem(from_i, count);
762 mgr->setInventoryModified(from_inv);
769 ICraftAction::ICraftAction(std::istream &is)
773 std::getline(is, ts, ' ');
776 std::getline(is, ts, ' ');
777 craft_inv.deSerialize(ts);
780 void ICraftAction::apply(InventoryManager *mgr,
781 ServerActiveObject *player, IGameDef *gamedef)
783 Inventory *inv_craft = mgr->getInventory(craft_inv);
786 infostream << "ICraftAction::apply(): FAIL: inventory not found: "
787 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
791 InventoryList *list_craft = inv_craft->getList("craft");
792 InventoryList *list_craftresult = inv_craft->getList("craftresult");
793 InventoryList *list_main = inv_craft->getList("main");
796 If a list doesn't exist or the source item doesn't exist
799 infostream << "ICraftAction::apply(): FAIL: craft list not found: "
800 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
803 if (!list_craftresult) {
804 infostream << "ICraftAction::apply(): FAIL: craftresult list not found: "
805 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
808 if (list_craftresult->getSize() < 1) {
809 infostream << "ICraftAction::apply(): FAIL: craftresult list too short: "
810 << "craft_inv=\"" << craft_inv.dump() << "\"" << std::endl;
815 ItemStack craftresultitem;
816 int count_remaining = count;
817 std::vector<ItemStack> output_replacements;
818 getCraftingResult(inv_craft, crafted, output_replacements, false, gamedef);
819 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
820 bool found = !crafted.empty();
822 while (found && list_craftresult->itemFits(0, crafted)) {
823 InventoryList saved_craft_list = *list_craft;
825 std::vector<ItemStack> temp;
826 // Decrement input and add crafting output
827 getCraftingResult(inv_craft, crafted, temp, true, gamedef);
828 PLAYER_TO_SA(player)->item_OnCraft(crafted, player, &saved_craft_list, craft_inv);
829 list_craftresult->addItem(0, crafted);
830 mgr->setInventoryModified(craft_inv);
832 // Add the new replacements to the list
833 IItemDefManager *itemdef = gamedef->getItemDefManager();
834 for (auto &itemstack : temp) {
835 for (auto &output_replacement : output_replacements) {
836 if (itemstack.name == output_replacement.name) {
837 itemstack = output_replacement.addItem(itemstack, itemdef);
838 if (itemstack.empty())
842 output_replacements.push_back(itemstack);
845 actionstream << player->getDescription()
847 << crafted.getItemString()
851 if (count_remaining == 1)
854 if (count_remaining > 1)
857 // Get next crafting result
858 getCraftingResult(inv_craft, crafted, temp, false, gamedef);
859 PLAYER_TO_SA(player)->item_CraftPredict(crafted, player, list_craft, craft_inv);
860 found = !crafted.empty();
863 // Put the replacements in the inventory or drop them on the floor, if
864 // the inventory is full
865 for (auto &output_replacement : output_replacements) {
867 output_replacement = list_main->addItem(output_replacement);
868 if (output_replacement.empty())
870 u16 count = output_replacement.count;
872 PLAYER_TO_SA(player)->item_OnDrop(output_replacement, player,
873 player->getBasePosition());
874 if (count >= output_replacement.count) {
875 errorstream << "Couldn't drop replacement stack " <<
876 output_replacement.getItemString() << " because drop loop didn't "
877 "decrease count." << std::endl;
881 } while (!output_replacement.empty());
884 infostream<<"ICraftAction::apply(): crafted "
885 <<" craft_inv=\""<<craft_inv.dump()<<"\""
889 void ICraftAction::clientApply(InventoryManager *mgr, IGameDef *gamedef)
891 // Optional InventoryAction operation that is run on the client
892 // to make lag less apparent.
897 bool getCraftingResult(Inventory *inv, ItemStack &result,
898 std::vector<ItemStack> &output_replacements,
899 bool decrementInput, IGameDef *gamedef)
903 // Get the InventoryList in which we will operate
904 InventoryList *clist = inv->getList("craft");
908 // Mangle crafting grid to an another format
910 ci.method = CRAFT_METHOD_NORMAL;
911 ci.width = clist->getWidth() ? clist->getWidth() : 3;
912 for (u16 i=0; i < clist->getSize(); i++)
913 ci.items.push_back(clist->getItem(i));
915 // Find out what is crafted and add it to result item slot
917 bool found = gamedef->getCraftDefManager()->getCraftResult(
918 ci, co, output_replacements, decrementInput, gamedef);
920 result.deSerialize(co.item, gamedef->getItemDefManager());
922 if (found && decrementInput) {
923 // CraftInput has been changed, apply changes in clist
924 for (u16 i=0; i < clist->getSize(); i++) {
925 clist->changeItem(i, ci.items[i]);