From b4ff4587ada6edeb7bbf4da42054d26df835341c Mon Sep 17 00:00:00 2001 From: alek13 Date: Fri, 15 Nov 2024 18:16:45 +0300 Subject: [PATCH] Artisan Benches: Grinder: migrate to node timer. Closes #1063 --- mods/lord/Blocks/grinder/src/grinder.lua | 8 ------ .../Blocks/grinder/src/grinder/Grinder.lua | 10 +++++-- .../Blocks/grinder/src/grinder/Processor.lua | 26 +++++++++++++++++++ .../src/grinder/definition/node/active.lua | 8 +++--- .../definition/node/inventory_callbacks.lua | 5 ++++ 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/mods/lord/Blocks/grinder/src/grinder.lua b/mods/lord/Blocks/grinder/src/grinder.lua index e82eb4aff..7a4147788 100644 --- a/mods/lord/Blocks/grinder/src/grinder.lua +++ b/mods/lord/Blocks/grinder/src/grinder.lua @@ -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 @@ -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 diff --git a/mods/lord/Blocks/grinder/src/grinder/Grinder.lua b/mods/lord/Blocks/grinder/src/grinder/Grinder.lua index be39debd4..8015e4d51 100644 --- a/mods/lord/Blocks/grinder/src/grinder/Grinder.lua +++ b/mods/lord/Blocks/grinder/src/grinder/Grinder.lua @@ -8,10 +8,13 @@ local machine_name = "Grinder" --- @class Grinder --- local Grinder = { + --- @static + --- @type number + TIMER_TICK = 1, --- @type table - position = nil, + position = nil, --- @type NodeMetaRef - meta = nil + meta = nil, } --- Constructor @@ -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 diff --git a/mods/lord/Blocks/grinder/src/grinder/Processor.lua b/mods/lord/Blocks/grinder/src/grinder/Processor.lua index 554a0005c..c396f7aa4 100644 --- a/mods/lord/Blocks/grinder/src/grinder/Processor.lua +++ b/mods/lord/Blocks/grinder/src/grinder/Processor.lua @@ -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) @@ -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 diff --git a/mods/lord/Blocks/grinder/src/grinder/definition/node/active.lua b/mods/lord/Blocks/grinder/src/grinder/definition/node/active.lua index b08495b7b..bee51d5b6 100644 --- a/mods/lord/Blocks/grinder/src/grinder/definition/node/active.lua +++ b/mods/lord/Blocks/grinder/src/grinder/definition/node/active.lua @@ -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. @@ -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), @@ -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, })) diff --git a/mods/lord/Blocks/grinder/src/grinder/definition/node/inventory_callbacks.lua b/mods/lord/Blocks/grinder/src/grinder/definition/node/inventory_callbacks.lua index a7de99138..659382bdd 100644 --- a/mods/lord/Blocks/grinder/src/grinder/definition/node/inventory_callbacks.lua +++ b/mods/lord/Blocks/grinder/src/grinder/definition/node/inventory_callbacks.lua @@ -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) @@ -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, }