Fix error when socket weld is broken with invalid plug. Fixes: #2299 (#2300)

* Fix error when socket weld is broken with invalid plug. Fixes: #2299

* Use DTVars instead of NWVars
This commit is contained in:
thegrb93 2022-03-20 19:14:42 -04:00 committed by GitHub
parent a8dbdc10cf
commit d339983e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 29 deletions

View File

@ -6,6 +6,11 @@ ENT.Purpose = "Links with a socket"
ENT.Instructions = "Move a plug close to a socket to link them, and data will be transferred through the link."
ENT.WireDebugName = "Plug"
function ENT:SetupDataTables()
self:NetworkVar( "Bool", 0, "PlayerHolding" )
self:NetworkVar( "Bool", 1, "Linked" )
end
function ENT:GetSocketClass()
return "gmod_wire_socket"
end
@ -17,7 +22,7 @@ function ENT:GetClosestSocket()
local Closest
for k,v in pairs( sockets ) do
if (v:GetClass() == self:GetSocketClass() and not v:GetNWBool( "Linked", false )) then
if (v:GetClass() == self:GetSocketClass() and not v:GetLinked()) then
local pos, _ = v:GetLinkPos()
local Dist = self:GetPos():Distance( pos )
if (ClosestDist == nil or ClosestDist > Dist) then
@ -53,7 +58,7 @@ function ENT:Initialize()
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetNWBool( "Linked", false )
self:SetLinked( false )
self.Memory = {}
end
@ -141,7 +146,7 @@ end
function ENT:Think()
BaseClass.Think( self )
self:SetNWBool( "PlayerHolding", self:IsPlayerHolding() )
self:SetPlayerHolding( self:IsPlayerHolding() )
end
function ENT:ResetValues()

View File

@ -55,7 +55,7 @@ function ENT:GetClosestPlug()
local Closest
for k,v in pairs( plugs ) do
if (v:GetClass() == self:GetPlugClass() and not v:GetNWBool( "Linked", false )) then
if (v:GetClass() == self:GetPlugClass() and not v:GetLinked()) then
local Dist = v:GetPos():Distance( Pos )
if (ClosestDist == nil or ClosestDist > Dist) then
ClosestDist = Dist
@ -71,6 +71,10 @@ function ENT:GetPlugClass()
return "gmod_wire_plug"
end
function ENT:SetupDataTables()
self:NetworkVar( "Bool", 0, "Linked" )
end
if CLIENT then
function ENT:DrawEntityOutline()
if (GetConVar("wire_plug_drawoutline"):GetBool()) then
@ -85,7 +89,7 @@ if CLIENT then
local Closest = self:GetClosestPlug()
if IsValid(Closest) and self:CanLink(Closest) and Closest:GetNWBool( "PlayerHolding", false ) and Closest:GetClosestSocket() == self then
if IsValid(Closest) and self:CanLink(Closest) and Closest:GetPlayerHolding() and Closest:GetClosestSocket() == self then
local plugpos = Closest:GetPos():ToScreen()
local socketpos = Pos:ToScreen()
surface.SetDrawColor(255,255,100,255)
@ -110,7 +114,7 @@ function ENT:Initialize()
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetNWBool( "Linked", false )
self:SetLinked( false )
self.Memory = {}
@ -229,27 +233,23 @@ function ENT:ResendValues()
end
end
function ENT:OnWeldRemoved()
self.Weld = nil
self.Plug:SetNWBool( "Linked", false )
self:SetNWBool( "Linked", false )
self.Plug.Socket = nil
self.Plug:ResetValues()
self.Plug = nil
self:ResetValues()
self.DoNextThink = CurTime() + NEW_PLUG_WAIT_TIME
end
function ENT:AttachWeld(weld)
if self.Plug then self.Plug:DeleteOnRemove( weld ) end
self:DeleteOnRemove( weld )
if self.Weld then self.Weld:RemoveCallOnRemove("wire_socket_remove_on_weld") end
self.Weld = weld
weld:CallOnRemove("wire_socket_remove_on_weld",function() self:OnWeldRemoved() end)
local plug = self.Plug
weld:CallOnRemove("wire_socket_remove_on_weld",function()
if self:IsValid() then
self.Weld = nil
self:SetLinked( false )
self:ResetValues()
self.DoNextThink = CurTime() + NEW_PLUG_WAIT_TIME
self.Plug = nil
end
if plug and plug:IsValid() then
plug:SetLinked( false )
plug.Socket = nil
plug:ResetValues()
end
end)
end
-- helper function
@ -308,8 +308,8 @@ function ENT:Think()
Closest:ResendValues()
self:ResendValues()
Closest:SetNWBool( "Linked", true )
self:SetNWBool( "Linked", true )
Closest:SetLinked( true )
self:SetLinked( true )
end
self:NextThink( CurTime() + 0.05 )
@ -372,8 +372,8 @@ function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID, GetConstByID)
plug.Socket = ent
ent.Weld = nil
plug:SetNWBool( "Linked", true )
ent:SetNWBool( "Linked", true )
plug:SetLinked( true )
ent:SetLinked( true )
-- Resend all values
plug:ResendValues()
ent:ResendValues()