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 "listring[current_name;fuel]"..
26 "listring[current_player;main]"..
27 default.get_hotbar_bg(0, 4.25)
31 local inactive_formspec =
36 "list[current_name;src;2.75,0.5;1,1;]"..
37 "list[current_name;fuel;2.75,2.5;1,1;]"..
38 "image[2.75,1.5;1,1;default_furnace_fire_bg.png]"..
39 "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
40 "list[current_name;dst;4.75,0.96;2,2;]"..
41 "list[current_player;main;0,4.25;8,1;]"..
42 "list[current_player;main;0,5.5;8,3;8]"..
43 "listring[current_name;dst]"..
44 "listring[current_player;main]"..
45 "listring[current_name;src]"..
46 "listring[current_player;main]"..
47 "listring[current_name;fuel]"..
48 "listring[current_player;main]"..
49 default.get_hotbar_bg(0, 4.25)
52 -- Node callback functions that are the same for active and inactive furnace
55 local function can_dig(pos, player)
56 local meta = minetest.get_meta(pos);
57 local inv = meta:get_inventory()
58 return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
61 local function allow_metadata_inventory_put(pos, listname, index, stack, player)
62 if minetest.is_protected(pos, player:get_player_name()) then
65 local meta = minetest.get_meta(pos)
66 local inv = meta:get_inventory()
67 if listname == "fuel" then
68 if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
69 if inv:is_empty("src") then
70 meta:set_string("infotext", "Furnace is empty")
72 return stack:get_count()
76 elseif listname == "src" then
77 return stack:get_count()
78 elseif listname == "dst" then
83 local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
84 local meta = minetest.get_meta(pos)
85 local inv = meta:get_inventory()
86 local stack = inv:get_stack(from_list, from_index)
87 return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
90 local function allow_metadata_inventory_take(pos, listname, index, stack, player)
91 if minetest.is_protected(pos, player:get_player_name()) then
94 return stack:get_count()
97 local function swap_node(pos, name)
98 local node = minetest.get_node(pos)
99 if node.name == name then
103 minetest.swap_node(pos, node)
106 local function furnace_node_timer(pos, elapsed)
108 -- Inizialize metadata
110 local meta = minetest.get_meta(pos)
111 local fuel_time = meta:get_float("fuel_time") or 0
112 local src_time = meta:get_float("src_time") or 0
113 local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
115 local inv = meta:get_inventory()
116 local srclist = inv:get_list("src")
117 local fuellist = inv:get_list("fuel")
123 -- Check if we have cookable content
124 local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
125 local cookable = true
127 if cooked.time == 0 then
131 -- Check if we have enough fuel to burn
132 if fuel_time < fuel_totaltime then
133 -- The furnace is currently active and has enough fuel
134 fuel_time = fuel_time + 1
136 -- If there is a cookable item then check if it is ready yet
138 src_time = src_time + 1
139 if src_time >= cooked.time then
140 -- Place result in dst list if possible
141 if inv:room_for_item("dst", cooked.item) then
142 inv:add_item("dst", cooked.item)
143 inv:set_stack("src", 1, aftercooked.items[1])
149 -- Furnace ran out of fuel
151 -- We need to get new fuel
152 local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
154 if fuel.time == 0 then
155 -- No valid fuel in fuel list
160 -- Take fuel from fuel list
161 inv:set_stack("fuel", 1, afterfuel.items[1])
163 fuel_totaltime = fuel.time
167 -- We don't need to get new fuel since there is no cookable item
175 -- Update formspec, infotext and node
177 local formspec = inactive_formspec
179 local item_percent = 0
181 item_percent = math.floor(src_time / cooked.time * 100)
182 if item_percent > 100 then
183 item_state = "100% (output full)"
185 item_state = item_percent .. "%"
188 if srclist[1]:is_empty() then
191 item_state = "Not cookable"
195 local fuel_state = "Empty"
196 local active = "inactive "
199 if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
201 local fuel_percent = math.floor(fuel_time / fuel_totaltime * 100)
202 fuel_state = fuel_percent .. "%"
203 formspec = active_formspec(fuel_percent, item_percent)
204 swap_node(pos, "default:furnace_active")
205 -- make sure timer restarts automatically
208 if not fuellist[1]:is_empty() then
211 swap_node(pos, "default:furnace")
212 -- stop timer on the inactive furnace
213 local timer = minetest.get_node_timer(pos)
217 local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
222 meta:set_float("fuel_totaltime", fuel_totaltime)
223 meta:set_float("fuel_time", fuel_time)
224 meta:set_float("src_time", src_time)
225 meta:set_string("formspec", formspec)
226 meta:set_string("infotext", infotext)
235 minetest.register_node("default:furnace", {
236 description = "Furnace",
238 "default_furnace_top.png", "default_furnace_bottom.png",
239 "default_furnace_side.png", "default_furnace_side.png",
240 "default_furnace_side.png", "default_furnace_front.png"
242 paramtype2 = "facedir",
244 legacy_facedir_simple = true,
245 is_ground_content = false,
246 sounds = default.node_sound_stone_defaults(),
250 on_timer = furnace_node_timer,
252 on_construct = function(pos)
253 local meta = minetest.get_meta(pos)
254 meta:set_string("formspec", inactive_formspec)
255 local inv = meta:get_inventory()
256 inv:set_size('src', 1)
257 inv:set_size('fuel', 1)
258 inv:set_size('dst', 4)
261 on_metadata_inventory_move = function(pos)
262 local timer = minetest.get_node_timer(pos)
265 on_metadata_inventory_put = function(pos)
266 -- start timer function, it will sort out whether furnace can burn or not.
267 local timer = minetest.get_node_timer(pos)
270 on_blast = function(pos)
272 default.get_inventory_drops(pos, "src", drops)
273 default.get_inventory_drops(pos, "fuel", drops)
274 default.get_inventory_drops(pos, "dst", drops)
275 drops[#drops+1] = "default:furnace"
276 minetest.remove_node(pos)
280 allow_metadata_inventory_put = allow_metadata_inventory_put,
281 allow_metadata_inventory_move = allow_metadata_inventory_move,
282 allow_metadata_inventory_take = allow_metadata_inventory_take,
285 minetest.register_node("default:furnace_active", {
286 description = "Furnace",
288 "default_furnace_top.png", "default_furnace_bottom.png",
289 "default_furnace_side.png", "default_furnace_side.png",
290 "default_furnace_side.png",
292 image = "default_furnace_front_active.png",
293 backface_culling = false,
295 type = "vertical_frames",
302 paramtype2 = "facedir",
304 drop = "default:furnace",
305 groups = {cracky=2, not_in_creative_inventory=1},
306 legacy_facedir_simple = true,
307 is_ground_content = false,
308 sounds = default.node_sound_stone_defaults(),
309 on_timer = furnace_node_timer,
313 allow_metadata_inventory_put = allow_metadata_inventory_put,
314 allow_metadata_inventory_move = allow_metadata_inventory_move,
315 allow_metadata_inventory_take = allow_metadata_inventory_take,