-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
469 lines (450 loc) · 22.1 KB
/
Copy pathmain.lua
File metadata and controls
469 lines (450 loc) · 22.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
local Core = exports.vorp_core:GetCore()
local talktoprompt = GetRandomIntInRange(0, 0xffffff)
local decision = GetRandomIntInRange(0, 0xffffff)
local progressbar = exports.vorp_progressbar:initiate()
local Menu = exports.vorp_menu:GetMenuData()
local StartNPCsOnce = false
local generalCooldown = Config.Cooldown
local npcCooldowns = {} --table for every NPC if generalCooldown = false
local jobs = {}
local hasjob = false
local talking = false
local talkingTime = Config.talkingTime --sec how long you are "talkin"
local MenuTriggered = false
local WeaponChoosed = false
local selectedWeapon = nil
local WeaponID = nil
function StartNPCs() --start function after user selected the character
Citizen.Wait(1000)
for i, v in ipairs(Config.NPCs) do
local x, y, z = table.unpack(v.coords)
local hashModel = GetHashKey(v.npcmodel) -- loading NPC skin
if IsModelValid(hashModel) then
RequestModel(hashModel)
while not HasModelLoaded(hashModel) do
Wait(100)
end
else
print(v.npcmodel .. " is not valid")
end
local npc = CreatePed(hashModel, x, y, z, v.heading, false, true, true, true) -- spawn NPC
Citizen.InvokeNative(0x283978A15512B2FE, npc, true) -- SetRandomOutfitVariation
SetEntityNoCollisionEntity(PlayerPedId(), npc, false)
SetEntityCanBeDamaged(npc, false) --NPC can't be damaged
SetEntityInvincible(npc, true)
Wait(1000)
FreezeEntityPosition(npc, true) -- NPC can't escape
SetBlockingOfNonTemporaryEvents(npc, true) -- NPC can't be scared
if not Config.generalCooldown then -- set cooldown for every NPC
if v.cooldown ~= nil then
npcCooldowns[i] = v.cooldown
else
npcCooldowns[i] = 0
end
if Config.Debug then --if debug = true print cooldowns on start
print("Loading NPC:", v.npc_name, "Cooldown:", v.cooldown)
end
else
npcCooldowns[i] = Config.Cooldown
if Config.Debug then
print("Loading NPC:", v.npc_name, "Cooldown = general Cooldown")
end
end
if v.blip ~= 0 then --create blip
local blip = Citizen.InvokeNative(0x554D9D53F696D002, 1664425300, x, y, z)
SetBlipSprite(blip, v.blip, true)
Citizen.InvokeNative(0x9CB1A1623062F402, blip, v.npc_name)
end
if v.scenario then --start scenario in place and loop it
TaskStartScenarioInPlace(npc, GetHashKey(v.scenario), 0, true, false, false, false)
end
if v.anim.animDict and v.anim.animName then --play animation in loop
RequestAnimDict(v.anim.animDict)
while not HasAnimDictLoaded(v.anim.animDict) do
Citizen.Wait(100)
end
TaskPlayAnim(npc, v.anim.animDict, v.anim.animName, 1.0, -1.0, -1, 1, 0, true, 0, false, 0, false)
end
end
end
Citizen.CreateThread(function() --creating npc interaction prompts
Citizen.Wait(5000)
talktonpc = PromptRegisterBegin()
PromptSetControlAction(talktonpc, Config.keys["G"])
talkLabel = CreateVarString(10, 'LITERAL_STRING', Config.Language.press)
PromptSetText(talktonpc, talkLabel)
PromptSetEnabled(talktonpc, 1)
PromptSetVisible(talktonpc, 1)
PromptSetStandardMode(talktonpc, 1)
PromptSetGroup(talktonpc, talktoprompt)
Citizen.InvokeNative(0xC5F428EE08FA7F2C, talktonpc, true)
PromptRegisterEnd(talktonpc)
noPrompt = PromptRegisterBegin()
PromptSetControlAction(noPrompt, Config.keys["DOWN"])
noLabel = CreateVarString(10, 'LITERAL_STRING', Config.Language.noLabel)
PromptSetText(noPrompt, noLabel)
PromptSetEnabled(noPrompt, 1)
PromptSetVisible(noPrompt, 1)
PromptSetStandardMode(noPrompt, 1)
PromptSetGroup(noPrompt, decision)
Citizen.InvokeNative(0xC5F428EE08FA7F2C, noPrompt, true)
PromptRegisterEnd(noPrompt)
yesPrompt = PromptRegisterBegin()
PromptSetControlAction(yesPrompt, Config.keys["UP"])
yesLabel = CreateVarString(10, 'LITERAL_STRING', Config.Language.yesLabel)
PromptSetText(yesPrompt, yesLabel)
PromptSetEnabled(yesPrompt, 1)
PromptSetVisible(yesPrompt, 1)
PromptSetStandardMode(yesPrompt, 1)
PromptSetGroup(yesPrompt, decision)
Citizen.InvokeNative(0xC5F428EE08FA7F2C, yesPrompt, true)
PromptRegisterEnd(yesPrompt)
end)
Citizen.CreateThread(function()
while true do
if LocalPlayer.state.IsInSession then
if not StartNPCsOnce then --loading NPCs once if player choosed a char and joins game-session
if Config.Debug then
print("Player joind game-session. Starting NPCs ...")
end
StartNPCs()
StartNPCsOnce = true
end
end
local sleep = true
local _source = source
for i, v in ipairs(Config.NPCs) do --check every NPC
local playerCoords = GetEntityCoords(PlayerPedId())
if Vdist(playerCoords, v.coords) <= v.radius and not talking then -- checking distance between player and NPC
if v.type ~= "nointeraction" then
sleep = false
local label = CreateVarString(10, 'LITERAL_STRING', Config.Language.talk)
PromptSetActiveGroupThisFrame(talktoprompt, label) --loads talktoNPC prompt
if Citizen.InvokeNative(0xC92AC953F0A982AE, talktonpc) then --when button is pressed
if (generalCooldown > 0 and Config.generalCooldown) or (npcCooldowns[i] > 0 and v.cooldown ~= nil) then
PromptRemoveGroup(talktoprompt)
Core.NotifyBottomRight(Config.Language.onCooldown, 4000)
Wait(4000)
else
talking = true
talkingTime = Config.talkingTime
end
if v.joblocked and #v.joblocked > 0 then --if joblocked has at least 1 entry then
jobs = v.joblocked
TriggerServerEvent("juSa_npc_rewards:jobcheck", jobs) --check the jobs
while hasjob == nil do
Wait(10)
end
if Config.Debug then
print("You need: ")
for i, job in ipairs(jobs) do
if job and job.name then
print("Job " .. i .. ": " .. job.name .. ", Grade: " .. tostring(job.grade))
else
print("Job " .. i .. " is nil or has no valid job name.")
end
end
end
Wait(100)
if not hasjob then --wait for results and if player has not correct job he can not talk to NPC
talking = false
PromptRemoveGroup(talktoprompt)
Core.NotifyBottomRight(Config.Language.wrongJob, 4000)
Wait(4000)
end
elseif v.joblocked == nil then --if no job required
hasjob = true
end
end
end
elseif Vdist(playerCoords, v.coords) <= v.radius and talking and hasjob then --if talking to a NPC, show decision
if v.type == "give" then
sleep = false
local playerPed = PlayerPedId()
FreezeEntityPosition(playerPed,true) --player cant move while performing animation
TaskStartScenarioInPlace(playerPed, GetHashKey("WORLD_HUMAN_BADASS"), v.taskbar, true, false, false, false) --Taskbar-Animation (change the text in "" if u want to have another animation instead)
progressbar.start(Config.Language.getting_item, v.taskbar, nil, linear)
Citizen.Wait(v.taskbar)
TriggerServerEvent("juSa_npc_rewards:give", v.giveitem, v.givemoney, v.giveweapon, v.usewebhook, v.npc_name) -- give item/money
ClearPedTasks(playerPed)
FreezeEntityPosition(playerPed,false)
Reset()
if Config.generalCooldown then
NewCooldowns()
else
npcCooldowns[i] = v.cooldown or 0
end
elseif v.type == "sell" then
sleep = false
local itemInfo = ""
for _, item in ipairs(v.takeitem) do
itemInfo = itemInfo .. tostring(item.amount) .. "x " .. item.label .. ", "
end
itemInfo = string.sub(itemInfo, 1, -3) -- removes trailing comma and space
local info = string.format(Config.Language.sellinfo, itemInfo, v.givemoney)
local label = CreateVarString(10, 'LITERAL_STRING', info)
PromptRemoveGroup(talktoprompt) --unload talktonpc prompt
PromptSetActiveGroupThisFrame(decision, label) --loads decision prompt
if Citizen.InvokeNative(0xC92AC953F0A982AE, yesPrompt) then
TriggerServerEvent("juSa_npc_rewards:infosell", v.takeitem, v.givemoney, v.taskbar, v.usewebhook, v.npc_name)
Wait(v.taskbar)
Reset()
if Config.generalCooldown then
NewCooldowns()
else
npcCooldowns[i] = v.cooldown or 0
end
elseif Citizen.InvokeNative(0xC92AC953F0A982AE, noPrompt) then
PromptRemoveGroup(decision)
Core.NotifyBottomRight(Config.Language.seeUlater,4000)
Wait(4000)
Reset()
end
elseif v.type == "exchange" then
sleep = false
local takeItemInfo = ""
if v.takeitem and #v.takeitem > 0 then
for _, item in ipairs(v.takeitem) do
takeItemInfo = takeItemInfo .. tostring(item.amount) .. "x " .. item.label .. ", "
end
else
takeItemInfo = Config.Language.noitemneeded
end
takeItemInfo = string.sub(takeItemInfo, 1, -3) -- remove the trailing comma and space
local giveItemInfo = ""
if v.giveitem and #v.giveitem > 0 then
for _, item in ipairs(v.giveitem) do
giveItemInfo = giveItemInfo .. tostring(item.amount) .. "x " .. item.label .. ", "
end
else
giveItemInfo = Config.Language.noitemrec
end
if not v.giveweapon == nil then -- check if giveweapon has items
for _, weapon in ipairs(v.giveweapon) do
giveItemInfo = giveItemInfo .. "1x " .. weapon.label .. ", "
end
end
giveItemInfo = string.sub(giveItemInfo, 1, -3)
local info = string.format(Config.Language.exchangeinfo, takeItemInfo, v.takemoney, giveItemInfo, v.givemoney)
local label = CreateVarString(10, 'LITERAL_STRING', info)
PromptRemoveGroup(talktoprompt) --unload talktonpc prompt
PromptSetActiveGroupThisFrame(decision, label) --loads decision prompt
if Citizen.InvokeNative(0xC92AC953F0A982AE, yesPrompt) then
TriggerServerEvent("juSa_npc_rewards:infoexchange", v.giveitem, v.takeitem, v.giveweapon, v.givemoney, v.takemoney, v.taskbar, v.usewebhook, v.npc_name)
Wait(v.taskbar)
Reset()
if Config.generalCooldown then
NewCooldowns()
else
npcCooldowns[i] = v.cooldown or 0
end
elseif Citizen.InvokeNative(0xC92AC953F0A982AE, noPrompt) then
PromptRemoveGroup(decision)
Core.NotifyBottomRight(Config.Language.seeUlater, 4000)
Wait(4000)
Reset()
end
elseif v.type == "sell_weapon" then
sleep = false
if not MenuTriggered then
TriggerServerEvent("juSa_npc_rewards:getAllWeaponsForMenu") --get all weapons in players inv
Wait(100)
elseif MenuTriggered and not WeaponChoosed then
if Config.Debug then
print("Waiting for the player to select a weapon from the menu ...")
end
Wait(100) --wait until menu gets triggered and player choosed a weapon
elseif MenuTriggered and WeaponChoosed then --when player choosed a weapon in menu to sell
local canSell = false
local weaponlabel = ""
local weaponreward = nil
local weapon
for i, w in ipairs(v.takeweapon) do --check if NPC buys that weapon
if w.weaponname == selectedWeapon then
weapon = w
weaponlabel = w.label
weaponreward = w.givemoney
canSell = true
break
end
end
if not canSell then --feedback NPC does not buy that kind of weapon
Core.NotifyBottomRight(Config.Language.notBuyingWeapon,4000)
Wait(4000)
Reset()
else
local itemInfo = ""
if weapon.giveitems then
for _, item in ipairs(weapon.giveitems) do
itemInfo = itemInfo .. tostring(item.amount) .. "x " .. item.label .. ", "
end
else
if Config.Debug then
print("No items available for this weapon in the config.")
end
end
itemInfo = string.sub(itemInfo, 1, -3) -- removes trailing comma and space
local info = string.format(Config.Language.sellWeaponInfo, weapon.label, itemInfo, weapon.givemoney)
local label = CreateVarString(10, 'LITERAL_STRING', info)
PromptRemoveGroup(talktoprompt) --unload talktonpc prompt
PromptSetActiveGroupThisFrame(decision, label) --loads decision prompt
if Citizen.InvokeNative(0xC92AC953F0A982AE, yesPrompt) then
TriggerServerEvent("juSa_npc_rewards:infosell_weapon", v.taskbar, v.usewebhook, v.npc_name, weapon, WeaponID)
Wait(v.taskbar)
Reset()
if Config.generalCooldown then
NewCooldowns()
else
npcCooldowns[i] = v.cooldown or 0
end
elseif Citizen.InvokeNative(0xC92AC953F0A982AE, noPrompt) then
PromptRemoveGroup(decision)
Core.NotifyBottomRight(Config.Language.seeUlater,4000)
Wait(4000)
Reset()
end
end
end
end
PromptRemoveGroup(talktoprompt)
PromptRemoveGroup(decision)
end
end
if sleep then
Citizen.Wait(500)
end
Citizen.Wait(1)
end
end)
RegisterNetEvent("juSa_npc_rewards:openWeaponMenu")
AddEventHandler("juSa_npc_rewards:openWeaponMenu", function(weapons) --opens menu with 1 entry for every weapon in players inv
local elements = {}
Menu.CloseAll()
MenuTriggered = true
print(MenuTriggered)
for i, weapon in ipairs(weapons) do -- load weapons and names
table.insert(elements, {
label = weapon.label,
value = weapon.id,
model = weapon.name,
desc = Config.Language.SN..weapon.serial_number.." \n"--..weapon.desc
})
end
Menu.Open("default", GetCurrentResourceName(), "weapon_menu", {
title = Config.Language.title,
align = "top-left",
elements = elements
}, function(data, menu)
WeaponChoosed = true
selectedWeapon = data.current.model
WeaponID = data.current.value
print(WeaponID)
menu.close()
end, function(data, menu) --if menu gets closed without choosing a weapon, everything gets a reset
Reset()
menu.close()
end)
end)
RegisterNetEvent("juSa_npc_rewards:infosellsend")
AddEventHandler("juSa_npc_rewards:infosellsend", function( takeitem, givemoney, taskbar, usewebhook, npc_name)
local playerPed = PlayerPedId()
FreezeEntityPosition(playerPed,true)
TaskStartScenarioInPlace(playerPed, GetHashKey("WORLD_HUMAN_BADASS"), taskbar, true, false, false, false)
progressbar.start(Config.Language.selling_item, taskbar, nil, linear)
Citizen.Wait(taskbar)
TriggerServerEvent("juSa_npc_rewards:sell", takeitem, givemoney, usewebhook, npc_name)
ClearPedTasks(playerPed)
FreezeEntityPosition(playerPed,false)
end)
RegisterNetEvent("juSa_npc_rewards:infoexchangesend")
AddEventHandler("juSa_npc_rewards:infoexchangesend", function(takeitem, giveitem, giveweapon, givemoney, takemoney, taskbar, usewebhook, npc_name)
local playerPed = PlayerPedId()
FreezeEntityPosition(playerPed,true)
TaskStartScenarioInPlace(playerPed, GetHashKey("WORLD_HUMAN_BADASS"), taskbar, true, false, false, false)
progressbar.start(Config.Language.exchanging_item, taskbar, nil, linear)
Citizen.Wait(taskbar)
TriggerServerEvent("juSa_npc_rewards:exchange", giveitem, takeitem, giveweapon, givemoney, takemoney, usewebhook, npc_name)
ClearPedTasks(playerPed)
FreezeEntityPosition(playerPed,false)
end)
RegisterNetEvent("juSa_npc_rewards:info_sell_weapon_send")
AddEventHandler("juSa_npc_rewards:info_sell_weapon_send", function(taskbar, usewebhook, npc_name, weapon, WeaponID, hasSpace)
if hasSpace then --if player has inv space
local playerPed = PlayerPedId()
FreezeEntityPosition(playerPed,true)
TaskStartScenarioInPlace(playerPed, GetHashKey("WORLD_HUMAN_BADASS"), taskbar, true, false, false, false)
progressbar.start(Config.Language.selling_weapon, taskbar, nil, linear)
Citizen.Wait(taskbar)
TriggerServerEvent("juSa_npc_rewards:sell_weapon", usewebhook, npc_name, weapon, WeaponID)
ClearPedTasks(playerPed)
FreezeEntityPosition(playerPed,false)
else
Core.NotifyBottomRight(Config.Language.invToFull,4000) --if inv to full
Reset()
end
end)
RegisterNetEvent("juSa_npc_rewards:jobchecked")
AddEventHandler("juSa_npc_rewards:jobchecked", function(result)
hasjob = result
end)
function NewCooldowns() --set new cooldowns
generalCooldown = Config.Cooldown
for i, v in ipairs(Config.NPCs) do
if Config.generalCooldown then -- set new cooldown for every NPC
if v.cooldown ~= nil then
npcCooldowns[i] = generalCooldown
else
npcCooldowns[i] = 0
end
end
end
end
function Reset() --resets vars
hasjob = false
talking = false
MenuTriggered = false
WeaponChoosed = false
selectedWeapon = nil
WeaponID = nil
end
Citizen.CreateThread(function() --handeling cooldown counting
while true do
Citizen.Wait(1000)
if generalCooldown > 0 then
generalCooldown = generalCooldown - 1
end
for i, cooldown in ipairs(npcCooldowns) do
if cooldown > 0 then
npcCooldowns[i] = cooldown - 1
end
end
if talkingTime > 0 then
talkingTime = talkingTime -1
else
Reset()
end
end
end)
---- Debug commands ----
RegisterCommand("printCd", function()
if Config.Debug then --command if debug = true to print every cooldown
PrintCooldowns()
end
end, false)
RegisterCommand('resetCd', function() --set all cooldowns to 0
if Config.Debug then
for i, v in ipairs(Config.NPCs) do
v.cooldown = 0
print("NPC name:", v.npc_name, "Cooldown was set to 0")
end
generalCooldown = 0
end
end, false)
function PrintCooldowns() --prints cooldowns for every NPC
print("General Cooldown:", generalCooldown)
for i, v in ipairs(Config.NPCs) do
local npcName = v.npc_name or "Unknown NPC"
local cooldown = npcCooldowns[i] or 0
print("NPC Name:", npcName, "Cooldown:", cooldown)
end
end