Skip to content

Commit

Permalink
Artisan Benches: Fuel Device: extract Processor from grinder mod.…
Browse files Browse the repository at this point in the history
… Relates to #1787
  • Loading branch information
alek13 committed Nov 19, 2024
1 parent abacf01 commit b1a86c1
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 49 deletions.
3 changes: 0 additions & 3 deletions mods/lord/Blocks/grinder/locale/grinder.en.tr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ Roll=Roll
Coal dust=Coal dust

Grinder is empty=Grinder is empty
Active=Active
Out Of Heat=Out Of Heat

2 changes: 0 additions & 2 deletions mods/lord/Blocks/grinder/locale/grinder.ru.tr
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ Roll=Барабан
Coal dust=Угольная пыль

Grinder is empty=В дробилке пусто
Active=работает
Out Of Fuel=закончилось топливо
2 changes: 0 additions & 2 deletions mods/lord/Blocks/grinder/locale/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ Roll=
Coal dust=

Grinder is empty=
Active=
Out Of Heat=
10 changes: 5 additions & 5 deletions mods/lord/Blocks/grinder/src/grinder.lua
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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],
Expand All @@ -32,7 +33,6 @@ end

return {
init = function()
minetest.CraftMethod.GRINDER = 'grinder'
minetest.register_craft_method(minetest.CraftMethod.GRINDER)
register_craft()
register_recipes()
Expand Down
17 changes: 17 additions & 0 deletions mods/lord/Blocks/grinder/src/grinder/Processor_.lua
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
}))
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local S = minetest.get_mod_translator()

local Processor = require('grinder.Processor')
local Processor = require('grinder.Processor_')


return {
Expand Down Expand Up @@ -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),
}
3 changes: 3 additions & 0 deletions mods/lord/Core/fuel_device/locale/fuel_device.en.tr
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# textdomain: fuel_device

Active=Active
Out Of Heat=Out Of Heat
3 changes: 3 additions & 0 deletions mods/lord/Core/fuel_device/locale/fuel_device.ru.tr
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# textdomain: fuel_device

Active=работает
Out Of Fuel=закончилось топливо
2 changes: 1 addition & 1 deletion mods/lord/Core/fuel_device/src/fuel_device.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -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


Expand Down
6 changes: 4 additions & 2 deletions mods/lord/Core/fuel_device/src/fuel_device/api.lua
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit b1a86c1

Please sign in to comment.