Skip to content

Instantly share code, notes, and snippets.

@AntumDeluge
Last active June 11, 2021 17:55
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/cce0c653d817205685b268b318302432 to your computer and use it in GitHub Desktop.
Save AntumDeluge/cce0c653d817205685b268b318302432 to your computer and use it in GitHub Desktop.
Minetest InvRef callbacks tests full
local slot_count = 2
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,
allow_put = function(inv, listname, index, stack, player)
print("\nAllow put " .. stack:get_count() .. " " .. stack:get_name()
.. " from " .. listname .. " " .. index)
return -1
end,
allow_take = function(inv, listname, index, stack, player)
print("\nAllow take " .. stack:get_count() .. " " .. stack:get_name()
.. " from " .. listname .. " " .. 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,
on_put = function(inv, listname, index, stack, player)
print("Putting " .. stack:get_count() .. " " .. stack:get_name())
-- FIXME: items in target slot are lost if they are not the same item
local to_stack = inv:get_stack(listname, index)
if not to_stack:is_empty() then
print("Replacing " .. to_stack:get_count() .. " " .. to_stack:get_name())
end
if to_stack:get_name() == stack:get_name() then
stack:set_count(stack:get_count() + to_stack:get_count())
end
inv:set_stack(listname, index, stack)
end,
on_take = function(inv, listname, index, stack, player)
print("Taking " .. stack:get_count() .. " " .. stack:get_name())
local from_stack = inv:get_stack(listname, index)
from_stack:set_count(from_stack:get_count() - stack:get_count())
inv:set_stack(listname, index, from_stack)
end,
})
dinv:set_size("deposit", slot_count)
local function get_formspec(pname)
local formspec = "size[12,10]"
.. "list[detached:dropbox;deposit;2,1.5;" .. slot_count .. ",1;0]"
.. "list[current_player;main;2,5.5;8,4;0]"
return formspec
end
core.register_chatcommand("testme", {
description = "Test command",
func = function(name, param)
core.show_formspec(name, "testme", get_formspec(name))
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment