Clothing and clipboard update

QBClothing and ESKSkin working. Need others (WIP)

Copy to clipboard now a utility function. Will json encode if ~= string
This commit is contained in:
gononono64 committed 2025-05-24 18:03:31 -05:00
1 parent b9f5a3ed64
commit d4908042be
16 files changed
+1306 -44

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
if GetResourceState('esx_skin') == 'missing' then return end
Clothing = Clothing or {}
function Clothing.OpenMenu()
TriggerEvent('esx_skin:openMenu', function()end, function()end, true)
end
+210
View File
@@ -0,0 +1,210 @@
if GetResourceState('esx_skin') == 'missing' then return end
Clothing = Clothing or {}
Clothing.Players = {}
Callback = Callback or Require("lib/utility/shared/callbacks.lua")
Table = Table or Require('lib/utility/shared/tables.lua')
ESX = ESX or exports['es_extended']:getSharedObject()
--- Internal function to get the full appearance data including skin, model, and converted format
--- @param src number The server ID of the player
--- @return table|nil The player's full appearance data or nil if not found
function Clothing.GetFullAppearanceData(src)
src = src and tonumber(src)
assert(src, "src is nil")
local Player = ESX.GetPlayerFromId(src)
if not Player then return end
local citId = Player.identifier
if not citId then return end
if Clothing.Players[citId] then return Clothing.Players[citId] end
local result = MySQL.query.await('SELECT skin FROM users WHERE identifier = ?', { citId})
if result[1] == nil then return end
local model = GetEntityModel(GetPlayerPed(src))
local skinData = json.decode(result[1].skin)
local converted = Clothing.ConvertToDefault(skinData)
-- Store complete data in the cache
Clothing.Players[citId] = {
model = model,
skin = skinData,
converted = converted
}
return Clothing.Players[citId]
end
--- Retrieves a player's converted appearance data for easy use across modules
--- @param src number The server ID of the player
--- @param fullData boolean Optional - If true, returns the full data object including skin and model
--- @return table|nil The player's converted appearance data or full appearance data if fullData=true
function Clothing.GetAppearance(src, fullData)
if fullData then
return Clothing.GetFullAppearanceData(src)
end
local completeData = Clothing.GetFullAppearanceData(src)
if not completeData then return nil end
return completeData.converted
end
function Clothing.Save(src)
src = src and tonumber(src)
assert(src, "src is nil")
local Player = ESX.GetPlayerFromId(src)
if not Player then return end
local citId = Player.identifier
if not citId then return end
local currentClothing = Clothing.GetFullAppearanceData(src)
if not currentClothing then return end
local currentSkin = currentClothing.skin
local encodedSkin = json.encode(currentSkin)
MySQL.update.await('UPDATE users SET skin = ? WHERE identifier = ?', {
encodedSkin,
citId
})
end
--- Sets a player's appearance based on the provided data
--- @param src number The server ID of the player
--- @param data table The appearance data to apply
--- @param updateBackup boolean Whether to update the backup appearance data
--- @return table|nil The updated player appearance data or nil if failed
function Clothing.SetAppearance(src, data, updateBackup, save)
src = src and tonumber(src)
assert(src, "src is nil")
local Player = ESX.GetPlayerFromId(src)
if not Player then return end
local citId = Player.identifier
if not citId then return end
local model = GetEntityModel(GetPlayerPed(src))
if not model then return end
local converted = Clothing.ConvertFromDefault(data)
-- Get full appearance data
local currentClothing = Clothing.GetFullAppearanceData(src)
if not currentClothing then return end
local currentSkin = currentClothing.skin
for k, v in pairs(converted) do
currentSkin[k] = v
end
if not Clothing.Players[citId].backup or updateBackup then
Clothing.Players[citId].backup = currentClothing.converted
end
Clothing.Players[citId] = Clothing.Players[citId] or {}
Clothing.Players[citId].skin = currentSkin
Clothing.Players[citId].model = model
Clothing.Players[citId].converted = Clothing.ConvertToDefault(currentSkin)
if save then
local encodedSkin = json.encode(currentSkin)
-- saving 1 sql call by adding the query here instead of calling save
MySQL.update.await('UPDATE users SET skin = ? WHERE identifier = ?', {
encodedSkin,
citId
})
end
TriggerClientEvent('community_bridge:client:SetAppearance', src, Clothing.Players[citId].converted)
return Clothing.Players[citId]
end
--- Sets a player's appearance based on gender-specific data
--- @param src number The server ID of the player
--- @param data table Table containing separate appearance data for male and female characters
--- @return table|nil Appearance updated player appearance data or nil if failed
function Clothing.SetAppearanceExt(src, data)
local tbl = Clothing.IsMale(src) and data.male or data.female
Clothing.SetAppearance(src, tbl)
end
--- Reverts a player's appearance to their backup appearance
--- @param src number The server ID of the player
--- @return boolean|nil Returns true if successful or nil if failed
function Clothing.Revert(src)
src = src and tonumber(src)
assert(src, "src is nil")
local currentClothing = Clothing.GetFullAppearanceData(src)
if not currentClothing then return end
local backup = currentClothing.backup
if not backup then return end
return Clothing.SetAppearance(src, backup)
end
function Clothing.OpenMenu(src)
src = src and tonumber(src)
assert(src, "src is nil")
TriggerClientEvent('esx_skin:openMenu', src)
end
--- Event handler for when a player loads into the server
--- Caches the player's appearance data
AddEventHandler('community_bridge:Server:OnPlayerLoaded', function(src)
src = src and tonumber(src)
assert(src, "src is nil")
-- Use GetFullAppearanceData to cache the complete appearance
Clothing.GetFullAppearanceData(src)
end)
--- Event handler for when a player disconnects from the server
--- Removes the player's appearance data from the cache
AddEventHandler('community_bridge:Server:OnPlayerUnload', function(src)
src = src and tonumber(src)
assert(src, "src is nil")
local strSrc = tostring(src)
Clothing.Players[strSrc] = nil
end)
--- Event handler for when the resource starts
--- Caches appearance data for all currently connected players
AddEventHandler('onResourceStart', function(resource)
if resource ~= GetCurrentResourceName() then return end
for _, playerId in ipairs(GetPlayers()) do
local src = tonumber(playerId)
local strSrc = tostring(src)
if not Clothing.Players[strSrc] then
Clothing.Players[strSrc] = Clothing.GetAppearance(src)
end
end
end)
--- Callback handler for retrieving a player's appearance data
Callback.Register('community_bridge:cb:GetAppearance', function(source)
local src = source
return Clothing.GetAppearance(src)
end)
RegisterCommand('clothing:debug', function(source, args, rawCommand)
local src = source
Clothing.SetAppearance(src, {
components ={
{component_id = 1, drawable = math.random(0, 50), texture = 0},
{component_id = 2, drawable = math.random(0, 50), texture = 0},
{component_id = 3, drawable = math.random(0, 50), texture = 0},
{component_id = 4, drawable = math.random(0, 50), texture = 0},
{component_id = 5, drawable = math.random(0, 50), texture = 0},
{component_id = 6, drawable = math.random(0, 50), texture = 0},
{component_id = 7, drawable = math.random(0, 50), texture = 0},
{component_id = 8, drawable = math.random(0, 50), texture = 0},
},
props = {
{prop_id = 3, drawable = 0, texture = 0},
{prop_id = 1, drawable = 0, texture = 0},
{prop_id = 2, drawable = 0, texture = 0},
}
})
end, false)
RegisterCommand('clothing:revert', function(source, args, rawCommand)
local src = source
Clothing.Revert(src)
end, false)
+216
View File
@@ -0,0 +1,216 @@
if GetResourceState('esx_skin') == 'missing' then return end
Clothing = Clothing or {}
-- {"glasses_2":0,"shoes_1":70,"dad":0,"shoes_2":2,"pants_1":28,"eye_squint":0,"ears_1":-1,"hair_color_1":61,"eyebrows_6":0,"bodyb_3":-1,"beard_1":11,"complexion_2":0,"arms_2":0,"hair_1":76,"nose_1":0,"blush_2":0,"bracelets_2":0,"blush_1":0,"jaw_2":0,"helmet_1":-1,"eyebrows_3":0,"watches_1":-1,"eyebrows_4":0,"jaw_1":0,"lipstick_1":0,"eyebrows_1":0,"nose_4":0,"age_2":0,"torso_1":23,"hair_2":0,"chin_2":0,"arms":1,"chain_1":22,"nose_2":0,"cheeks_1":2,"tshirt_1":4,"glasses_1":0,"pants_2":3,"lipstick_4":0,"chin_13":0,"beard_4":0,"beard_3":0,"chain_2":2,"cheeks_3":6,"sex":0,"lipstick_3":0,"makeup_1":0,"hair_color_2":29,"mask_2":0,"chin_1":0,"eyebrows_5":0,"bodyb_2":0,"sun_2":0,"watches_2":0,"sun_1":0,"chin_4":0,"nose_3":0,"helmet_2":0,"bags_2":0,"moles_2":0,"mask_1":0,"blemishes_2":0,"chest_1":0,"cheeks_2":-10,"age_1":0,"chest_2":0,"beard_2":10,"torso_2":2,"blush_3":0,"bproof_1":0,"moles_1":0,"chin_3":0,"lip_thickness":-2,"lipstick_2":0,"chest_3":0,"complexion_1":0,"bodyb_4":0,"neck_thickness":0,"bproof_2":0,"makeup_3":0,"tshirt_2":2,"makeup_2":0,"makeup_4":0,"bracelets_1":-1,"decals_2":0,"nose_6":0,"bodyb_1":-1,"bags_1":0,"blemishes_1":0,"decals_1":0,"mom":21,"eyebrows_2":0,"eye_color":0,"skin_md_weight":50,"face_md_weight":50,"nose_5":10,"ears_2":0}
Components = {}
Components.Map = {
[1] = 'mask', -- componentId
[2] = 'ears',
[3] = 'arms',
[4] = 'pants',
[5] = 'bags',
[6] = 'shoes',
[7] = 'chain',
[8] = 'tshirt',
[9] = 'bproof',
[10] = 'decals',
[11] = 'torso'
}
Components.InverseMap = {
mask = 1, -- componentId
arms = 3,
pants = 4,
bags = 5,
shoes = 6,
chain = 7,
tshirt = 8,
bproof = 9,
decals = 10,
torso = 11,
}
Props = {}
Props.Map = {
[0] = 'helmet', -- propId
[1] = 'glasses',
[2] = 'ears',
[6] = 'watches',
[7] = 'bracelets'
}
Props.InverseMap = {
helmet_1 = 0,
helmet_2 = 0,
glasses_1 = 1,
glasses_2 = 1,
ears_1 = 2,
ears_2 = 2,
watches_1 = 6,
watches_2 = 6,
bracelets_1 = 7,
bracelets_2 = 7,
}
-- Converts from illinium format to esx format
function Components.ConvertFromDefault(defaultComponents)
local returnComponents = {}
for index, componentData in pairs(defaultComponents or {}) do
local componentId = Components.Map[componentData.component_id]
if componentId then
returnComponents[componentId .. '_1'] = componentData.drawable
returnComponents[componentId .. '_2'] = componentData.texture
end
end
return returnComponents
end
function Components.ConvertToDefault(esxComponents)
local returnComponents = {}
for componentIndex, componentData in pairs(esxComponents or {}) do
local isTexture = componentIndex:find('_2')
componentIndex = componentIndex:gsub('_1', ''):gsub('_2', '')
local componentId = Components.InverseMap[componentIndex]
if componentId then
if isTexture then
returnComponents[componentId] = returnComponents[componentId] or {}
returnComponents[componentId].texture = componentData
else
returnComponents[componentId] = returnComponents[componentId] or {}
returnComponents[componentId].component_id = componentId
returnComponents[componentId].drawable = componentData
returnComponents[componentId].texture = returnComponents[componentId].texture or 0
end
end
end
return returnComponents
end
function Props.ConvertFromDefault(defaultProps)
local returnProps = {}
for index, propData in pairs(defaultProps or {}) do
local propId = Props.Map[propData.prop_id]
if propId then
returnProps[propId .. '_1'] = propData.drawable
returnProps[propId .. '_2'] = propData.texture
end
end
return returnProps
end
function Props.ConvertToDefault(esxProps)
local returnProps = {}
for propIndex, propData in pairs(esxProps or {}) do
local isTexture = propIndex:find('_2')
propIndex = propIndex:gsub('_1', ''):gsub('_2', '')
local propId = Props.InverseMap[propIndex]
if propId then
if isTexture then
returnProps[propId] = returnProps[propId] or {}
returnProps[propId].texture = propData
else
returnProps[propId] = returnProps[propId] or {}
returnProps[propId].prop_id = propId
returnProps[propId].drawable = propData
returnProps[propId].texture = returnProps[propId].texture or 0
end
end
table.sort(returnProps, function(a, b)
return a.prop_id < b.prop_id
end)
end
return returnProps
end
function Clothing.ConvertFromDefault(defaultClothing)
local components = Components.ConvertFromDefault(defaultClothing.components)
local props = Props.ConvertFromDefault(defaultClothing.props)
for propsIndex, propData in pairs(props) do
components[propsIndex] = propData
end
return components --skin
end
function Clothing.ConvertToDefault(esxClothing)
local components = Components.ConvertToDefault(esxClothing)
local props = Props.ConvertToDefault(esxClothing)
return { components = components, props = props }
end
-- Clothing = {}
-- StoredOldClothing = {}
-- Clothing.SetAppearance = function(clothingData)
-- if GetEntityModel(cache.ped) == `mp_m_freemode_01` then
-- clothingData = clothingData.male
-- else
-- clothingData = clothingData.female
-- end
-- local repackedTable = {}
-- local componentMap = {
-- [1] = "mask",
-- [3] = "arms",
-- [4] = "pants",
-- [5] = "bag",
-- [6] = "shoes",
-- [7] = "accessory",
-- [8] = "t-shirt",
-- [9] = "vest",
-- [10] = "decals",
-- [11] = "torso2"
-- }
-- local propMap = {
-- [0] = "hat",
-- [1] = "glass",
-- [2] = "ear",
-- [6] = "watch",
-- [7] = "bracelet"
-- }
-- local specialMap = {
-- eye_color_id = "eye_color",
-- moles_id = "moles",
-- ageing_id = "ageing",
-- hair_id = "hair",
-- face_id = "face"
-- }
-- for _, data in pairs(clothingData) do
-- if componentMap[data.component_id] then
-- repackedTable[componentMap[data.component_id]] = {drawable = data.drawable, texture = data.texture}
-- elseif propMap[data.prop_id] then
-- repackedTable[propMap[data.prop_id]] = {drawable = data.drawable, texture = data.texture}
-- elseif specialMap[data.eye_color_id] then
-- repackedTable[specialMap[data.eye_color_id]] = {drawable = data.drawable, texture = data.texture}
-- elseif specialMap[data.moles_id] then
-- repackedTable[specialMap[data.moles_id]] = {drawable = data.drawable, texture = data.texture}
-- elseif specialMap[data.ageing_id] then
-- repackedTable[specialMap[data.ageing_id]] = {drawable = data.drawable, texture = data.texture}
-- elseif specialMap[data.hair_id] then
-- repackedTable[specialMap[data.hair_id]] = {drawable = data.drawable, texture = data.texture}
-- elseif specialMap[data.face_id] then
-- repackedTable[specialMap[data.face_id]] = {drawable = data.drawable, texture = data.texture}
-- end
-- end
-- TriggerEvent('esx-clothing:client:loadOutfit', repackedTable)
-- end