-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
122 lines (104 loc) · 3.51 KB
/
server.lua
File metadata and controls
122 lines (104 loc) · 3.51 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
local dataPath = "data/shells.json"
-- Permission Check
local function IsAdmin(src)
if Config.UseAcePermissions then
return IsPlayerAceAllowed(src, "command." .. Config.CommandName)
else
local identifiers = GetPlayerIdentifiers(src)
for _, id in ipairs(identifiers) do
for _, adminId in ipairs(Config.AdminIdentifiers) do
if id == adminId then return true end
end
end
end
return false
end
-- Load shells from the JSON file
local function LoadShells()
local content = LoadResourceFile(GetCurrentResourceName(), dataPath)
if content then
return json.decode(content)
end
return {}
end
-- Save shells to the JSON file
local function SaveShells(data)
SaveResourceFile(GetCurrentResourceName(), dataPath, json.encode(data, { indent = true }), -1)
end
-- Initialize the database
Citizen.CreateThread(function()
local shells = LoadShells()
print("^4[Shell Management]^7 Loaded " .. #shells .. " persistent shells.")
end)
-- Callback to get all shells for the client
RegisterNetEvent("shellspawner:requestShells", function()
local src = source
local shells = LoadShells()
TriggerClientEvent("shellspawner:syncShells", src, shells)
end)
-- Request permission status for menu opening
RegisterNetEvent("shellspawner:checkPermission", function()
local src = source
if IsAdmin(src) then
TriggerClientEvent("shellspawner:openMenu", src)
else
print("^1[Shell Management]^7 Unauthorized access attempt by " .. GetPlayerName(src))
end
end)
-- Register a new shell
RegisterNetEvent("shellspawner:registerShell", function(shellData)
local src = source
if not IsAdmin(src) then return end
local shells = LoadShells()
-- Assign a unique ID if not present
shellData.id = #shells + 1
table.insert(shells, shellData)
SaveShells(shells)
TriggerClientEvent("shellspawner:syncShells", -1, shells) -- Sync to all
if Config.LogActions then
print("^2[Shell Management]^7 Registered new " .. shellData.model .. " at X: " .. string.format("%.2f", shellData.pos.x) .. " Y: " .. string.format("%.2f", shellData.pos.y))
end
end)
-- Delete a shell
RegisterNetEvent("shellspawner:deleteShell", function(id)
local src = source
if not IsAdmin(src) then return end
local shells = LoadShells()
local deletedIdx = nil
for i, shell in ipairs(shells) do
if shell.id == id then
deletedIdx = i
break
end
end
if deletedIdx then
table.remove(shells, deletedIdx)
SaveShells(shells)
TriggerClientEvent("shellspawner:syncShells", -1, shells) -- Sync to all
if Config.LogActions then
print("^1[Shell Management]^7 Deleted persistent shell ID: " .. id)
end
end
end)
-- Update a shell
RegisterNetEvent("shellspawner:updateShell", function(data)
local src = source
if not IsAdmin(src) then return end
local shells = LoadShells()
local updatedIdx = nil
for i, shell in ipairs(shells) do
if shell.id == data.id then
shells[i].pos = data.pos
shells[i].rot = data.rot
updatedIdx = i
break
end
end
if updatedIdx then
SaveShells(shells)
TriggerClientEvent("shellspawner:syncShells", -1, shells) -- Sync to all
if Config.LogActions then
print("^3[Shell Management]^7 Updated persistent shell ID: " .. data.id)
end
end
end)