1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
--
-- Item callbacks
--
local function print_to_everything(msg)
minetest.log("action", "[callbacks] " .. msg)
minetest.chat_send_all(msg)
end
minetest.register_craftitem("callbacks:callback_item_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".."\n"..
"Aux1 + pickup item: Print additional on_pickup arguments",
inventory_image = "callbacks_callback_item_1.png",
wield_image = "callbacks_callback_item_1.png",
groups = { callback_test = 1 },
on_secondary_use = function(itemstack, user, pointed_thing)
print_to_everything("[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)
print_to_everything("[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, ...)
print_to_everything("[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
print_to_everything("on_pickup dump:")
print_to_everything(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)
print_to_everything("[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
print_to_everything("[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"..
"Punch to switch to Callback Test Item 1",
inventory_image = "callbacks_callback_item_2.png",
wield_image = "callbacks_callback_item_2.png",
groups = { callback_test = 1 },
on_use = function(itemstack, user, pointed_thing)
print_to_everything("[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
print_to_everything("["..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)
|