Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lua/notagain.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
AddCSLuaFile()

if SERVER then
util.AddNetworkString("userdata")
util.AddNetworkString("userdata_broadcast")
end

local root_dir = "notagain"
notagain = notagain or {}
notagain.loaded_libraries = notagain.loaded_libraries or {}
Expand Down
111 changes: 54 additions & 57 deletions lua/notagain/essential/libraries/userdata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,60 @@ local function load(key)
end
end

if SERVER then
net.Receive("userdata", function(len, ply)
local key = net.ReadString()
if not userdata.known[key] then return end

local val = net.ReadType()

if userdata.known[key].callback then
local ok, err = pcall(userdata.known[key].callback, ply, val)
if not ok then
print("userdata error: ", err)
end
end

userdata.players[ply:UniqueID()] = userdata.players[ply:UniqueID()] or {}
userdata.players[ply:UniqueID()][key] = val

net.Start("userdata_broadcast")
net.WriteEntity(ply)
net.WriteString(key)
net.WriteType(val)
net.SendOmit(ply)
end)

hook.Add("PlayerFullLoad", "userdata", function(ply)
userdata.players[ply:UniqueID()] = userdata.players[ply:UniqueID()] or {}

for _, other in pairs(player.GetAll()) do
if ply == other then continue end

userdata.players[other:UniqueID()] = userdata.players[other:UniqueID()] or {}

local data = userdata.players[other:UniqueID()]
for key, val in pairs(data) do
net.Start("userdata_broadcast")
net.WriteEntity(other)
net.WriteString(key)
net.WriteType(val)
net.Send(ply)
end
end
end)

hook.Add("EntityRemoved", "userdata", function(ply)
if not ply:IsValid() or not ply:IsPlayer() then return end

userdata.players[ply:UniqueID()] = nil
end)

for _, ply in ipairs(player.GetAll()) do
userdata.players[ply:UniqueID()] = {}
end
end

function userdata.SetupConVar(key, default, callback)
userdata.known[key] = {
default = default,
Expand Down Expand Up @@ -151,61 +205,4 @@ if CLIENT then
end)
end

if SERVER then
util.AddNetworkString("userdata")
util.AddNetworkString("userdata_broadcast")

net.Receive("userdata", function(len, ply)
local key = net.ReadString()
if not userdata.known[key] then return end

local val = net.ReadType()

if userdata.known[key].callback then
local ok, err = pcall(userdata.known[key].callback, ply, val)
if not ok then
print("userdata error: ", err)
end
end

userdata.players[ply:UniqueID()] = userdata.players[ply:UniqueID()] or {}
userdata.players[ply:UniqueID()][key] = val

net.Start("userdata_broadcast")
net.WriteEntity(ply)
net.WriteString(key)
net.WriteType(val)
net.SendOmit(ply)
end)

hook.Add("PlayerFullLoad", "userdata", function(ply)
userdata.players[ply:UniqueID()] = userdata.players[ply:UniqueID()] or {}

for _, other in pairs(player.GetAll()) do
if ply == other then continue end

userdata.players[other:UniqueID()] = userdata.players[other:UniqueID()] or {}

local data = userdata.players[other:UniqueID()]
for key, val in pairs(data) do
net.Start("userdata_broadcast")
net.WriteEntity(other)
net.WriteString(key)
net.WriteType(val)
net.Send(ply)
end
end
end)

hook.Add("EntityRemoved", "userdata", function(ply)
if not ply:IsValid() or not ply:IsPlayer() then return end

userdata.players[ply:UniqueID()] = nil
end)

for _, ply in ipairs(player.GetAll()) do
userdata.players[ply:UniqueID()] = {}
end
end

return userdata