Skip to content

Commit bb6f836

Browse files
authored
Small cleanups/code styling (#522)
Slightly optimizes and cleans up paste code
1 parent 5f89b31 commit bb6f836

File tree

1 file changed

+74
-82
lines changed

1 file changed

+74
-82
lines changed

lua/advdupe2/sv_clipboard.lua

Lines changed: 74 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -788,84 +788,72 @@ end
788788
Params: <player> Player, <table> data
789789
Returns: <entity> Entity, <table> data
790790
]]
791-
local function DoGenericPhysics(Entity, data, Player)
791+
local function DoGenericPhysics(entity, data, ply)
792+
if not data.PhysicsObjects then return end
792793

793-
if (not data) then return end
794-
if (not data.PhysicsObjects) then return end
795-
local Phys
796-
if (Player) then
797-
for Bone, Args in pairs(data.PhysicsObjects) do
798-
Phys = Entity:GetPhysicsObjectNum(Bone)
799-
if (IsValid(Phys)) then
800-
Phys:SetPos(Args.Pos)
801-
Phys:SetAngles(Args.Angle)
802-
Phys:EnableMotion(false)
803-
Player:AddFrozenPhysicsObject(Entity, Phys)
804-
end
805-
end
806-
else
807-
for Bone, Args in pairs(data.PhysicsObjects) do
808-
Phys = Entity:GetPhysicsObjectNum(Bone)
809-
if (IsValid(Phys)) then
810-
Phys:SetPos(Args.Pos)
811-
Phys:SetAngles(Args.Angle)
812-
Phys:EnableMotion(false)
794+
for bone, args in ipairs(data.PhysicsObjects) do
795+
local phys = entity:GetPhysicsObjectNum(bone)
796+
797+
if IsValid(phys) then
798+
phys:SetPos(args.Pos)
799+
phys:SetAngles(args.Angle)
800+
phys:EnableMotion(false)
801+
802+
if ply then
803+
ply:AddFrozenPhysicsObject(entity, phys)
813804
end
814805
end
815806
end
816807
end
817808

818-
local function reportclass(ply, class)
819-
net.Start("AdvDupe2_ReportClass")
820-
net.WriteString(class)
821-
net.Send(ply)
822-
end
823-
824-
local function reportmodel(ply, model)
825-
net.Start("AdvDupe2_ReportModel")
826-
net.WriteString(model)
827-
net.Send(ply)
828-
end
829-
830-
local strictConvar = GetConVar("AdvDupe2_Strict")
831-
832809
--[[
833810
Name: GenericDuplicatorFunction
834811
Desc: Override the default duplicator's GenericDuplicatorFunction function
835812
Params: <table> data, <player> Player
836813
Returns: <entity> Entity
837814
]]
838-
local function GenericDuplicatorFunction(data, Player)
815+
local strictConvar = GetConVar("AdvDupe2_Strict")
839816

840-
local Entity = ents.Create(data.Class)
841-
if (not IsValid(Entity)) then
842-
if (Player) then
843-
reportclass(Player, data.Class)
817+
local function GenericDuplicatorFunction(data, ply)
818+
if not util.IsValidModel(data.Model) then
819+
if ply then
820+
net.Start("AdvDupe2_ReportModel")
821+
net.WriteString(data.Model)
822+
net.Send(ply)
844823
else
845-
print("Advanced Duplicator 2 Invalid Class: " .. data.Class)
824+
print("Advanced Duplicator 2 Invalid Model: " .. data.Model)
846825
end
847-
return nil
826+
827+
return
848828
end
849829

850-
if (not util.IsValidModel(data.Model) and not file.Exists(data.Model, "GAME")) then
851-
if (Player) then
852-
reportmodel(Player, data.Model)
830+
local entity = ents.Create(data.Class)
831+
832+
if not IsValid(entity) then
833+
if ply then
834+
net.Start("AdvDupe2_ReportClass")
835+
net.WriteString(class)
836+
net.Send(ply)
853837
else
854-
print("Advanced Duplicator 2 Invalid Model: " .. data.Model)
838+
print("Advanced Duplicator 2 Invalid Class: " .. data.Class)
855839
end
856-
return nil
840+
841+
return
857842
end
858843

