Skip to content

Instantly share code, notes, and snippets.

@AntumDeluge
Last active June 11, 2021 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntumDeluge/0236efc1b2c22bdd454177cc3490570c to your computer and use it in GitHub Desktop.
Save AntumDeluge/0236efc1b2c22bdd454177cc3490570c to your computer and use it in GitHub Desktop.
Minetest InvRef callbacks tests
local dinv = core.create_detached_inventory("dropbox", {
-- NOTE: this is not called when moving items between two different inventories
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
local from_stack = inv:get_stack(from_list, from_index)
print("\nAllow move " .. count .. " " .. from_stack:get_name() .. " from "
.. from_list .. " " .. from_index .. " to " .. to_list .. " " .. to_index)
return -1
end,
-- NOTE: this is not called when moving items between two different inventories
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
local from_stack = inv:get_stack(from_list, from_index)
local to_stack = inv:get_stack(to_list, to_index)
-- FIXME: from_stack & to_stack are empty
if from_stack:is_empty() and to_stack:is_empty() then
print("Nothing to move")
else
print("From stack: " .. from_list .. " " .. from_stack:get_count() .. " "
.. from_stack:get_name() .. " index " .. from_index)
print("To stack: " .. to_list .. " " .. to_stack:get_count() .. " "
.. to_stack:get_name() .. " index " .. to_index)
print("\nMoving " .. count .. " " .. from_stack:get_name())
if from_stack:get_name() == to_stack:get_name() then
to_stack:set_count(to_stack:get_count() + count)
end
from_stack:set_count(from_stack:get_count() - count)
inv:set_stack(from_list, from_index, from_stack)
inv:set_stack(to_list, to_index, to_stack)
end
end,
})
dinv:set_size("deposit", 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment