Skip to content

Commit 1066356

Browse files
committed
Refactor HasTeam logic for clarity and robustness
Rewrote the HasTeam function to improve readability, handle more edge cases, and ensure correct type checks. The new implementation better distinguishes between string, entity, and table inputs, and uses table.isArray to validate team arrays.
1 parent cd0de35 commit 1066356

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

lua/background_npcs_core/classes/actor/sh_actor_base.lua

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ local table_RemoveByValue = table.RemoveByValue
2525
local table_Copy = table.Copy
2626
local table_Add = table.Add
2727
local table_insert = table.insert
28+
local table_isArray = table.isArray
2829
local string_find = string.find
2930
local string_lower = string.lower
3031
local hook_Run = hook.Run
@@ -1032,36 +1033,46 @@ function BaseClass:GetRelationship(value, default)
10321033
end
10331034

10341035
function BaseClass:HasTeam(value)
1035-
local value = value
1036-
if self.data.team ~= nil and value ~= nil then
1037-
if isstring(value) then
1038-
return table_HasValueBySeq(self.data.team, value)
1036+
if self.data == nil or self.data.team == nil or value == nil then
1037+
return false
1038+
end
1039+
1040+
if isstring(value) then
1041+
return table_HasValueBySeq(self.data.team, value)
1042+
end
1043+
1044+
if isentity(value) then
1045+
if isfunction(value.IsPlayer) and value:IsPlayer() then
1046+
if table_HasValueBySeq(self.data.team, 'player') then
1047+
return true
1048+
else
1049+
return bgNPC:GetModule('team_parent'):HasParent(value, self)
1050+
end
1051+
elseif value.isBgnActor and ((isfunction(value.IsNPC) and value:IsNPC()) or (isfunction(value.IsNextBot) and value:IsNextBot())) then
1052+
local actor = bgNPC:GetActor(value)
1053+
if actor then value = actor end
10391054
end
1055+
end
10401056

1041-
if isentity(value) then
1042-
if value:IsPlayer() then
1043-
if table_HasValueBySeq(self.data.team, 'player') then
1044-
return true
1045-
else
1046-
return bgNPC:GetModule('team_parent'):HasParent(value, self)
1047-
end
1048-
elseif value.isBgnActor and (value:IsNPC() or value:IsNextBot()) then
1049-
local actor = bgNPC:GetActor(value)
1050-
if not actor then return false end
1051-
value = actor:GetData().team
1057+
if istable(value) then
1058+
if value.isBgnClass and isfunction(value.GetData) then
1059+
local data = value:GetData()
1060+
if istable(data) and istable(data.team) then
1061+
value = data.team
10521062
end
10531063
end
10541064

1055-
if istable(value) then
1056-
if value.isBgnClass then value = value:GetData().team end
1065+
if not table_isArray(value) then
1066+
return false
1067+
end
10571068

1058-
for i = 1, #self.data.team do
1059-
for k = 1, #value do
1060-
if self.data.team[i] == value[k] then return true end
1061-
end
1069+
for i = 1, #self.data.team do
1070+
for k = 1, #value do
1071+
if self.data.team[i] == value[k] then return true end
10621072
end
10631073
end
10641074
end
1075+
10651076
return false
10661077
end
10671078

0 commit comments

Comments
 (0)