DevTest: Move callback items to callbacks mod

This commit is contained in:
Wuzzy 2022-10-09 14:22:42 +02:00 committed by sfan5
parent 68df0fb2ea
commit cb7b96fc90
6 changed files with 161 additions and 165 deletions

@ -1,51 +1,2 @@
local function print_to_everything(msg)
minetest.log("action", msg)
minetest.chat_send_all(msg)
end
minetest.register_node("callbacks:callback_node", {
description = "Callback Test Node (construct/destruct/timer)".."\n"..
"Tests callbacks: on_construct, after_place_node, on_destruct, after_destruct, after_dig_node, on_timer",
tiles = {"callbacks_callback_node.png"},
groups = {dig_immediate=3},
-- This was known to cause a bug in minetest.item_place_node() when used
-- via minetest.place_node(), causing a placer with no position
paramtype2 = "facedir",
drop = "",
on_construct = function(pos)
print_to_everything("callbacks:callback_node:on_construct("..minetest.pos_to_string(pos)..")")
local meta = minetest.get_meta(pos)
meta:set_string("mine", "test")
local timer = minetest.get_node_timer(pos)
timer:start(4, 3)
end,
after_place_node = function(pos, placer)
print_to_everything("callbacks:callback_node:after_place_node("..minetest.pos_to_string(pos)..")")
local meta = minetest.get_meta(pos)
if meta:get_string("mine") == "test" then
print_to_everything("correct metadata found")
else
print_to_everything("incorrect metadata found")
end
end,
on_destruct = function(pos)
print_to_everything("callbacks:callback_node:on_destruct("..minetest.pos_to_string(pos)..")")
end,
after_destruct = function(pos)
print_to_everything("callbacks:callback_node:after_destruct("..minetest.pos_to_string(pos)..")")
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
print_to_everything("callbacks:callback_node:after_dig_node("..minetest.pos_to_string(pos)..")")
end,
on_timer = function(pos, elapsed)
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed))
return true
end,
})
dofile(minetest.get_modpath("callbacks").."/items.lua")
dofile(minetest.get_modpath("callbacks").."/nodes.lua")

