CraftSystem: first working version #31

Merged
varttist merged 27 commits from craft-system into main 2026-02-18 19:30:39 +00:00
2 changed files with 69 additions and 1 deletions
Showing only changes of commit 7b43def5fb - Show all commits

View File

@@ -6,4 +6,5 @@ local require = modInfo.require
require('Lib.globalStep')
require('Lib.color')
require('Lib.immersiveCraftData')
require('Lib.getObjectByGUID')
require('Lib.getObjectByGUID')
require('Lib.receiveFields')

View File

@@ -0,0 +1,67 @@
---Register a callback for a specific player action in FormSpec.
---
---The callback is guaranteed to be called only for the specified form and the specified field.
---@param formName string the name of the formspec for which the callback will be called.
---@param fieldName string the name of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called.
--- * `fieldValue` - the field value that is passed in the `fields` table.
function Smc.registerOnFormFieldAction(formName, fieldName, callback)
core.register_on_player_receive_fields(function(player, formname, fields)
-- If the form name does not match the given...
if formName ~= formname then
-- ...we do nothing.
return
end
local fieldValue = fields[fieldName]
-- If the field value is not nil (the player called this field)...
if fieldValue ~= nil then
-- ...calling a callback.
callback(player, fieldName, fieldValue)
end
end)
end
---Register a callback function to close the FormSpec.
---
---The callback is guaranteed to be called only for the specified form.
---@param formName string the name of the form that is being closed.
---@param callback fun(player:Player) Callback to handle the close. Parameters:
--- * `player` - the player who closed the form.
function Smc.registerOnFormQuit(formName, callback)
Smc.registerOnFormFieldAction(formName, 'quit', callback)
end
---Register a callback for a specific player action in FormSpec.
---
---The callback function is guaranteed to be called only for the specified form and
---for the field whose name matches the pattern.
---@param formName string the name of the formspec for which the callback will be called.
---@param fieldNamePattern string the name pattern of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called (will match the pattern).
--- * `fieldValue` - the field value that is passed in the `fields` table.
function Smc.registerOnFormFieldActionMatch(formName, fieldNamePattern, callback)
core.register_on_player_receive_fields(function(player, formname, fields)
-- If the form name does not match the given...
if formName ~= formname then
-- ...we do nothing.
return
end
-- We go through each field in the list of fields.
for fieldName, fieldValue in pairs(fields) do
-- We check the field name pattern compliance.
local capturedName = string.match(fieldName, fieldNamePattern)
-- If the captured name is not nil...
if capturedName ~= nil then
-- ...calling a callback.
callback(player, fieldName, fieldValue)
end
end
end)
end