forked from DaniSalam4eto/SAFS_FREECAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
66 lines (59 loc) · 2.1 KB
/
server.lua
File metadata and controls
66 lines (59 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local resourceName = GetCurrentResourceName()
local fileName = "presets.json"
local presets = {}
local function loadPresets()
local raw = LoadResourceFile(resourceName, fileName)
if not raw or raw == "" then
presets = {}
SaveResourceFile(resourceName, fileName, json.encode(presets, {indent=true}), -1)
return
end
local ok, data = pcall(json.decode, raw)
if ok and type(data) == "table" then
presets = data
else
presets = {}
end
end
local function savePresets()
SaveResourceFile(resourceName, fileName, json.encode(presets, {indent=true}), -1)
end
local function ownerKeyFor(src)
local num = GetNumPlayerIdentifiers(src) or 0
local fallback = "src:"..tostring(src)
local best = nil
for i=0,num-1 do
local id = GetPlayerIdentifier(src, i)
if id and id ~= "" then
if string.sub(id,1,8) == "license:" then return id end
if string.sub(id,1,6) == "steam:" then best = best or id end
if string.sub(id,1,8) == "discord:" then best = best or id end
end
end
return best or fallback
end
AddEventHandler("onResourceStart", function(res)
if res == resourceName then
loadPresets()
end
end)
RegisterNetEvent("fc:savePreset", function(vehName, preset)
local src = source
if type(vehName) ~= "string" or type(preset) ~= "table" or type(preset.name) ~= "string" then return end
local owner = ownerKeyFor(src)
local vkey = string.lower(vehName)
local skey = string.lower(preset.name)
presets[owner] = presets[owner] or {}
presets[owner][vkey] = presets[owner][vkey] or {}
presets[owner][vkey][skey] = preset
savePresets()
end)
RegisterNetEvent("fc:requestPreset", function(vehName, slot)
local src = source
if type(vehName) ~= "string" or type(slot) ~= "string" then return end
local owner = ownerKeyFor(src)
local vkey = string.lower(vehName)
local skey = string.lower(slot)
local p = presets[owner] and presets[owner][vkey] and presets[owner][vkey][skey] or nil
TriggerClientEvent("fc:applyPreset", src, p, vehName)
end)