forked from AlterGamingNetwork/mellotrainer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcl_player_skin.lua
585 lines (457 loc) · 14.1 KB
/
cl_player_skin.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
--[[--------------------------------------------------------------------------
*
* Mello Trainer
* (C) Michael Goodwin 2017
* http://github.com/thestonedturtle/mellotrainer/releases
*
* This menu used the Scorpion Trainer as a framework to build off of.
* https://github.com/pongo1231/ScorpionTrainer
* (C) Emre Cürgül 2017
*
* A lot of useful functionality has been converted from the lambda menu.
* https://lambda.menu
* (C) Oui 2017
*
* Additional Contributors:
* WolfKnight (https://forum.fivem.net/u/WolfKnight)
*
---------------------------------------------------------------------------]]
-- Skin DB
local components = {
{ name = 'Head/Face', t = 0},
{ name = 'Beard/Mask', t = 1},
{ name = 'Hair/Hats', t = 2},
{ name = 'Top/Shirts', t = 3},
{ name = 'Legs/Pants', t = 4},
{ name = 'Gloves/Hands', t = 5},
{ name = 'Shoes/Feet', t = 6},
{ name = 'Neck/Eyes', t = 7},
{ name = 'Accessories-Top', t = 8},
{ name = 'Accessories-Extra', t = 9},
{ name = 'Badges/Decals', t = 10},
{ name = 'Shirt/Jacket', t = 11}
}
-- Prop DB
local propComponents = {
{ name = "Hats/Mask/Helmets", t = 0},
{ name = "Glasses", t = 1},
{ name = "Ears/Accessories", t = 2}
}
-- Error message
local modifyEmpty = "~r~Nothing to modify!"
-- Set Skin Function
function SetSkin( skin )
local ped = GetPlayerPed( -1 )
if ( DoesEntityExist( ped ) and not IsEntityDead( ped ) ) then
if ( IsModelValid( skin ) ) then
_LoadModel( skin )
SetPlayerModel( PlayerId(), skin )
SetPedDefaultComponentVariation( PlayerId() )
SetModelAsNoLongerNeeded( skin )
resetTrainerMenus("playerskinmodify playerpropmodify")
end
end
end
--[[------------------------------------------------------------------------
Skin Saving and Loading
------------------------------------------------------------------------]]--
local skins = {}
local skinsCount = 0
RegisterNetEvent( 'wk:RecieveSavedSkins' )
AddEventHandler( 'wk:RecieveSavedSkins', function( dataTable )
skins = dataTable
skinsCount = getTableLength( dataTable )
end )
function CreateSkinOptions( index )
local spawnSkin = {
[ "menuName" ] = "Apply To Player",
[ "data" ] = {
[ "action" ] = "spawnsavedskin " .. index
}
}
local overwriteSave = {
[ "menuName" ] = "Overwrite With Current",
[ "data" ] = {
[ "action" ] = "skinsave " .. index
}
}
local renameSkin = {
[ "menuName" ] = "Rename Save",
[ "data" ] = {
[ "action" ] = "skinsave " .. index .. " r"
}
}
local deleteSkin = {
[ "menuName" ] = "Delete",
[ "data" ] = {
[ "action" ] = "deletesavedskin " .. index
}
}
local options = { spawnSkin, overwriteSave, renameSkin, deleteSkin }
return options
end
RegisterNUICallback( "loadsavedskins", function( data, cb )
local validOptions = {}
for k, v in pairs( skins ) do
local skinOptions = CreateSkinOptions( k )
table.insert( validOptions, 1, {
[ "menuName" ] = v[ "saveName" ],
[ "data" ] = {
[ "sub" ] = k
},
[ "submenu" ] = skinOptions
} )
end
table.insert( validOptions, {
[ "menuName" ] = "Create New Skin Save",
[ "data" ] = {
[ "action" ] = "skinsave"
}
} )
local customJSON = "{}"
if ( getTableLength( validOptions ) > 0 ) then
customJSON = json.encode( validOptions, { indent = true } )
end
SendNUIMessage( {
createmenu = true,
menuName = "loadsavedskins",
menudata = customJSON
} )
if ( cb ) then cb( "ok" ) end
end )
RegisterNUICallback( "skinsave", function( data, cb )
Citizen.CreateThread( function()
local ped = GetPlayerPed( -1 )
local skinTableData = {}
local saveName = nil
local overwriting
local renaming
local index
if ( data.action == nil ) then
overwriting = false
renaming = false
else
if ( data.data[3] ~= nil ) then
renaming = true
else
overwriting = true
end
index = tonumber( data.action )
end
if ( DoesEntityExist( ped ) and not IsEntityDead( ped ) ) then
while ( (saveName == nil and not overwriting) or (saveName == nil and renaming) ) do
saveName = requestInput( "Enter save name", 24 )
Citizen.Wait( 1 )
end
if ( saveName or overwriting or renaming ) then
local model = GetEntityModel( ped )
if ( renaming or not overwriting ) then
skinTableData[ "saveName" ] = saveName
else
skinTableData[ "saveName" ] = skins[index][ "saveName" ]
end
skinTableData[ "model" ] = tostring( model )
local components = {}
for i = 0, 11 do
local drawable = GetPedDrawableVariation( ped, i )
local texture = GetPedTextureVariation( ped, i )
components[i] = { drawable, texture }
end
skinTableData[ "components" ] = components
local props = {}
for i = 0, 9 do
local drawable = GetPedPropIndex( ped, i )
local texture = GetPedPropTextureIndex( ped, i )
props[i] = { drawable, texture }
end
skinTableData[ "props" ] = props
if ( not renaming and not overwriting ) then
skinsCount = skinsCount + 1
skins[skinsCount] = skinTableData
TriggerServerEvent( 'wk:DataSave', "skins", skinTableData, skinsCount )
SendNUIMessage({
reshowmenu = true
})
else
skins[index] = skinTableData
TriggerServerEvent( 'wk:DataSave', "skins", skinTableData, index )
SendNUIMessage({
trainerback = true
})
SendNUIMessage({
reshowmenu = true
})
end
resetTrainerMenus( "loadsavedskins" )
end
end
end )
end )
RegisterNUICallback( "spawnsavedskin", function( data, cb )
local saveData = skins[ tonumber( data.action ) ]
local model = tonumber( saveData[ "model" ] )
local ped = GetPlayerPed( -1 )
if ( DoesEntityExist( ped ) and not IsEntityDead( ped ) ) then
SetSkin( model )
ApplySavedSettingsToPed( saveData )
end
end )
RegisterNUICallback( "deletesavedskin", function( data, cb )
local index = tonumber( data.action )
-- Citizen.Trace( "Found " .. index .. " with type " .. type( index ) )
if ( skinsCount > 0 ) then
skins[index] = nil
TriggerServerEvent( 'wk:DataSave', "skins", nil, index )
SendNUIMessage({
trainerback = true
})
SendNUIMessage({
trainerback = true
})
resetTrainerMenus( "loadsavedskins" )
end
end )
function ApplySavedSettingsToPed( data )
local components = data[ "components" ]
local props = data[ "props" ]
local ped = GetPlayerPed( -1 )
for k, v in pairs( components ) do
-- Citizen.Trace( k .. " " .. type(k) .. " - (" .. v[1] .. " " .. type( v[1] ) .. ", " .. v[2] .. " " .. type( v[2] ) .. ")" )
local index = tonumber( k )
SetPedComponentVariation( ped, index, v[1], v[2], 0 )
end
ClearAllPedProps( ped )
for k, v in pairs( props ) do
-- Citizen.Trace( k .. " " .. type(k) .. " - (" .. v[1] .. " " .. type( v[1] ) .. ", " .. v[2] .. " " .. type( v[2] ) .. ")" )
local index = tonumber( k )
SetPedPropIndex( ped, index, v[1], v[2], 0 )
end
end
-- Change players skin.
RegisterNUICallback("playerskin", function(data, cb)
local model
if data.action == "input" then
model = GetHashKey(requestInput("", 60))
else
model = GetHashKey(data.action)
end
if(not(IsModelValid(model)))then
drawNotification("~r~Invalid Model Name")
return
end
RequestModel(model)
while not HasModelLoaded(model) do
Wait(1)
end
SetPlayerModel(PlayerId(), model)
SetPedDefaultComponentVariation(GetPlayerPed(-1))
drawNotification("~g~Changed Player Model.")
resetTrainerMenus("playerskinmodify playerpropmodify")
if(cb)then cb("ok")end
end)
-- Return all player skin variations in JSON format.
RegisterNUICallback("playerskinmodify", function(data, cb)
local validOptions = {}
local optCount = 0
for i=1,#components,1 do
local validComponents = checkValidComponents(components[i].t)
if #validComponents > 0 then
table.insert(validOptions, 1, {
["menuName"] = components[i].name.." ("..#validComponents..")",
["data"] = {
["sub"] = components[i].t
},
["submenu"] = validComponents
})
optCount = optCount + 1
end
end
if (optCount > 0) then
local SkinJSON = json.encode(validOptions, {indent = true})
--Citizen.Trace(SkinJSON);
SendNUIMessage({
createmenu = true,
menuName = "playerskinmodify",
menudata = SkinJSON
})
else
drawNotification(modifyEmpty)
end
end)
-- Return all player prop components in JSON format.
RegisterNUICallback("playerpropmodify", function(data, cb)
local validOptions = {}
local optCount = 0
for i=1,#propComponents,1 do
local validComponents = checkValidPropComponents(propComponents[i].t)
if #validComponents > 0 then
table.insert(validOptions, 1, {
["menuName"] = propComponents[i].name.." ("..#validComponents..")",
["data"] = {
["sub"] = propComponents[i].t
},
["submenu"] = validComponents
})
optCount = optCount + 1
end
end
if (optCount > 0) then
local PropJSON = json.encode(validOptions, {indent = true})
--Citizen.Trace(PropJSON);
SendNUIMessage({
createmenu = true,
menuName = "playerpropmodify",
menudata = PropJSON
})
else
drawNotification(modifyEmpty)
end
end)
-- Default Skin/Clear Props for current skin.
RegisterNUICallback("defaultskin", function(data, cb)
local playerPed = GetPlayerPed(-1)
if(data.action == "skin") then
SetPedDefaultComponentVariation(playerPed)
end
if(data.action == "props") then
ClearAllPedProps(playerPed)
end
if(cb)then cb("ok")end
end)
-- Remove Individual Props from the current skin.
RegisterNUICallback("clearpropid", function(data, cb)
local componentID = tonumber(data.action)
ClearPedProp(GetPlayerPed(-1), componentID)
if(cb)then cb("ok")end
end)
-- Random skin/prop variations for the current skin.
RegisterNUICallback("randomskin", function(data, cb)
local playerPed = GetPlayerPed(-1)
if(data.action == "skin") then
SetPedRandomComponentVariation(playerPed)
end
if(data.action == "props") then
SetPedRandomProps(playerPed)
end
if(cb)then cb("ok")end
end)
-- Changes the current component to the requested variation (Skin/Props)
local defaultSkinAction = "changeskin skin"
local defaultPropAction = "changeskin props"
RegisterNUICallback("changeskin", function(data, cb)
local playerPed = GetPlayerPed(-1)
local componentID = tonumber(data.data[3])
local drawableID = tonumber(data.data[4]) - 1
local textureID = tonumber(data.data[5])
if(textureID == nil)then
textureID = 0
end
if(data.action == "skin") then
SetPedComponentVariation(playerPed, componentID, drawableID, textureID)
elseif(data.action == "props") then
SetPedPropIndex(playerPed, componentID, drawableID, textureID, true)
end
--Citizen.Trace("ComponentID: "..tostring(componentID)..", Drawable ID:"..tostring(drawableID)..", Texture ID:"..tostring(textureID))
if(cb)then cb("ok")end
end)
-- Get all valid skin variations for the current skin.
function checkValidComponents(componentID)
local playerPed = GetPlayerPed(-1)
local valid = {}
local pedCount = GetNumberOfPedDrawableVariations(playerPed, componentID)
for i = 1, pedCount, 1 do
local textureCount = GetNumberOfPedTextureVariations(playerPed, componentID, i - 1)
local textures = {}
for textureIndex=0,textureCount-1,1 do
local textureObj = {
["menuName"] = "Texture #"..tostring(textureIndex),
["data"] = {
["hover"] = defaultSkinAction.." "..tostring(componentID).." "..tostring(i).." "..tostring(textureIndex)
}
}
table.insert(textures, textureObj)
end
if(textureCount > 0)then
valid[i] = {
["menuName"] = "Drawable #"..tostring(i),
["data"] = {
["hover"] = defaultSkinAction.." "..tostring(componentID).." "..tostring(i).." 0",
["sub"] = i
},
["submenu"] = textures
}
end
end
return valid
end
-- Get all Valid Prop components for the current skin
function checkValidPropComponents(propID)
local playerPed = GetPlayerPed(-1)
local valid = {}
local propCount = GetNumberOfPedPropDrawableVariations(playerPed, propID)
for i = 1, propCount, 1 do
local textureCount = GetNumberOfPedPropTextureVariations(playerPed, propID, i - 1)
local textures = {}
for textureIndex=0,textureCount-1,1 do
local textureObj = {
["menuName"] = "Texture #"..tostring(textureIndex),
["data"] = {
["hover"] = defaultPropAction.." "..tostring(propID).." "..tostring(i).." "..tostring(textureIndex)
}
}
table.insert(textures, textureObj)
end
if(textureIndex ~= nil)then
end
if(textureCount > 0)then
table.insert(valid, {
["menuName"] = "Drawable #"..tostring(i),
["data"] = {
["hover"] = defaultPropAction.." "..tostring(propID).." "..tostring(i).." 0",
["sub"] = i
},
["submenu"] = textures
})
end
end
if(propCount ~= nil)then
table.insert(valid, 1, {
["menuName"] = "Clear Prop",
["data"] = {
["hover"] = "clearpropid "..tostring(propID)
}
})
end
return valid
end
local playerSkin = {}
local pmodel = nil
function savePlayerAppearanceVariables(pmod)
pmodel = pmod
local ped = PlayerPedId()
for i=0,12,1 do
table.insert(playerSkin, {
drawable = GetPedDrawableVariation(ped, i),
dtexture = GetPedTextureVariation(ped, i),
prop = GetPedPropIndex(ped, i),
ptexture = GetPedPropTextureIndex(ped, i),
palette = GetPedPaletteVariation(ped, i),
index = i
})
end
end
function restorePlayerAppearance()
_LoadModel(pmodel)
SetPlayerModel(PlayerId(),pmodel)
local ped = GetPlayerPed(-1)
for _,obj in pairs(playerSkin) do
SetPedComponentVariation(ped, obj.index, obj.drawable, obj.dtexture, obj.palette)
SetPedPropIndex(ped, obj.index, obj.prop, obj.ptexture)
end
SetModelAsNoLongerNeeded(pmodel)
end
AddEventHandler("mellotrainer:playerSpawned",function()
if(featureRestoreAppearance)then
restorePlayerAppearance()
end
end)