From 7b43def5fb508a505afa726512163531dadb67fd Mon Sep 17 00:00:00 2001 From: Vanic Date: Tue, 17 Feb 2026 21:34:56 +0300 Subject: [PATCH] `HelperLib`: add receive fields methods --- mods/SYSTEM/HelperLib/src/Lib.lua | 3 +- .../HelperLib/src/Lib/receiveFields.lua | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 mods/SYSTEM/HelperLib/src/Lib/receiveFields.lua diff --git a/mods/SYSTEM/HelperLib/src/Lib.lua b/mods/SYSTEM/HelperLib/src/Lib.lua index 4cdfbb1..e8050ee 100644 --- a/mods/SYSTEM/HelperLib/src/Lib.lua +++ b/mods/SYSTEM/HelperLib/src/Lib.lua @@ -6,4 +6,5 @@ local require = modInfo.require require('Lib.globalStep') require('Lib.color') require('Lib.immersiveCraftData') -require('Lib.getObjectByGUID') \ No newline at end of file +require('Lib.getObjectByGUID') +require('Lib.receiveFields') \ No newline at end of file diff --git a/mods/SYSTEM/HelperLib/src/Lib/receiveFields.lua b/mods/SYSTEM/HelperLib/src/Lib/receiveFields.lua new file mode 100644 index 0000000..d6ce543 --- /dev/null +++ b/mods/SYSTEM/HelperLib/src/Lib/receiveFields.lua @@ -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