forked from Gravxd/enginesound-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
293 lines (257 loc) · 10.2 KB
/
client.lua
File metadata and controls
293 lines (257 loc) · 10.2 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
local DisplayLabels = {}
Config.EngineSounds["Restore Default"] = "resetenginesound" -- hash is invalid so game resets to base sound
for k, _ in pairs(Config.EngineSounds) do
DisplayLabels[#DisplayLabels + 1] = k
end
local format = string.format
local GetEntityModel = GetEntityModel
local storeSoundsForModel = GetResourceKvpInt("storeSoundsForModel") ~= 0 and true or Config.StoreSoundsByModel
lib.print.debug("Store sounds for model: ", storeSoundsForModel)
local hasPermissions = lib.callback.await("GLabs:EngineSounds:GetPerms", false)
local Index = 1
local Favourites = {}
local function loadFavourites()
local jsonFavourites = GetResourceKvpString("favouriteEngineSounds")
if jsonFavourites then
Favourites = json.decode(jsonFavourites)
end
end
local function saveFavourites()
SetResourceKvp("favouriteEngineSounds", json.encode(Favourites))
end
loadFavourites()
local function showEngineSoundMenu()
lib.setMenuOptions(
"engine_sound_menu",
{
label = "Change Engine Sound",
icon = "arrows-up-down-left-right",
values = DisplayLabels,
defaultIndex = Index,
close = false
},
1
)
lib.setMenuOptions("engine_sound_menu",
{ label = "Store Sounds Per Model", description =
"If you use an engine sound on a vehicle it will save, if you spawn this vehicle again, it will restore that last used engine sound.", checked =
storeSoundsForModel, icon = "fa-solid fa-floppy-disk" }, 4)
lib.showMenu("engine_sound_menu")
end
local function showRemoveFavouritesMenu()
local removeOptions = {}
for fav, _ in pairs(Favourites) do
removeOptions[#removeOptions + 1] = { label = fav, icon = 'trash' }
end
lib.registerMenu(
{
id = "remove_favourites_menu",
title = "Remove from Favourites",
position = Config.MenuPosition,
options = removeOptions,
onClose = function()
showFavouritesMenu()
end
},
function(selected)
local soundToRemove = removeOptions[selected].label
Favourites[soundToRemove] = nil
saveFavourites()
Config.Notify("Engine sound removed from favourites!", "success")
if next(Favourites) == nil then
Config.Notify("You have no more favourites!", "info")
showEngineSoundMenu()
else
showRemoveFavouritesMenu()
end
end
)
lib.showMenu("remove_favourites_menu")
end
local function IsRestricted()
if LocalPlayer.state.dead then
return true
end
return false
end
function showFavouritesMenu()
if next(Favourites) == nil then
Config.Notify("You don't have any favourites!", "error")
return
end
local favouriteOptions = {}
for fav, _ in pairs(Favourites) do
favouriteOptions[#favouriteOptions + 1] = { label = fav, icon = 'star' }
end
favouriteOptions[#favouriteOptions + 1] = { label = "Remove from Favourites", icon = "trash" }
lib.registerMenu(
{
id = "favourites_menu",
title = "Favourites",
position = Config.MenuPosition,
options = favouriteOptions,
onClose = function()
showEngineSoundMenu()
end
},
function(selected)
local selectedFav = favouriteOptions[selected].label
if selectedFav == "Remove from Favourites" then
showRemoveFavouritesMenu()
else
if not cache.vehicle or cache.seat ~= -1 then
return Config.Notify("You need to be driving a vehicle to use this!", "error")
end
if IsRestricted() then
return Config.Notify("You aren't able to use this right now!", "error")
end
local success = changeSoundForVehicle(cache.vehicle, Config.EngineSounds[selectedFav], selectedFav)
if not success then return end
Config.Notify(string.format("Engine sound changed to: %s", selectedFav), "success")
lib.showMenu("favourites_menu")
end
end
)
lib.showMenu("favourites_menu")
end
function changeSoundForVehicle(vehicle, sound, label)
if IsVehicleSirenOn(vehicle) then
-- weird bug with LVC that caused sirens to emit noise whilst lights disabled
Config.Notify("You can't change the engine sound while the siren is on!", "error")
return false
end
TriggerServerEvent(
"GLabs:EngineSounds:ChangeEngineSound",
{
net = VehToNet(vehicle),
sound = sound,
label = label
}
)
return true
end
local storedModelSounds = GetResourceKvpString("storedModelSounds") and
json.decode(GetResourceKvpString("storedModelSounds")) or {}
lib.onCache("vehicle", function(value)
if not hasPermissions then return end
if not value then return end
if not storeSoundsForModel then return end
local driverPed = GetPedInVehicleSeat(value, -1)
if driverPed ~= cache.ped then
lib.print.debug("I am not driver, do not set engine sound.")
return
end
local vehicleModel = GetEntityModel(value)
lib.print.debug(format("[vehicleChange] Model: %s - %s - %s", vehicleModel, storedModelSounds[vehicleModel],
storedModelSounds[tostring(vehicleModel)]))
local savedModelSound = storedModelSounds[tostring(vehicleModel)]
if savedModelSound then
lib.print.debug(format("Found sound for model %s: %s", vehicleModel, savedModelSound.label))
changeSoundForVehicle(value, savedModelSound.sound, savedModelSound.label)
end
end)
lib.registerMenu(
{
id = "engine_sound_menu",
title = "Engine Sound Menu",
position = Config.MenuPosition,
onSideScroll = function(_, scrollIndex)
Index = scrollIndex
end,
onCheck = function(selected, checked, args)
if selected == 4 then
-- store sounds on models
storeSoundsForModel = checked
SetResourceKvpInt("storeSoundsForModel", storeSoundsForModel and 1 or 0)
if storeSoundsForModel then
Config.Notify("Engine sounds will now be stored per model!", "success")
else
Config.Notify("Engine sounds will no longer be stored per model!", "error")
end
end
end,
options = {
{ label = "Change Engine Sound", icon = "arrows-up-down-left-right", values = DisplayLabels },
{ label = "Add to Favourites", icon = "heart" },
{ label = "View Favourites", icon = "star" },
{ label = "Store Sounds Per Model", description = "If you use an engine sound on a vehicle it will save, if you spawn this vehicle again, it will restore that last used engine sound.", checked = storeSoundsForModel, icon = "fa-solid fa-floppy-disk" }
}
},
function(selected, scrollIndex)
if selected == 1 then
-- change engine sound
if not cache.vehicle or cache.seat ~= -1 then
return Config.Notify("You need to be driving a vehicle to use this!", "error")
end
if IsRestricted() then
return Config.Notify("You aren't able to use this right now!", "error")
end
local success = changeSoundForVehicle(cache.vehicle, Config.EngineSounds[DisplayLabels[scrollIndex]],
DisplayLabels[scrollIndex])
if not success then return end
if storeSoundsForModel then
local vehicleModel = GetEntityModel(cache.vehicle)
if DisplayLabels[scrollIndex] == "Restore Default" then
storedModelSounds[tostring(vehicleModel)] = nil
else
storedModelSounds[tostring(vehicleModel)] = {
sound = Config.EngineSounds[DisplayLabels[scrollIndex]],
label = DisplayLabels[scrollIndex]
}
end
SetResourceKvp("storedModelSounds", json.encode(storedModelSounds))
lib.print.debug(format("Updated sound for model %s: %s", vehicleModel, DisplayLabels[scrollIndex]))
end
Config.Notify(string.format("Engine sound changed to: %s", DisplayLabels[scrollIndex]), "success")
elseif selected == 2 then
-- add to favourites
local soundName = DisplayLabels[Index]
if soundName == "Restore Default" then
Config.Notify("You can't favourite the default engine sound!", "error")
return
end
if not Favourites[soundName] then
Favourites[soundName] = true
saveFavourites()
Config.Notify("Engine sound added to favourites!", "success")
else
Config.Notify("This engine sound is already in your favourites!", "error")
end
showEngineSoundMenu()
elseif selected == 3 then
-- view favourites
showFavouritesMenu()
end
end
)
RegisterNetEvent(
"GLabs:EngineSounds:OpenMenu",
function()
hasPermissions = true -- incase they get permissions added after startup
if not cache.vehicle or cache.seat ~= -1 then
return Config.Notify("You need to be driving a vehicle to use this!", "error")
end
showEngineSoundMenu()
end
)
AddStateBagChangeHandler(
"vehdata:sound",
nil,
function(bagName, _, value)
local entity = GetEntityFromStateBagName(bagName)
if entity == 0 then return end
if not IsEntityAVehicle(entity) then return end
ForceUseAudioGameObject(entity, value)
end
)
lib.addKeybind(
{
name = "open_enginesound_menu",
description = "Open Engine Sound Menu",
defaultKey = Config.Keybind,
onPressed = function()
ExecuteCommand("enginesound")
end
}
)
TriggerEvent("chat:addSuggestion", "/enginesound", "Open the Engine Sound Menu!")