Skip to content

Commit 24c9273

Browse files
authored
Fix player core invalid return (#3358)
* Fix player core invalid return + new function Fixes return values ​​in player core and adds a function that allows players to enable/disable noclip * Don't need to do this checks * Left it for other PR * Minor change
1 parent 15b1986 commit 24c9273

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

lua/entities/gmod_wire_expression2/core/player.lua

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
local IsValid = IsValid
66

7-
87
local spawnAlert = {}
98
local lastJoined = NULL
109

@@ -694,75 +693,80 @@ end
694693
__e2setcost(5)
695694

696695
e2function number entity:ping()
697-
if not IsValid(this) then return 0 end
698-
if(this:IsPlayer()) then return this:Ping() else return 0 end
696+
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
697+
if not this:IsPlayer() then return self:throw("Expected a Player, got Entity!", 0) end
698+
return this:Ping()
699699
end
700700

701701
e2function number entity:timeConnected()
702-
if not IsValid(this) then return 0 end
703-
if(this:IsPlayer()) then return this:TimeConnected() else return 0 end
702+
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
703+
if not this:IsPlayer() then return self:throw("Expected a Player, got Entity!", 0) end
704+
return this:TimeConnected()
704705
end
705706

706707
e2function entity entity:vehicle()
707-
if not IsValid(this) then return nil end
708-
if not this:IsPlayer() then return nil end
708+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
709+
if not this:IsPlayer() then return self:throw("Expected a Player, got Entity!", NULL) end
709710
return this:GetVehicle()
710711
end
711712

712713
e2function number entity:inVehicle()
713-
if not IsValid(this) then return 0 end
714-
return this:IsPlayer() and this:InVehicle() and 1 or 0
714+
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
715+
if not this:IsPlayer() then return self:throw("Expected a Player, got Entity!", 0) end
716+
return this:InVehicle() and 1 or 0
715717
end
716718

717719
--- Returns 1 if the player <this> is in noclip mode, 0 if not.
718720
e2function number entity:inNoclip()
719-
if not IsValid(this) or this:GetMoveType() ~= MOVETYPE_NOCLIP then return 0 end
720-
return 1
721+
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
722+
return this:GetMoveType() == MOVETYPE_NOCLIP and 1 or 0
721723
end
722724

723725
e2function number entity:inGodMode()
724-
return IsValid(this) and this:IsPlayer() and this:HasGodMode() and 1 or 0
726+
if not IsValid(this) then return self:throw("Invalid entity!", 0) end
727+
if not this:IsPlayer() then return self:throw("Expected a Player, got Entity!", 0) end
728+
return this:HasGodMode() and 1 or 0
725729
end
726730

727731
--------------------------------------------------------------------------------
728732

729-
local player = player
730-
731733
__e2setcost(10)
732734

733735
e2function array players()
734736
return player.GetAll()
735737
end
736738

737739
e2function array playersAdmins()
738-
local Admins = {}
739-
for _,ply in ipairs(player.GetAll()) do
740-
if (ply:IsAdmin()) then
741-
table.insert(Admins,ply)
740+
local admins = {}
741+
742+
for _, ply in ipairs(player.GetAll()) do
743+
if ply:IsAdmin() then
744+
table.insert(admins, ply)
742745
end
743746
end
744-
return Admins
747+
748+
return admins
745749
end
746750

747751
e2function array playersSuperAdmins()
748-
local Admins = {}
749-
for _,ply in ipairs(player.GetAll()) do
750-
if (ply:IsSuperAdmin()) then
751-
table.insert(Admins,ply)
752+
local superadmins = {}
753+
754+
for _, ply in ipairs(player.GetAll()) do
755+
if ply:IsSuperAdmin() then
756+
table.insert(superadmins, ply)
752757
end
753758
end
754-
return Admins
759+
760+
return superadmins
755761
end
756762

757763
--------------------------------------------------------------------------------
758764

759765
e2function entity entity:aimEntity()
760-
if not IsValid(this) then return self:throw("Invalid entity!", nil) end
761-
if not this:IsPlayer() then return self:throw("Expected a Player, got Entity", nil) end
766+
if not IsValid(this) then return self:throw("Invalid entity!", NULL) end
767+
if not this:IsPlayer() then return self:throw("Expected a Player, got Entity", NULL) end
762768

763-
local ent = this:GetEyeTraceNoCursor().Entity
764-
if not ent:IsValid() then return nil end
765-
return ent
769+
return this:GetEyeTraceNoCursor().Entity
766770
end
767771

768772
e2function vector entity:aimPos()

0 commit comments

Comments
 (0)