Server Open Files

Open Source Files

if Framework.ESX() then
    return
end

exports['nf-core']:CheckVersion("nf-skin")

local QBCore = exports['qb-core']:GetCoreObject()

lib.callback.register('nf-skin:server:getSkin', function(source, citizen_id)
    local currentSkin = MySQL.single.await('SELECT * FROM `playerskins` WHERE citizenid = ?', {
        citizen_id or QBCore.Functions.GetPlayer(source).PlayerData.citizenid
    })

    return currentSkin.model, json.decode(currentSkin.skin)
end)

RegisterNetEvent('qb-clothes:loadPlayerSkin', function()
    local _source = source
    if not _source then
        return
    end

    local citizen_id = QBCore.Functions.GetPlayer(_source).PlayerData.citizenid
    local currentSkin = MySQL.single.await('SELECT * FROM `playerskins` WHERE citizenid = ?', {
        citizen_id
    })

    if currentSkin then
        TriggerClientEvent('nf-skin:client:loadSkin', _source, currentSkin.model, json.decode(currentSkin.skin))
    elseif not currentSkin then
        TriggerClientEvent('nf-skin:client:loadSkin', _source, false)
    end
end)

RegisterNetEvent('nf-skin:server:loadPlayerSkin', function()
    local _source = source
    if not _source then
        return
    end

    local citizen_id = QBCore.Functions.GetPlayer(_source).PlayerData.citizenid
    local currentSkin = MySQL.single.await('SELECT * FROM `playerskins` WHERE citizenid = ?', {
        citizen_id
    })

    if currentSkin then
        TriggerClientEvent('nf-skin:client:loadSkin', _source, currentSkin.model, json.decode(currentSkin.skin))
    elseif not currentSkin then
        TriggerClientEvent('nf-skin:client:loadSkin', _source, false)
    end
end)

RegisterNetEvent('nf-skin:server:saveSkin', function(outfitname, model, skinData)
    local _source = source
    if not _source then
        return
    end

    local citizen_id = QBCore.Functions.GetPlayer(_source).PlayerData.citizenid

    if outfitname then
        MySQL.insert.await(
            'INSERT INTO `player_outfits` (citizenid, model, skin, outfitname) VALUES (?, ?, ?, ?)', {
                citizen_id,
                model,
                json.encode({
                    components = skinData.components,
                    props = skinData.props
                }),
                outfitname
            })
    end

    local skin_exists = MySQL.single.await('SELECT `id` FROM `playerskins` WHERE citizenid = ?',
        { citizen_id })

    if skin_exists then
        MySQL.update.await('UPDATE `playerskins` SET model = ?, skin = ? WHERE citizenid = ?', {
            model,
            json.encode(skinData),
            citizen_id
        })
    else
        MySQL.insert.await(
            'INSERT INTO `playerskins` (citizenid, model, skin) VALUES (?, ?, ?)', {
                citizen_id,
                model,
                json.encode(skinData)
            })
    end
end)

local function getOutfits(source, citizen_id)
    return MySQL.query.await(
        'SELECT `id`, `outfitname`, `outfitId` FROM `player_outfits` WHERE citizenid = ?', {
            citizen_id or QBCore.Functions.GetPlayer(source).PlayerData.citizenid
        })
end

lib.callback.register('nf-skin:server:getOutfits', function(source, citizen_id)
    return getOutfits(source, citizen_id)
end)

lib.callback.register('nf-skin:server:addOutfit', function(source, outfitname, model, skinData)
    local citizen_id = QBCore.Functions.GetPlayer(source).PlayerData.citizenid

    MySQL.insert.await('INSERT INTO `player_outfits` (citizenid, model, skin, outfitname) VALUES (?, ?, ?, ?)', {
        citizen_id,
        model,
        json.encode({
            components = skinData.components,
            props = skinData.props
        }),
        outfitname
    })

    return getOutfits(source, citizen_id)
end)

lib.callback.register('nf-skin:server:renameOutfit', function(source, outfit)
    MySQL.update.await('UPDATE `player_outfits` SET outfitname = ? WHERE id = ?', {
        outfit.outfitname,
        outfit.id
    })

    return getOutfits(source)
end)

lib.callback.register('nf-skin:server:deleteOutfit', function(source, outfit_id)
    MySQL.query.await('DELETE FROM `player_outfits` WHERE id = ?', {
        outfit_id
    })

    return getOutfits(source)
end)

lib.callback.register('nf-skin:server:importOutfit', function(_, outfitId)
    local outfit = MySQL.single.await('SELECT * FROM `player_outfits` WHERE outfitId = ?', {
        outfitId
    })

    if not outfit then
        return nil
    end

    return json.decode(outfit.skin)
end)

