Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local functions =
- {
- AddVelocity = { args = {"Vector"}, server = function(ent, vec) if ent:IsPlayer() then ent:SetVelocity(vec) return end local phys = ent:GetPhysicsObject() if phys:IsValid() then phys:AddVelocity(vec) end end },
- SetVelocity = { args = {"Vector"}, server = function(ent, args) if ent:IsPlayer() then ent:SetVelocity(vec) return end local phys = ent:GetPhysicsObject() if phys:IsValid() then phys:SetVelocity(vec) end end },
- SetPos = { args = {"Vector"}, server = function(ent, vec) ent:SetPos(vec) end },
- }
- local function check_args(args, ...)
- for i = 1, #args do
- local t = type(select(i, ...))
- if t ~= args[i] then
- if CLIENT then
- error(("%q expected got %q"):format(args[i], t), 3)
- else
- return false
- end
- end
- end
- return true
- end
- if CLIENT then
- local function run(typ, ...)
- RunConsoleCommand("hm", typ, ...)
- end
- local META = {}
- META.__index = META
- for key, data in pairs(functions) do
- META[key] = function(self, ...)
- if not self:IsValid() then
- setmetatable(self, getmetatable(NULL))
- return
- end
- check_args(data.args, ...)
- if data.server then
- net.Start("server_entity")
- net.WriteTable({key, self.__entid, ...})
- net.SendToServer()
- end
- if data.client then
- data.client(self:GetEntity(), ...)
- end
- end
- end
- for func_name, func in pairs(FindMetaTable("Entity")) do
- META[func_name] = META[func_name] or function(self, ...)
- if self:IsValid() then
- return func(self:GetEntity(), ...)
- end
- end
- end
- function META:GetEntity()
- return Entity(self.__entid)
- end
- function META:IsValid()
- return self:GetEntity():IsValid()
- end
- function ServerEntity(id)
- if IsEntity(id) and id:IsValid() then
- id = id:EntIndex()
- end
- return setmetatable({__entid = id}, META)
- end
- end
- if SERVER then
- util.AddNetworkString("server_entity")
- net.Receive("server_entity", function(len, ply)
- local args = net.ReadTable()
- local func_name = args[1]
- local ent = Entity(args[2]) or NULL
- if not ent:IsValid() then return end
- table.remove(args, 1)
- table.remove(args, 1)
- if functions[func_name] and IsEntity(ent) and ent:IsValid() and (ent == ply or ent:CPPICanTool(ply)) and check_args(functions[func_name].args, unpack(args)) then
- functions[func_name].server(ent, unpack(args))
- else
- ErrorNoHalt(("bogus data received from %q (%s)"):format(ply:Nick(), ply:SteamID()))
- PrintTable({player = ply, func_name = func_name, entity = ent, arguments = args})
- end
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement