CraftSystem: first working version
#31
@@ -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')
|
||||
67
mods/SYSTEM/HelperLib/src/Lib/receiveFields.lua
Normal file
67
mods/SYSTEM/HelperLib/src/Lib/receiveFields.lua
Normal 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
|
||||
Reference in New Issue
Block a user