-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.lua
More file actions
300 lines (264 loc) · 10 KB
/
client.lua
File metadata and controls
300 lines (264 loc) · 10 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
local RSGCore = exports['rsg-core']:GetCoreObject()
-- ========= ox_lib notifications (fallback minimal în chat) =========
local function Notify(payload)
-- payload: { title?, description, type?, duration?, position? }
local t = payload or {}
t.title = t.title or 'Postman'
t.type = t.type or 'inform' -- success | inform | warning | error
t.duration = t.duration or 4000
t.position = t.position or 'top-right'
if lib and lib.notify then
return lib.notify(t)
else
local txt = (t.title and (t.title .. ': ') or '') .. (t.description or '')
TriggerEvent('chat:addMessage', { args = { '^3Postman', txt } })
end
end
-- Server -> client notifications go through here
RegisterNetEvent('rsg-postman:notify', function(t)
Notify(t)
end)
-- ==================================================================
-- ========== Waypoint helpers (RedM-safe) ==========
local function SetWaypointSafe(coords)
if type(SetNewWaypoint) == 'function' then
SetNewWaypoint(coords.x + 0.0, coords.y + 0.0)
return true
end
if type(StartGpsMultiRoute) == 'function'
and type(AddPointToGpsMultiRoute) == 'function'
and type(SetGpsMultiRouteRender) == 'function'
and type(ClearGpsMultiRoute) == 'function' then
ClearGpsMultiRoute()
StartGpsMultiRoute(6, true, true)
AddPointToGpsMultiRoute(coords.x + 0.0, coords.y + 0.0, coords.z + 0.0)
SetGpsMultiRouteRender(true)
return true
end
return false
end
local function ClearWaypointSafe()
if type(IsWaypointActive) == 'function' and IsWaypointActive() and type(SetWaypointOff) == 'function' then
SetWaypointOff()
end
if type(ClearGpsMultiRoute) == 'function' then
ClearGpsMultiRoute()
end
end
-- ================================================
-- ========= Handover animation =========
local function PlayHandoverAnim()
local ped = PlayerPedId()
local scene = CreateAnimScene('script@beat@town@townRobbery@handover_money', 64, 0, false, true)
if not DoesAnimSceneExist(scene) then return end
local p = GetEntityCoords(ped)
local fwd = GetEntityForwardVector(ped)
local origin = vector3(p.x + fwd.x * 1.0, p.y + fwd.y * 1.0, p.z)
local rot = GetEntityRotation(ped, 2)
SetAnimSceneOrigin(scene, origin.x, origin.y, origin.z, rot.x, rot.y, rot.z - 175.0, 2)
SetAnimSceneEntity(scene, "pedPlayer", ped, 0)
local objectModel = `s_herbalpouch01x`
local prop = 0
RequestModel(objectModel)
local timeout = GetGameTimer() + 3000
while not HasModelLoaded(objectModel) and GetGameTimer() < timeout do Wait(0) end
if HasModelLoaded(objectModel) then
prop = CreateObject(objectModel, origin.x, origin.y, origin.z, true, true, false, false, true)
SetEntityVisible(prop, false)
SetAnimSceneEntity(scene, "objPouch", prop, 0)
end
LoadAnimScene(scene)
Wait(100)
StartAnimScene(scene)
Wait(250)
if prop ~= 0 then SetEntityVisible(prop, true) end
Wait(2800)
if prop ~= 0 then
SetEntityAsMissionEntity(prop, true, true)
DeleteObject(prop)
end
if DoesAnimSceneExist(scene) then
Citizen.InvokeNative(0x84EEDB2C6E650000, scene) -- _DELETE_ANIM_SCENE
end
end
-- =====================================
-- ========= State =========
local delivering, deliveryBlip, currentDelivery, packagesToDeliver =
false, nil, nil, 0
-- ========================
-- ========= Helpers =========
local function DrawTxt3D(x, y, z, text)
local onScreen, _x, _y = GetScreenCoordFromWorldCoord(x, y, z)
if onScreen then
SetTextScale(0.35, 0.35)
SetTextFontForCurrentCommand(6)
SetTextCentre(true)
DisplayText(CreateVarString(10, "LITERAL_STRING", text), _x, _y)
end
end
local function clearNav()
if deliveryBlip then RemoveBlip(deliveryBlip) deliveryBlip = nil end
ClearWaypointSafe()
end
-- ==========================
-- ========= Spawn Postman NPC + ox_target + blip =========
CreateThread(function()
local npc = Config.PostmanNPC
local model = joaat(npc.model)
RequestModel(model)
while not HasModelLoaded(model) do Wait(50) end
-- z - 1.0 + fade-in + place on ground
local ped = CreatePed(model, npc.coords.x, npc.coords.y, npc.coords.z - 1.0, npc.coords.w, false, false, 0, 0)
SetEntityAlpha(ped, 0, false)
SetRandomOutfitVariation(ped, true)
SetEntityCanBeDamaged(ped, false)
SetEntityInvincible(ped, true)
FreezeEntityPosition(ped, true)
SetBlockingOfNonTemporaryEvents(ped, true)
SetPedCanBeTargetted(ped, false)
PlaceEntityOnGroundProperly(ped, true)
for a = 0, 255, 51 do
Wait(50)
SetEntityAlpha(ped, a, false)
end
exports.ox_target:addLocalEntity(ped, {
{
name = 'postman_open_menu',
icon = 'far fa-envelope',
label = 'Open Post Office',
distance = 3.0,
onSelect = function()
TriggerEvent('rsg-postman:openMenu')
end
}
})
local npcBlip = BlipAddForCoords(1664425300, npc.coords.x, npc.coords.y, npc.coords.z)
SetBlipSprite(npcBlip, `blip_post_office`)
SetBlipScale(npcBlip, 0.8)
SetBlipName(npcBlip, 'Postman')
BlipAddModifier(npcBlip, joaat('BLIP_MODIFIER_MP_COLOR_6'))
end)
-- ========================================================
-- ========= Meniu ox_lib =========
RegisterNetEvent('rsg-postman:openMenu', function()
if not lib or not lib.registerContext then
Notify({ description = '^1ox_lib not loaded. Put @ox_lib/init.lua before client.lua and ensure ox_lib is started first.', type = 'error' })
return
end
local options = {}
local max = Config.MaxPackages or 50
local step = 5
for i = step, max, step do
options[#options+1] = {
title = ("Take %d packages"):format(i),
description = ("Pick up %d packages for delivery"):format(i),
icon = 'fa-solid fa-box',
event = 'rsg-postman:client:startJob',
args = { amount = i }
}
end
options[#options+1] = {
title = "Stop Deliveries",
description = "Return all remaining packages and cancel deliveries",
icon = "fa-solid fa-ban",
event = "rsg-postman:client:stopJob"
}
lib.registerContext({
id = 'postman_menu',
title = 'Post Office',
position = 'top-right',
options = options
})
lib.showContext('postman_menu')
end)
-- Start job
RegisterNetEvent('rsg-postman:client:startJob', function(data)
local amount
if type(data) == 'table' then
amount = data.amount or data.value or data[1]
else
amount = data
end
amount = tonumber(amount) or 5
TriggerServerEvent('rsg-postman:startJob', amount)
end)
-- Stop job (progress bar + animation + server)
RegisterNetEvent('rsg-postman:client:stopJob', function()
if lib and lib.progressBar then
local ok = lib.progressBar({
duration = 3500,
label = 'Returning remaining packages...',
useWhileDead = false,
canCancel = true,
disable = { move = true, car = true, mouse = false, combat = true }
})
if not ok then
Notify({ description = "Cancelled.", type = "error" })
return
end
end
-- Animation before returning items
PlayHandoverAnim()
TriggerServerEvent('rsg-postman:stopJob')
delivering = false
currentDelivery = nil
packagesToDeliver = 0
clearNav()
Notify({ description = "You have stopped all deliveries.", type = "error" })
end)
-- Receive delivery (blip + waypoint)
RegisterNetEvent('rsg-postman:setDelivery', function(location, amount)
delivering = true
currentDelivery = location
packagesToDeliver = amount
if deliveryBlip then RemoveBlip(deliveryBlip) end
deliveryBlip = BlipAddForCoords(1664425300, location.coords.x, location.coords.y, location.coords.z)
SetBlipSprite(deliveryBlip, `blip_post_office`)
SetBlipScale(deliveryBlip, 0.8)
SetBlipName(deliveryBlip, 'Package Delivery')
BlipAddModifier(deliveryBlip, joaat('BLIP_MODIFIER_MP_COLOR_6'))
ClearWaypointSafe()
SetWaypointSafe(location.coords)
Notify({ description = ('Deliver %d packages to %s'):format(amount, location.name), type = 'success' })
end)
-- Delivery loop (E) + progress bar + animation
CreateThread(function()
while true do
Wait(0)
if delivering and currentDelivery then
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
if #(coords - currentDelivery.coords) < 3.0 then
DrawTxt3D(currentDelivery.coords.x, currentDelivery.coords.y, currentDelivery.coords.z, "[E] Deliver packages")
if IsControlJustPressed(0, 0xCEFD9220) then
local ok = true
if lib and lib.progressBar then
ok = lib.progressBar({
duration = 3000,
label = 'Handing over packages...',
useWhileDead = false,
canCancel = true,
disable = { move = true, car = true, mouse = false, combat = true }
})
end
if not ok then
Notify({ description = "Delivery cancelled.", type = "error" })
else
-- Animation before confirming the delivery
PlayHandoverAnim()
TriggerServerEvent('rsg-postman:completeDelivery', packagesToDeliver)
delivering = false
currentDelivery = nil
packagesToDeliver = 0
clearNav()
end
end
end
end
end
end)
-- Cleanup
AddEventHandler("onResourceStop", function(resourceName)
if GetCurrentResourceName() ~= resourceName then return end
clearNav()
end)