-
Notifications
You must be signed in to change notification settings - Fork 30
/
server.lua
192 lines (162 loc) · 5.51 KB
/
server.lua
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
ESX = nil
QBCore = nil
RegisterUsableItem = nil
Initialized()
local stancer = {}
local sql = setmetatable({},{
__call = function(self)
self.insert = function(plate,...)
local str = 'INSERT INTO %s (%s, %s) VALUES(?, ?)'
return MySQL.insert.await(str:format('renzu_stancer','plate','setting'),{plate,...})
end
self.save = function(string, data)
local str = 'UPDATE %s SET setting = ? WHERE %s = ?'
local isExist = self.query(string)
if isExist[1] then
return MySQL.update(str:format('renzu_stancer','plate'),{data,string})
else
return self.insert(string,data)
end
end
self.query = function(string)
local str = 'SELECT * FROM %s WHERE %s = ?'
return MySQL.query.await(str:format('renzu_stancer','plate'),{string})
end
self.fetch = function()
return MySQL.query.await('SELECT * FROM renzu_stancer')
end
return self
end
})
local db = sql()
Citizen.CreateThreadNow(function()
local success, result = pcall(MySQL.scalar.await, 'SELECT 1 FROM renzu_stancer')
if not success then
MySQL.query.await([[CREATE TABLE `renzu_stancer` (
`plate` varchar(128) NOT NULL,
`setting` longtext DEFAULT NULL
)]])
print("^2SQL INSTALL SUCCESSFULLY ^0")
end
local backup = GetResourceKvpString('stancer')
if backup then
local data = json.decode(backup)
for plate,v in pairs(data) do
if v.setting.wheelsetting then
stancer[plate] = v.setting.wheelsetting
end
end
end
local fetch = db.fetch()
for k,v in pairs(fetch) do
stancer[v.plate] = json.decode(v.setting)
end
for k, vehicle in ipairs(GetAllVehicles()) do
local plate = GetVehicleNumberPlateText(vehicle)
if stancer[plate] then
Entity(vehicle).state:set('stancer', stancer[plate], true)
Wait(0)
end
end
end)
function SaveStancer(data)
local result = db.query(data.plate)
if result[1] == nil then
db.save(plate,json.encode(data.setting))
elseif result[1] then
db.save(plate,json.encode(data.setting))
end
end
function firstToUpper(str) return (str:gsub("^%l", string.upper)) end
Citizen.CreateThread(function()
if Config.Framework ~= 'Standalone' then
for k, v in pairs(Config.items) do
local stanceritem = string.lower(v)
RegisterUsableItem(stanceritem, function(source)
local xPlayer = GetPlayerFromId(source)
local veh = GetVehiclePedIsIn(GetPlayerPed(source), false)
if stanceritem ~= nil and veh ~= 0 then
xPlayer.removeInventoryItem(stanceritem, 1)
AddStancerKit(veh)
end
end)
end
end
print(" STANCER LOADED ")
end)
function AddStancerKit(veh)
local veh = veh
if veh == nil then veh = GetVehiclePedIsIn(GetPlayerPed(source), false) end
local plate = GetVehicleNumberPlateText(veh)
print("ADD")
if not stancer[plate] then
stancer[plate] = {}
local ent = Entity(veh).state
if not ent.stancer then
ent:set('stancer',{},true)
db.save(plate,'[]')
end
end
end
exports('AddStancerKit', function(vehicle) return AddStancerKit(vehicle) end)
local servervehicles = {}
local callbacks = {}
registercallback = function(name,cb) -- create callbacks. so we wont have much convertion for other frameworks
callbacks[name] = cb
end
RegisterNetEvent('renzu_stancer:servercallback', function(name,...)
local source = source
TriggerClientEvent('renzu_stancer:servercallback',source,name,callbacks[name](source,...))
end)
registercallback('stancers', function(source,plate)
local plate = plate
return stancer[plate]
end)
SetStancer = function(entity)
if DoesEntityExist(entity) and GetEntityPopulationType(entity) == 7 and GetEntityType(entity) == 2 then
local plate = GetVehicleNumberPlateText(entity)
if stancer[plate] then
Entity(entity).state:set('stancer',stancer[plate],true)
end
end
end
AddStateBagChangeHandler('VehicleProperties' --[[key filter]], nil --[[bag filter]], function(bagName, key, value, _unused, replicated)
Wait(0)
local net = tonumber(bagName:gsub('entity:', ''), 10)
if not value then return end
local entity = NetworkGetEntityFromNetworkId(net)
Wait(1000)
if DoesEntityExist(entity) then
SetStancer(entity) -- compatibility with ESX onesync server setter vehicle spawn
end
end)
AddEventHandler('entityCreated', function(entity)
local entity = entity
Wait(1000)
SetStancer(entity)
end)
AddEventHandler('entityRemoved', function(entity)
local entity = entity
if DoesEntityExist(entity) and GetEntityPopulationType(entity) == 7 and GetEntityType(entity) == 2 then
local ent = Entity(entity).state
if ent.stancer then
local plate = GetVehicleNumberPlateText(entity)
db.save(plate,json.encode(ent.stancer))
end
end
end)
RegisterNetEvent("renzu_stancer:save")
AddEventHandler("renzu_stancer:save", function(stance)
local vehicle = GetVehiclePedIsIn(GetPlayerPed(source), false)
local ent = Entity(vehicle).state
if ent.stancer then
local plate = GetVehicleNumberPlateText(vehicle)
if not stancer[plate] then stancer[plate] = {} end
stancer[plate] = stance
db.save(plate,json.encode(ent.stancer))
end
end)
RegisterNetEvent("renzu_stancer:addstancer") -- Standalone Purpose
AddEventHandler("renzu_stancer:addstancer",function(vehicle)
AddStancerKit(vehicle)
end)