forked from TechNolaByte/Food_Expansion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mortar_pestle.lua
194 lines (176 loc) · 5.66 KB
/
mortar_pestle.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
mortar_pestle = {
setting = {
item_displacement = -1/16,
}
}
minetest.register_tool("food_expansion:pestle", {
description = "Pestle",
inventory_image = "unknown.png",
})
local tmp = {}
minetest.register_entity("food_expansion:item2",{
hp_max = 1,
visual="wielditem",
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 + mortar_pestle.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:item2" 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 + mortar_pestle.setting.item_displacement
tmp.nodename = node.name
tmp.texture = inv:get_stack("input", 1):get_name()
local e = minetest.add_entity(pos,"food_expansion:item2")
local yaw = math.pi*2 - node.param2 * math.pi/2
e:setyaw(yaw)
end
end
minetest.register_node("food_expansion:mortar", {
description = "Mortar",
tiles = {"mortar_pestle.png"},
groups = {cracky=1},
drawtype = "nodebox",
paramtype = "light",
node_box = {
type = "fixed",
fixed = {
{-0.1875, -0.5, -0.1875, 0.1875, -0.4375, 0.1875}, -- NodeBox2
{-0.25, -0.5, 0.1875, 0.25, -0.0625, 0.25}, -- NodeBox3
{-0.25, -0.5, -0.25, 0.25, -0.0625, -0.1875}, -- NodeBox4
{-0.25, -0.5, -0.25, -0.1875, -0.0625, 0.25}, -- NodeBox5
{0.1875, -0.5, -0.25, 0.25, -0.0625, 0.25}, -- NodeBox6
{-0.3125, -0.4375, -0.25, -0.25, 3.72529e-009, 0.25}, -- NodeBox7
{0.25, -0.4375, -0.25, 0.3125, 3.72529e-009, 0.25}, -- NodeBox8
{-0.3125, -0.4375, -0.3125, 0.3125, 2.04891e-008, -0.25}, -- NodeBox9
{-0.3125, -0.4375, 0.25, 0.3125, 2.98023e-008, 0.3125}, -- NodeBox10
}
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("input", 1)
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 stack:get_name() == "food_expansion:pestle" 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,
on_punch = function(pos, node, puncher)
local wielded = puncher:get_wielded_item()
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if wielded:get_name() ~= 'food_expansion:pestle' then
return
end
local input = inv:get_stack('input',1)
local function register_grind(inp, outp)
if input:get_name() == inp then
pos.y = pos.y + mortar_pestle.setting.item_displacement
minetest.sound_play({name="default_dig_cracky"}, {pos=pos})
inv:set_stack("input", 1, nil)
remove_item(pos,node)
update_item(pos,node)
minetest.spawn_item(pos, outp)
end
end
register_grind("default:stone", "default:cobble")
-- damage the pestle slightly
wielded:add_wear(500)
puncher:set_wielded_item(wielded)
end,
is_ground_content = false,
})
-- automatically restore entities lost due to /clearobjects or similar
minetest.register_lbm({
name = "food_expansion:mortar_pestle_item_restoration",
nodenames = { "food_expansion:mortar" },
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 + mortar_pestle.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
})