@ -0,0 +1,108 @@
--
-- Item callbacks
--
minetest.register_craftitem("callbacks:callback_item_1", {
description = "Callback test item 1\n(Use/Drop + Sneak to switch to item 2)",
inventory_image = "callbacks_callback_item_1.png",
wield_image = "callbacks_callback_item_1.png",
on_secondary_use = function(itemstack, user, pointed_thing)
minetest.log("[callbacks:callback_item_1 on_secondary_use] " .. itemstack:get_name())
local ctrl = user and user:get_player_control() or {}
if ctrl.sneak then
itemstack = ItemStack(itemstack)
itemstack:set_name("callbacks:callback_item_2")
return itemstack
end
end,
on_drop = function(itemstack, dropper, pos)
minetest.log("[callbacks:callback_item_1 on_drop] " .. itemstack:get_name())
local ctrl = dropper and dropper:get_player_control() or {}
if ctrl.sneak then
itemstack = ItemStack(itemstack)
itemstack:set_name("callbacks:callback_item_2")
end
return minetest.item_drop(itemstack, dropper, pos)
end,
on_pickup = function(itemstack, picker, pointed_thing, ...)
minetest.log("[callbacks:callback_item_1 on_pickup]")
assert(pointed_thing.ref:get_luaentity().name == "__builtin:item")
local ctrl = picker and picker:get_player_control() or {}
if ctrl.aux1 then
-- Debug message
minetest.log(dump({...}))
end
if ctrl.sneak then
-- Pick up one item of the other kind at once
local taken = itemstack:take_item()
taken:set_name("callbacks:callback_item_2")
local leftover = minetest.item_pickup(taken, picker, pointed_thing, ...)
leftover:set_name("callbacks:callback_item_1")
itemstack:add_item(leftover)
return itemstack
elseif ctrl.up then
-- Don't pick up
return
elseif ctrl.left then
-- Eat it
return minetest.do_item_eat(2, nil, itemstack, picker, pointed_thing)
else
-- Normal: pick up everything
return minetest.item_pickup(itemstack, picker, pointed_thing, ...)
end
end,
on_use = function(itemstack, user, pointed_thing)
minetest.log("[callbacks:callback_item_1 on_use] " .. itemstack:get_name())
local ctrl = user and user:get_player_control() or {}
if ctrl.sneak then
itemstack = ItemStack(itemstack)
itemstack:set_name("callbacks:callback_item_2")
return itemstack
end
end,
after_use = function(itemstack, user, node, digparams) -- never called
minetest.log("[callbacks:callback_item_1 after_use]")
local ctrl = user and user:get_player_control() or {}
if ctrl.up then
itemstack = ItemStack(itemstack)
itemstack:set_name("callbacks:callback_item_2")
return itemstack
end
end,
})
minetest.register_craftitem("callbacks:callback_item_2", {
description = "Callback test item 2\n(Use to switch to item 1)",
inventory_image = "callbacks_callback_item_2.png",
wield_image = "callbacks_callback_item_2.png",
on_use = function(itemstack, user, pointed_thing)
minetest.log("[callbacks:callback_item_2 on_use]")
itemstack = ItemStack(itemstack)
itemstack:set_name("callbacks:callback_item_1")
return itemstack
end,
})
minetest.register_on_item_pickup(function(itemstack, picker, pointed_thing, time_from_last_punch, ...)
assert(not pointed_thing or pointed_thing.ref:get_luaentity().name == "__builtin:item")
local item_name = itemstack:get_name()
if item_name ~= "callbacks:callback_item_1" and item_name ~= "callbacks:callback_item_2" then
return
end
minetest.log("["..item_name.." register_on_item_pickup]")
local ctrl = picker and picker:get_player_control() or {}
if item_name == "callbacks:callback_item_2" and not ctrl.sneak then
-- Same here. Pick up the other item type.
itemstack:set_name("callbacks:callback_item_1")
return picker:get_inventory():add_item("main", itemstack)
end
end)

@ -0,0 +1,51 @@
local function print_to_everything(msg)
minetest.log("action", msg)
minetest.chat_send_all(msg)
end
minetest.register_node("callbacks:callback_node", {
description = "Callback Test Node (construct/destruct/timer)".."\n"..
"Tests callbacks: on_construct, after_place_node, on_destruct, after_destruct, after_dig_node, on_timer",
tiles = {"callbacks_callback_node.png"},
groups = {dig_immediate=3},
-- This was known to cause a bug in minetest.item_place_node() when used
-- via minetest.place_node(), causing a placer with no position
paramtype2 = "facedir",
drop = "",
on_construct = function(pos)
print_to_everything("callbacks:callback_node:on_construct("..minetest.pos_to_string(pos)..")")
local meta = minetest.get_meta(pos)
meta:set_string("mine", "test")
local timer = minetest.get_node_timer(pos)
timer:start(4, 3)
end,
after_place_node = function(pos, placer)
print_to_everything("callbacks:callback_node:after_place_node("..minetest.pos_to_string(pos)..")")
local meta = minetest.get_meta(pos)
if meta:get_string("mine") == "test" then
print_to_everything("correct metadata found")
else
print_to_everything("incorrect metadata found")
end
end,
on_destruct = function(pos)
print_to_everything("callbacks:callback_node:on_destruct("..minetest.pos_to_string(pos)..")")
end,
after_destruct = function(pos)
print_to_everything("callbacks:callback_node:after_destruct("..minetest.pos_to_string(pos)..")")
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
print_to_everything("callbacks:callback_node:after_dig_node("..minetest.pos_to_string(pos)..")")
end,
on_timer = function(pos, elapsed)
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed))
return true
end,
})

Before

Width:  |  Height:  |  Size: 148 B

After

Width:  |  Height:  |  Size: 148 B

Before

Width:  |  Height:  |  Size: 148 B

After

Width:  |  Height:  |  Size: 148 B

