Files
community_bridge/modules/clothing/esx_skin/server.lua
T

177 lines
6.6 KiB
Lua
Raw Normal View History

---@diagnostic disable: duplicate-set-field
2025-05-24 18:03:31 -05:00
if GetResourceState('esx_skin') == 'missing' then return end
2025-07-11 15:23:24 -04:00
if GetResourceState('rcore_clothing') ~= 'missing' then return end
2025-05-24 18:03:31 -05:00
Clothing = Clothing or {}
Clothing.Players = {}
Callback = Callback or Require("lib/utility/shared/callbacks.lua")
Table = Table or Require('lib/utility/shared/tables.lua')
---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
2025-05-24 18:03:31 -05:00
function Clothing.GetFullAppearanceData(src)
src = src and tonumber(src)
2025-06-04 15:29:21 -05:00
assert(src, "src is nil")
2025-06-13 09:26:47 -04:00
local citId = Bridge.Framework.GetPlayerIdentifier(src)
2025-05-24 18:03:31 -05:00
if not citId then return end
2025-06-04 15:29:21 -05:00
2025-05-24 18:03:31 -05:00
if Clothing.Players[citId] then return Clothing.Players[citId] end
2025-06-04 15:29:21 -05:00
2025-07-11 15:23:24 -04:00
local result = MySQL.query.await('SELECT skin FROM users WHERE identifier = ?', { citId })
2025-05-24 18:03:31 -05:00
if result[1] == nil then return end
2025-06-04 15:29:21 -05:00
2025-05-24 18:03:31 -05:00
local model = GetEntityModel(GetPlayerPed(src))
local skinData = json.decode(result[1].skin)
local converted = Clothing.ConvertToDefault(skinData)
2025-06-04 15:29:21 -05:00
2025-05-24 18:03:31 -05:00
-- Store complete data in the cache
Clothing.Players[citId] = {
model = model,
2025-06-04 15:29:21 -05:00
skin = skinData,
converted = converted
2025-05-24 18:03:31 -05:00
}
2025-06-04 15:29:21 -05:00
2025-05-24 18:03:31 -05:00
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
2025-05-24 18:03:31 -05:00
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
---This will save the player's current appearance to the database
---@param src number|string
2025-05-24 18:03:31 -05:00
function Clothing.Save(src)
src = src and tonumber(src)
assert(src, "src is nil")
2025-06-13 09:26:47 -04:00
local citId = Bridge.Framework.GetPlayerIdentifier(src)
2025-05-24 18:03:31 -05:00
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
2025-07-11 15:23:24 -04:00
---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
2025-05-24 18:03:31 -05:00
function Clothing.SetAppearance(src, data, updateBackup, save)
src = src and tonumber(src)
assert(src, "src is nil")
2025-06-13 09:26:47 -04:00
local citId = Bridge.Framework.GetPlayerIdentifier(src)
2025-05-24 18:03:31 -05:00
if not citId then return end
local model = GetEntityModel(GetPlayerPed(src))
if not model then return end
local converted = Clothing.ConvertFromDefault(data)
2025-06-04 15:29:21 -05:00
2025-05-24 18:03:31 -05:00
-- Get full appearance data
local currentClothing = Clothing.GetFullAppearanceData(src)
if not currentClothing then return end
2025-06-04 15:29:21 -05:00
2025-05-24 18:03:31 -05:00
local currentSkin = currentClothing.skin
for k, v in pairs(converted) do
currentSkin[k] = v
end
2025-06-04 15:29:21 -05:00
2025-05-24 18:03:31 -05:00
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)
2025-06-04 15:29:21 -05:00
if save then
2025-05-24 18:03:31 -05:00
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
2025-05-24 18:03:31 -05:00
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
2025-05-24 18:03:31 -05:00
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
---This will open the menu for the passed src
---@param src number|string
2025-05-24 18:03:31 -05:00
function Clothing.OpenMenu(src)
src = src and tonumber(src)
2025-06-04 15:29:21 -05:00
assert(src, "src is nil")
2025-05-24 18:03:31 -05:00
TriggerClientEvent('esx_skin:openMenu', src)
end
---Event handler for when a player loads into the server
---Caches the player's appearance data
2025-05-24 18:03:31 -05:00
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
2025-05-24 18:03:31 -05:00
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
2025-05-24 18:03:31 -05:00
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
2025-05-24 18:03:31 -05:00
Callback.Register('community_bridge:cb:GetAppearance', function(source)
local src = source
return Clothing.GetAppearance(src)
2025-07-11 15:23:24 -04:00
end)