Files
community_bridge/modules/framework/es_extended/server.lua
T

501 lines
15 KiB
Lua
Raw Normal View History

2025-04-15 03:00:44 -04:00
---@diagnostic disable: duplicate-set-field
2025-01-08 06:33:31 -05:00
if GetResourceState('es_extended') ~= 'started' then return end
2025-05-18 23:45:37 -04:00
2025-05-11 20:18:46 -04:00
Prints = Prints or Require("lib/utility/shared/prints.lua")
Callback = Callback or Require("lib/utility/shared/callbacks.lua")
2025-01-08 06:33:31 -05:00
ESX = exports["es_extended"]:getSharedObject()
2025-05-09 19:06:50 -05:00
Framework = Framework or {}
2025-01-08 06:33:31 -05:00
2025-07-09 22:17:24 -04:00
local cachedItemList = nil
---This will return the name of the framework in use.
---@return string
2025-01-31 19:23:47 -05:00
Framework.GetFrameworkName = function()
return 'es_extended'
end
-- This is an internal function, its here to attempt to emulate qbs shared items mainly.
Framework.ItemList = function()
2025-07-09 22:17:24 -04:00
if cachedItemList then return cachedItemList end
local items = ESX.Items
local repackedTable = {}
for k, v in pairs(items) do
if v.label then
repackedTable[k] = {
name = k,
label = v.label,
weight = v.weight,
type = "item",
image = k .. ".png",
unique = false,
useable = true,
shouldClose = true,
description = 'No description provided.',
}
end
end
2025-07-09 22:17:24 -04:00
cachedItemList = { Items = repackedTable or {} }
return cachedItemList
end
2025-06-01 23:12:32 -04:00
---This will return if the player is an admin in the framework.
---@param src any
---@return boolean
Framework.GetIsFrameworkAdmin = function(src)
if not src then return false end
local xPlayer = Framework.GetPlayer(src)
if not xPlayer then return false end
local group = xPlayer.getGroup()
if group == 'admin' or group == 'superadmin' then return true end
return false
end
---This will get the players birth date
---@return string|nil
2025-02-26 01:34:47 -05:00
Framework.GetPlayerDob = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-02-26 01:34:47 -05:00
local dob = xPlayer.get("dateofbirth")
return dob
end
--- Returns the player data of the specified source in the framework defualt format.
---@param src any
---@return table | nil
Framework.GetPlayer = function(src)
local xPlayer = ESX.GetPlayerFromId(src)
if not xPlayer then return end
return xPlayer
end
2025-01-08 06:33:31 -05:00
-- Framework.GetPlayerIdentifier(src)
-- Returns the citizen ID of the player.
---@param src number
---@return string | nil
2025-01-08 06:33:31 -05:00
Framework.GetPlayerIdentifier = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-01-08 06:33:31 -05:00
return xPlayer.getIdentifier()
end
---This will return the jobs registered in the framework in a table.
---Format of the table is:
---{name = jobName, label = jobLabel, grade = {name = gradeName, level = gradeLevel}}
---@return table
2025-03-10 01:17:18 -04:00
Framework.GetFrameworkJobs = function()
return ESX.GetJobs()
end
2025-01-08 06:33:31 -05:00
-- Framework.GetPlayerName(src)
-- Returns the first and last name of the player.
---@return string|nil, string|nil
2025-01-08 06:33:31 -05:00
Framework.GetPlayerName = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-01-08 06:33:31 -05:00
return xPlayer.variables.firstName, xPlayer.variables.lastName
end
2025-04-14 01:16:06 -04:00
---This will return a table of all logged in players
---@return table
Framework.GetPlayers = function()
local players = ESX.GetExtendedPlayers()
local playerList = {}
for _, xPlayer in pairs(players) do
table.insert(playerList, xPlayer.source)
end
return playerList
end
---Returns a table of items matching the specified name and if passed metadata from the player's inventory.
---returns {name = v.name, count = v.amount, metadata = v.info, slot = v.slot}
---@param src number
---@param item string
---@param _ table
---@return table|nil
2025-01-08 06:33:31 -05:00
Framework.GetItem = function(src, item, _)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
local playerItems = xPlayer.getInventory()
2025-01-08 06:33:31 -05:00
local repackedTable = {}
for _, v in pairs(playerItems) do
if v.name == item then
table.insert(repackedTable, {
name = v.name,
count = v.count,
--metadata = v.metadata,
--slot = v.slot,
})
end
end
return repackedTable
end
---This will return the count of the item in the players inventory, if not found will return 0.
---if metadata is passed it will find the matching items count (esx_core does not feature metadata items).
---@param src number
---@param item string
---@param _ table
---@return number
2025-01-08 06:33:31 -05:00
Framework.GetItemCount = function(src, item, _)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-01-08 06:33:31 -05:00
return xPlayer.getInventoryItem(item).count
end
---This will return a boolean if the player has the item.
2025-03-07 04:08:40 -05:00
---@param src number
---@param item string
---@return boolean
Framework.HasItem = function(src, item)
local getCount = Framework.GetItemCount(src, item, nil)
return getCount > 0
end
2025-01-08 06:33:31 -05:00
-- Framework.GetPlayerInventory(src)
-- Returns the entire inventory of the player as a table.
-- returns {name = v.name, count = v.amount, metadata = v.info, slot = v.slot}
---@param src number
---@return table | nil
2025-01-08 06:33:31 -05:00
Framework.GetPlayerInventory = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
local playerItems = xPlayer.getInventory()
2025-01-08 06:33:31 -05:00
local repackedTable = {}
for _, v in pairs(playerItems) do
if v.count > 0 then
table.insert(repackedTable, {
name = v.name,
count = v.count,
--metadata = v.metadata,
--slot = v.slot,
})
end
2025-01-08 06:33:31 -05:00
end
return repackedTable
end
-- Framework.SetMetadata(src, metadata, value)
-- Adds the specified metadata key and number value to the player's data.
---@return boolean|nil
2025-01-08 06:33:31 -05:00
Framework.SetPlayerMetadata = function(src, metadata, value)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-01-08 06:33:31 -05:00
xPlayer.setMeta(metadata, value, nil)
return true
end
-- Framework.GetMetadata(src, metadata)
-- Gets the specified metadata key to the player's data.
---@param src number
---@param metadata string
---@return any|nil
2025-01-08 06:33:31 -05:00
Framework.GetPlayerMetadata = function(src, metadata)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-01-08 06:33:31 -05:00
return xPlayer.getMeta(metadata) or false
end
-- defualt esx Available tables are
-- identifier, accounts, group, inventory, job, job_grade, loadout,
-- metadata, position, firstname, lastname, dateofbirth, sex, height,
-- skin, status, is_dead, id, disabled, last_property, created_at, last_seen,
2025-01-08 06:33:31 -05:00
-- phone_number, pincode
Framework.GetStatus = function(src, column)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-01-08 06:33:31 -05:00
return xPlayer.get(column) or nil
end
---This will return a boolean if the player is dead or in last stand.
---@param src number
---@return boolean|nil
2025-05-02 03:13:07 -05:00
Framework.GetIsPlayerDead = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-05-02 03:13:07 -05:00
if not xPlayer then return end
return xPlayer.get("is_dead") or false
end
---This will revive a player, if the player is dead or in last stand.
---@param src number
---@return boolean
2025-05-09 19:06:50 -05:00
Framework.RevivePlayer = function(src)
src = tonumber(src)
if not src then return false end
2025-05-09 20:09:57 -05:00
TriggerEvent('esx_ambulancejob:revive', src)
2025-05-09 19:06:50 -05:00
return true
end
2025-01-08 06:33:31 -05:00
-- Framework.AddThirst(src, value)
-- Adds the specified value from the player's thirst level.
---@param src number
---@param value number
---@return number | nil
2025-01-08 06:33:31 -05:00
Framework.AddThirst = function(src, value)
local clampIT = Math.Clamp(value, 0, 200000)
local levelForEsx = clampIT * 2000
TriggerClientEvent('esx_status:add', src, 'thirst', levelForEsx)
return levelForEsx
end
-- Framework.AddHunger(src, value)
-- Adds the specified value from the player's hunger level.
---@param src number
---@param value number
---@return number | nil
2025-01-08 06:33:31 -05:00
Framework.AddHunger = function(src, value)
local clampIT = Math.Clamp(value, 0, 200000)
local levelForEsx = clampIT * 2000
TriggerClientEvent('esx_status:add', src, 'hunger', levelForEsx)
return levelForEsx
end
---This will get the hunger of a player
---@param src number
---@return number | nil
2025-01-08 06:33:31 -05:00
Framework.GetHunger = function(src)
local status = Framework.GetStatus(src, "status")
if not status then return 0 end
return status.hunger
end
---This will get the thirst of a player
---@param src any
---@return number | nil
2025-01-08 06:33:31 -05:00
Framework.GetThirst = function(src)
local status = Framework.GetStatus(src, "status")
if not status then return 0 end
return status.thirst
end
-- Framework.GetPlayerPhone(src)
-- Returns the phone number of the player.
---@param src number
---@return string | nil
2025-01-08 06:33:31 -05:00
Framework.GetPlayerPhone = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-01-08 06:33:31 -05:00
return xPlayer.get("phone_number")
end
2025-04-15 08:04:43 -04:00
2025-05-14 04:48:23 -04:00
---Depricated: Returns the job name, label, grade name, and grade level of the player.
2025-04-15 08:04:43 -04:00
---@param src number
---@return string | nil
---@return string | nil
---@return string | nil
---@return string | nil
2025-01-08 06:33:31 -05:00
Framework.GetPlayerJob = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-04-15 08:04:43 -04:00
local job = xPlayer.getJob()
return job.name, job.label, job.grade_label, job.grade
end
2025-05-14 04:48:23 -04:00
---This will return the players job name, job label, job grade label job grade level, boss status, and duty status in a table
---@param src number
---@return table | nil
Framework.GetPlayerJobData = function(src)
local xPlayer = Framework.GetPlayer(src)
if not xPlayer then return end
local job = xPlayer.getJob()
local isBoss = (job.grade_name == "boss")
return {
jobName = job.name,
jobLabel = job.label,
gradeName = job.grade_name,
gradeLabel = job.grade_label,
gradeRank = job.grade,
boss = isBoss,
onDuty = job.onduty,
}
end
2025-04-15 08:04:43 -04:00
---Returns the players duty status.
---@param src number
---@return boolean
Framework.GetPlayerDuty = function(src)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-04-15 08:04:43 -04:00
if not xPlayer then return false end
local job = xPlayer.getJob()
if not job.onDuty then return false end
return true
end
---This will toggle a players duty status
2025-04-15 08:04:43 -04:00
---@param src number
---@param status boolean
2025-04-15 08:04:43 -04:00
---@return boolean
2025-06-21 07:53:49 -04:00
Framework.SetPlayerDuty = function(src, status)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-04-15 08:04:43 -04:00
if not xPlayer then return false end
local job = xPlayer.getJob()
if not job.onDuty then return false end
2025-06-21 07:53:49 -04:00
xPlayer.setJob(job.name, job.grade, status)
return true
2025-01-08 06:33:31 -05:00
end
---This will get a table of player sources that have the specified job name.
---@param job any
---@return table
2025-01-08 06:33:31 -05:00
Framework.GetPlayersByJob = function(job)
local players = GetPlayers()
local playerList = {}
for _, src in pairs(players) do
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
if xPlayer and xPlayer.getJob().name == job then
2025-01-08 06:33:31 -05:00
table.insert(playerList, src)
end
end
return playerList
end
-- Sets the player's job to the specified name and grade.
---@param src number
---@param name string
---@param grade string
---@return nil
2025-01-08 06:33:31 -05:00
Framework.SetPlayerJob = function(src, name, grade)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
if not ESX.DoesJobExist(name, grade) then
2025-05-11 20:18:46 -04:00
Prints.Error("Job Does Not Exsist In Framework :NAME " .. name .. " Grade:" .. grade)
return
end
xPlayer.setJob(name, grade, true)
return true
2025-01-12 12:55:29 -05:00
end
---This will add money based on the type of account (money/bank)
---@param src number
---@param _type string
---@param amount number
---@return boolean | nil
2025-01-08 06:33:31 -05:00
Framework.AddAccountBalance = function(src, _type, amount)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-05-05 10:45:00 -04:00
if _type == 'cash' then _type = 'money' end
xPlayer.addAccountMoney(_type, amount)
return true
2025-01-08 06:33:31 -05:00
end
---This will remove money based on the type of account (money/bank)
---@param src number
---@param _type string
---@param amount number
---@return boolean | nil
2025-01-08 06:33:31 -05:00
Framework.RemoveAccountBalance = function(src, _type, amount)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-05-05 10:45:00 -04:00
if _type == 'cash' then _type = 'money' end
2025-05-01 11:44:22 -04:00
xPlayer.removeAccountMoney(_type, amount)
return true
2025-01-08 06:33:31 -05:00
end
---This will remove money based on the type of account (money/bank)
---@param src number
---@param _type string
---@return string | nil
2025-01-08 06:33:31 -05:00
Framework.GetAccountBalance = function(src, _type)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
2025-05-05 10:45:00 -04:00
if _type == 'cash' then _type = 'money' end
2025-01-08 06:33:31 -05:00
return xPlayer.getAccount(_type).money
end
-- Framework.AddItem(src, item, amount, slot, metadata)
2025-01-08 06:33:31 -05:00
-- Adds the specified item to the player's inventory.
---@param src number
---@param item string
---@param amount number
---@param slot number
---@param metadata table
---@return boolean | nil
2025-01-08 06:33:31 -05:00
Framework.AddItem = function(src, item, amount, slot, metadata)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
xPlayer.addInventoryItem(item, amount)
return true
2025-01-08 06:33:31 -05:00
end
-- Framework.RemoveItem(src, item, amount, slot, metadata)
2025-01-08 06:33:31 -05:00
-- Removes the specified item from the player's inventory.
---@param src number
---@param item string
---@param amount number
---@param slot number
---@param metadata table
---@return boolean | nil
2025-01-08 06:33:31 -05:00
Framework.RemoveItem = function(src, item, amount, slot, metadata)
2025-05-13 01:06:52 -04:00
local xPlayer = Framework.GetPlayer(src)
2025-03-03 02:05:53 -05:00
if not xPlayer then return end
xPlayer.removeInventoryItem(item, amount)
return true
2025-01-08 06:33:31 -05:00
end
---This will get all owned vehicles for the player
---@param src number
---@return table
Framework.GetOwnedVehicles = function(src)
local citizenId = Framework.GetPlayerIdentifier(src)
local result = MySQL.Sync.fetchAll("SELECT vehicle, plate FROM owned_vehicles WHERE owner = '" .. citizenId .. "'")
local vehicles = {}
for i = 1, #result do
local vehicle = result[i].vehicle
local plate = result[i].plate
local model = json.decode(vehicle).model
table.insert(vehicles, { vehicle = model, plate = plate })
end
return vehicles
end
-- Framework.RegisterUsableItem(item, cb)
2025-01-08 06:33:31 -05:00
-- Registers a usable item with a callback function.
---@param itemName string
---@param cb function
2025-01-14 10:55:14 -06:00
Framework.RegisterUsableItem = function(itemName, cb)
local func = function(src, item, itemData)
itemData = itemData or item
itemData.metadata = itemData.metadata or itemData.info or {}
2025-05-06 14:39:29 -04:00
itemData.slot = itemData.id or itemData.slot
2025-01-26 12:07:26 -06:00
cb(src, itemData)
2025-01-14 10:55:14 -06:00
end
ESX.RegisterUsableItem(itemName, func)
2025-01-12 13:01:16 -05:00
end
2025-06-02 22:19:42 -05:00
RegisterNetEvent("esx:playerLoaded", function(src)
src = src or source
2025-03-09 23:13:03 -05:00
TriggerEvent("community_bridge:Server:OnPlayerLoaded", src)
end)
2025-06-02 22:19:42 -05:00
RegisterNetEvent("esx:playerLogout", function(src)
src = src or source
2025-02-10 02:02:33 -05:00
TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
2025-01-27 14:41:16 -05:00
end)
2025-01-27 18:42:38 -05:00
AddEventHandler("playerDropped", function()
local src = source
TriggerEvent("community_bridge:Server:OnPlayerUnload", src)
end)
Callback.Register('community_bridge:Callback:GetFrameworkJobs', function(source)
return Framework.GetFrameworkJobs() or {}
2025-03-10 01:17:18 -04:00
end)
-- This is linked to an internal function, its an attempt to standardize the item list across frameworks.
Callback.Register('community_bridge:Callback:GetFrameworkItems', function(source)
return Framework.ItemList() or {}
end)
2025-01-12 13:01:16 -05:00
Framework.Commands = {}
Framework.Commands.Add = function(name, help, arguments, argsrequired, callback, permission, ...)
ESX.RegisterCommand(name, permission, function(xPlayer, args, showError)
callback(xPlayer, args)
end, false, {
help = help,
arguments = arguments
})
end
2025-05-11 20:18:46 -04:00
return Framework