Skip to content

Commit

Permalink
Artisan Benches: Grinder: fix grinding possibility check, reset meta-…
Browse files Browse the repository at this point in the history
…counters on deactivate. Relates to #1784
  • Loading branch information
alek13 committed Nov 14, 2024
1 parent 70fe578 commit f2a4e8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 15 additions & 2 deletions mods/lord/Blocks/grinder/src/grinder/Grinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,27 @@ local function get_initiated_meta(pos)
"fuel_totaltime",
"fuel_time",
"src_totaltime",
"src_time" }) do
-- init with 0.0 if var not set
"src_time"
}) do
if not meta:get_float(name) then
meta:set_float(name, 0.0)
end
end
return meta
end

--- @param meta NodeMetaRef
local function reset_meta_vars(meta)
for _, name in pairs({
"fuel_totaltime",
"fuel_time",
"src_totaltime",
"src_time"
}) do
meta:set_float(name, 0.0)
end
end

--- Swaps node if node is not same and return old node name.
--- @see minetest.swap_node (https://dev.minetest.net/minetest.swap_node)
---
Expand Down Expand Up @@ -90,6 +102,7 @@ end
--- @public
--- @param hint_en string A template for hinting in English. Use "%s" for machine name placeholder.
function Grinder:deactivate(hint_en)
reset_meta_vars(self:get_meta())
swap_node(self.position, "grinder:grinder")
self:get_meta():set_string("infotext", SL((hint_en):format(machine_name)))
self:get_meta():set_string("formspec", form.get('inactive'))
Expand Down
17 changes: 10 additions & 7 deletions mods/lord/Blocks/grinder/src/grinder/Processor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ local Grinder = require("grinder.Grinder")
--- - Returns whether grinding possible &
--- - if possible, also returns (RecipeOutput - result, RecipeInput - remaining) for source & fuel.
--- - So, returns `possible, result_source, remaining_source, result_fuel, remaining_fuel`
--- @param inv InvRef
--- @param inv InvRef
--- @param meta NodeMetaRef
--- @return boolean, RecipeOutput|nil, RecipeInput|nil, RecipeOutput|nil, RecipeInput|nil
local function grinding_possible(inv)
local function grinding_possible(inv, meta)
local result_source, remaining_source = minetest.get_craft_result({
method = 'grinder',
type = 'cooking',
Expand All @@ -24,7 +25,10 @@ local function grinding_possible(inv)
items = inv:get_list("fuel")
})

local possible = result_source.time > 0 and result_fuel.time > 0 and inv:room_for_item("dst", result_source.item)
local possible =
result_source.time > 0 and
(result_fuel.time > 0 or meta:get_int("fuel_time") > 0) and
inv:room_for_item("dst", result_source.item)
if not possible then
return false, nil, nil, nil, nil
end
Expand All @@ -39,11 +43,10 @@ local function burn_fuel(meta, remaining_fuel, result_fuel)
local fuel_time = meta:get_int("fuel_time")
local fuel_totaltime = meta:get_int("fuel_totaltime")

if fuel_totaltime ~= result_fuel.time then
meta:set_int("fuel_totaltime", result_fuel.time)
end
if fuel_time == 0 then
meta:get_inventory():set_list("fuel", remaining_fuel.items)
meta:set_int("fuel_totaltime", result_fuel.time)
fuel_totaltime = result_fuel.time
end

fuel_time = fuel_time + 1
Expand Down Expand Up @@ -96,7 +99,7 @@ function Processor.act(pos)
local meta = g:get_meta()
local inv = meta:get_inventory()

local possible, result_source, remaining_source, result_fuel, remaining_fuel = grinding_possible(inv)
local possible, result_source, remaining_source, result_fuel, remaining_fuel = grinding_possible(inv, meta)
if possible then
g:activate("%s Grinding")

Expand Down

0 comments on commit f2a4e8f

Please sign in to comment.