Skip to content

Commit

Permalink
Feat: move group functions to seperate client file (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
FjamZoo authored Jun 27, 2024
1 parent 7565e3d commit 02301d4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 74 deletions.
62 changes: 62 additions & 0 deletions client/functions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

---@type table <number, BlipData>
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)
63 changes: 0 additions & 63 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
22 changes: 11 additions & 11 deletions server/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 02301d4

Please sign in to comment.