-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.lua
175 lines (158 loc) · 4.76 KB
/
tools.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
chopping_board = {
setting = {
item_displacement = -1/16,
}
}
minetest.register_tool("food_expansion:clever", {
description = "Kitchen clever",
inventory_image = "unknown.png",
})
local tmp = {}
minetest.register_entity("food_expansion:item",{
hp_max = 1,
visual="mesh",
mesh = 'item.obj',
visual_size={x=.33,y=.33},
collisionbox = {0,0,0,0,0,0},
physical=false,
textures={"air"},
on_activate = function(self, staticdata)
if tmp.nodename ~= nil and tmp.texture ~= nil then
self.nodename = tmp.nodename
tmp.nodename = nil
self.texture = tmp.texture
tmp.texture = nil
else
if staticdata ~= nil and staticdata ~= "" then
local data = staticdata:split(';')
if data and data[1] and data[2] then
self.nodename = data[1]
self.texture = data[2]
end
end
end
if self.texture ~= nil then
self.object:set_properties({textures={self.texture}})
end
end,
get_staticdata = function(self)
if self.nodename ~= nil and self.texture ~= nil then
return self.nodename .. ';' .. self.texture
end
return ""
end,
})
local remove_item = function(pos, node)
local objs = minetest.get_objects_inside_radius({x = pos.x, y = pos.y + chopping_board.setting.item_displacement, z = pos.z}, .5)
if objs then
for _, obj in ipairs(objs) do
if obj and obj:get_luaentity() and obj:get_luaentity().name == "food_expansion:item" then
obj:remove()
end
end
end
end
local update_item = function(pos, node)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if not inv:is_empty("input") then
pos.y = pos.y + chopping_board.setting.item_displacement
tmp.nodename = node.name
tmp.texture = inv:get_stack("input", 1):get_name()
local e = minetest.add_entity(pos,"food_expansion:item")
local yaw = math.pi*2 - node.param2 * math.pi/2
e:setyaw(yaw)
end
end
minetest.register_node("food_expansion:chopping_board", {
drawtype = "nodebox",
description = "Chopping board",
tiles = {
"default_aspen_tree_top.png",
"default_aspen_tree_top.png",
"default_aspen_tree.png",
"default_aspen_tree.png",
"default_aspen_tree.png",
"default_aspen_tree.png"
},
paramtype = "light",
groups = {choppy=2},
node_box = {
type = "fixed",
fixed = {
{-0.375, -0.5, -0.4375, 0.4375, -0.375, 0.4375}, -- NodeBox1
}
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("input", 1)
end,
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if not inv:is_empty("input") then
return false
end
return true
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if listname~="input" then
return 0
end
if (listname=='input'
and(stack:get_name() == "food_expansion:clever" )) then
return 0
end
if meta:get_inventory():room_for_item("input", stack) then
return stack:get_count()
end
return 0
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if listname~="input" then
return 0
end
return stack:get_count()
end,
on_rightclick = function(pos, node, clicker, itemstack)
if itemstack:get_count() == 0 then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if not inv:is_empty("input") then
local return_stack = inv:get_stack("input", 1)
inv:set_stack("input", 1, nil)
local wield_index = clicker:get_wield_index()
clicker:get_inventory():set_stack("main", wield_index, return_stack)
remove_item(pos, node)
return return_stack
end
end
local this_def = minetest.registered_nodes[node.name]
if this_def.allow_metadata_inventory_put(pos, "input", 1, itemstack:peek_item(), clicker) > 0 then
local s = itemstack:take_item()
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:add_item("input", s)
update_item(pos,node)
end
return itemstack
end,
is_ground_content = false,
})
-- automatically restore entities lost due to /clearobjects or similar
minetest.register_lbm({
name = "food_expansion:chopping_board_item_restoration",
nodenames = { "food_expansion:chopping_board" },
run_at_every_load = true,
action = function(pos, node, active_object_count, active_object_count_wider)
local test_pos = {x=pos.x, y=pos.y + chopping_board.setting.item_displacement, z=pos.z}
if #minetest.get_objects_inside_radius(test_pos, 0.5) > 0 then return end
update_item(pos, node)
end
})