Skip to content

Commit

Permalink
chore: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Aug 2, 2023
1 parent a74add0 commit ee1d90f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
78 changes: 30 additions & 48 deletions client/api.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
---@class TargetOptions
---@field label string
---@field name? string
---@field icon? string
---@field iconColor? string
---@field distance? number
---@field bones? string | string[]
---@field groups? string | string[] | table<string, number>
---@field items? string | string[] | table<string, number>
---@field anyItem? boolean
---@field canInteract? fun(entity?: number, distance: number, coords: vector3, name?: string, bone?: number): boolean?
---@field onSelect? fun(data: TargetOptions | number)
---@field export? string
---@field event? string
---@field serverEvent? string
---@field command? string
---@class OxTargetOption
---@field resource? string
---@field openMenu? string
---@field menuName? string
---@field [string] any

local api = setmetatable({}, {
__newindex = function(self, index, value)
Expand All @@ -26,21 +8,21 @@ local api = setmetatable({}, {
end
})

---@param data table
---@param data OxTargetPolyZone
---@return number
function api.addPolyZone(data)
data.resource = GetInvokingResource()
return lib.zones.poly(data).id
end

---@param data table
---@param data OxTargetBoxZone
---@return number
function api.addBoxZone(data)
data.resource = GetInvokingResource()
return lib.zones.box(data).id
end

---@param data table
---@param data OxTargetSphereZone
---@return number
function api.addSphereZone(data)
data.resource = GetInvokingResource()
Expand Down Expand Up @@ -81,7 +63,7 @@ local function typeError(variable, expected, received)
end

---@param target table
---@param options TargetOptions | TargetOptions[]
---@param options OxTargetOption | OxTargetOption[]
---@param resource string
local function addTarget(target, options, resource)
local optionsType = type(options)
Expand All @@ -93,12 +75,12 @@ local function addTarget(target, options, resource)
local tableType = table.type(options)

if tableType == 'hash' and options.label then
options = { options --[[@as TargetOptions]] }
options = { options --[[@as OxTargetOption]] }
elseif tableType ~= 'array' then
typeError('options', 'array', ('%s table'):format(tableType))
end

---@cast options TargetOptions[]
---@cast options OxTargetOption[]

local num = #target

Expand All @@ -122,7 +104,7 @@ local function addTarget(target, options, resource)
end

---@param target table
---@param remove table
---@param remove string | string[]
---@param resource string
local function removeTarget(target, remove, resource)
if type(remove) ~= 'table' then remove = { remove } end
Expand All @@ -140,63 +122,63 @@ local function removeTarget(target, remove, resource)
end
end

---@type table<number, TargetOptions[]>
---@type table<number, OxTargetOption[]>
local peds = {}

---@param options TargetOptions | TargetOptions[]
---@param options OxTargetOption | OxTargetOption[]
function api.addGlobalPed(options)
addTarget(peds, options, GetInvokingResource())
end

---@param options TargetOptions | TargetOptions[]
---@param options string | string[]
function api.removeGlobalPed(options)
removeTarget(peds, options, GetInvokingResource())
end

---@type table<number, TargetOptions[]>
---@type table<number, OxTargetOption[]>
local vehicles = {}

---@param options TargetOptions | TargetOptions[]
---@param options OxTargetOption | OxTargetOption[]
function api.addGlobalVehicle(options)
addTarget(vehicles, options, GetInvokingResource())
end

---@param options TargetOptions | TargetOptions[]
---@param options string | string[]
function api.removeGlobalVehicle(options)
removeTarget(vehicles, options, GetInvokingResource())
end

---@type table<number, TargetOptions[]>
---@type table<number, OxTargetOption[]>
local objects = {}

---@param options TargetOptions | TargetOptions[]
---@param options OxTargetOption | OxTargetOption[]
function api.addGlobalObject(options)
addTarget(objects, options, GetInvokingResource())
end

---@param options TargetOptions | TargetOptions[]
---@param options string | string[]
function api.removeGlobalObject(options)
removeTarget(objects, options, GetInvokingResource())
end

---@type table<number, TargetOptions[]>
---@type table<number, OxTargetOption[]>
local players = {}

---@param options TargetOptions | TargetOptions[]
---@param options OxTargetOption | OxTargetOption[]
function api.addGlobalPlayer(options)
addTarget(players, options, GetInvokingResource())
end

---@param options TargetOptions | TargetOptions[]
---@param options string | string[]
function api.removeGlobalPlayer(options)
removeTarget(players, options, GetInvokingResource())
end

---@type table<number, TargetOptions[]>
---@type table<number, OxTargetOption[]>
local models = {}

---@param arr number | number[]
---@param options TargetOptions | TargetOptions[]
---@param arr (number | string) | (number | string)[]
---@param options OxTargetOption | OxTargetOption[]
function api.addModel(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand All @@ -213,8 +195,8 @@ function api.addModel(arr, options)
end
end

---@param arr number | number[]
---@param options? table
---@param arr (number | string) | (number | string)[]
---@param options? string | string[]
function api.removeModel(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand All @@ -235,11 +217,11 @@ function api.removeModel(arr, options)
end
end

---@type table<number, TargetOptions[]>
---@type table<number, OxTargetOption[]>
local entities = {}

---@param arr number | number[]
---@param options TargetOptions | TargetOptions[]
---@param options OxTargetOption | OxTargetOption[]
function api.addEntity(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand All @@ -262,7 +244,7 @@ function api.addEntity(arr, options)
end

---@param arr number | number[]
---@param options? table
---@param options? string | string[]
function api.removeEntity(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand All @@ -284,11 +266,11 @@ end

RegisterNetEvent('ox_target:removeEntity', api.removeEntity)

---@type table<number, TargetOptions[]>
---@type table<number, OxTargetOption[]>
local localEntities = {}

---@param arr number | number[]
---@param options TargetOptions | TargetOptions[]
---@param options OxTargetOption | OxTargetOption[]
function api.addLocalEntity(arr, options)
if type(arr) ~= 'table' then arr = { arr } end
local resource = GetInvokingResource()
Expand Down
4 changes: 2 additions & 2 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ local function startTargeting()
end
end

---@type table<string, TargetOptions[]>
---@type table<string, OxTargetOption[]>
options = newOptions or options or {}
newOptions = (newOptions or zonesChanged or entityHit ~= lastEntity) and true
lastEntity = entityHit
Expand Down Expand Up @@ -384,7 +384,7 @@ RegisterNUICallback('select', function(data, cb)

local zone = data[3] and nearbyZones[data[3]]

---@type TargetOptions?
---@type OxTargetOption?
local option = zone and zone.options[data[2]] or options[data[1]][data[2]]

if option then
Expand Down

0 comments on commit ee1d90f

Please sign in to comment.