lib.callback.register('nf-skin:server:updateOutfit', function(source, outfit_id, skinData)
    MySQL.update.await('UPDATE `player_outfits` SET skin = ? WHERE id = ?', {
        json.encode({
            components = skinData.components,
            props = skinData.props
        }),
        outfit_id
    })

    return getOutfits(source)
end)

lib.callback.register('nf-skin:server:wearOutfit', function(source, outfit_id)
    local wantedOutfit = MySQL.single.await('SELECT * FROM `player_outfits` WHERE id = ? LIMIT 1', {
        outfit_id
    })
    wantedOutfit.skin = json.decode(wantedOutfit.skin)

    if not wantedOutfit then
        return nil, nil
    end

    local citizen_id = QBCore.Functions.GetPlayer(source).PlayerData.citizenid
    local playerSkin = MySQL.single.await('SELECT * FROM `playerskins` WHERE citizenid = ? LIMIT 1', {
        citizen_id
    })
    playerSkin.skin = json.decode(playerSkin.skin)

    if not playerSkin then
        return nil, nil
    end

    playerSkin.skin.components = wantedOutfit.skin.components
    playerSkin.skin.props = wantedOutfit.skin.props

    MySQL.update.await('UPDATE `playerskins` SET model = ?, skin = ? WHERE citizenid = ?', {
        wantedOutfit.model,
        json.encode(playerSkin.skin),
        citizen_id
    })

    return wantedOutfit.model, wantedOutfit.skin
end)


local function GetRoomOutfits(source, edit, type)
    local xPlayer = QBCore.Functions.GetPlayer(source).PlayerData

    local response = {}

    if edit then
        response = MySQL.query.await(
            'SELECT `id`, `outfitname`, `grades` FROM `workoutfits` WHERE gender = ? AND job = ?',
            {
                xPlayer.charinfo.gender,
                xPlayer[type].name
            })
    else
        response = MySQL.query.await(
            'SELECT `id`, `outfitname`, `grades` FROM `workoutfits` WHERE gender = ? AND job = ? AND grades LIKE ?',
            {
                xPlayer.charinfo.gender,
                xPlayer[type].name,
                '%' .. xPlayer[type].grade.level .. '%'
            })
    end

    for i = 1, #response do
        response[i].skin = json.decode(response[i].skin)
        response[i].grades = json.decode(response[i].grades)
    end


    return response
end

lib.callback.register('nf-skin:server:getRoomOutfits', function(source, edit, type)
    return GetRoomOutfits(source, edit, type)
end)

lib.callback.register('nf-skin:server:wearRoomOutfit', function(_, outfit_id)
    local row = MySQL.single.await('SELECT `skin` FROM `workoutfits` WHERE `id` = ? LIMIT 1', {
        outfit_id
    })

    return json.decode(row.skin)
end)

lib.callback.register('nf-skin:server:addRoomOutfit', function(source, outfit, skinData, type)
    local xPlayer = QBCore.Functions.GetPlayer(source).PlayerData

    MySQL.insert.await(
        'INSERT INTO `workoutfits` (`gender`, `job`, `grades`, `outfitname`, `skin`) VALUES (?, ?, ?, ?, ?)', {
            xPlayer.charinfo.gender,
            xPlayer.job.name,
            json.encode(outfit.grades),
            outfit.outfitname,
            json.encode({
                components = skinData.components,
                props = skinData.props
            })
        })

    return GetRoomOutfits(source, true, type)
end)

lib.callback.register('nf-skin:server:updateRoomOutfit', function(source, outfit_id, skinData, type)
    MySQL.update.await('UPDATE `workoutfits` SET skin = ? WHERE id = ?', {
        json.encode({
            components = skinData.components,
            props = skinData.props
        }),
        outfit_id
    })

    return GetRoomOutfits(source, true, type)
end)

lib.callback.register('nf-skin:server:renameRoomOutfit', function(source, outfit, type)
    MySQL.update.await('UPDATE `workoutfits` SET outfitname = ?, grades = ? WHERE id = ?', {
        outfit.outfitname,
        json.encode(outfit.grades),
        outfit.id
    })

    return GetRoomOutfits(source, true, type)
end)

lib.callback.register('nf-skin:server:deleteRoomOutfit', function(source, outfit_id, type)
    MySQL.query.await('DELETE FROM `workoutfits` WHERE id = ?', {
        outfit_id
    })

    return GetRoomOutfits(source, true, type)
end)

lib.callback.register("nf-skin:server:getPlayerAces", function(source)
    local allowedAces = {}
    for i = 1, #Config.Aces do
        local ace = Config.Aces[i]
        if IsPlayerAceAllowed(source, ace) then
            allowedAces[#allowedAces + 1] = ace
        end
    end
    return allowedAces
end)

Last updated