Skip to content

Commit

Permalink
Artisan Benches: Grinder: migrate to node timer. Closes #1063
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed Nov 15, 2024
1 parent bca6f26 commit b4ff458
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
8 changes: 0 additions & 8 deletions mods/lord/Blocks/grinder/src/grinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ local craft = require('grinder.definition.craft')
local node = require('grinder.definition.node')
local recipes = require('grinder.definition.recipes')

local Processor = require('grinder.Processor')


local function register_craft()
for _, craftRecipe in pairs(craft.recipes) do
Expand All @@ -29,12 +27,6 @@ end
local function register_nodes()
minetest.register_node("grinder:grinder", node.inactive)
minetest.register_node("grinder:grinder_active", node.active)
minetest.register_abm({
nodenames = { "grinder:grinder", "grinder:grinder_active" },
interval = 1,
chance = 1,
action = Processor.act,
})
end


Expand Down
10 changes: 8 additions & 2 deletions mods/lord/Blocks/grinder/src/grinder/Grinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ local machine_name = "Grinder"
--- @class Grinder
---
local Grinder = {
--- @static
--- @type number
TIMER_TICK = 1,
--- @type table<number,number,number>
position = nil,
position = nil,
--- @type NodeMetaRef
meta = nil
meta = nil,
}

--- Constructor
Expand Down Expand Up @@ -96,16 +99,19 @@ function Grinder:activate(hint_en)
swap_node(self.position, "grinder:grinder_active")
self:get_meta():set_string("infotext", SL((hint_en):format(machine_name)) .. " (" .. percent .. "%)")
self:get_meta():set_string("formspec", form.get('active', percent, item_percent))
minetest.get_node_timer(self.position):start(self.TIMER_TICK)
end

--- Sets Node into inactive grinder with new hint.
--- @public
--- @param hint_en string A template for hinting in English. Use "%s" for machine name placeholder.
function Grinder:deactivate(hint_en)
minetest.get_node_timer(self.position):stop()
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'))
end


return Grinder
26 changes: 26 additions & 0 deletions mods/lord/Blocks/grinder/src/grinder/Processor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ local Processor = {}
-- -----------------------------------------------------------------------------------------------
-- Public functions:

--- @static
--- @param position Position
function Processor.start_or_stop(position)
local grinder = Grinder:new(position)
local meta = grinder:get_meta()
local inv = meta:get_inventory()

local possible = grinding_possible(inv, meta)
if possible then
grinder:activate("%s Grinding")
else
grinder:deactivate("%s Out Of Heat")
end
end

--- @static
--- @param position Position
function Processor.act(position)
Expand All @@ -109,5 +124,16 @@ function Processor.act(position)
end
end

--- @static
--- @param position Position
--- @param elapsed number
function Processor.on_timer(position, elapsed)
for i = 1, math.floor(elapsed/Grinder.TIMER_TICK) do
Processor.act(position)
end

minetest.get_node_timer(position):set(Grinder.TIMER_TICK, elapsed % Grinder.TIMER_TICK)
end


return Processor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local SL = minetest.get_mod_translator()

local common = require('grinder.definition.node.common')
local inventory_callbacks = require('grinder.definition.node.inventory_callbacks')
local Processor = require('grinder.Processor')


--- @param width number Width of a frame in pixels.
Expand All @@ -21,8 +22,8 @@ end


return table.merge(common, table.merge(inventory_callbacks, {
description = SL("Grinder"),
tiles = {
description = SL("Grinder"),
tiles = {
animated_tile("grinder_top_active.png", 32, 32, 1.6),
"grinder_bottom.png",
animated_tile("grinder_side_left_active.png", 32, 32, 3.2),
Expand All @@ -31,5 +32,6 @@ return table.merge(common, table.merge(inventory_callbacks, {
animated_tile("grinder_front_active.png", 32, 32, 1.0)
},
light_source = 8,
groups = { not_in_creative_inventory = 1, hot = 1 },
groups = { not_in_creative_inventory = 1, hot = 1 },
on_timer = Processor.on_timer,
}))
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local SL = minetest.get_mod_translator()

local Processor = require('grinder.Processor')


return {
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
Expand Down Expand Up @@ -51,4 +53,7 @@ return {
end
return stack:get_count()
end,
on_metadata_inventory_move = Processor.start_or_stop,
on_metadata_inventory_put = Processor.start_or_stop,
on_metadata_inventory_take = Processor.start_or_stop,
}

0 comments on commit b4ff458

Please sign in to comment.