@ -51,117 +51,3 @@ minetest.register_craftitem("testitems:overlay_global", {
wield_overlay = "testitems_overlay_overlay.png",
color = GLOBAL_COLOR_ARG,
})
--
-- Item callbacks
--
minetest.register_craftitem("testitems:callback_1", {
description = "Callback Test Item 1".."\n"..
"Tests callbacks: on_secondary_use, on_drop, on_pickup, on_use, after_use".."\n"..
"Punch/Drop + Sneak: Switch to Callback Test Item 2",
"Aux1 + pickup item: Print additional on_pickup arguments",
inventory_image = "testitems_callback_1.png",
wield_image = "testitems_callback_1.png",
on_secondary_use = function(itemstack, user, pointed_thing)
minetest.log("[testitems:callback_1 on_secondary_use] " .. itemstack:get_name())
local ctrl = user and user:get_player_control() or {}
if ctrl.sneak then
itemstack = ItemStack(itemstack)
itemstack:set_name("testitems:callback_2")
return itemstack
end
end,
on_drop = function(itemstack, dropper, pos)
minetest.log("[testitems:callback_1 on_drop] " .. itemstack:get_name())
local ctrl = dropper and dropper:get_player_control() or {}
if ctrl.sneak then
itemstack = ItemStack(itemstack)
itemstack:set_name("testitems:callback_2")
end
return minetest.item_drop(itemstack, dropper, pos)
end,
on_pickup = function(itemstack, picker, pointed_thing, ...)
minetest.log("[testitems:callback_1 on_pickup]")
assert(pointed_thing.ref:get_luaentity().name == "__builtin:item")
local ctrl = picker and picker:get_player_control() or {}
if ctrl.aux1 then
-- Debug message
minetest.log(dump({...}))
end
if ctrl.sneak then
-- Pick up one item of the other kind at once
local taken = itemstack:take_item()
taken:set_name("testitems:callback_2")
local leftover = minetest.item_pickup(taken, picker, pointed_thing, ...)
leftover:set_name("testitems:callback_1")
itemstack:add_item(leftover)
return itemstack
elseif ctrl.up then
-- Don't pick up
return
elseif ctrl.left then
-- Eat it
return minetest.do_item_eat(2, nil, itemstack, picker, pointed_thing)
else
-- Normal: pick up everything
return minetest.item_pickup(itemstack, picker, pointed_thing, ...)
end
end,
on_use = function(itemstack, user, pointed_thing)
minetest.log("[testitems:callback_1 on_use] " .. itemstack:get_name())
local ctrl = user and user:get_player_control() or {}
if ctrl.sneak then
itemstack = ItemStack(itemstack)
itemstack:set_name("testitems:callback_2")
return itemstack
end
end,
after_use = function(itemstack, user, node, digparams) -- never called
minetest.log("[testitems:callback_1 after_use]")
local ctrl = user and user:get_player_control() or {}
if ctrl.up then
itemstack = ItemStack(itemstack)
itemstack:set_name("testitems:callback_2")
return itemstack
end
end,
})
minetest.register_craftitem("testitems:callback_2", {
description = "Callback Test Item 2".."\n"..
"Punch to switch to Callback Test Item 1",
inventory_image = "testitems_callback_2.png",
wield_image = "testitems_callback_2.png",
on_use = function(itemstack, user, pointed_thing)
minetest.log("[testitems:callback_2 on_use]")
itemstack = ItemStack(itemstack)
itemstack:set_name("testitems:callback_1")
return itemstack
end,
})
minetest.register_on_item_pickup(function(itemstack, picker, pointed_thing, time_from_last_punch, ...)
assert(not pointed_thing or pointed_thing.ref:get_luaentity().name == "__builtin:item")
local item_name = itemstack:get_name()
if item_name ~= "testitems:callback_1" and item_name ~= "testitems:callback_2" then
return
end
minetest.log("["..item_name.." register_on_item_pickup]")
local ctrl = picker and picker:get_player_control() or {}
if item_name == "testitems:callback_2" and not ctrl.sneak then
-- Same here. Pick up the other item type.
itemstack:set_name("testitems:callback_1")
return picker:get_inventory():add_item("main", itemstack)
end
end)