diff --git a/server/api.lua b/server/api.lua index d633ba4..b611659 100644 --- a/server/api.lua +++ b/server/api.lua @@ -34,13 +34,25 @@ function api.pNotifyGroup(groupID, header, msg) return lib.print.error('pNotifyGroup was sent an invalid groupID :'..groupID) end - lib.triggerClientEvent('slrn_groups:client:CustomNotification', group:getGroupMembers(), - header or 'NO HEADER', - msg or 'NO MSG' - ) + group:triggerGroupEvent('slrn_groups:client:CustomNotification', header or 'NO HEADER', msg or 'NO MSG') end utils.exportHandler('pNotifyGroup', api.pNotifyGroup) +---Triggers a client event for each member of a group (stolen from ox_lib) +---@param eventName string +---@param groupId number +---@param ... any +function api.triggerGroupEvent(eventName, groupId, ...) + local group = groups[groupId] + + if not group then + return lib.print.error('triggerGroupEvent was sent an invalid groupID :'..groupId) + end + + group:triggerGroupEvent(eventName, ...) +end +utils.exportHandler('triggerGroupEvent', api.triggerGroupEvent) + ---@class BlipData ---@field entity number? ---@field netId number? @@ -65,7 +77,7 @@ function api.CreateBlipForGroup(groupID, name, data) return lib.print.error('CreateBlipForGroup was sent an invalid groupID :'..groupID) end - lib.triggerClientEvent('groups:createBlip', group:getGroupMembers(), name, data) + group:triggerGroupEvent('groups:createBlip', name, data) end utils.exportHandler('CreateBlipForGroup', api.CreateBlipForGroup) @@ -79,7 +91,7 @@ function api.RemoveBlipForGroup(groupID, name) return lib.print.error('RemoveBlipForGroup was sent an invalid groupID :'..groupID) end - lib.triggerClientEvent('slrn_groups:client:RemoveBlipForGroup', group:getGroupMembers(), name) + group:triggerGroupEvent('groups:removeBlip', name) end utils.exportHandler('RemoveBlipForGroup', api.RemoveBlipForGroup) @@ -260,7 +272,7 @@ function api.setJobStatus(groupID, status, stages) group.status = status group.stage = stages - lib.triggerClientEvent('slrn_groups:client:updateGroupStage', group:getGroupMembers(), status, stages) + group:triggerGroupEvent('slrn_groups:client:updateGroupStage', status, stages) end utils.exportHandler('setJobStatus', api.setJobStatus) diff --git a/server/groups.lua b/server/groups.lua index a1c6642..d9b4a4d 100644 --- a/server/groups.lua +++ b/server/groups.lua @@ -88,6 +88,18 @@ end +---Triggers an event for all members of the group. (Stolen from ox_lib so thanks to the original author) +---@param eventName string +---@param ... any +function groups:triggerGroupEvent(eventName, ...) + local payload = msgpack.pack_args(...) + local payloadLen = #payload + + for i = 1, #self.members do + TriggerClientEventInternal(eventName, self.members[i].Player --[[@as string]], payload, payloadLen) + end +end +