2022-10-09 14:22:42 +02:00
|
|
|
--
|
|
|
|
-- Item callbacks
|
|
|
|
--
|
|
|
|
|
2022-10-09 16:41:55 +02:00
|
|
|
local function print_to_everything(msg)
|
2024-10-28 15:57:54 +01:00
|
|
|
core.log("action", "[callbacks] " .. msg)
|
|
|
|
core.chat_send_all(msg)
|
2022-10-09 16:41:55 +02:00
|
|
|
end
|
|
|
|
|
2024-10-28 15:57:54 +01:00
|
|
|
core.register_craftitem("callbacks:callback_item_1", {
|
2022-10-09 14:34:10 +02:00
|
|
|
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".."\n"..
|
|
|
|
"Aux1 + pickup item: Print additional on_pickup arguments",
|
2022-10-09 14:22:42 +02:00
|
|
|
inventory_image = "callbacks_callback_item_1.png",
|
|
|
|
wield_image = "callbacks_callback_item_1.png",
|
2022-10-08 18:58:02 +02:00
|
|
|
groups = { callback_test = 1 },
|
2022-10-09 14:22:42 +02:00
|
|
|
|
|
|
|
on_secondary_use = function(itemstack, user, pointed_thing)
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("[callbacks:callback_item_1 on_secondary_use] " .. itemstack:get_name())
|
2022-10-09 14:22:42 +02:00
|
|
|
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)
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("[callbacks:callback_item_1 on_drop] " .. itemstack:get_name())
|
2022-10-09 14:22:42 +02:00
|
|
|
local ctrl = dropper and dropper:get_player_control() or {}
|
|
|
|
if ctrl.sneak then
|
|
|
|
itemstack = ItemStack(itemstack)
|
|
|
|
itemstack:set_name("callbacks:callback_item_2")
|
|
|
|
end
|
|
|
|
|
2024-10-28 15:57:54 +01:00
|
|
|
return core.item_drop(itemstack, dropper, pos)
|
2022-10-09 14:22:42 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_pickup = function(itemstack, picker, pointed_thing, ...)
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("[callbacks:callback_item_1 on_pickup]")
|
2022-10-09 14:22:42 +02:00
|
|
|
assert(pointed_thing.ref:get_luaentity().name == "__builtin:item")
|
|
|
|
local ctrl = picker and picker:get_player_control() or {}
|
|
|
|
if ctrl.aux1 then
|
|
|
|
-- Debug message
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("on_pickup dump:")
|
|
|
|
print_to_everything(dump({...}))
|
2022-10-09 14:22:42 +02:00
|
|
|
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")
|
2024-10-28 15:57:54 +01:00
|
|
|
local leftover = core.item_pickup(taken, picker, pointed_thing, ...)
|
2022-10-09 14:22:42 +02:00
|
|
|
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
|
2024-10-28 15:57:54 +01:00
|
|
|
return core.do_item_eat(2, nil, itemstack, picker, pointed_thing)
|
2022-10-09 14:22:42 +02:00
|
|
|
else
|
|
|
|
-- Normal: pick up everything
|
2024-10-28 15:57:54 +01:00
|
|
|
return core.item_pickup(itemstack, picker, pointed_thing, ...)
|
2022-10-09 14:22:42 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_use = function(itemstack, user, pointed_thing)
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("[callbacks:callback_item_1 on_use] " .. itemstack:get_name())
|
2022-10-09 14:22:42 +02:00
|
|
|
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
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("[callbacks:callback_item_1 after_use]")
|
2022-10-09 14:22:42 +02:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2024-10-28 15:57:54 +01:00
|
|
|
core.register_craftitem("callbacks:callback_item_2", {
|
2022-10-09 14:34:10 +02:00
|
|
|
description = "Callback Test Item 2".."\n"..
|
|
|
|
"Punch to switch to Callback Test Item 1",
|
2022-10-09 14:22:42 +02:00
|
|
|
inventory_image = "callbacks_callback_item_2.png",
|
|
|
|
wield_image = "callbacks_callback_item_2.png",
|
2022-10-08 18:58:02 +02:00
|
|
|
groups = { callback_test = 1 },
|
2022-10-09 14:22:42 +02:00
|
|
|
|
|
|
|
on_use = function(itemstack, user, pointed_thing)
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("[callbacks:callback_item_2 on_use]")
|
2022-10-09 14:22:42 +02:00
|
|
|
itemstack = ItemStack(itemstack)
|
|
|
|
itemstack:set_name("callbacks:callback_item_1")
|
|
|
|
return itemstack
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2024-10-28 15:57:54 +01:00
|
|
|
core.register_on_item_pickup(function(itemstack, picker, pointed_thing, time_from_last_punch, ...)
|
2022-10-09 14:22:42 +02:00
|
|
|
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
|
2022-10-09 16:41:55 +02:00
|
|
|
print_to_everything("["..item_name.." register_on_item_pickup]")
|
2022-10-09 14:22:42 +02:00
|
|
|
|
|
|
|
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)
|