Initialize random atts properly when spawned in hand (fix #82)

This commit is contained in:
TheOnly8Z 2023-09-03 19:17:01 -05:00
parent 8d29d6c482
commit 5e55f78252
4 changed files with 29 additions and 22 deletions

View File

@ -127,15 +127,7 @@ hook.Add( "OnEntityCreated", "ArcCW_NPCWeaponReplacement", function(ent)
timer.Simple(0, function()
if !ent:IsValid() then return end
if IsValid(ent:GetOwner()) then return end
if ent.ArcCW then
-- Handled by the weapon
--[[]
if engine.ActiveGamemode() == "terrortown" and ArcCW.ConVars["ttt_atts"]:GetBool() then
ent:NPC_SetupAttachments()
end
]]
return
end
if ent.ArcCW then return end
local class = ent:GetClass()
@ -150,10 +142,6 @@ hook.Add( "OnEntityCreated", "ArcCW_NPCWeaponReplacement", function(ent)
wpnent:Spawn()
-- if engine.ActiveGamemode() == "terrortown" and ArcCW.ConVars["ttt_atts"]:GetBool() then
-- wpnent:NPC_SetupAttachments()
-- end
timer.Simple(0, function()
if !ent:IsValid() then return end
wpnent:OnDrop(true)
@ -200,10 +188,19 @@ hook.Add("onDarkRPWeaponDropped", "ArcCW_DarkRP", function(ply, spawned_weapon,
end)
hook.Add("PlayerGiveSWEP", "ArcCW_SpawnRandomAttachments", function(ply, class, tbl)
if tbl.ArcCW and ArcCW.ConVars["atts_spawnrand"]:GetBool() then
timer.Simple(0, function()
if IsValid(ply) and IsValid(ply:GetWeapon(class)) then
ply:GetWeapon(class):NPC_SetupAttachments()
if weapons.IsBasedOn(class, "arccw_base") and ArcCW.ConVars["atts_spawnrand"]:GetBool() then
-- We can't get the weapon entity here - it's spawned after the hook call.
-- Mark the player to disable autosave tempoarily if we're spawning random attachments.
ply.ArcCW_Sandbox_RandomAtts = true
timer.Simple(0.0001, function()
local wpn = ply:GetWeapon(class)
if IsValid(ply) and IsValid(wpn) then
wpn:NPC_SetupAttachments()
if ply.ArcCW_Sandbox_FirstSpawn then
wpn:RestoreAmmo()
ply.ArcCW_Sandbox_FirstSpawn = nil
end
end
end)
end

View File

@ -473,6 +473,10 @@ if SERVER then
local wpn = net.ReadEntity()
if wpn:GetOwner() != ply or !wpn.ArcCW then return end
if ply.ArcCW_DisableAutosave or ply.ArcCW_Sandbox_RandomAtts then
ply.ArcCW_Sandbox_RandomAtts = nil
return
end
for k, v in pairs(wpn.Attachments) do
wpn:Detach(k, true, true)
@ -503,6 +507,7 @@ if SERVER then
end
wpn:AdjustAtts()
wpn:RefreshBGs()
if ply.ArcCW_Sandbox_FirstSpawn then
-- Curiously, RestoreAmmo has a sync delay only in singleplayer
@ -517,7 +522,6 @@ if SERVER then
net.Start("arccw_applypreset")
net.WriteEntity(wpn)
net.Send(ply)
end)
else
net.Receive("arccw_applypreset", function()

View File

@ -1055,7 +1055,7 @@ function SWEP:RefreshBGs()
end
end
local tpmdl = self.WMModel or self
local tpmdl = IsValid(self.WMModel) and self.WMModel or self
if IsValid(vm) then
for i = 0, (vm:GetNumBodyGroups()) do

View File

@ -106,10 +106,16 @@ function SWEP:NPC_SetupAttachments()
self:AdjustAtts()
self:RefreshBGs()
timer.Simple(0.25, function()
if !IsValid(self) then return end
if self:GetOwner():IsNPC() then
timer.Simple(0.1, function()
if !IsValid(self) then return end
self:NetworkWeapon()
end)
else
self:NetworkWeapon()
end)
self:SetupModel(false)
self:SetupModel(true)
end
end
function SWEP:NPC_Shoot()