From 7773e25a81cc40a0a8a6d5f46d98c25872e3b33a Mon Sep 17 00:00:00 2001 From: Vanic Date: Mon, 29 Dec 2025 18:51:22 +0300 Subject: [PATCH] Callbacks -> Events. Fihish Control --- .emmyrc.json | 2 +- mods/API/ApiLoader/src/Api/GlobalStep.lua | 4 +- mods/API/Callbacks/init.lua | 33 --------- mods/API/Callbacks/mod.conf | 2 - mods/API/Events/init.lua | 33 +++++++++ mods/API/Events/mod.conf | 2 + .../{CallbacksControl => Control}/init.lua | 0 .../{CallbacksControl => Control}/mod.conf | 0 .../src/Control.lua | 73 ++++++++++++++----- 9 files changed, 94 insertions(+), 55 deletions(-) delete mode 100644 mods/API/Callbacks/init.lua delete mode 100644 mods/API/Callbacks/mod.conf create mode 100644 mods/API/Events/init.lua create mode 100644 mods/API/Events/mod.conf rename mods/CORE/{CallbacksControl => Control}/init.lua (100%) rename mods/CORE/{CallbacksControl => Control}/mod.conf (100%) rename mods/CORE/{CallbacksControl => Control}/src/Control.lua (51%) diff --git a/.emmyrc.json b/.emmyrc.json index 1b4debb..6e540e8 100644 --- a/.emmyrc.json +++ b/.emmyrc.json @@ -25,7 +25,7 @@ "globals": [ "core", "table", - "Callbacks", + "Events", "VoxelArea", "VoxelManip", "vector", diff --git a/mods/API/ApiLoader/src/Api/GlobalStep.lua b/mods/API/ApiLoader/src/Api/GlobalStep.lua index c5ab45b..7ecb4f8 100644 --- a/mods/API/ApiLoader/src/Api/GlobalStep.lua +++ b/mods/API/ApiLoader/src/Api/GlobalStep.lua @@ -21,7 +21,7 @@ core.register_globalstep(function(deltaTime) if timer >= callback.timeDelay then callback.callbackFunc(deltaTime) - callback.timer = 0.0 + callback.timer = timer - callback.timeDelay else callback.timer = timer end @@ -51,7 +51,7 @@ core.register_globalstep(function(deltaTime) if timer >= callback.timeDelay then callback.callbackFunc(deltaTime, player) - callback.timer = 0.0 + callback.timer = timer - callback.timeDelay else callback.timer = timer end diff --git a/mods/API/Callbacks/init.lua b/mods/API/Callbacks/init.lua deleted file mode 100644 index a9f35a6..0000000 --- a/mods/API/Callbacks/init.lua +++ /dev/null @@ -1,33 +0,0 @@ -Callbacks = {} - ----@alias Callbacks.callbackFunc fun(callbackName, ...) - ----@type table -local registeredCallbacks = {} - ----@param callbackName string -function Callbacks.registerCallback(callbackName) - assert(registeredCallbacks[callbackName] == nil, ('Callback %s is already registered.'):format(callbackName)) - - registeredCallbacks[callbackName] = {} -end - ----@param callbackName string ----@param callbackFunc Callbacks.callbackFunc -function Callbacks.subscribe(callbackName, callbackFunc) - local callbacks = registeredCallbacks[callbackName] - assert(callbacks ~= nil, ('Callback %s does not exist'):format(callbackName)) - - table.insert(callbacks, callbackFunc) -end - ----@param callbackName string ----@param ... any -function Callbacks.call(callbackName, ...) - local callbacks = registeredCallbacks[callbackName] - assert(callbacks ~= nil, ('Callback %s does not exist'):format(callbackName)) - - for _, func in ipairs(callbacks) do - func(callbackName, ...) - end -end \ No newline at end of file diff --git a/mods/API/Callbacks/mod.conf b/mods/API/Callbacks/mod.conf deleted file mode 100644 index c491b0b..0000000 --- a/mods/API/Callbacks/mod.conf +++ /dev/null @@ -1,2 +0,0 @@ -name = smc__api__callbacks -depends = smc__api__table diff --git a/mods/API/Events/init.lua b/mods/API/Events/init.lua new file mode 100644 index 0000000..42bea3f --- /dev/null +++ b/mods/API/Events/init.lua @@ -0,0 +1,33 @@ +Events = {} + +---@alias Events.callback fun(eventName, ...) + +---@type table +local registeredEvents = {} + +---@param eventName string +function Events.registerEvent(eventName) + assert(registeredEvents[eventName] == nil, ('Event %s is already registered.'):format(eventName)) + + registeredEvents[eventName] = {} +end + +---@param eventName string +---@param callback Events.callback +function Events.subscribe(eventName, callback) + local callbacks = registeredEvents[eventName] + assert(callbacks ~= nil, ('Event %s does not exist'):format(eventName)) + + table.insert(callbacks, callback) +end + +---@param eventName string +---@param ... any +function Events.call(eventName, ...) + local callbacks = registeredEvents[eventName] + assert(callbacks ~= nil, ('Event %s does not exist'):format(eventName)) + + for _, func in ipairs(callbacks) do + func(eventName, ...) + end +end \ No newline at end of file diff --git a/mods/API/Events/mod.conf b/mods/API/Events/mod.conf new file mode 100644 index 0000000..ed9793f --- /dev/null +++ b/mods/API/Events/mod.conf @@ -0,0 +1,2 @@ +name = smc__api__events +depends = smc__api__table diff --git a/mods/CORE/CallbacksControl/init.lua b/mods/CORE/Control/init.lua similarity index 100% rename from mods/CORE/CallbacksControl/init.lua rename to mods/CORE/Control/init.lua diff --git a/mods/CORE/CallbacksControl/mod.conf b/mods/CORE/Control/mod.conf similarity index 100% rename from mods/CORE/CallbacksControl/mod.conf rename to mods/CORE/Control/mod.conf diff --git a/mods/CORE/CallbacksControl/src/Control.lua b/mods/CORE/Control/src/Control.lua similarity index 51% rename from mods/CORE/CallbacksControl/src/Control.lua rename to mods/CORE/Control/src/Control.lua index 3e915a9..2872f9b 100644 --- a/mods/CORE/CallbacksControl/src/Control.lua +++ b/mods/CORE/Control/src/Control.lua @@ -1,49 +1,84 @@ ----@class Callbacks.Control +---@class Events.Control local Control = {} -- control.on_press -Callbacks.registerCallback('control.on_press') +Events.registerEvent('control.on_press') ---@param callbackFunc fun(player:Player, controlName:string) function Control.subcribeOnPress(callbackFunc) - Callbacks.subscribe('control.on_press', callbackFunc) + Events.subscribe('control.on_press', callbackFunc) end -- control.on_release -Callbacks.registerCallback('control.on_release') +Events.registerEvent('control.on_release') ---@param callbackFunc fun(player:Player, controlName:string, holdTime:number) function Control.subcribeOnRelease(callbackFunc) - Callbacks.subscribe('control.on_release', callbackFunc) + Events.subscribe('control.on_release', callbackFunc) end -- control.on_hold -Callbacks.registerCallback('control.on_hold') +Events.registerEvent('control.on_hold') ---@param callbackFunc fun(player:Player, controlName:string, holdTime:number, dtime:number) function Control.subcribeOnHold(callbackFunc) - Callbacks.subscribe('control.on_hold', callbackFunc) + Events.subscribe('control.on_hold', callbackFunc) end -- control.on_wield_change -Callbacks.registerCallback('control.on_wield_change') +Events.registerEvent('control.on_wield_change') ---@param callbackFunc fun(player:Player, player_wield_index:number, player_last_wield_index:number) function Control.subcribeOnWieldChange(callbackFunc) - Callbacks.subscribe('control.on_wield_change', callbackFunc) + Events.subscribe('control.on_wield_change', callbackFunc) end -local ControlKeys = { + +-- --- Call events --- + +local ControlPressKeys = { 'up', 'down', 'left', 'right', 'jump', 'aux1', 'sneak', 'dig', 'place', 'zoom' } +local ControlMovementKeys = { + 'movement_x', 'movement_y' +} + local playersLastControl = {} local playersControlTimer = {} ---TODO: on_join & on_leave +core.register_on_joinplayer(function(player, last_login) + local playerName = player:get_player_name() + if playerName == nil then + return + end + + local nowControl = player:get_player_control() + if nowControl == nil then + return + end + + local controlTimer = {} + for _, key in ipairs(ControlPressKeys) do + controlTimer[key] = 0.0 + end + + playersLastControl[playerName] = nowControl + playersControlTimer[playerName] = controlTimer +end) + +core.register_on_leaveplayer(function(player, timed_out) + local playerName = player:get_player_name() + if playerName == nil then + return + end + + playersLastControl[playerName] = nil + playersControlTimer[playerName] = nil +end) Api.registerGlobalStepForEachPlayer(0, function(player, deltaTime) if player == nil then @@ -70,32 +105,36 @@ Api.registerGlobalStepForEachPlayer(0, function(player, deltaTime) return end - local control - for _, key in ipairs(ControlKeys) do + for _, key in ipairs(ControlPressKeys) do local lastPressed = lastControl[key] local nowPressed = nowControl[key] -- on_press if not lastPressed and nowPressed then - Callbacks.call('control.on_press', key) + Events.call('control.on_press', key) controlTimer[key] = 0 -- on_release elseif lastPressed and not nowPressed then - Callbacks.call('control.on_release', key, controlTimer[key]) + Events.call('control.on_release', key, controlTimer[key]) controlTimer[key] = 0 -- on_hold elseif lastPressed and nowPressed then - Callbacks.call('control.on_hold', key, controlTimer[key], deltaTime) + Events.call('control.on_hold', key, controlTimer[key], deltaTime) controlTimer[key] = controlTimer[key] + deltaTime end end - playersLastControl[playerName] = nowControl + --TODO: add player movement callbacks + + --TODO: add mouse movement callbacks on dir + + playersLastControl[playerName] = nowControl + playersControlTimer[playerName] = controlTimer end) return Control \ No newline at end of file