Skip to content

Commit

Permalink
fix(client): options setter, getter and wiping
Browse files Browse the repository at this point in the history
Resolves #145.
- Included minor refactoring.
- Resolved a compatibility issue with getTargetOptions.
  • Loading branch information
thelindat committed May 13, 2024
1 parent cdb83a6 commit 91a9a78
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 40 deletions.
58 changes: 37 additions & 21 deletions client/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,33 @@ function options_mt:wipe()
self.localEntity = nil
end

---@param entity? number
---@param _type? number
---@param model? number
function options_mt:set(entity, _type, model)
if not entity then return options end

if _type == 1 then
if IsPedAPlayer(entity) then
self:wipe()
self.globalTarget = players
options_mt.size += 1
end
end

local netId = NetworkGetEntityIsNetworked(entity) and NetworkGetNetworkIdFromEntity(entity)

self.globalTarget = _type == 1 and peds or _type == 2 and vehicles or objects
self.model = models[model]
self.entity = netId and entities[netId] or nil
self.localEntity = localEntities[entity]
options_mt.size += 1

if self.model then options_mt.size += 1 end
if self.entity then options_mt.size += 1 end
if self.localEntity then options_mt.size += 1 end
end

local global = {}

---@param options OxTargetOption | OxTargetOption[]
Expand All @@ -448,29 +475,18 @@ local options = setmetatable({
function api.getTargetOptions(entity, _type, model)
if not entity then return options end

if _type == 1 then
if IsPedAPlayer(entity) then
options:wipe()
options.globalTarget = players
options_mt.size += 1

return options
end
if IsPedAPlayer(entity) then
return {
global = players,
}
end

local netId = NetworkGetEntityIsNetworked(entity) and NetworkGetNetworkIdFromEntity(entity)

options.globalTarget = _type == 1 and peds or _type == 2 and vehicles or objects
options.model = models[model]
options.entity = netId and entities[netId] or nil
options.localEntity = localEntities[entity]
options_mt.size += 1

if options.model then options_mt.size += 1 end
if options.entity then options_mt.size += 1 end
if options.localEntity then options_mt.size += 1 end

return options
return {
global = _type == 1 and peds or _type == 2 and vehicles or objects,
model = models[model],
entity = netId and entities[netId] or nil,
localEntity = localEntities[entity],
}
end

local state = require 'client.state'
Expand Down
39 changes: 20 additions & 19 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lib.locale()

local utils = require 'client.utils'
local state = require 'client.state'
local getTargetOptions = require 'client.api'.getTargetOptions
local options = require 'client.api'.getTargetOptions()

require 'client.debug'
require 'client.defaults'
Expand All @@ -24,7 +24,6 @@ local DisableControlAction = DisableControlAction
local DisablePlayerFiring = DisablePlayerFiring
local GetModelDimensions = GetModelDimensions
local GetOffsetFromEntityInWorldCoords = GetOffsetFromEntityInWorldCoords
local options = getTargetOptions()
local currentTarget = {}
local currentMenu
local menuHistory = {}
Expand Down Expand Up @@ -202,19 +201,12 @@ local function startTargeting()
end
end

if not hit and hasTarget and hasTarget > 1 then
SendNuiMessage('{"event": "leftTarget"}')

if debug and lastEntity > 0 then SetEntityDrawOutline(lastEntity, false) end
if options then options:wipe() end

lastEntity = nil
hasTarget = false
end

nearbyZones, zonesChanged = utils.getNearbyZones(endCoords)

if entityHit > 0 and entityHit ~= lastEntity then
local entityChanged = entityHit ~= lastEntity
local newOptions = (zonesChanged or entityChanged) and true

if entityHit > 0 and entityChanged then
currentMenu = nil

if flag ~= 511 then
Expand All @@ -231,17 +223,26 @@ local function startTargeting()
end
end

if entityHit ~= 0 then
if entityHit > 0 then
local success, result = pcall(GetEntityModel, entityHit)
entityModel = success and result

if entityModel then
options = getTargetOptions(entityHit, entityType, entityModel)
end
end
end

local newOptions = (zonesChanged or entityHit ~= lastEntity) and true
if hasTarget and (zonesChanged or entityChanged and hasTarget > 1) then
SendNuiMessage('{"event": "leftTarget"}')

if entityChanged then options:wipe() end

if debug and lastEntity > 0 then SetEntityDrawOutline(lastEntity, false) end

hasTarget = false
end

if newOptions and entityModel and entityHit > 0 then
options:set(entityHit, entityType, entityModel)
end

lastEntity = entityHit
currentTarget.entity = entityHit
currentTarget.coords = endCoords
Expand Down

0 comments on commit 91a9a78

Please sign in to comment.