E2Lib.RegisterExtension("spawnhook", true) --[[ Spawn Hooks by SonicXVe xvsgame@gmail.com This extension adds support for pre and post spawning of objects (Props, Effects and Ragdolls) It also allows admins to deny spawning using the Pre hooks Command syntax of Post-Spawn functions: ]] local rchips = {} local hookrun = 0 local prerchips = {} local prehookrun = 0 ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- __e2setcost(10) --- Returns the entity created by the hook e2function entity spawnObject() if hookrun then return hookProp end end __e2setcost(20) --- Returns player that created the object e2function entity spawnClk() if hookrun then return hookOwner end end ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- local function spawnTick(player,model,enty) local rcopy = {} -- This is necessary because the E2 cannot modify its registration if the table is being iterated. rcopy=rchips hookrun = 1 hookProp = enty hookOwner = player for ent,dex in pairs(rcopy) do if not ValidEntity(ent) then table.remove(rchips,table.KeyFromValue(rchips,ent)) continue end local exec = false if dex["any"] == 1 then exec = true else for val,_ in pairs(dex) do if val == "any" or not ValidEntity(val) then continue elseif val == player then exec = true end end end if exec then ent:Execute() end end hookrun = 0 hookProp = nil hookOwner = nil end ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- __e2setcost(10) --- Set to a player to run on spawned prop, ragdoll, or effect by that player (Recommended, considerably fewer 'Ops' than running on any player) e2function void runOnSpawn(entity ply, run) if not ValidEntity(ply) then return end local idex = {} if ValidEntity(rchips[self.entity]) then idex = rchips[self.entity] end if run ~= 0 then if not idex["any"] then idex["any"] = 0 end idex[ply] = 1 else table.remove(idex,table.KeyFromValue(idex,ply)) end rchips[self.entity] = idex end __e2setcost(2) --- Set to 1 to run on any spawned prop, ragdoll, or effect (Not recommended, heavy prop spam may force chip to exceed tick quota) e2function void runOnSpawn(run) local idex = {} if ValidEntity(rchips[self.entity]) then idex = rchips[self.entity] end if run ~= 0 then idex["any"] = 1 else idex["any"] = 0 end rchips[self.entity] = idex end ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- --[[ PRESPAWN HOOKS Command syntax of Pre-Spawn functions: ]] ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- __e2setcost(10) --- Returns player that is creating the object e2function entity prespawnClk() if prehookrun then return prehookOwner end end __e2setcost(10) --- Set to 1 to deny prop from spawning (Admin only) e2function void prespawnDeny(block) if self.player:IsAdmin() and prehookrun and block ~= 0 then prehookbreak=1 else prehookbreak=0 end end ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- local function prespawnTick(player) local rcopy = {} -- This is necessary because the E2 cannot modify its registration if the table is being iterated. rcopy=prerchips prehookrun = 1 prehookbreak = 0 prehookOwner = player for ent,dex in pairs(rcopy) do if not ValidEntity(ent) then table.remove(prerchips,table.KeyFromValue(prerchips,ent)) continue end local exec = false if dex["any"] == 1 then exec = true else for val,_ in pairs(dex) do if val == "any" or not ValidEntity(val) then continue elseif val == player then exec = true end end end if exec then ent:Execute() end if prehookbreak != 0 then break end end prehookrun = 0 prehookOwner = nil if prehookbreak != 0 then return false end end ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- __e2setcost(4) --- Set to a player to run before spawned prop, ragdoll, or effect by that player (Allows admins to deny spawning) e2function void runOnPrespawn(entity ply, run) if not ValidEntity(ply) then return end local idex = {} if ValidEntity(prerchips[self.entity]) then idex = prerchips[self.entity] end if run ~= 0 then if not idex["any"] then idex["any"] = 0 end idex[ply] = 1 else table.remove(idex,table.KeyFromValue(idex,ply)) end prerchips[self.entity] = idex end __e2setcost(1) --- Set to 1 to run before any spawned prop, ragdoll, or effect (Allows admins to deny spawning) e2function void runOnPrespawn(run) local idex = {} if ValidEntity(prerchips[self.entity]) then idex = prerchips[self.entity] end if run ~= 0 then idex["any"] = 1 else idex["any"] = 0 end prerchips[self.entity] = idex end ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- hook.Add( "PlayerSpawnObject", "E2PreSpawnHook", prespawnTick) hook.Add( "PlayerSpawnedProp", "E2SpawnHook1", spawnTick) hook.Add( "PlayerSpawnedRagdoll", "E2SpawnHook2", spawnTick) hook.Add( "PlayerSpawnedEffect", "E2SpawnHook3", spawnTick)