-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
151 lines (130 loc) · 3.56 KB
/
init.lua
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
cycle = {}
-- The version ID indicating API features.
-- Increase this number on every changes that touched
-- the behaviour of the APIs.
-- Mods can then check the compactibility by comparing
-- this value to the target version.
-- Ideally, all changes should be backward-compactible, so
-- mods should only check for a lower limit of this variable.
cycle.version = 1
local err = {}
function err.unreg(v)
error("Please only call cycle functions after the registrations of the items!" .. v)
end
--## cycle.node APIs ##--
cycle.node = {}
function cycle.node.punchcycle(list)
for i,v in ipairs(list) do
local n = i + 1
if n > #list then
n = 1
end
local nodedef = minetest.registered_nodes[v]
if not nodedef then
err.unreg(v)
end
local old_on_punch = nodedef.on_punch
local new_on_punch = function(pos, node, puncher, pointed_thing)
node.name = list[n]
minetest.swap_node(pos,node)
if old_on_punch then
return old_on_punch(pos, node, puncher, pointed_thing)
end
end
minetest.override_item(v,{on_punch=new_on_punch})
end
end
function cycle.node.rightclickcycle(list)
for i,v in ipairs(list) do
local n = i + 1
if n > #list then
n = 1
end
local nodedef = minetest.registered_nodes[v]
if not nodedef then
err.unreg(v)
end
local old_on_rightclick = nodedef.on_rightclick
local new_on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
node.name = list[n]
minetest.swap_node(pos,node)
if old_on_rightclick then
return old_on_rightclick(pos, node, clicker, itemstack, pointed_thing)
end
end
minetest.override_item(v,{on_rightclick=new_on_rightclick})
end
end
--## cycle.item APIs ##--
cycle.item = {}
function cycle.item.placecycle(list)
for i,v in ipairs(list) do
local n = i + 1
if n > #list then
n = 1
end
local itemdef = minetest.registered_items[v]
if not itemdef then
err.unreg(v)
end
local old_on_place = itemdef.on_place
local new_on_place = function(itemstack, placer, pointed_thing)
itemstack:set_name(list[n])
if old_on_place then
return old_on_place(itemstack, placer, pointed_thing)
end
return itemstack
end
minetest.override_item(v,{on_place=new_on_place})
end
end
function cycle.item.secondarycycle(list)
for i,v in ipairs(list) do
local n = i + 1
if n > #list then
n = 1
end
local itemdef = minetest.registered_items[v]
if not itemdef then
err.unreg(v)
end
local old_on_secondary_use = itemdef.on_secondary_use
local new_on_secondary_use = function(itemstack, user, pointed_thing)
itemstack:set_name(list[n])
if old_on_secondary_use then
return old_on_place(itemstack, user, pointed_thing)
end
return itemstack
end
minetest.override_item(v,{on_secondary_use=new_on_secondary_use})
end
end
function cycle.item.usecycle(list)
for i,v in ipairs(list) do
local n = i + 1
if n > #list then
n = 1
end
local itemdef = minetest.registered_items[v]
if not itemdef then
err.unreg(v)
end
local old_on_place = itemdef.on_place
local new_on_place = function(itemstack, placer, pointed_thing)
itemstack:set_name(list[n])
if old_on_place then
return old_on_place(itemstack, placer, pointed_thing)
end
return itemstack
end
local old_on_secondary_use = itemdef.on_secondary_use
local new_on_secondary_use = function(itemstack, user, pointed_thing)
itemstack:set_name(list[n])
if old_on_secondary_use then
return old_on_place(itemstack, user, pointed_thing)
end
return itemstack
end
minetest.override_item(v,{on_place=new_on_place,on_secondary_use=new_on_secondary_use})
end
end