From 3d569b3e9a22b4c6ed71250868052976fccb2241 Mon Sep 17 00:00:00 2001 From: Mycroft Date: Wed, 31 Aug 2022 15:17:28 +0100 Subject: [PATCH] es_extended -> formatting --- [esx]/es_extended/server/common.lua | 148 ++-- [esx]/es_extended/server/functions.lua | 599 +++++++------- [esx]/es_extended/server/main.lua | 1048 +++++++++++------------- [esx]/es_extended/server/paycheck.lua | 78 +- 4 files changed, 895 insertions(+), 978 deletions(-) diff --git a/[esx]/es_extended/server/common.lua b/[esx]/es_extended/server/common.lua index 468ead43..4a78d2cc 100644 --- a/[esx]/es_extended/server/common.lua +++ b/[esx]/es_extended/server/common.lua @@ -13,115 +13,101 @@ Core.PickupId = 0 Core.PlayerFunctionOverrides = {} AddEventHandler('esx:getSharedObject', function(cb) - cb(ESX) + cb(ESX) end) exports('getSharedObject', function() - return ESX + return ESX end) if GetResourceState('ox_inventory') ~= 'missing' then - Config.OxInventory = true - Config.PlayerFunctionOverride = 'OxInventory' - SetConvarReplicated('inventory:framework', 'esx') - SetConvarReplicated('inventory:weight', Config.MaxWeight * 1000) + Config.OxInventory = true + Config.PlayerFunctionOverride = 'OxInventory' + SetConvarReplicated('inventory:framework', 'esx') + SetConvarReplicated('inventory:weight', Config.MaxWeight * 1000) end local function StartDBSync() - CreateThread(function() - while true do - Wait(10 * 60 * 1000) - Core.SavePlayers() - end - end) + CreateThread(function() + while true do + Wait(10 * 60 * 1000) + Core.SavePlayers() + end + end) end MySQL.ready(function() - if not Config.OxInventory then - local items = MySQL.query.await('SELECT * FROM items') - for k, v in ipairs(items) do - ESX.Items[v.name] = { - label = v.label, - weight = v.weight, - rare = v.rare, - canRemove = v.can_remove - } - end - else - TriggerEvent('__cfx_export_ox_inventory_Items', function(ref) - if ref then - ESX.Items = ref() - end - end) + if not Config.OxInventory then + local items = MySQL.query.await('SELECT * FROM items') + for k, v in ipairs(items) do + ESX.Items[v.name] = {label = v.label, weight = v.weight, rare = v.rare, canRemove = v.can_remove} + end + else + TriggerEvent('__cfx_export_ox_inventory_Items', function(ref) + if ref then + ESX.Items = ref() + end + end) - AddEventHandler('ox_inventory:itemList', function(items) - ESX.Items = items - end) + AddEventHandler('ox_inventory:itemList', function(items) + ESX.Items = items + end) - while not next(ESX.Items) do Wait(0) end - end + while not next(ESX.Items) do + Wait(0) + end + end - local Jobs = {} - local jobs = MySQL.query.await('SELECT * FROM jobs') + local Jobs = {} + local jobs = MySQL.query.await('SELECT * FROM jobs') - for _, v in ipairs(jobs) do - Jobs[v.name] = v - Jobs[v.name].grades = {} - end + for _, v in ipairs(jobs) do + Jobs[v.name] = v + Jobs[v.name].grades = {} + end - local jobGrades = MySQL.query.await('SELECT * FROM job_grades') + local jobGrades = MySQL.query.await('SELECT * FROM job_grades') - for _, v in ipairs(jobGrades) do - if Jobs[v.job_name] then - Jobs[v.job_name].grades[tostring(v.grade)] = v - else - print(('[^3WARNING^7] Ignoring job grades for ^5"%s"^0 due to missing job'):format(v.job_name)) - end - end + for _, v in ipairs(jobGrades) do + if Jobs[v.job_name] then + Jobs[v.job_name].grades[tostring(v.grade)] = v + else + print(('[^3WARNING^7] Ignoring job grades for ^5"%s"^0 due to missing job'):format(v.job_name)) + end + end - for _, v in pairs(Jobs) do - if ESX.Table.SizeOf(v.grades) == 0 then - Jobs[v.name] = nil - print(('[^3WARNING^7] Ignoring job ^5"%s"^0 due to no job grades found'):format(v.name)) - end - end + for _, v in pairs(Jobs) do + if ESX.Table.SizeOf(v.grades) == 0 then + Jobs[v.name] = nil + print(('[^3WARNING^7] Ignoring job ^5"%s"^0 due to no job grades found'):format(v.name)) + end + end - if not Jobs then - -- Fallback data, if no jobs exist - ESX.Jobs['unemployed'] = { - label = 'Unemployed', - grades = { - ['0'] = { - grade = 0, - label = 'Unemployed', - salary = 200, - onDuty = false, - skin_male = {}, - skin_female = {} - } - } - } - else - ESX.Jobs = Jobs - end + if not Jobs then + -- Fallback data, if no jobs exist + ESX.Jobs['unemployed'] = {label = 'Unemployed', + grades = {['0'] = {grade = 0, label = 'Unemployed', salary = 200, skin_male = {}, skin_female = {}}}} + else + ESX.Jobs = Jobs + end - print('[^2INFO^7] ESX ^5Legacy^0 initialized') - StartDBSync() - StartPayCheck() + print('[^2INFO^7] ESX ^5Legacy^0 initialized') + StartDBSync() + StartPayCheck() end) RegisterServerEvent('esx:clientLog') AddEventHandler('esx:clientLog', function(msg) - if Config.EnableDebug then - print(('[^2TRACE^7] %s^7'):format(msg)) - end + if Config.EnableDebug then + print(('[^2TRACE^7] %s^7'):format(msg)) + end end) RegisterServerEvent('esx:triggerServerCallback') AddEventHandler('esx:triggerServerCallback', function(name, requestId, ...) - local playerId = source + local playerId = source - ESX.TriggerServerCallback(name, requestId, playerId, function(...) - TriggerClientEvent('esx:serverCallback', playerId, requestId, ...) - end, ...) + ESX.TriggerServerCallback(name, requestId, playerId, function(...) + TriggerClientEvent('esx:serverCallback', playerId, requestId, ...) + end, ...) end) diff --git a/[esx]/es_extended/server/functions.lua b/[esx]/es_extended/server/functions.lua index a80b3c61..a9df8d1b 100644 --- a/[esx]/es_extended/server/functions.lua +++ b/[esx]/es_extended/server/functions.lua @@ -1,414 +1,403 @@ function ESX.Trace(msg) - if Config.EnableDebug then - print(('[^2TRACE^7] %s^7'):format(msg)) - end + if Config.EnableDebug then + print(('[^2TRACE^7] %s^7'):format(msg)) + end end function ESX.SetTimeout(msec, cb) - local id = Core.TimeoutCount + 1 + local id = Core.TimeoutCount + 1 - SetTimeout(msec, function() - if Core.CancelledTimeouts[id] then - Core.CancelledTimeouts[id] = nil - else - cb() - end - end) + SetTimeout(msec, function() + if Core.CancelledTimeouts[id] then + Core.CancelledTimeouts[id] = nil + else + cb() + end + end) - Core.TimeoutCount = id + Core.TimeoutCount = id - return id + return id end function ESX.RegisterCommand(name, group, cb, allowConsole, suggestion) - if type(name) == 'table' then - for k,v in ipairs(name) do - ESX.RegisterCommand(v, group, cb, allowConsole, suggestion) - end + if type(name) == 'table' then + for k, v in ipairs(name) do + ESX.RegisterCommand(v, group, cb, allowConsole, suggestion) + end - return - end + return + end - if Core.RegisteredCommands[name] then - print(('[^3WARNING^7] Command ^5"%s" ^7already registered, overriding command'):format(name)) + if Core.RegisteredCommands[name] then + print(('[^3WARNING^7] Command ^5"%s" ^7already registered, overriding command'):format(name)) - if Core.RegisteredCommands[name].suggestion then - TriggerClientEvent('chat:removeSuggestion', -1, ('/%s'):format(name)) - end - end + if Core.RegisteredCommands[name].suggestion then + TriggerClientEvent('chat:removeSuggestion', -1, ('/%s'):format(name)) + end + end - if suggestion then - if not suggestion.arguments then suggestion.arguments = {} end - if not suggestion.help then suggestion.help = '' end + if suggestion then + if not suggestion.arguments then + suggestion.arguments = {} + end + if not suggestion.help then + suggestion.help = '' + end - TriggerClientEvent('chat:addSuggestion', -1, ('/%s'):format(name), suggestion.help, suggestion.arguments) - end + TriggerClientEvent('chat:addSuggestion', -1, ('/%s'):format(name), suggestion.help, suggestion.arguments) + end - Core.RegisteredCommands[name] = {group = group, cb = cb, allowConsole = allowConsole, suggestion = suggestion} + Core.RegisteredCommands[name] = {group = group, cb = cb, allowConsole = allowConsole, suggestion = suggestion} - RegisterCommand(name, function(playerId, args, rawCommand) - local command = Core.RegisteredCommands[name] + RegisterCommand(name, function(playerId, args, rawCommand) + local command = Core.RegisteredCommands[name] - if not command.allowConsole and playerId == 0 then - print(('[^3WARNING^7] ^5%s'):format(_U('commanderror_console'))) - else - local xPlayer, error = ESX.Players[playerId], nil + if not command.allowConsole and playerId == 0 then + print(('[^3WARNING^7] ^5%s'):format(_U('commanderror_console'))) + else + local xPlayer, error = ESX.Players[playerId], nil - if command.suggestion then - if command.suggestion.validate then - if #args ~= #command.suggestion.arguments then - error = _U('commanderror_argumentmismatch', #args, #command.suggestion.arguments) - end - end + if command.suggestion then + if command.suggestion.validate then + if #args ~= #command.suggestion.arguments then + error = _U('commanderror_argumentmismatch', #args, #command.suggestion.arguments) + end + end - if not error and command.suggestion.arguments then - local newArgs = {} + if not error and command.suggestion.arguments then + local newArgs = {} - for k,v in ipairs(command.suggestion.arguments) do - if v.type then - if v.type == 'number' then - local newArg = tonumber(args[k]) + for k, v in ipairs(command.suggestion.arguments) do + if v.type then + if v.type == 'number' then + local newArg = tonumber(args[k]) - if newArg then - newArgs[v.name] = newArg - else - error = _U('commanderror_argumentmismatch_number', k) - end - elseif v.type == 'player' or v.type == 'playerId' then - local targetPlayer = tonumber(args[k]) + if newArg then + newArgs[v.name] = newArg + else + error = _U('commanderror_argumentmismatch_number', k) + end + elseif v.type == 'player' or v.type == 'playerId' then + local targetPlayer = tonumber(args[k]) - if args[k] == 'me' then targetPlayer = playerId end + if args[k] == 'me' then + targetPlayer = playerId + end - if targetPlayer then - local xTargetPlayer = ESX.GetPlayerFromId(targetPlayer) + if targetPlayer then + local xTargetPlayer = ESX.GetPlayerFromId(targetPlayer) - if xTargetPlayer then - if v.type == 'player' then - newArgs[v.name] = xTargetPlayer - else - newArgs[v.name] = targetPlayer - end - else - error = _U('commanderror_invalidplayerid') - end - else - error = _U('commanderror_argumentmismatch_number', k) - end - elseif v.type == 'string' then - newArgs[v.name] = args[k] - elseif v.type == 'item' then - if ESX.Items[args[k]] then - newArgs[v.name] = args[k] - else - error = _U('commanderror_invaliditem') - end - elseif v.type == 'weapon' then - if ESX.GetWeapon(args[k]) then - newArgs[v.name] = string.upper(args[k]) - else - error = _U('commanderror_invalidweapon') - end - elseif v.type == 'any' then - newArgs[v.name] = args[k] - end - end - - if v.validate == false then - error = nil - end + if xTargetPlayer then + if v.type == 'player' then + newArgs[v.name] = xTargetPlayer + else + newArgs[v.name] = targetPlayer + end + else + error = _U('commanderror_invalidplayerid') + end + else + error = _U('commanderror_argumentmismatch_number', k) + end + elseif v.type == 'string' then + newArgs[v.name] = args[k] + elseif v.type == 'item' then + if ESX.Items[args[k]] then + newArgs[v.name] = args[k] + else + error = _U('commanderror_invaliditem') + end + elseif v.type == 'weapon' then + if ESX.GetWeapon(args[k]) then + newArgs[v.name] = string.upper(args[k]) + else + error = _U('commanderror_invalidweapon') + end + elseif v.type == 'any' then + newArgs[v.name] = args[k] + end + end - if error then break end - end + if v.validate == false then + error = nil + end - args = newArgs - end - end + if error then + break + end + end - if error then - if playerId == 0 then - print(('[^3WARNING^7] %s^7'):format(error)) - else - xPlayer.showNotification(error) - end - else - cb(xPlayer or false, args, function(msg) - if playerId == 0 then - print(('[^3WARNING^7] %s^7'):format(msg)) - else - xPlayer.showNotification(msg) - end - end) - end - end - end, true) + args = newArgs + end + end - if type(group) == 'table' then - for k,v in ipairs(group) do - ExecuteCommand(('add_ace group.%s command.%s allow'):format(v, name)) - end - else - ExecuteCommand(('add_ace group.%s command.%s allow'):format(group, name)) - end + if error then + if playerId == 0 then + print(('[^3WARNING^7] %s^7'):format(error)) + else + xPlayer.showNotification(error) + end + else + cb(xPlayer or false, args, function(msg) + if playerId == 0 then + print(('[^3WARNING^7] %s^7'):format(msg)) + else + xPlayer.showNotification(msg) + end + end) + end + end + end, true) + + if type(group) == 'table' then + for k, v in ipairs(group) do + ExecuteCommand(('add_ace group.%s command.%s allow'):format(v, name)) + end + else + ExecuteCommand(('add_ace group.%s command.%s allow'):format(group, name)) + end end function ESX.ClearTimeout(id) - Core.CancelledTimeouts[id] = true + Core.CancelledTimeouts[id] = true end function ESX.RegisterServerCallback(name, cb) - Core.ServerCallbacks[name] = cb + Core.ServerCallbacks[name] = cb end function ESX.TriggerServerCallback(name, requestId, source, cb, ...) - if Core.ServerCallbacks[name] then - Core.ServerCallbacks[name](source, cb, ...) - else - print(('[^3WARNING^7] Server callback ^5"%s"^0 does not exist. ^1Please Check The Server File for Errors!'):format(name)) - end + if Core.ServerCallbacks[name] then + Core.ServerCallbacks[name](source, cb, ...) + else + print(('[^3WARNING^7] Server callback ^5"%s"^0 does not exist. ^1Please Check The Server File for Errors!'):format(name)) + end end function Core.SavePlayer(xPlayer, cb) - MySQL.prepare('UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ? WHERE `identifier` = ?', { - json.encode(xPlayer.getAccounts(true)), - xPlayer.job.name, - xPlayer.job.grade, - xPlayer.group, - json.encode(xPlayer.getCoords()), - json.encode(xPlayer.getInventory(true)), - json.encode(xPlayer.getLoadout(true)), - xPlayer.identifier - }, function(affectedRows) - if affectedRows == 1 then - print(('[^2INFO^7] Saved player ^5"%s^7"'):format(xPlayer.name)) - TriggerEvent('esx:playerSaved', xPlayer.playerId, xPlayer) - end - if cb then cb() end - end) + MySQL.prepare( + 'UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ? WHERE `identifier` = ?', + {json.encode(xPlayer.getAccounts(true)), xPlayer.job.name, xPlayer.job.grade, xPlayer.group, json.encode(xPlayer.getCoords()), + json.encode(xPlayer.getInventory(true)), json.encode(xPlayer.getLoadout(true)), xPlayer.identifier}, function(affectedRows) + if affectedRows == 1 then + print(('[^2INFO^7] Saved player ^5"%s^7"'):format(xPlayer.name)) + TriggerEvent('esx:playerSaved', xPlayer.playerId, xPlayer) + end + if cb then + cb() + end + end) end function Core.SavePlayers(cb) - local xPlayers = ESX.GetExtendedPlayers() - local count = #xPlayers - if count > 0 then - local parameters = {} - local time = os.time() - for i=1, count do - local xPlayer = xPlayers[i] - parameters[#parameters+1] = { - json.encode(xPlayer.getAccounts(true)), - xPlayer.job.name, - xPlayer.job.grade, - xPlayer.group, - json.encode(xPlayer.getCoords()), - json.encode(xPlayer.getInventory(true)), - json.encode(xPlayer.getLoadout(true)), - xPlayer.identifier - } - end - MySQL.prepare("UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ? WHERE `identifier` = ?", parameters, - function(results) - if results then - if type(cb) == 'function' then cb() else print(('[^2INFO^7] Saved %s %s over %s ms'):format(count, count > 1 and 'players' or 'player', (os.time() - time) / 1000000)) end - end - end) - end + local xPlayers = ESX.GetExtendedPlayers() + local count = #xPlayers + if count > 0 then + local parameters = {} + local time = os.time() + for i = 1, count do + local xPlayer = xPlayers[i] + parameters[#parameters + 1] = {json.encode(xPlayer.getAccounts(true)), xPlayer.job.name, xPlayer.job.grade, xPlayer.group, + json.encode(xPlayer.getCoords()), json.encode(xPlayer.getInventory(true)), json.encode(xPlayer.getLoadout(true)), + xPlayer.identifier} + end + MySQL.prepare( + "UPDATE `users` SET `accounts` = ?, `job` = ?, `job_grade` = ?, `group` = ?, `position` = ?, `inventory` = ?, `loadout` = ? WHERE `identifier` = ?", + parameters, function(results) + if results then + if type(cb) == 'function' then + cb() + else + print(('[^2INFO^7] Saved %s %s over %s ms'):format(count, count > 1 and 'players' or 'player', (os.time() - time) / 1000000)) + end + end + end) + end end function ESX.GetPlayers() - local sources = {} + local sources = {} - for k,v in pairs(ESX.Players) do - sources[#sources + 1] = k - end + for k, v in pairs(ESX.Players) do + sources[#sources + 1] = k + end - return sources + return sources end function ESX.GetExtendedPlayers(key, val) - local xPlayers = {} - for k, v in pairs(ESX.Players) do - if key then - if (key == 'job' and v.job.name == val) or v[key] == val then - xPlayers[#xPlayers + 1] = v - end - else - xPlayers[#xPlayers + 1] = v - end - end - return xPlayers + local xPlayers = {} + for k, v in pairs(ESX.Players) do + if key then + if (key == 'job' and v.job.name == val) or v[key] == val then + xPlayers[#xPlayers + 1] = v + end + else + xPlayers[#xPlayers + 1] = v + end + end + return xPlayers end function ESX.GetPlayerFromId(source) - return ESX.Players[tonumber(source)] + return ESX.Players[tonumber(source)] end function ESX.GetPlayerFromIdentifier(identifier) - for k,v in pairs(ESX.Players) do - if v.identifier == identifier then - return v - end - end + for k, v in pairs(ESX.Players) do + if v.identifier == identifier then + return v + end + end end function ESX.GetIdentifier(playerId) - for k,v in ipairs(GetPlayerIdentifiers(playerId)) do - if string.match(v, 'license:') then - local identifier = string.gsub(v, 'license:', '') - return identifier - end - end + for k, v in ipairs(GetPlayerIdentifiers(playerId)) do + if string.match(v, 'license:') then + local identifier = string.gsub(v, 'license:', '') + return identifier + end + end end -function ESX.RefreshJobs() - local Jobs = {} - local jobs = MySQL.query.await('SELECT * FROM jobs') +function ESX.RefreshJobs() + local Jobs = {} + local jobs = MySQL.query.await('SELECT * FROM jobs') - for _, v in ipairs(jobs) do - Jobs[v.name] = v - Jobs[v.name].grades = {} - end + for _, v in ipairs(jobs) do + Jobs[v.name] = v + Jobs[v.name].grades = {} + end - local jobGrades = MySQL.query.await('SELECT * FROM job_grades') + local jobGrades = MySQL.query.await('SELECT * FROM job_grades') - for _, v in ipairs(jobGrades) do - if Jobs[v.job_name] then - Jobs[v.job_name].grades[tostring(v.grade)] = v - else - print(('[^3WARNING^7] Ignoring job grades for ^5"%s"^0 due to missing job'):format(v.job_name)) - end - end + for _, v in ipairs(jobGrades) do + if Jobs[v.job_name] then + Jobs[v.job_name].grades[tostring(v.grade)] = v + else + print(('[^3WARNING^7] Ignoring job grades for ^5"%s"^0 due to missing job'):format(v.job_name)) + end + end - for _, v in pairs(Jobs) do - if ESX.Table.SizeOf(v.grades) == 0 then - Jobs[v.name] = nil - print(('[^3WARNING^7] Ignoring job ^5"%s"^0 due to no job grades found'):format(v.name)) - end - end + for _, v in pairs(Jobs) do + if ESX.Table.SizeOf(v.grades) == 0 then + Jobs[v.name] = nil + print(('[^3WARNING^7] Ignoring job ^5"%s"^0 due to no job grades found'):format(v.name)) + end + end - if not Jobs then - -- Fallback data, if no jobs exist - ESX.Jobs['unemployed'] = { - label = 'Unemployed', - grades = { - ['0'] = { - grade = 0, - label = 'Unemployed', - salary = 200, - skin_male = {}, - skin_female = {} - } - } - } - else - ESX.Jobs = Jobs - end + if not Jobs then + -- Fallback data, if no jobs exist + ESX.Jobs['unemployed'] = {label = 'Unemployed', + grades = {['0'] = {grade = 0, label = 'Unemployed', salary = 200, skin_male = {}, skin_female = {}}}} + else + ESX.Jobs = Jobs + end end function ESX.RegisterUsableItem(item, cb) - Core.UsableItemsCallbacks[item] = cb + Core.UsableItemsCallbacks[item] = cb end function ESX.UseItem(source, item, ...) - if ESX.Items[item] then - local itemCallback = Core.UsableItemsCallbacks[item] + if ESX.Items[item] then + local itemCallback = Core.UsableItemsCallbacks[item] - if itemCallback then - local success, result = pcall(itemCallback, source, item, ...) + if itemCallback then + local success, result = pcall(itemCallback, source, item, ...) - if not success then - return result and print(result) or print(('[^3WARNING^7] An error occured when using item ^5"%s"^7! This was not caused by ESX.'):format(item)) - end - end - else - print(('[^3WARNING^7] Item ^5"%s"^7 was used but does not exist!'):format(item)) - end + if not success then + return result and print(result) or + print(('[^3WARNING^7] An error occured when using item ^5"%s"^7! This was not caused by ESX.'):format(item)) + end + end + else + print(('[^3WARNING^7] Item ^5"%s"^7 was used but does not exist!'):format(item)) + end end -function ESX.RegisterPlayerFunctionOverrides(index,overrides) - Core.PlayerFunctionOverrides[index] = overrides +function ESX.RegisterPlayerFunctionOverrides(index, overrides) + Core.PlayerFunctionOverrides[index] = overrides end function ESX.SetPlayerFunctionOverride(index) - if not index - or not Core.PlayerFunctionOverrides[index] - then - return print('[^3WARNING^7] No valid index provided.') - end + if not index or not Core.PlayerFunctionOverrides[index] then + return print('[^3WARNING^7] No valid index provided.') + end - Config.PlayerFunctionOverride = index + Config.PlayerFunctionOverride = index end function ESX.GetItemLabel(item) - if Config.OxInventory then - item = exports.ox_inventory:Items(item) - if item then return item.label end - end - - if ESX.Items[item] then - return ESX.Items[item].label - else - print('[^3WARNING^7] Attemting to get invalid Item -> '..item ) - end + if Config.OxInventory then + item = exports.ox_inventory:Items(item) + if item then + return item.label + end + end + + if ESX.Items[item] then + return ESX.Items[item].label + else + print('[^3WARNING^7] Attemting to get invalid Item -> ' .. item) + end end function ESX.GetJobs() - return ESX.Jobs + return ESX.Jobs end function ESX.GetUsableItems() - local Usables = {} - for k in pairs(Core.UsableItemsCallbacks) do - Usables[k] = true - end - return Usables + local Usables = {} + for k in pairs(Core.UsableItemsCallbacks) do + Usables[k] = true + end + return Usables end if not Config.OxInventory then - function ESX.CreatePickup(type, name, count, label, playerId, components, tintIndex) - local pickupId = (Core.PickupId == 65635 and 0 or Core.PickupId + 1) - local xPlayer = ESX.Players[playerId] - local coords = xPlayer.getCoords() + function ESX.CreatePickup(type, name, count, label, playerId, components, tintIndex) + local pickupId = (Core.PickupId == 65635 and 0 or Core.PickupId + 1) + local xPlayer = ESX.Players[playerId] + local coords = xPlayer.getCoords() - Core.Pickups[pickupId] = { - type = type, name = name, - count = count, label = label, - coords = coords - } + Core.Pickups[pickupId] = {type = type, name = name, count = count, label = label, coords = coords} - if type == 'item_weapon' then - Core.Pickups[pickupId].components = components - Core.Pickups[pickupId].tintIndex = tintIndex - end + if type == 'item_weapon' then + Core.Pickups[pickupId].components = components + Core.Pickups[pickupId].tintIndex = tintIndex + end - TriggerClientEvent('esx:createPickup', -1, pickupId, label, coords, type, name, components, tintIndex) - Core.PickupId = pickupId - end + TriggerClientEvent('esx:createPickup', -1, pickupId, label, coords, type, name, components, tintIndex) + Core.PickupId = pickupId + end end function ESX.DoesJobExist(job, grade) - grade = tostring(grade) + grade = tostring(grade) - if job and grade then - if ESX.Jobs[job] and ESX.Jobs[job].grades[grade] then - return true - end - end + if job and grade then + if ESX.Jobs[job] and ESX.Jobs[job].grades[grade] then + return true + end + end - return false + return false end function Core.IsPlayerAdmin(playerId) - if (IsPlayerAceAllowed(playerId, 'command') or GetConvar('sv_lan', '') == 'true') and true or false then - return true - end + if (IsPlayerAceAllowed(playerId, 'command') or GetConvar('sv_lan', '') == 'true') and true or false then + return true + end - local xPlayer = ESX.Players[playerId] + local xPlayer = ESX.Players[playerId] - if xPlayer then - if xPlayer.group == 'admin' then - return true - end - end + if xPlayer then + if xPlayer.group == 'admin' then + return true + end + end - return false + return false end diff --git a/[esx]/es_extended/server/main.lua b/[esx]/es_extended/server/main.lua index 538a01a6..7ca0a701 100644 --- a/[esx]/es_extended/server/main.lua +++ b/[esx]/es_extended/server/main.lua @@ -5,675 +5,613 @@ local newPlayer = 'INSERT INTO `users` SET `accounts` = ?, `identifier` = ?, `gr local loadPlayer = 'SELECT `accounts`, `job`, `job_grade`, `group`, `position`, `inventory`, `skin`, `loadout`' if Config.Multichar then - newPlayer = newPlayer .. ', `firstname` = ?, `lastname` = ?, `dateofbirth` = ?, `sex` = ?, `height` = ?' + newPlayer = newPlayer .. ', `firstname` = ?, `lastname` = ?, `dateofbirth` = ?, `sex` = ?, `height` = ?' end if Config.Multichar or Config.Identity then - loadPlayer = loadPlayer .. ', `firstname`, `lastname`, `dateofbirth`, `sex`, `height`' + loadPlayer = loadPlayer .. ', `firstname`, `lastname`, `dateofbirth`, `sex`, `height`' end loadPlayer = loadPlayer .. ' FROM `users` WHERE identifier = ?' if Config.Multichar then - AddEventHandler('esx:onPlayerJoined', function(src, char, data) - while not next(ESX.Jobs) do - Wait(50) - end + AddEventHandler('esx:onPlayerJoined', function(src, char, data) + while not next(ESX.Jobs) do + Wait(50) + end - if not ESX.Players[src] then - local identifier = char .. ':' .. ESX.GetIdentifier(src) - if data then - createESXPlayer(identifier, src, data) - else - loadESXPlayer(identifier, src, false) - end - end - end) + if not ESX.Players[src] then + local identifier = char .. ':' .. ESX.GetIdentifier(src) + if data then + createESXPlayer(identifier, src, data) + else + loadESXPlayer(identifier, src, false) + end + end + end) else - RegisterNetEvent('esx:onPlayerJoined') - AddEventHandler('esx:onPlayerJoined', function() - local _source = source - while not next(ESX.Jobs) do - Wait(50) - end + RegisterNetEvent('esx:onPlayerJoined') + AddEventHandler('esx:onPlayerJoined', function() + local _source = source + while not next(ESX.Jobs) do + Wait(50) + end - if not ESX.Players[_source] then - onPlayerJoined(_source) - end - end) + if not ESX.Players[_source] then + onPlayerJoined(_source) + end + end) end function onPlayerJoined(playerId) - local identifier = ESX.GetIdentifier(playerId) - if identifier then - if ESX.GetPlayerFromIdentifier(identifier) then - DropPlayer(playerId, - ('there was an error loading your character!\nError code: identifier-active-ingame\n\nThis error is caused by a player on this server who has the same identifier as you have. Make sure you are not playing on the same Rockstar account.\n\nYour Rockstar identifier: %s'):format( - identifier)) - else - local result = MySQL.scalar.await('SELECT 1 FROM users WHERE identifier = ?', {identifier}) - if result then - loadESXPlayer(identifier, playerId, false) - else - createESXPlayer(identifier, playerId) - end - end + local identifier = ESX.GetIdentifier(playerId) + if identifier then + if ESX.GetPlayerFromIdentifier(identifier) then + DropPlayer(playerId, + ('there was an error loading your character!\nError code: identifier-active-ingame\n\nThis error is caused by a player on this server who has the same identifier as you have. Make sure you are not playing on the same Rockstar account.\n\nYour Rockstar identifier: %s'):format( + identifier)) else - DropPlayer(playerId, - 'there was an error loading your character!\nError code: identifier-missing-ingame\n\nThe cause of this error is not known, your identifier could not be found. Please come back later or report this problem to the server administration team.') + local result = MySQL.scalar.await('SELECT 1 FROM users WHERE identifier = ?', {identifier}) + if result then + loadESXPlayer(identifier, playerId, false) + else + createESXPlayer(identifier, playerId) + end end + else + DropPlayer(playerId, + 'there was an error loading your character!\nError code: identifier-missing-ingame\n\nThe cause of this error is not known, your identifier could not be found. Please come back later or report this problem to the server administration team.') + end end function createESXPlayer(identifier, playerId, data) - local accounts = {} + local accounts = {} - for account, money in pairs(Config.StartingAccountMoney) do - accounts[account] = money - end + for account, money in pairs(Config.StartingAccountMoney) do + accounts[account] = money + end - if Core.IsPlayerAdmin(playerId) then - print(('^2[INFO] ^0 Player ^5%s ^0Has been granted admin permissions via ^5Ace Perms.^7'):format(playerId)) - defaultGroup = "admin" - else - defaultGroup = "user" - end + if Core.IsPlayerAdmin(playerId) then + print(('^2[INFO] ^0 Player ^5%s ^0Has been granted admin permissions via ^5Ace Perms.^7'):format(playerId)) + defaultGroup = "admin" + else + defaultGroup = "user" + end - if not Config.Multichar then - MySQL.prepare(newPlayer, {json.encode(accounts), identifier, defaultGroup}, function() - loadESXPlayer(identifier, playerId, true) - end) - else - MySQL.prepare(newPlayer, - {json.encode(accounts), identifier, defaultGroup, data.firstname, data.lastname, data.dateofbirth, data.sex, - data.height}, function() - loadESXPlayer(identifier, playerId, true) - end) - end + if not Config.Multichar then + MySQL.prepare(newPlayer, {json.encode(accounts), identifier, defaultGroup}, function() + loadESXPlayer(identifier, playerId, true) + end) + else + MySQL.prepare(newPlayer, + {json.encode(accounts), identifier, defaultGroup, data.firstname, data.lastname, data.dateofbirth, data.sex, data.height}, function() + loadESXPlayer(identifier, playerId, true) + end) + end end if not Config.Multichar then - AddEventHandler('playerConnecting', function(name, setCallback, deferrals) - deferrals.defer() - local playerId = source - local identifier = ESX.GetIdentifier(playerId) + AddEventHandler('playerConnecting', function(name, setCallback, deferrals) + deferrals.defer() + local playerId = source + local identifier = ESX.GetIdentifier(playerId) - if identifier then - if ESX.GetPlayerFromIdentifier(identifier) then - deferrals.done( - ('There was an error loading your character!\nError code: identifier-active\n\nThis error is caused by a player on this server who has the same identifier as you have. Make sure you are not playing on the same account.\n\nYour identifier: %s'):format( - identifier)) - else - deferrals.done() - end - else - deferrals.done( - 'There was an error loading your character!\nError code: identifier-missing\n\nThe cause of this error is not known, your identifier could not be found. Please come back later or report this problem to the server administration team.') - end - end) + if identifier then + if ESX.GetPlayerFromIdentifier(identifier) then + deferrals.done( + ('There was an error loading your character!\nError code: identifier-active\n\nThis error is caused by a player on this server who has the same identifier as you have. Make sure you are not playing on the same account.\n\nYour identifier: %s'):format( + identifier)) + else + deferrals.done() + end + else + deferrals.done( + 'There was an error loading your character!\nError code: identifier-missing\n\nThe cause of this error is not known, your identifier could not be found. Please come back later or report this problem to the server administration team.') + end + end) end function loadESXPlayer(identifier, playerId, isNew) - local userData = { - accounts = {}, - inventory = {}, - job = {}, - loadout = {}, - playerName = GetPlayerName(playerId), - weight = 0 - } + local userData = {accounts = {}, inventory = {}, job = {}, loadout = {}, playerName = GetPlayerName(playerId), weight = 0} - local result = MySQL.prepare.await(loadPlayer, {identifier}) - local job, grade, jobObject, gradeObject = result.job, tostring(result.job_grade) - local foundAccounts, foundItems = {}, {} + local result = MySQL.prepare.await(loadPlayer, {identifier}) + local job, grade, jobObject, gradeObject = result.job, tostring(result.job_grade) + local foundAccounts, foundItems = {}, {} - -- Accounts - if result.accounts and result.accounts ~= '' then - local accounts = json.decode(result.accounts) + -- Accounts + if result.accounts and result.accounts ~= '' then + local accounts = json.decode(result.accounts) - for account, money in pairs(accounts) do - foundAccounts[account] = money - end + for account, money in pairs(accounts) do + foundAccounts[account] = money end + end - for account, data in pairs(Config.Accounts) do - if data.round == nil then - data.round = true - end - userData.accounts[#userData.accounts + 1] ={ - name = account, - money = foundAccounts[account] or Config.StartingAccountMoney[account] or 0, - label = data.label, - round = data.round - } + for account, data in pairs(Config.Accounts) do + if data.round == nil then + data.round = true end + userData.accounts[#userData.accounts + 1] = {name = account, money = foundAccounts[account] or Config.StartingAccountMoney[account] or 0, + label = data.label, round = data.round} + end - -- Job - if ESX.DoesJobExist(job, grade) then - jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade] - else - print(('[^3WARNING^7] Ignoring invalid job for %s [job: %s, grade: %s]'):format(identifier, job, grade)) - job, grade = 'unemployed', '0' - jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade] - end + -- Job + if ESX.DoesJobExist(job, grade) then + jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade] + else + print(('[^3WARNING^7] Ignoring invalid job for %s [job: %s, grade: %s]'):format(identifier, job, grade)) + job, grade = 'unemployed', '0' + jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade] + end - userData.job.id = jobObject.id - userData.job.name = jobObject.name - userData.job.label = jobObject.label + userData.job.id = jobObject.id + userData.job.name = jobObject.name + userData.job.label = jobObject.label - userData.job.grade = tonumber(grade) - userData.job.grade_name = gradeObject.name - userData.job.grade_label = gradeObject.label - userData.job.grade_salary = gradeObject.salary + userData.job.grade = tonumber(grade) + userData.job.grade_name = gradeObject.name + userData.job.grade_label = gradeObject.label + userData.job.grade_salary = gradeObject.salary - userData.job.skin_male = {} - userData.job.skin_female = {} + userData.job.skin_male = {} + userData.job.skin_female = {} - if gradeObject.skin_male then - userData.job.skin_male = json.decode(gradeObject.skin_male) - end - if gradeObject.skin_female then - userData.job.skin_female = json.decode(gradeObject.skin_female) - end + if gradeObject.skin_male then + userData.job.skin_male = json.decode(gradeObject.skin_male) + end + if gradeObject.skin_female then + userData.job.skin_female = json.decode(gradeObject.skin_female) + end - -- Inventory - if not Config.OxInventory then - if result.inventory and result.inventory ~= '' then - local inventory = json.decode(result.inventory) + -- Inventory + if not Config.OxInventory then + if result.inventory and result.inventory ~= '' then + local inventory = json.decode(result.inventory) - for name, count in pairs(inventory) do - local item = ESX.Items[name] + for name, count in pairs(inventory) do + local item = ESX.Items[name] - if item then - foundItems[name] = count - else - print(('[^3WARNING^7] Ignoring invalid item "%s" for "%s"'):format(name, identifier)) - end - end - end - - for name, item in pairs(ESX.Items) do - local count = foundItems[name] or 0 - if count > 0 then - userData.weight = userData.weight + (item.weight * count) - end - - table.insert(userData.inventory, { - name = name, - count = count, - label = item.label, - weight = item.weight, - usable = Core.UsableItemsCallbacks[name] ~= nil, - rare = item.rare, - canRemove = item.canRemove - }) - end - - table.sort(userData.inventory, function(a, b) - return a.label < b.label - end) - else - if result.inventory and result.inventory ~= '' then - userData.inventory = json.decode(result.inventory) + if item then + foundItems[name] = count else - userData.inventory = {} + print(('[^3WARNING^7] Ignoring invalid item "%s" for "%s"'):format(name, identifier)) end + end end - -- Group - if result.group then - if result.group == "superadmin" then - userData.group = "admin" - print("[^3WARNING^7] Superadmin detected, setting group to admin") - else - userData.group = result.group - end + for name, item in pairs(ESX.Items) do + local count = foundItems[name] or 0 + if count > 0 then + userData.weight = userData.weight + (item.weight * count) + end + + table.insert(userData.inventory, + {name = name, count = count, label = item.label, weight = item.weight, usable = Core.UsableItemsCallbacks[name] ~= nil, rare = item.rare, + canRemove = item.canRemove}) + end + + table.sort(userData.inventory, function(a, b) + return a.label < b.label + end) + else + if result.inventory and result.inventory ~= '' then + userData.inventory = json.decode(result.inventory) else - userData.group = 'user' + userData.inventory = {} end + end - -- Loadout - if not Config.OxInventory then - if result.loadout and result.loadout ~= '' then - local loadout = json.decode(result.loadout) - - for name, weapon in pairs(loadout) do - local label = ESX.GetWeaponLabel(name) - - if label then - if not weapon.components then - weapon.components = {} - end - if not weapon.tintIndex then - weapon.tintIndex = 0 - end - - table.insert(userData.loadout, { - name = name, - ammo = weapon.ammo, - label = label, - components = weapon.components, - tintIndex = weapon.tintIndex - }) - end - end - end - end - - -- Position - if result.position and result.position ~= '' then - userData.coords = json.decode(result.position) + -- Group + if result.group then + if result.group == "superadmin" then + userData.group = "admin" + print("[^3WARNING^7] Superadmin detected, setting group to admin") else - print( - '[^3WARNING^7] Column ^5"position"^0 in ^5"users"^0 table is missing required default value. Using backup coords, fix your database.') - userData.coords = { - x = -269.4, - y = -955.3, - z = 31.2, - heading = 205.8 - } + userData.group = result.group end + else + userData.group = 'user' + end - -- Skin - if result.skin and result.skin ~= '' then - userData.skin = json.decode(result.skin) + -- Loadout + if not Config.OxInventory then + if result.loadout and result.loadout ~= '' then + local loadout = json.decode(result.loadout) + + for name, weapon in pairs(loadout) do + local label = ESX.GetWeaponLabel(name) + + if label then + if not weapon.components then + weapon.components = {} + end + if not weapon.tintIndex then + weapon.tintIndex = 0 + end + + table.insert(userData.loadout, + {name = name, ammo = weapon.ammo, label = label, components = weapon.components, tintIndex = weapon.tintIndex}) + end + end + end + end + + -- Position + if result.position and result.position ~= '' then + userData.coords = json.decode(result.position) + else + print('[^3WARNING^7] Column ^5"position"^0 in ^5"users"^0 table is missing required default value. Using backup coords, fix your database.') + userData.coords = {x = -269.4, y = -955.3, z = 31.2, heading = 205.8} + end + + -- Skin + if result.skin and result.skin ~= '' then + userData.skin = json.decode(result.skin) + else + if userData.sex == 'f' then + userData.skin = {sex = 1} else - if userData.sex == 'f' then - userData.skin = { - sex = 1 - } - else - userData.skin = { - sex = 0 - } - end + userData.skin = {sex = 0} end + end - -- Identity - if result.firstname and result.firstname ~= '' then - userData.firstname = result.firstname - userData.lastname = result.lastname - userData.playerName = userData.firstname .. ' ' .. userData.lastname - if result.dateofbirth then - userData.dateofbirth = result.dateofbirth - end - if result.sex then - userData.sex = result.sex - end - if result.height then - userData.height = result.height - end + -- Identity + if result.firstname and result.firstname ~= '' then + userData.firstname = result.firstname + userData.lastname = result.lastname + userData.playerName = userData.firstname .. ' ' .. userData.lastname + if result.dateofbirth then + userData.dateofbirth = result.dateofbirth end - - local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, - userData.weight, userData.job, userData.loadout, userData.playerName, userData.coords) - ESX.Players[playerId] = xPlayer - - if userData.firstname then - xPlayer.set('firstName', userData.firstname) - xPlayer.set('lastName', userData.lastname) - if userData.dateofbirth then - xPlayer.set('dateofbirth', userData.dateofbirth) - end - if userData.sex then - xPlayer.set('sex', userData.sex) - end - if userData.height then - xPlayer.set('height', userData.height) - end + if result.sex then + userData.sex = result.sex end - - TriggerEvent('esx:playerLoaded', playerId, xPlayer, isNew) - - xPlayer.triggerEvent('esx:playerLoaded', { - accounts = xPlayer.getAccounts(), - coords = xPlayer.getCoords(), - identifier = xPlayer.getIdentifier(), - inventory = xPlayer.getInventory(), - job = xPlayer.getJob(), - loadout = xPlayer.getLoadout(), - maxWeight = xPlayer.getMaxWeight(), - money = xPlayer.getMoney(), - dead = false - }, isNew, userData.skin) - - if not Config.OxInventory then - xPlayer.triggerEvent('esx:createMissingPickups', Core.Pickups) - else - exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory) + if result.height then + userData.height = result.height end + end - xPlayer.triggerEvent('esx:registerSuggestions', Core.RegisteredCommands) - print(('[^2INFO^0] Player ^5"%s" ^0has connected to the server. ID: ^5%s^7'):format(xPlayer.getName(), playerId)) + local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, userData.weight, userData.job, + userData.loadout, userData.playerName, userData.coords) + ESX.Players[playerId] = xPlayer + + if userData.firstname then + xPlayer.set('firstName', userData.firstname) + xPlayer.set('lastName', userData.lastname) + if userData.dateofbirth then + xPlayer.set('dateofbirth', userData.dateofbirth) + end + if userData.sex then + xPlayer.set('sex', userData.sex) + end + if userData.height then + xPlayer.set('height', userData.height) + end + end + + TriggerEvent('esx:playerLoaded', playerId, xPlayer, isNew) + + xPlayer.triggerEvent('esx:playerLoaded', + {accounts = xPlayer.getAccounts(), coords = xPlayer.getCoords(), identifier = xPlayer.getIdentifier(), inventory = xPlayer.getInventory(), + job = xPlayer.getJob(), loadout = xPlayer.getLoadout(), maxWeight = xPlayer.getMaxWeight(), money = xPlayer.getMoney(), dead = false}, isNew, + userData.skin) + + if not Config.OxInventory then + xPlayer.triggerEvent('esx:createMissingPickups', Core.Pickups) + else + exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory) + end + + xPlayer.triggerEvent('esx:registerSuggestions', Core.RegisteredCommands) + print(('[^2INFO^0] Player ^5"%s" ^0has connected to the server. ID: ^5%s^7'):format(xPlayer.getName(), playerId)) end AddEventHandler('chatMessage', function(playerId, author, message) - local xPlayer = ESX.GetPlayerFromId(playerId) - if message:sub(1, 1) == '/' and playerId > 0 then - CancelEvent() - local commandName = message:sub(1):gmatch("%w+")() - xPlayer.showNotification(_U('commanderror_invalidcommand', commandName)) - end + local xPlayer = ESX.GetPlayerFromId(playerId) + if message:sub(1, 1) == '/' and playerId > 0 then + CancelEvent() + local commandName = message:sub(1):gmatch("%w+")() + xPlayer.showNotification(_U('commanderror_invalidcommand', commandName)) + end end) AddEventHandler('playerDropped', function(reason) - local playerId = source - local xPlayer = ESX.GetPlayerFromId(playerId) + local playerId = source + local xPlayer = ESX.GetPlayerFromId(playerId) - if xPlayer then - TriggerEvent('esx:playerDropped', playerId, reason) + if xPlayer then + TriggerEvent('esx:playerDropped', playerId, reason) - Core.SavePlayer(xPlayer, function() - ESX.Players[playerId] = nil - end) - end + Core.SavePlayer(xPlayer, function() + ESX.Players[playerId] = nil + end) + end end) AddEventHandler('esx:playerLogout', function(playerId, cb) - local xPlayer = ESX.GetPlayerFromId(playerId) - if xPlayer then - TriggerEvent('esx:playerDropped', playerId) + local xPlayer = ESX.GetPlayerFromId(playerId) + if xPlayer then + TriggerEvent('esx:playerDropped', playerId) - Core.SavePlayer(xPlayer, function() - ESX.Players[playerId] = nil - if cb then - cb() - end - end) - end - TriggerClientEvent("esx:onPlayerLogout", playerId) + Core.SavePlayer(xPlayer, function() + ESX.Players[playerId] = nil + if cb then + cb() + end + end) + end + TriggerClientEvent("esx:onPlayerLogout", playerId) end) RegisterNetEvent('esx:updateCoords') AddEventHandler('esx:updateCoords', function() - local source = source - local xPlayer = ESX.GetPlayerFromId(source) + local source = source + local xPlayer = ESX.GetPlayerFromId(source) - if xPlayer then - xPlayer.updateCoords() - end + if xPlayer then + xPlayer.updateCoords() + end end) if not Config.OxInventory then - RegisterNetEvent('esx:updateWeaponAmmo') - AddEventHandler('esx:updateWeaponAmmo', function(weaponName, ammoCount) - local xPlayer = ESX.GetPlayerFromId(source) + RegisterNetEvent('esx:updateWeaponAmmo') + AddEventHandler('esx:updateWeaponAmmo', function(weaponName, ammoCount) + local xPlayer = ESX.GetPlayerFromId(source) - if xPlayer then - xPlayer.updateWeaponAmmo(weaponName, ammoCount) - end - end) + if xPlayer then + xPlayer.updateWeaponAmmo(weaponName, ammoCount) + end + end) - RegisterNetEvent('esx:giveInventoryItem') - AddEventHandler('esx:giveInventoryItem', function(target, type, itemName, itemCount) - local playerId = source - local sourceXPlayer = ESX.GetPlayerFromId(playerId) - local targetXPlayer = ESX.GetPlayerFromId(target) - local distance = #(GetEntityCoords(GetPlayerPed(playerId)) - GetEntityCoords(GetPlayerPed(target))) - if not sourceXPlayer or not targetXPlayer or distance > Config.DistanceGive then - print("[WARNING] Player Detected Cheating: " .. GetPlayerName(playerId)) - return - end + RegisterNetEvent('esx:giveInventoryItem') + AddEventHandler('esx:giveInventoryItem', function(target, type, itemName, itemCount) + local playerId = source + local sourceXPlayer = ESX.GetPlayerFromId(playerId) + local targetXPlayer = ESX.GetPlayerFromId(target) + local distance = #(GetEntityCoords(GetPlayerPed(playerId)) - GetEntityCoords(GetPlayerPed(target))) + if not sourceXPlayer or not targetXPlayer or distance > Config.DistanceGive then + print("[WARNING] Player Detected Cheating: " .. GetPlayerName(playerId)) + return + end - if type == 'item_standard' then - local sourceItem = sourceXPlayer.getInventoryItem(itemName) + if type == 'item_standard' then + local sourceItem = sourceXPlayer.getInventoryItem(itemName) - if itemCount > 0 and sourceItem.count >= itemCount then - if targetXPlayer.canCarryItem(itemName, itemCount) then - sourceXPlayer.removeInventoryItem(itemName, itemCount) - targetXPlayer.addInventoryItem(itemName, itemCount) + if itemCount > 0 and sourceItem.count >= itemCount then + if targetXPlayer.canCarryItem(itemName, itemCount) then + sourceXPlayer.removeInventoryItem(itemName, itemCount) + targetXPlayer.addInventoryItem(itemName, itemCount) - sourceXPlayer.showNotification(_U('gave_item', itemCount, sourceItem.label, targetXPlayer.name)) - targetXPlayer.showNotification(_U('received_item', itemCount, sourceItem.label, sourceXPlayer.name)) - else - sourceXPlayer.showNotification(_U('ex_inv_lim', targetXPlayer.name)) - end - else - sourceXPlayer.showNotification(_U('imp_invalid_quantity')) - end - elseif type == 'item_account' then - if itemCount > 0 and sourceXPlayer.getAccount(itemName).money >= itemCount then - sourceXPlayer.removeAccountMoney(itemName, itemCount) - targetXPlayer.addAccountMoney(itemName, itemCount) - - sourceXPlayer.showNotification(_U('gave_account_money', ESX.Math.GroupDigits(itemCount), - Config.Accounts[itemName].label, targetXPlayer.name)) - targetXPlayer.showNotification(_U('received_account_money', ESX.Math.GroupDigits(itemCount), - Config.Accounts[itemName].label, sourceXPlayer.name)) - else - sourceXPlayer.showNotification(_U('imp_invalid_amount')) - end - elseif type == 'item_weapon' then - if sourceXPlayer.hasWeapon(itemName) then - local weaponLabel = ESX.GetWeaponLabel(itemName) - if not targetXPlayer.hasWeapon(itemName) then - local _, weapon = sourceXPlayer.getWeapon(itemName) - local _, weaponObject = ESX.GetWeapon(itemName) - itemCount = weapon.ammo - local weaponComponents = ESX.Table.Clone(weapon.components) - local weaponTint = weapon.tintIndex - if weaponTint then - targetXPlayer.setWeaponTint(itemName, weaponTint) - end - if weaponComponents then - for k, v in pairs(weaponComponents) do - targetXPlayer.addWeaponComponent(itemName, v) - end - end - sourceXPlayer.removeWeapon(itemName) - targetXPlayer.addWeapon(itemName, itemCount) - - if weaponObject.ammo and itemCount > 0 then - local ammoLabel = weaponObject.ammo.label - sourceXPlayer.showNotification(_U('gave_weapon_withammo', weaponLabel, itemCount, ammoLabel, - targetXPlayer.name)) - targetXPlayer.showNotification(_U('received_weapon_withammo', weaponLabel, itemCount, ammoLabel, - sourceXPlayer.name)) - else - sourceXPlayer.showNotification(_U('gave_weapon', weaponLabel, targetXPlayer.name)) - targetXPlayer.showNotification(_U('received_weapon', weaponLabel, sourceXPlayer.name)) - end - else - sourceXPlayer.showNotification(_U('gave_weapon_hasalready', targetXPlayer.name, weaponLabel)) - targetXPlayer.showNotification(_U('received_weapon_hasalready', sourceXPlayer.name, weaponLabel)) - end - end - elseif type == 'item_ammo' then - if sourceXPlayer.hasWeapon(itemName) then - local weaponNum, weapon = sourceXPlayer.getWeapon(itemName) - - if targetXPlayer.hasWeapon(itemName) then - local _, weaponObject = ESX.GetWeapon(itemName) - - if weaponObject.ammo then - local ammoLabel = weaponObject.ammo.label - - if weapon.ammo >= itemCount then - sourceXPlayer.removeWeaponAmmo(itemName, itemCount) - targetXPlayer.addWeaponAmmo(itemName, itemCount) - - sourceXPlayer.showNotification(_U('gave_weapon_ammo', itemCount, ammoLabel, weapon.label, - targetXPlayer.name)) - targetXPlayer.showNotification(_U('received_weapon_ammo', itemCount, ammoLabel, - weapon.label, sourceXPlayer.name)) - end - end - else - sourceXPlayer.showNotification(_U('gave_weapon_noweapon', targetXPlayer.name)) - targetXPlayer.showNotification(_U('received_weapon_noweapon', sourceXPlayer.name, weapon.label)) - end - end - end - end) - - RegisterNetEvent('esx:removeInventoryItem') - AddEventHandler('esx:removeInventoryItem', function(type, itemName, itemCount) - local playerId = source - local xPlayer = ESX.GetPlayerFromId(playerId) - - if type == 'item_standard' then - if itemCount == nil or itemCount < 1 then - xPlayer.showNotification(_U('imp_invalid_quantity')) - else - local xItem = xPlayer.getInventoryItem(itemName) - - if (itemCount > xItem.count or xItem.count < 1) then - xPlayer.showNotification(_U('imp_invalid_quantity')) - else - xPlayer.removeInventoryItem(itemName, itemCount) - local pickupLabel = ('%s [%s]'):format(xItem.label, itemCount) - ESX.CreatePickup('item_standard', itemName, itemCount, pickupLabel, playerId) - xPlayer.showNotification(_U('threw_standard', itemCount, xItem.label)) - end - end - elseif type == 'item_account' then - if itemCount == nil or itemCount < 1 then - xPlayer.showNotification(_U('imp_invalid_amount')) - else - local account = xPlayer.getAccount(itemName) - - if (itemCount > account.money or account.money < 1) then - xPlayer.showNotification(_U('imp_invalid_amount')) - else - xPlayer.removeAccountMoney(itemName, itemCount) - local pickupLabel = ('%s [%s]'):format(account.label, _U('locale_currency', ESX.Math - .GroupDigits(itemCount))) - ESX.CreatePickup('item_account', itemName, itemCount, pickupLabel, playerId) - xPlayer.showNotification(_U('threw_account', ESX.Math.GroupDigits(itemCount), - string.lower(account.label))) - end - end - elseif type == 'item_weapon' then - itemName = string.upper(itemName) - - if xPlayer.hasWeapon(itemName) then - local _, weapon = xPlayer.getWeapon(itemName) - local _, weaponObject = ESX.GetWeapon(itemName) - local components, pickupLabel = ESX.Table.Clone(weapon.components) - xPlayer.removeWeapon(itemName) - - if weaponObject.ammo and weapon.ammo > 0 then - local ammoLabel = weaponObject.ammo.label - pickupLabel = ('%s [%s %s]'):format(weapon.label, weapon.ammo, ammoLabel) - xPlayer.showNotification(_U('threw_weapon_ammo', weapon.label, weapon.ammo, ammoLabel)) - else - pickupLabel = ('%s'):format(weapon.label) - xPlayer.showNotification(_U('threw_weapon', weapon.label)) - end - - ESX.CreatePickup('item_weapon', itemName, weapon.ammo, pickupLabel, playerId, components, - weapon.tintIndex) - end - end - end) - - RegisterNetEvent('esx:useItem') - AddEventHandler('esx:useItem', function(itemName) - local source = source - local xPlayer = ESX.GetPlayerFromId(source) - local count = xPlayer.getInventoryItem(itemName).count - - if count > 0 then - ESX.UseItem(source, itemName) + sourceXPlayer.showNotification(_U('gave_item', itemCount, sourceItem.label, targetXPlayer.name)) + targetXPlayer.showNotification(_U('received_item', itemCount, sourceItem.label, sourceXPlayer.name)) else - xPlayer.showNotification(_U('act_imp')) + sourceXPlayer.showNotification(_U('ex_inv_lim', targetXPlayer.name)) end - end) + else + sourceXPlayer.showNotification(_U('imp_invalid_quantity')) + end + elseif type == 'item_account' then + if itemCount > 0 and sourceXPlayer.getAccount(itemName).money >= itemCount then + sourceXPlayer.removeAccountMoney(itemName, itemCount) + targetXPlayer.addAccountMoney(itemName, itemCount) - RegisterNetEvent('esx:onPickup') - AddEventHandler('esx:onPickup', function(pickupId) - local pickup, xPlayer, success = Core.Pickups[pickupId], ESX.GetPlayerFromId(source) - - if pickup then - if pickup.type == 'item_standard' then - if xPlayer.canCarryItem(pickup.name, pickup.count) then - xPlayer.addInventoryItem(pickup.name, pickup.count) - success = true - else - xPlayer.showNotification(_U('threw_cannot_pickup')) - end - elseif pickup.type == 'item_account' then - success = true - xPlayer.addAccountMoney(pickup.name, pickup.count) - elseif pickup.type == 'item_weapon' then - if xPlayer.hasWeapon(pickup.name) then - xPlayer.showNotification(_U('threw_weapon_already')) - else - success = true - xPlayer.addWeapon(pickup.name, pickup.count) - xPlayer.setWeaponTint(pickup.name, pickup.tintIndex) - - for k, v in ipairs(pickup.components) do - xPlayer.addWeaponComponent(pickup.name, v) - end - end + sourceXPlayer.showNotification(_U('gave_account_money', ESX.Math.GroupDigits(itemCount), Config.Accounts[itemName].label, targetXPlayer.name)) + targetXPlayer.showNotification(_U('received_account_money', ESX.Math.GroupDigits(itemCount), Config.Accounts[itemName].label, + sourceXPlayer.name)) + else + sourceXPlayer.showNotification(_U('imp_invalid_amount')) + end + elseif type == 'item_weapon' then + if sourceXPlayer.hasWeapon(itemName) then + local weaponLabel = ESX.GetWeaponLabel(itemName) + if not targetXPlayer.hasWeapon(itemName) then + local _, weapon = sourceXPlayer.getWeapon(itemName) + local _, weaponObject = ESX.GetWeapon(itemName) + itemCount = weapon.ammo + local weaponComponents = ESX.Table.Clone(weapon.components) + local weaponTint = weapon.tintIndex + if weaponTint then + targetXPlayer.setWeaponTint(itemName, weaponTint) + end + if weaponComponents then + for k, v in pairs(weaponComponents) do + targetXPlayer.addWeaponComponent(itemName, v) end + end + sourceXPlayer.removeWeapon(itemName) + targetXPlayer.addWeapon(itemName, itemCount) - if success then - Core.Pickups[pickupId] = nil - TriggerClientEvent('esx:removePickup', -1, pickupId) - end + if weaponObject.ammo and itemCount > 0 then + local ammoLabel = weaponObject.ammo.label + sourceXPlayer.showNotification(_U('gave_weapon_withammo', weaponLabel, itemCount, ammoLabel, targetXPlayer.name)) + targetXPlayer.showNotification(_U('received_weapon_withammo', weaponLabel, itemCount, ammoLabel, sourceXPlayer.name)) + else + sourceXPlayer.showNotification(_U('gave_weapon', weaponLabel, targetXPlayer.name)) + targetXPlayer.showNotification(_U('received_weapon', weaponLabel, sourceXPlayer.name)) + end + else + sourceXPlayer.showNotification(_U('gave_weapon_hasalready', targetXPlayer.name, weaponLabel)) + targetXPlayer.showNotification(_U('received_weapon_hasalready', sourceXPlayer.name, weaponLabel)) end - end) + end + elseif type == 'item_ammo' then + if sourceXPlayer.hasWeapon(itemName) then + local weaponNum, weapon = sourceXPlayer.getWeapon(itemName) + + if targetXPlayer.hasWeapon(itemName) then + local _, weaponObject = ESX.GetWeapon(itemName) + + if weaponObject.ammo then + local ammoLabel = weaponObject.ammo.label + + if weapon.ammo >= itemCount then + sourceXPlayer.removeWeaponAmmo(itemName, itemCount) + targetXPlayer.addWeaponAmmo(itemName, itemCount) + + sourceXPlayer.showNotification(_U('gave_weapon_ammo', itemCount, ammoLabel, weapon.label, targetXPlayer.name)) + targetXPlayer.showNotification(_U('received_weapon_ammo', itemCount, ammoLabel, weapon.label, sourceXPlayer.name)) + end + end + else + sourceXPlayer.showNotification(_U('gave_weapon_noweapon', targetXPlayer.name)) + targetXPlayer.showNotification(_U('received_weapon_noweapon', sourceXPlayer.name, weapon.label)) + end + end + end + end) + + RegisterNetEvent('esx:removeInventoryItem') + AddEventHandler('esx:removeInventoryItem', function(type, itemName, itemCount) + local playerId = source + local xPlayer = ESX.GetPlayerFromId(playerId) + + if type == 'item_standard' then + if itemCount == nil or itemCount < 1 then + xPlayer.showNotification(_U('imp_invalid_quantity')) + else + local xItem = xPlayer.getInventoryItem(itemName) + + if (itemCount > xItem.count or xItem.count < 1) then + xPlayer.showNotification(_U('imp_invalid_quantity')) + else + xPlayer.removeInventoryItem(itemName, itemCount) + local pickupLabel = ('%s [%s]'):format(xItem.label, itemCount) + ESX.CreatePickup('item_standard', itemName, itemCount, pickupLabel, playerId) + xPlayer.showNotification(_U('threw_standard', itemCount, xItem.label)) + end + end + elseif type == 'item_account' then + if itemCount == nil or itemCount < 1 then + xPlayer.showNotification(_U('imp_invalid_amount')) + else + local account = xPlayer.getAccount(itemName) + + if (itemCount > account.money or account.money < 1) then + xPlayer.showNotification(_U('imp_invalid_amount')) + else + xPlayer.removeAccountMoney(itemName, itemCount) + local pickupLabel = ('%s [%s]'):format(account.label, _U('locale_currency', ESX.Math.GroupDigits(itemCount))) + ESX.CreatePickup('item_account', itemName, itemCount, pickupLabel, playerId) + xPlayer.showNotification(_U('threw_account', ESX.Math.GroupDigits(itemCount), string.lower(account.label))) + end + end + elseif type == 'item_weapon' then + itemName = string.upper(itemName) + + if xPlayer.hasWeapon(itemName) then + local _, weapon = xPlayer.getWeapon(itemName) + local _, weaponObject = ESX.GetWeapon(itemName) + local components, pickupLabel = ESX.Table.Clone(weapon.components) + xPlayer.removeWeapon(itemName) + + if weaponObject.ammo and weapon.ammo > 0 then + local ammoLabel = weaponObject.ammo.label + pickupLabel = ('%s [%s %s]'):format(weapon.label, weapon.ammo, ammoLabel) + xPlayer.showNotification(_U('threw_weapon_ammo', weapon.label, weapon.ammo, ammoLabel)) + else + pickupLabel = ('%s'):format(weapon.label) + xPlayer.showNotification(_U('threw_weapon', weapon.label)) + end + + ESX.CreatePickup('item_weapon', itemName, weapon.ammo, pickupLabel, playerId, components, weapon.tintIndex) + end + end + end) + + RegisterNetEvent('esx:useItem') + AddEventHandler('esx:useItem', function(itemName) + local source = source + local xPlayer = ESX.GetPlayerFromId(source) + local count = xPlayer.getInventoryItem(itemName).count + + if count > 0 then + ESX.UseItem(source, itemName) + else + xPlayer.showNotification(_U('act_imp')) + end + end) + + RegisterNetEvent('esx:onPickup') + AddEventHandler('esx:onPickup', function(pickupId) + local pickup, xPlayer, success = Core.Pickups[pickupId], ESX.GetPlayerFromId(source) + + if pickup then + if pickup.type == 'item_standard' then + if xPlayer.canCarryItem(pickup.name, pickup.count) then + xPlayer.addInventoryItem(pickup.name, pickup.count) + success = true + else + xPlayer.showNotification(_U('threw_cannot_pickup')) + end + elseif pickup.type == 'item_account' then + success = true + xPlayer.addAccountMoney(pickup.name, pickup.count) + elseif pickup.type == 'item_weapon' then + if xPlayer.hasWeapon(pickup.name) then + xPlayer.showNotification(_U('threw_weapon_already')) + else + success = true + xPlayer.addWeapon(pickup.name, pickup.count) + xPlayer.setWeaponTint(pickup.name, pickup.tintIndex) + + for k, v in ipairs(pickup.components) do + xPlayer.addWeaponComponent(pickup.name, v) + end + end + end + + if success then + Core.Pickups[pickupId] = nil + TriggerClientEvent('esx:removePickup', -1, pickupId) + end + end + end) end ESX.RegisterServerCallback('esx:getPlayerData', function(source, cb) - local xPlayer = ESX.GetPlayerFromId(source) + local xPlayer = ESX.GetPlayerFromId(source) - cb({ - identifier = xPlayer.identifier, - accounts = xPlayer.getAccounts(), - inventory = xPlayer.getInventory(), - job = xPlayer.getJob(), - loadout = xPlayer.getLoadout(), - money = xPlayer.getMoney(), - position = xPlayer.getCoords(true) - }) + cb({identifier = xPlayer.identifier, accounts = xPlayer.getAccounts(), inventory = xPlayer.getInventory(), job = xPlayer.getJob(), + loadout = xPlayer.getLoadout(), money = xPlayer.getMoney(), position = xPlayer.getCoords(true)}) end) ESX.RegisterServerCallback('esx:isUserAdmin', function(source, cb) - cb(Core.IsPlayerAdmin(source)) + cb(Core.IsPlayerAdmin(source)) end) ESX.RegisterServerCallback('esx:getOtherPlayerData', function(source, cb, target) - local xPlayer = ESX.GetPlayerFromId(target) + local xPlayer = ESX.GetPlayerFromId(target) - cb({ - identifier = xPlayer.identifier, - accounts = xPlayer.getAccounts(), - inventory = xPlayer.getInventory(), - job = xPlayer.getJob(), - loadout = xPlayer.getLoadout(), - money = xPlayer.getMoney(), - position = xPlayer.getCoords(true) - }) + cb({identifier = xPlayer.identifier, accounts = xPlayer.getAccounts(), inventory = xPlayer.getInventory(), job = xPlayer.getJob(), + loadout = xPlayer.getLoadout(), money = xPlayer.getMoney(), position = xPlayer.getCoords(true)}) end) ESX.RegisterServerCallback('esx:getPlayerNames', function(source, cb, players) - players[source] = nil + players[source] = nil - for playerId, v in pairs(players) do - local xPlayer = ESX.GetPlayerFromId(playerId) + for playerId, v in pairs(players) do + local xPlayer = ESX.GetPlayerFromId(playerId) - if xPlayer then - players[playerId] = xPlayer.getName() - else - players[playerId] = nil - end + if xPlayer then + players[playerId] = xPlayer.getName() + else + players[playerId] = nil end + end - cb(players) + cb(players) end) AddEventHandler('txAdmin:events:scheduledRestart', function(eventData) - if eventData.secondsRemaining == 60 then - CreateThread(function() - Wait(50000) - Core.SavePlayers() - end) - end + if eventData.secondsRemaining == 60 then + CreateThread(function() + Wait(50000) + Core.SavePlayers() + end) + end end) AddEventHandler('txAdmin:events:serverShuttingDown', function() - Core.SavePlayers() + Core.SavePlayers() end) diff --git a/[esx]/es_extended/server/paycheck.lua b/[esx]/es_extended/server/paycheck.lua index a532599d..e13e1df0 100644 --- a/[esx]/es_extended/server/paycheck.lua +++ b/[esx]/es_extended/server/paycheck.lua @@ -1,41 +1,45 @@ function StartPayCheck() - CreateThread(function() - while true do - Wait(Config.PaycheckInterval) - local xPlayers = ESX.GetExtendedPlayers() - for i=1, #(xPlayers) do - local xPlayer = xPlayers[i] - local job = xPlayer.job.grade_name - local salary = xPlayer.job.grade_salary + CreateThread(function() + while true do + Wait(Config.PaycheckInterval) + local xPlayers = ESX.GetExtendedPlayers() + for i = 1, #(xPlayers) do + local xPlayer = xPlayers[i] + local job = xPlayer.job.grade_name + local salary = xPlayer.job.grade_salary - if salary > 0 then - if job == 'unemployed' then -- unemployed - xPlayer.addAccountMoney('bank', salary) - TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), _U('received_help', salary), 'CHAR_BANK_MAZE', 9) - elseif Config.EnableSocietyPayouts then -- possibly a society - TriggerEvent('esx_society:getSociety', xPlayer.job.name, function (society) - if society ~= nil then -- verified society - TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function (account) - if account.money >= salary then -- does the society money to pay its employees? - xPlayer.addAccountMoney('bank', salary) - account.removeMoney(salary) + if salary > 0 then + if job == 'unemployed' then -- unemployed + xPlayer.addAccountMoney('bank', salary) + TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), _U('received_help', salary), + 'CHAR_BANK_MAZE', 9) + elseif Config.EnableSocietyPayouts then -- possibly a society + TriggerEvent('esx_society:getSociety', xPlayer.job.name, function(society) + if society ~= nil then -- verified society + TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account) + if account.money >= salary then -- does the society money to pay its employees? + xPlayer.addAccountMoney('bank', salary) + account.removeMoney(salary) - TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), _U('received_salary', salary), 'CHAR_BANK_MAZE', 9) - else - TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), '', _U('company_nomoney'), 'CHAR_BANK_MAZE', 1) - end - end) - else -- not a society - xPlayer.addAccountMoney('bank', salary) - TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), _U('received_salary', salary), 'CHAR_BANK_MAZE', 9) - end - end) - else -- generic job - xPlayer.addAccountMoney('bank', salary) - TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), _U('received_salary', salary), 'CHAR_BANK_MAZE', 9) - end - end - end - end - end) + TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), + _U('received_salary', salary), 'CHAR_BANK_MAZE', 9) + else + TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), '', _U('company_nomoney'), 'CHAR_BANK_MAZE', 1) + end + end) + else -- not a society + xPlayer.addAccountMoney('bank', salary) + TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), _U('received_salary', salary), + 'CHAR_BANK_MAZE', 9) + end + end) + else -- generic job + xPlayer.addAccountMoney('bank', salary) + TriggerClientEvent('esx:showAdvancedNotification', xPlayer.source, _U('bank'), _U('received_paycheck'), _U('received_salary', salary), + 'CHAR_BANK_MAZE', 9) + end + end + end + end + end) end