Fix possible arg count error (#417)

* Don't spawn entity if arg counts don't match up

* Style

* Use tab indentation

* Delete last created entity instead

* Use SafeRemoveEntity instead

* Move remove down

* Support multiple created entities

* Style fix

* Fix print grammar

* Apply feedback
This commit is contained in:
Redox 2023-04-19 01:52:08 +02:00 committed by GitHub
parent df12047d51
commit 3b969bac9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -916,6 +916,7 @@ local function CreateEntityFromTable(EntTable, Player)
local sent = false local sent = false
local status, valid local status, valid
local GENERIC = false local GENERIC = false
local CreatedEntities = {}
-- This class is unregistered. Instead of failing try using a generic -- This class is unregistered. Instead of failing try using a generic
-- Duplication function to make a new copy. -- Duplication function to make a new copy.
@ -1000,8 +1001,13 @@ local function CreateEntityFromTable(EntTable, Player)
sent = true sent = true
end end
hook.Add( "OnEntityCreated", "AdvDupe2_GetLastEntitiesCreated", function( ent )
table.insert( CreatedEntities, ent )
end )
status, valid = pcall(EntityClass.Func, Player, unpack(ArgList, 1, #EntityClass.Args)) status, valid = pcall(EntityClass.Func, Player, unpack(ArgList, 1, #EntityClass.Args))
if not status then ErrorNoHalt(valid) end
hook.Remove( "OnEntityCreated", "AdvDupe2_GetLastEntitiesCreated" )
else else
print("Advanced Duplicator 2: ENTITY CLASS IS BLACKLISTED, CLASS NAME: " .. EntTable.Class) print("Advanced Duplicator 2: ENTITY CLASS IS BLACKLISTED, CLASS NAME: " .. EntTable.Class)
return nil return nil
@ -1050,6 +1056,14 @@ local function CreateEntityFromTable(EntTable, Player)
return valid return valid
else else
if (status == false) then
print("Advanced Duplicator 2: Error creating entity, removing last created entities")
for _, CreatedEntity in pairs(CreatedEntities) do
SafeRemoveEntity(CreatedEntity)
end
ErrorNoHaltWithStack(valid)
end
if (valid == false) then if (valid == false) then
return false return false
else else