-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathclient.lua
More file actions
252 lines (227 loc) · 7.32 KB
/
client.lua
File metadata and controls
252 lines (227 loc) · 7.32 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
local Action = {
name = '',
duration = 0,
label = '',
useWhileDead = false,
canCancel = true,
disarm = true,
controlDisables = {
disableMovement = false,
disableCarMovement = false,
disableMouse = false,
disableCombat = false,
},
animation = {
animDict = nil,
anim = nil,
flags = 0,
task = nil,
},
prop = {
model = nil,
bone = nil,
coords = vec3(0.0, 0.0, 0.0),
rotation = vec3(0.0, 0.0, 0.0),
},
propTwo = {
model = nil,
bone = nil,
coords = vec3(0.0, 0.0, 0.0),
rotation = vec3(0.0, 0.0, 0.0),
},
}
local isDoingAction = false
local wasCancelled = false
local prop_net = nil
local propTwo_net = nil
local isAnim = false
local isProp = false
local isPropTwo = false
local controls = {
disableMouse = { 1, 2, 106 },
disableMovement = { 30, 31, 36, 21, 75 },
disableCarMovement = { 63, 64, 71, 72 },
disableCombat = { 24, 25, 37, 47, 58, 140, 141, 142, 143, 263, 264, 257 }
}
-- Functions
local function loadAnimDict(dict)
RequestAnimDict(dict)
while not HasAnimDictLoaded(dict) do
Wait(5)
end
end
local function loadModel(model)
RequestModel(model)
while not HasModelLoaded(model) do
Wait(5)
end
end
local function createAndAttachProp(prop, ped)
loadModel(prop.model)
local coords = GetOffsetFromEntityInWorldCoords(ped, 0.0, 0.0, 0.0)
local propEntity = CreateObject(GetHashKey(prop.model), coords.x, coords.y, coords.z, true, true, true)
local netId = ObjToNet(propEntity)
SetNetworkIdExistsOnAllMachines(netId, true)
NetworkUseHighPrecisionBlending(netId, true)
SetNetworkIdCanMigrate(netId, false)
local boneIndex = GetPedBoneIndex(ped, prop.bone or 60309)
AttachEntityToEntity(
propEntity, ped, boneIndex,
prop.coords.x, prop.coords.y, prop.coords.z,
prop.rotation.x, prop.rotation.y, prop.rotation.z,
true, true, false, true, 0, true
)
return netId
end
local function disableControls()
CreateThread(function()
while isDoingAction do
for disableType, isEnabled in pairs(Action.controlDisables) do
if isEnabled and controls[disableType] then
for _, control in ipairs(controls[disableType]) do
DisableControlAction(0, control, true)
end
end
end
if Action.controlDisables.disableCombat then
DisablePlayerFiring(PlayerId(), true)
end
Wait(0)
end
end)
end
local function StartActions()
local ped = PlayerPedId()
if isDoingAction then
if not isAnim and Action.animation then
if Action.animation.task then
TaskStartScenarioInPlace(ped, Action.animation.task, 0, true)
else
local anim = Action.animation
if anim.animDict and anim.anim and DoesEntityExist(ped) and not IsEntityDead(ped) then
loadAnimDict(anim.animDict)
TaskPlayAnim(ped, anim.animDict, anim.anim, 3.0, 3.0, -1, anim.flags or 1, 0, false, false, false)
end
end
isAnim = true
end
if not isProp and Action.prop and Action.prop.model then
prop_net = createAndAttachProp(Action.prop, ped)
isProp = true
end
if not isPropTwo and Action.propTwo and Action.propTwo.model then
propTwo_net = createAndAttachProp(Action.propTwo, ped)
isPropTwo = true
end
disableControls()
end
end
local function StartProgress(action, onStart, onTick, onFinish)
local playerPed = PlayerPedId()
local isPlayerDead = IsEntityDead(playerPed)
if (not isPlayerDead or action.useWhileDead) and not isDoingAction then
isDoingAction = true
LocalPlayer.state:set('inv_busy', true, true)
Action = action
SendNUIMessage({
action = 'progress',
duration = action.duration,
label = action.label
})
StartActions()
CreateThread(function()
if onStart then onStart() end
while isDoingAction do
Wait(1)
if onTick then onTick() end
if IsControlJustPressed(0, 200) and action.canCancel then
TriggerEvent('progressbar:client:cancel')
wasCancelled = true
break
end
if IsEntityDead(playerPed) and not action.useWhileDead then
TriggerEvent('progressbar:client:cancel')
wasCancelled = true
break
end
end
if onFinish then onFinish(wasCancelled) end
isDoingAction = false
end)
end
end
local function ActionCleanup()
local ped = PlayerPedId()
if Action.animation then
if Action.animation.task or (Action.animation.animDict and Action.animation.anim) then
StopAnimTask(ped, Action.animation.animDict, Action.animation.anim, 1.0)
ClearPedSecondaryTask(ped)
else
ClearPedTasks(ped)
end
end
if prop_net then
DetachEntity(NetToObj(prop_net), true, true)
DeleteObject(NetToObj(prop_net))
end
if propTwo_net then
DetachEntity(NetToObj(propTwo_net), true, true)
DeleteObject(NetToObj(propTwo_net))
end
prop_net = nil
propTwo_net = nil
isDoingAction = false
wasCancelled = false
isAnim = false
isProp = false
isPropTwo = false
LocalPlayer.state:set('inv_busy', false, true)
end
-- Events
RegisterNetEvent('progressbar:client:ToggleBusyness', function(bool)
isDoingAction = bool
end)
RegisterNetEvent('progressbar:client:progress', function(action, finish)
StartProgress(action, nil, nil, finish)
end)
RegisterNetEvent('progressbar:client:ProgressWithStartEvent', function(action, start, finish)
StartProgress(action, start, nil, finish)
end)
RegisterNetEvent('progressbar:client:ProgressWithTickEvent', function(action, tick, finish)
StartProgress(action, nil, tick, finish)
end)
RegisterNetEvent('progressbar:client:ProgressWithStartAndTick', function(action, start, tick, finish)
StartProgress(action, start, tick, finish)
end)
RegisterNetEvent('progressbar:client:cancel', function()
ActionCleanup()
SendNUIMessage({
action = 'cancel'
})
end)
-- NUI Callback
RegisterNUICallback('FinishAction', function(data, cb)
ActionCleanup()
cb('ok')
end)
-- Exports
local function Progress(action, finish)
StartProgress(action, nil, nil, finish)
end
exports('Progress', Progress)
local function ProgressWithStartEvent(action, start, finish)
StartProgress(action, start, nil, finish)
end
exports('ProgressWithStartEvent', ProgressWithStartEvent)
local function ProgressWithTickEvent(action, tick, finish)
StartProgress(action, nil, tick, finish)
end
exports('ProgressWithTickEvent', ProgressWithTickEvent)
local function ProgressWithStartAndTick(action, start, tick, finish)
StartProgress(action, start, tick, finish)
end
exports('ProgressWithStartAndTick', ProgressWithStartAndTick)
local function isDoingSomething()
return isDoingAction
end
exports('isDoingSomething', isDoingSomething)