6 local function active_formspec(fuel_percent, item_percent)
12 "list[current_name;src;2.75,0.5;1,1;]"..
13 "list[current_name;fuel;2.75,2.5;1,1;]"..
14 "image[2.75,1.5;1,1;default_furnace_fire_bg.png^[lowpart:"..
15 (100-fuel_percent)..":default_furnace_fire_fg.png]"..
16 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
17 (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
18 "list[current_name;dst;4.75,0.96;2,2;]"..
19 "list[current_player;main;0,4.25;8,1;]"..
20 "list[current_player;main;0,5.5;8,3;8]"..
21 "listring[current_name;dst]"..
22 "listring[current_player;main]"..
23 "listring[current_name;src]"..
24 "listring[current_player;main]"..
25 default.get_hotbar_bg(0, 4.25)
29 local inactive_formspec =
34 "list[current_name;src;2.75,0.5;1,1;]"..
35 "list[current_name;fuel;2.75,2.5;1,1;]"..
36 "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
37 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
38 "list[current_name;dst;4.75,0.96;2,2;]"..
39 "list[current_player;main;0,4.25;8,1;]"..
40 "list[current_player;main;0,5.5;8,3;8]"..
41 "listring[current_name;dst]"..
42 "listring[current_player;main]"..
43 "listring[current_name;src]"..
44 "listring[current_player;main]"..
45 default.get_hotbar_bg(0, 4.25)
48 -- Node callback functions that are the same for active and inactive furnace
51 local function can_dig(pos, player)
52 local meta = minetest.get_meta(pos);
53 local inv = meta:get_inventory()
54 return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
57 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
58 if minetest.is_protected(pos, player:get_player_name()) then
61 local meta = minetest.get_meta(pos)
62 local inv = meta:get_inventory()
63 if listname == "fuel" then
64 if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
65 if inv:is_empty("src") then
66 meta:set_string("infotext", "Furnace is empty")
68 return stack:get_count()
72 elseif listname == "src" then
73 return stack:get_count()
74 elseif listname == "dst" then
79 local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
80 local meta = minetest.get_meta(pos)
81 local inv = meta:get_inventory()
82 local stack = inv:get_stack(from_list, from_index)
83 return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
86 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
87 if minetest.is_protected(pos, player:get_player_name()) then
90 return stack:get_count()
93 local function swap_node(pos, name)
94 local node = minetest.get_node(pos)
95 if node.name == name then
99 minetest.swap_node(pos, node)
102 local function furnace_node_timer(pos, elapsed)
104 -- Inizialize metadata
106 local meta = minetest.get_meta(pos)
107 local fuel_time = meta:get_float("fuel_time") or 0
108 local src_time = meta:get_float("src_time") or 0
109 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
111 local inv = meta:get_inventory()
112 local srclist = inv:get_list("src")
113 local fuellist = inv:get_list("fuel")
114 local dstlist = inv:get_list("dst")
120 -- Check if we have cookable content
121 local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
122 local cookable = true
124 if cooked.time == 0 then
128 -- Check if we have enough fuel to burn
129 if fuel_time < fuel_totaltime then
130 -- The furnace is currently active and has enough fuel
131 fuel_time = fuel_time + 1
133 -- If there is a cookable item then check if it is ready yet
135 src_time = src_time + 1
136 if src_time >= cooked.time then
137 -- Place result in dst list if possible
138 if inv:room_for_item("dst", cooked.item) then
139 inv:add_item("dst", cooked.item)
140 inv:set_stack("src", 1, aftercooked.items[1])
146 -- Furnace ran out of fuel
148 -- We need to get new fuel
149 local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
151 if fuel.time == 0 then
152 -- No valid fuel in fuel list
157 -- Take fuel from fuel list
158 inv:set_stack("fuel", 1, afterfuel.items[1])
160 fuel_totaltime = fuel.time
164 -- We don't need to get new fuel since there is no cookable item
172 -- Update formspec, infotext and node
174 local formspec = inactive_formspec
175 local item_state = ""
176 local item_percent = 0
178 item_percent = math.floor(src_time / cooked.time * 100)
179 item_state = item_percent .. "%"
181 if srclist[1]:is_empty() then
184 item_state = "Not cookable"
188 local fuel_state = "Empty"
189 local active = "inactive "
192 if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
194 local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
195 fuel_state = fuel_percent .. "%"
196 formspec = active_formspec(fuel_percent, item_percent)
197 swap_node(pos, "default:furnace_active")
198 -- make sure timer restarts automatically
201 if not fuellist[1]:is_empty() then
204 swap_node(pos, "default:furnace")
205 -- stop timer on the inactive furnace
206 local timer = minetest.get_node_timer(pos)
210 local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
215 meta:set_float("fuel_totaltime", fuel_totaltime)
216 meta:set_float("fuel_time", fuel_time)
217 meta:set_float("src_time", src_time)
218 meta:set_string("formspec", formspec)
219 meta:set_string("infotext", infotext)
228 minetest.register_node("default:furnace", {
229 description = "Furnace",
231 "default_furnace_top.png", "default_furnace_bottom.png",
232 "default_furnace_side.png", "default_furnace_side.png",
233 "default_furnace_side.png", "default_furnace_front.png"
235 paramtype2 = "facedir",
237 legacy_facedir_simple = true,
238 is_ground_content = false,
239 sounds = default.node_sound_stone_defaults(),
243 on_timer = furnace_node_timer,
245 on_construct = function(pos)
246 local meta = minetest.get_meta(pos)
247 meta:set_string("formspec", inactive_formspec)
248 local inv = meta:get_inventory()
249 inv:set_size('src', 1)
250 inv:set_size('fuel', 1)
251 inv:set_size('dst', 4)
254 on_metadata_inventory_move = function(pos)
255 local timer = minetest.get_node_timer(pos)
258 on_metadata_inventory_put = function(pos)
259 -- start timer function, it will sort out whether furnace can burn or not.
260 local timer = minetest.get_node_timer(pos)
263 on_blast = function(pos)
265 default.get_inventory_drops(pos, "src", drops)
266 default.get_inventory_drops(pos, "fuel", drops)
267 default.get_inventory_drops(pos, "dst", drops)
268 drops[#drops+1] = "default:furnace"
269 minetest.remove_node(pos)
273 allow_metadata_inventory_put = allow_metadata_inventory_put,
274 allow_metadata_inventory_move = allow_metadata_inventory_move,
275 allow_metadata_inventory_take = allow_metadata_inventory_take,
278 minetest.register_node("default:furnace_active", {
279 description = "Furnace",
281 "default_furnace_top.png", "default_furnace_bottom.png",
282 "default_furnace_side.png", "default_furnace_side.png",
283 "default_furnace_side.png",
285 image = "default_furnace_front_active.png",
286 backface_culling = false,
288 type = "vertical_frames",
295 paramtype2 = "facedir",
297 drop = "default:furnace",
298 groups = {cracky=2, not_in_creative_inventory=1},
299 legacy_facedir_simple = true,
300 is_ground_content = false,
301 sounds = default.node_sound_stone_defaults(),
302 on_timer = furnace_node_timer,
306 allow_metadata_inventory_put = allow_metadata_inventory_put,
307 allow_metadata_inventory_move = allow_metadata_inventory_move,
308 allow_metadata_inventory_take = allow_metadata_inventory_take,