Skip to content

Commit

Permalink
fix(client/compat): (Add/Remove)TargetEntity
Browse files Browse the repository at this point in the history
Ensure entities is always a table to remove duplicate code.
Convert entityId to netId when entity is networked.
Convert options at the top of the function, rather than during iteration.
  • Loading branch information
thelindat committed Nov 8, 2022
1 parent b3708d9 commit df6cf08
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
41 changes: 17 additions & 24 deletions client/compat/qb-target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,30 @@ exportHandler('AddTargetBone', function(bones, options)
end)

exportHandler('AddTargetEntity', function(entities, options)
if type(entities) == 'table' then
for k, v in pairs(entities) do
if NetworkGetEntityIsNetworked(v) then
target.addEntity(v, convert(options))
else
target.addLocalEntity(v, convert(options))
end
end
else
if NetworkGetEntityIsNetworked(entities) then
target.addEntity(entities, convert(options))
if type(entities) ~= 'table' then entities = { entities } end
options = convert(options)

for i = 1, #entities do
local entity = entities[i]

if NetworkGetEntityIsNetworked(entity) then
target.addEntity(NetworkGetNetworkIdFromEntity(entity), options)
else
target.addLocalEntity(entities, convert(options))
target.addLocalEntity(entity, options)
end
end
end)

exportHandler('RemoveTargetEntity', function(entities, labels)
if type(entities) == 'table' then
for k, v in pairs(entities) do
if NetworkGetEntityIsNetworked(v) then
target.removeEntity(v, labels)
else
target.removeLocalEntity(v, labels)
end
end
else
if NetworkGetEntityIsNetworked(entities) then
target.removeEntity(entities, labels)
if type(entities) ~= 'table' then entities = { entities } end

for i = 1, #entities do
local entity = entities[i]

if NetworkGetEntityIsNetworked(entity) then
target.removeEntity(NetworkGetNetworkIdFromEntity(entity), labels)
else
target.removeLocalEntity(entities, labels)
target.removeLocalEntity(entity, labels)
end
end
end)
Expand Down
41 changes: 17 additions & 24 deletions client/compat/qtarget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,30 @@ exportHandler('AddTargetBone', function(bones, options)
end)

exportHandler('AddTargetEntity', function(entities, options)
if type(entities) == 'table' then
for k, v in pairs(entities) do
if NetworkGetEntityIsNetworked(v) then
target.addEntity(v, convert(options))
else
target.addLocalEntity(v, convert(options))
end
end
else
if NetworkGetEntityIsNetworked(entities) then
target.addEntity(entities, convert(options))
if type(entities) ~= 'table' then entities = { entities } end
options = convert(options)

for i = 1, #entities do
local entity = entities[i]

if NetworkGetEntityIsNetworked(entity) then
target.addEntity(NetworkGetNetworkIdFromEntity(entity), options)
else
target.addLocalEntity(entities, convert(options))
target.addLocalEntity(entity, options)
end
end
end)

exportHandler('RemoveTargetEntity', function(entities, labels)
if type(entities) == 'table' then
for k, v in pairs(entities) do
if NetworkGetEntityIsNetworked(v) then
target.removeEntity(v, labels)
else
target.removeLocalEntity(v, labels)
end
end
else
if NetworkGetEntityIsNetworked(entities) then
target.removeEntity(entities, labels)
if type(entities) ~= 'table' then entities = { entities } end

for i = 1, #entities do
local entity = entities[i]

if NetworkGetEntityIsNetworked(entity) then
target.removeEntity(NetworkGetNetworkIdFromEntity(entity), labels)
else
target.removeLocalEntity(entities, labels)
target.removeLocalEntity(entity, labels)
end
end
end)
Expand Down

0 comments on commit df6cf08

Please sign in to comment.