859-
duplicator.DoGeneric(Entity, data)
860-
if (Player) then Entity:SetCreator(Player) end
861-
Entity:Spawn()
862-
Entity:Activate()
863-
DoGenericPhysics(Entity, data, Player)
844+
duplicator.DoGeneric(entity, data)
845+
if ply then entity:SetCreator(ply) end
846+
847+
entity:Spawn()
848+
entity:Activate()
864849

865-
if (not strictConvar:GetBool()) then
866-
table.Add(Entity:GetTable(), data)
850+
DoGenericPhysics(entity, data, ply)
851+
852+
if not strictConvar:GetBool() then
853+
table.Add(entity:GetTable(), data)
867854
end
868-
return Entity
855+
856+
return entity
869857
end
870858

871859
--[[
@@ -874,43 +862,47 @@ end
874862
Params: <player> Player, <vector> Pos, <angle> Ang, <string> Model, <table> PhysicsObject, <table> Data
875863
Returns: <entity> Prop
876864
]]
877-
local function MakeProp(Player, Pos, Ang, Model, PhysicsObject, Data)
878-
879-
if Data.ModelScale then Data.ModelScale = math.Clamp(Data.ModelScale, 1e-5, 1e5) end
880-
881-
if (not util.IsValidModel(Model) and not file.Exists(Data.Model, "GAME")) then
882-
if (Player) then
883-
reportmodel(Player, Data.Model)
865+
local function MakeProp(ply, pos, ang, model, physicsobject, data)
866+
if data.ModelScale then Data.ModelScale = math.Clamp(data.ModelScale, 1e-5, 1e5) end
867+
868+
if not util.IsValidModel(model) then
869+
if ply then
870+
net.Start("AdvDupe2_ReportModel")
871+
net.WriteString(model)
872+
net.Send(ply)
884873
else
885-
print("Advanced Duplicator 2 Invalid Model: " .. Model)
874+
print("Advanced Duplicator 2 Invalid Model: " .. model)
886875
end
887-
return nil
876+
877+
return
888878
end
889879

890-
Data.Pos = Pos
891-
Data.Angle = Ang
892-
Data.Model = Model
893-
Data.Frozen = true
894-
-- Make sure this is allowed
895-
if (Player) then
896-
if (not gamemode.Call("PlayerSpawnProp", Player, Model)) then
897-
return false
880+
data.Pos = pos
881+
data.Angle = ang
882+
data.Model = model
883+
data.Frozen = true
884+
885+
if ply then
886+
if not gamemode.Call("PlayerSpawnProp", ply, model) then
887+
return
898888
end
899889
end
900890

901-
local Prop = ents.Create("prop_physics")
902-
if not IsValid(Prop) then return false end
891+
local prop = ents.Create("prop_physics")
892+
if not IsValid(prop) then return end
893+
894+
duplicator.DoGeneric(prop, data)
895+
896+
if ply then prop:SetCreator(ply) end
897+
prop:Spawn()
898+
899+
DoGenericPhysics(prop, data, ply)
903900

904-
duplicator.DoGeneric(Prop, Data)
905-
if (Player) then Prop:SetCreator(Player) end
906-
Prop:Spawn()
907-
Prop:Activate()
908-
DoGenericPhysics(Prop, Data, Player)
909-
if (Data.Flex) then
910-
duplicator.DoFlex(Prop, Data.Flex, Data.FlexScale)
901+
if data.Flex then
902+
duplicator.DoFlex(prop, data.Flex, data.FlexScale)
911903
end
912904

913-
return Prop
905+
return prop
914906
end
915907

916908
local function RestoreBodyGroups(ent, BodyG)

0 commit comments

Comments
 (0)