From 02301d49feddf2c0deba4f75cf0727eee24fa134 Mon Sep 17 00:00:00 2001 From: Zoo <34508112+FjamZoo@users.noreply.github.com> Date: Thu, 27 Jun 2024 12:59:06 +0200 Subject: [PATCH] Feat: move group functions to seperate client file (#3) --- client/functions.lua | 62 +++++++++++++++++++++++++++++++++++++++++++ client/main.lua | 63 -------------------------------------------- server/api.lua | 22 ++++++++-------- 3 files changed, 73 insertions(+), 74 deletions(-) create mode 100644 client/functions.lua diff --git a/client/functions.lua b/client/functions.lua new file mode 100644 index 0000000..15fa497 --- /dev/null +++ b/client/functions.lua @@ -0,0 +1,62 @@ + +---@type table +local GroupBlips = {} + +---Removes a blip by name +---@param name string +local function removeBlipByName(name) + for i = 1, #GroupBlips do + local blip = GroupBlips[i] + + if blip?.name == name then + SetBlipRoute(blip.blip, false) + RemoveBlip(blip.blip) + table.remove(GroupBlips, i) + break + end + end +end + + +RegisterNetEvent('groups:removeBlip', removeBlipByName) + +---Creates a blip from the data passed +---@param name string +---@param data BlipData +RegisterNetEvent('groups:createBlip', function(name, data) + if not data then return + print('Invalid Data was passed to the create blip event') + end + + -- We remove the blip if it already exists with the same name + removeBlipByName(name) + + local blip + if data.entity then + blip = AddBlipForEntity(data.entity) + elseif data.netId then + blip = AddBlipForEntity(NetworkGetEntityFromNetworkId(data.netId)) + elseif data.radius then + blip = AddBlipForRadius(data.coords.x, data.coords.y, data.coords.z, data.radius) + else + blip = AddBlipForCoord(data.coords.x, data.coords.y, data.coords.z) + end + + if not data.radius then + SetBlipSprite(blip, data.sprite or 1) + SetBlipScale(blip, data.scale or 0.7) + BeginTextCommandSetBlipName('STRING') + AddTextComponentSubstringPlayerName(data.label or 'NO LABEL FOUND') + EndTextCommandSetBlipName(blip) + end + + SetBlipColour(blip, data.color or 1) + SetBlipAlpha(blip, data.alpha or 255) + + if data.route then + SetBlipRoute(blip, true) + SetBlipRouteColour(blip, data.routeColor or 1) + end + + GroupBlips[#GroupBlips + 1] = { name = name, blip = blip } +end) \ No newline at end of file diff --git a/client/main.lua b/client/main.lua index 7edbdf8..9e8dffd 100644 --- a/client/main.lua +++ b/client/main.lua @@ -44,69 +44,6 @@ CreateThread(function() end) end) -local GroupBlips = {} - -local function FindBlipByName(name) - for i = 1, #GroupBlips do - if GroupBlips[i] and GroupBlips[i].name == name then - return i - end - end -end - -RegisterNetEvent('groups:removeBlip', function(name) - local i = FindBlipByName(name) - if i then - local blip = GroupBlips[i]['blip'] - SetBlipRoute(blip, false) - RemoveBlip(blip) - GroupBlips[i] = nil - end -end) - -RegisterNetEvent('groups:createBlip', function(name, data) - if not data then return print('Invalid Data was passed to the create blip event') end - - if FindBlipByName(name) then - TriggerEvent('groups:removeBlip', name) - end - - local blip - if data.entity then - blip = AddBlipForEntity(data.entity) - elseif data.netId then - blip = AddBlipForEntity(NetworkGetEntityFromNetworkId(data.netId)) - elseif data.radius then - blip = AddBlipForRadius(data.coords.x, data.coords.y, data.coords.z, data.radius) - else - blip = AddBlipForCoord(data.coords.x, data.coords.y, data.coords.z) - end - - if not data.color then data.color = 1 end - if not data.alpha then data.alpha = 255 end - - if not data.radius then - if not data.sprite then data.sprite = 1 end - if not data.scale then data.scale = 0.7 end - if not data.label then data.label = 'NO LABEL FOUND' end - - SetBlipSprite(blip, data.sprite) - SetBlipScale(blip, data.scale) - BeginTextCommandSetBlipName('STRING') - AddTextComponentSubstringPlayerName(data.label) - EndTextCommandSetBlipName(blip) - end - - SetBlipColour(blip, data.color) - SetBlipAlpha(blip, data.alpha) - - if data.route then - SetBlipRoute(blip, true) - SetBlipRouteColour(blip, data.routeColor) - end - GroupBlips[#GroupBlips + 1] = { name = name, blip = blip } -end) - RegisterNuiCallback('getPlayerData', function(_, cb) local citizenId = getPlayerData() local playerData = { diff --git a/server/api.lua b/server/api.lua index ade5b32..58a2c3c 100644 --- a/server/api.lua +++ b/server/api.lua @@ -42,17 +42,17 @@ end utils.exportHandler('pNotifyGroup', api.pNotifyGroup) ---@class BlipData ----@field entity number ----@field netId number ----@field radius number ----@field coords vector3 ----@field color number ----@field alpha number ----@field sprite number ----@field scale number ----@field label string ----@field route boolean ----@field routeColor number +---@field entity number? +---@field netId number? +---@field radius number? +---@field coords vector3? +---@field color number? +---@field alpha number? +---@field sprite number? +---@field scale number? +---@field label string? +---@field route boolean? +---@field routeColor number? --- Creates a blip for all members of a group ---@param groupID number