Skip to content

Commit 050964b

Browse files
appgurueuSmallJoker
andauthoredSep 4, 2020
Fix inventory swapping not calling all callbacks (#9923)
"Predicts" whether something will be swapped for allow callbacks, then calls callbacks a second time with swapped properties. Co-authored-by: SmallJoker <SmallJoker@users.noreply.github.com>
1 parent 4ba5046 commit 050964b

File tree

4 files changed

+210
-139
lines changed

4 files changed

+210
-139
lines changed
 

‎doc/lua_api.txt

+25
Original file line numberDiff line numberDiff line change
@@ -5888,6 +5888,31 @@ An `InvRef` is a reference to an inventory.
58885888
`minetest.get_inventory(location)`.
58895889
* returns `{type="undefined"}` in case location is not known
58905890

5891+
### Callbacks
5892+
5893+
Detached & nodemeta inventories provide the following callbacks for move actions:
5894+
5895+
#### Before
5896+
5897+
The `allow_*` callbacks return how many items can be moved.
5898+
5899+
* `allow_move`/`allow_metadata_inventory_move`: Moving items in the inventory
5900+
* `allow_take`/`allow_metadata_inventory_take`: Taking items from the inventory
5901+
* `allow_put`/`allow_metadata_inventory_put`: Putting items to the inventory
5902+
5903+
#### After
5904+
5905+
The `on_*` callbacks are called after the items have been placed in the inventories.
5906+
5907+
* `on_move`/`on_metadata_inventory_move`: Moving items in the inventory
5908+
* `on_take`/`on_metadata_inventory_take`: Taking items from the inventory
5909+
* `on_put`/`on_metadata_inventory_put`: Putting items to the inventory
5910+
5911+
#### Swapping
5912+
5913+
When a player tries to put an item to a place where another item is, the items are *swapped*.
5914+
This means that all callbacks will be called twice (once for each action).
5915+
58915916
`ItemStack`
58925917
-----------
58935918

‎src/inventory.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -732,26 +732,25 @@ void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
732732
u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i,
733733
u32 count, bool swap_if_needed, bool *did_swap)
734734
{
735-
if(this == dest && i == dest_i)
735+
if (this == dest && i == dest_i)
736736
return count;
737737

738738
// Take item from source list
739739
ItemStack item1;
740-
if(count == 0)
740+
if (count == 0)
741741
item1 = changeItem(i, ItemStack());
742742
else
743743
item1 = takeItem(i, count);
744744

745-
if(item1.empty())
745+
if (item1.empty())
746746
return 0;
747747

748748
// Try to add the item to destination list
749749
u32 oldcount = item1.count;
750750
item1 = dest->addItem(dest_i, item1);
751751

752752
// If something is returned, the item was not fully added
753-
if(!item1.empty())
754-
{
753+
if (!item1.empty()) {
755754
// If olditem is returned, nothing was added.
756755
bool nothing_added = (item1.count == oldcount);
757756

0 commit comments

Comments
 (0)
Please sign in to comment.