From b1a86c1a707e25273f2e120d9d24e6e97da2b85e Mon Sep 17 00:00:00 2001 From: alek13 Date: Tue, 19 Nov 2024 04:08:39 +0300 Subject: [PATCH] Artisan Benches: Fuel Device: extract `Processor` from `grinder` mod. Relates to #1787 --- mods/lord/Blocks/grinder/locale/grinder.en.tr | 3 - mods/lord/Blocks/grinder/locale/grinder.ru.tr | 2 - mods/lord/Blocks/grinder/locale/template.txt | 2 - mods/lord/Blocks/grinder/src/grinder.lua | 10 +- .../Blocks/grinder/src/grinder/Processor_.lua | 17 ++++ .../src/grinder/definition/node/active.lua | 4 +- .../definition/node/inventory_callbacks.lua | 8 +- .../Core/fuel_device/locale/fuel_device.en.tr | 3 + .../Core/fuel_device/locale/fuel_device.ru.tr | 3 + .../lord/Core/fuel_device/src/fuel_device.lua | 2 +- .../src/fuel_device}/Processor.lua | 91 +++++++++++++------ .../Core/fuel_device/src/fuel_device/api.lua | 6 +- 12 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 mods/lord/Blocks/grinder/src/grinder/Processor_.lua rename mods/lord/{Blocks/grinder/src/grinder => Core/fuel_device/src/fuel_device}/Processor.lua (56%) diff --git a/mods/lord/Blocks/grinder/locale/grinder.en.tr b/mods/lord/Blocks/grinder/locale/grinder.en.tr index c02f296de..89e00ca12 100644 --- a/mods/lord/Blocks/grinder/locale/grinder.en.tr +++ b/mods/lord/Blocks/grinder/locale/grinder.en.tr @@ -5,6 +5,3 @@ Roll=Roll Coal dust=Coal dust Grinder is empty=Grinder is empty -Active=Active -Out Of Heat=Out Of Heat - diff --git a/mods/lord/Blocks/grinder/locale/grinder.ru.tr b/mods/lord/Blocks/grinder/locale/grinder.ru.tr index 2f102da27..ed534db3c 100644 --- a/mods/lord/Blocks/grinder/locale/grinder.ru.tr +++ b/mods/lord/Blocks/grinder/locale/grinder.ru.tr @@ -5,5 +5,3 @@ Roll=Барабан Coal dust=Угольная пыль Grinder is empty=В дробилке пусто -Active=работает -Out Of Fuel=закончилось топливо diff --git a/mods/lord/Blocks/grinder/locale/template.txt b/mods/lord/Blocks/grinder/locale/template.txt index 6a208f68b..7e4d13a0a 100644 --- a/mods/lord/Blocks/grinder/locale/template.txt +++ b/mods/lord/Blocks/grinder/locale/template.txt @@ -5,5 +5,3 @@ Roll= Coal dust= Grinder is empty= -Active= -Out Of Heat= diff --git a/mods/lord/Blocks/grinder/src/grinder.lua b/mods/lord/Blocks/grinder/src/grinder.lua index 77e22b5b2..e48bc9188 100644 --- a/mods/lord/Blocks/grinder/src/grinder.lua +++ b/mods/lord/Blocks/grinder/src/grinder.lua @@ -1,6 +1,7 @@ -local craft = require('grinder.definition.craft') -local node = require('grinder.definition.node') -local recipes = require('grinder.definition.recipes') +minetest.CraftMethod.GRINDER = 'grinder' +local craft = require('grinder.definition.craft') +local node = require('grinder.definition.node') +local recipes = require('grinder.definition.recipes') local function register_craft() @@ -15,7 +16,7 @@ end local function register_recipes() for _, data in pairs(recipes) do minetest.register_craft({ - method = 'grinder', + method = minetest.CraftMethod.GRINDER, type = 'cooking', input = data[1], output = data[2], @@ -32,7 +33,6 @@ end return { init = function() - minetest.CraftMethod.GRINDER = 'grinder' minetest.register_craft_method(minetest.CraftMethod.GRINDER) register_craft() register_recipes() diff --git a/mods/lord/Blocks/grinder/src/grinder/Processor_.lua b/mods/lord/Blocks/grinder/src/grinder/Processor_.lua new file mode 100644 index 000000000..44a2d084d --- /dev/null +++ b/mods/lord/Blocks/grinder/src/grinder/Processor_.lua @@ -0,0 +1,17 @@ +local Grinder = require('grinder.Grinder_') + + +--- +--- @class Processor: fuel_device.Processor +--- +local Processor = fuel_device.Processor:extended({ + --- @static + --- @type fuel_device.Device + DeviceClass = Grinder, + --- @static + --- @type string + craft_method = minetest.CraftMethod.GRINDER, +}) + + +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 8c4f896e8..cae174849 100644 --- a/mods/lord/Blocks/grinder/src/grinder/definition/node/active.lua +++ b/mods/lord/Blocks/grinder/src/grinder/definition/node/active.lua @@ -2,7 +2,7 @@ local S = 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') +local Processor = require('grinder.Processor_') --- @param width number Width of a frame in pixels. @@ -33,5 +33,5 @@ return table.merge(common, table.merge(inventory_callbacks, { }, light_source = 8, groups = { not_in_creative_inventory = 1, hot = 1 }, - on_timer = Processor.on_timer, + on_timer = Processor.get_on_timer_function(Processor), })) 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 08b984fa5..9940b4f4b 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,6 +1,6 @@ local S = minetest.get_mod_translator() -local Processor = require('grinder.Processor') +local Processor = require('grinder.Processor_') return { @@ -53,7 +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, + on_metadata_inventory_move = Processor.get_start_or_stop_function(Processor), + on_metadata_inventory_put = Processor.get_start_or_stop_function(Processor), + on_metadata_inventory_take = Processor.get_start_or_stop_function(Processor), } diff --git a/mods/lord/Core/fuel_device/locale/fuel_device.en.tr b/mods/lord/Core/fuel_device/locale/fuel_device.en.tr index 1a4e745b6..16d0f2ee7 100644 --- a/mods/lord/Core/fuel_device/locale/fuel_device.en.tr +++ b/mods/lord/Core/fuel_device/locale/fuel_device.en.tr @@ -1 +1,4 @@ # textdomain: fuel_device + +Active=Active +Out Of Heat=Out Of Heat diff --git a/mods/lord/Core/fuel_device/locale/fuel_device.ru.tr b/mods/lord/Core/fuel_device/locale/fuel_device.ru.tr index 1a4e745b6..52e62870d 100644 --- a/mods/lord/Core/fuel_device/locale/fuel_device.ru.tr +++ b/mods/lord/Core/fuel_device/locale/fuel_device.ru.tr @@ -1 +1,4 @@ # textdomain: fuel_device + +Active=работает +Out Of Fuel=закончилось топливо diff --git a/mods/lord/Core/fuel_device/src/fuel_device.lua b/mods/lord/Core/fuel_device/src/fuel_device.lua index 8e2ad2325..491a93117 100644 --- a/mods/lord/Core/fuel_device/src/fuel_device.lua +++ b/mods/lord/Core/fuel_device/src/fuel_device.lua @@ -1,4 +1,4 @@ -local api = require("fuel_device.api") +local api = require("fuel_device.api") fuel_device = {} -- luacheck: ignore unused global variable fuel_device diff --git a/mods/lord/Blocks/grinder/src/grinder/Processor.lua b/mods/lord/Core/fuel_device/src/fuel_device/Processor.lua similarity index 56% rename from mods/lord/Blocks/grinder/src/grinder/Processor.lua rename to mods/lord/Core/fuel_device/src/fuel_device/Processor.lua index 04d57f273..f49a4834a 100644 --- a/mods/lord/Blocks/grinder/src/grinder/Processor.lua +++ b/mods/lord/Core/fuel_device/src/fuel_device/Processor.lua @@ -1,21 +1,19 @@ local math_floor = math.floor ---- @type Grinder -local Grinder = require('grinder.Grinder_') - local S = minetest.get_mod_translator() ---- - Returns whether grinding possible & +--- - Returns whether processing 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 meta NodeMetaRef +--- @param inv InvRef +--- @param meta NodeMetaRef +--- @param method string --- @return boolean, RecipeOutput|nil, RecipeInput|nil, RecipeOutput|nil, RecipeInput|nil -local function grinding_possible(inv, meta) +local function process_possible(inv, meta, method) local result_source, remaining_source = minetest.get_craft_result({ - method = 'grinder', + method = method, type = 'cooking', width = 1, items = inv:get_list('src'), @@ -89,55 +87,92 @@ end --- ---- @class Processor +--- @class fuel_device.Processor --- -local Processor = {} +local Processor = { + --- @static + --- @type fuel_device.Device + DeviceClass = nil, + --- @static + --- @type string + craft_method = nil, +} + +--- @public +--- @generic GenericProcessor: fuel_device.Processor +--- @param child_class GenericProcessor +--- @return GenericProcessor +function Processor:extended(child_class) + return setmetatable(child_class or {}, { __index = self }) +end -- ----------------------------------------------------------------------------------------------- -- Public functions: --- @static +--- @overload fun():fun(position:Position):void +--- @generic GenericProcessor: fuel_device.Processor +--- @param self GenericProcessor just specify your own extended Processor class, or `fuel_device.Processor` well be used +function Processor.get_start_or_stop_function(self) + self = self or Processor + return function(position) + self:start_or_stop(position) + end +end + +--- @static +--- @overload fun():fun(position:Position,elapsed:number):void +--- @generic GenericProcessor: fuel_device.Processor +--- @param self GenericProcessor just specify your own extended Processor class, or `fuel_device.Processor` well be used +function Processor.get_on_timer_function(self) + self = self or Processor + return function(position, elapsed) + self:on_timer(position, elapsed) + end +end + --- @param position Position -function Processor.start_or_stop(position) - local grinder = Grinder:new(position) - local meta = grinder:get_meta() - local inv = meta:get_inventory() +function Processor:start_or_stop(position) + local device = self.DeviceClass:new(position) + local meta = device:get_meta() + local inv = meta:get_inventory() - local possible = grinding_possible(inv, meta) + local possible = process_possible(inv, meta, self.craft_method) if possible then - grinder:activate(S('Active')) + device:activate(S('Active')) else - grinder:deactivate(S('Out Of Fuel')) + device:deactivate(S('Out Of Fuel')) end end --- @static --- @param position Position -function Processor.act(position) - local grinder = Grinder:new(position) - local meta = grinder:get_meta() - local inv = meta:get_inventory() +function Processor:act(position) + local device = self.DeviceClass:new(position) + local meta = device:get_meta() + local inv = meta:get_inventory() - local possible, result_source, remaining_source, result_fuel, remaining_fuel = grinding_possible(inv, meta) + local possible, result_source, remaining_source, result_fuel, remaining_fuel + = process_possible(inv, meta, self.craft_method) if possible then - grinder:activate(S('Active')) + device:activate(S('Active')) burn_fuel(meta, remaining_fuel, result_fuel) grind_source(meta, remaining_source, result_source) else - grinder:deactivate(S('Out Of Fuel')) + device:deactivate(S('Out Of Fuel')) 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) +function Processor:on_timer(position, elapsed) + for i = 1, math_floor(elapsed / self.DeviceClass.TIMER_TICK) do + self:act(position) end - minetest.get_node_timer(position):set(Grinder.TIMER_TICK, elapsed % Grinder.TIMER_TICK) + minetest.get_node_timer(position):set(self.DeviceClass.TIMER_TICK, elapsed % self.DeviceClass.TIMER_TICK) end diff --git a/mods/lord/Core/fuel_device/src/fuel_device/api.lua b/mods/lord/Core/fuel_device/src/fuel_device/api.lua index cfeb58014..06b90db43 100644 --- a/mods/lord/Core/fuel_device/src/fuel_device/api.lua +++ b/mods/lord/Core/fuel_device/src/fuel_device/api.lua @@ -1,6 +1,8 @@ -local Device = require("fuel_device.Device") +local Device = require("fuel_device.Device") +local Processor = require("fuel_device.Processor") return { - Device = Device, + Device = Device, + Processor = Processor, }