0.0.1
This commit is contained in:
80
resources/[core]/es_extended/shared/config/adjustments.lua
Normal file
80
resources/[core]/es_extended/shared/config/adjustments.lua
Normal file
@@ -0,0 +1,80 @@
|
||||
Config.DisableHealthRegeneration = false -- Player will no longer regenerate health
|
||||
Config.DisableVehicleRewards = false -- Disables Player Receiving weapons from vehicles
|
||||
Config.DisableNPCDrops = false -- stops NPCs from dropping weapons on death
|
||||
Config.DisableDispatchServices = true -- Disable Dispatch services
|
||||
Config.DisableScenarios = true -- Disable Scenarios
|
||||
Config.DisableAimAssist = false -- disables AIM assist (mainly on controllers)
|
||||
Config.DisableVehicleSeatShuff = false -- Disables vehicle seat shuff
|
||||
Config.DisableDisplayAmmo = false -- Disable ammunition display
|
||||
Config.EnablePVP = true -- Allow Player to player combat
|
||||
Config.EnableWantedLevel = false -- Use Normal GTA wanted Level?
|
||||
|
||||
Config.RemoveHudComponents = {
|
||||
[1] = false, --WANTED_STARS,
|
||||
[2] = false, --WEAPON_ICON
|
||||
[3] = false, --CASH
|
||||
[4] = false, --MP_CASH
|
||||
[5] = false, --MP_MESSAGE
|
||||
[6] = true, --VEHICLE_NAME
|
||||
[7] = true, -- AREA_NAME
|
||||
[8] = true, -- VEHICLE_CLASS
|
||||
[9] = true, --STREET_NAME
|
||||
[10] = false, --HELP_TEXT
|
||||
[11] = false, --FLOATING_HELP_TEXT_1
|
||||
[12] = false, --FLOATING_HELP_TEXT_2
|
||||
[13] = false, --CASH_CHANGE
|
||||
[14] = false, --RETICLE
|
||||
[15] = false, --SUBTITLE_TEXT
|
||||
[16] = false, --RADIO_STATIONS
|
||||
[17] = false, --SAVING_GAME,
|
||||
[18] = false, --GAME_STREAM
|
||||
[19] = false, --WEAPON_WHEEL
|
||||
[20] = false, --WEAPON_WHEEL_STATS
|
||||
[21] = false, --HUD_COMPONENTS
|
||||
[22] = false, --HUD_WEAPONS
|
||||
}
|
||||
|
||||
Config.Multipliers = {
|
||||
pedDensity = 1.0,
|
||||
scenarioPedDensityInterior = 0.0,
|
||||
scenarioPedDensityExterior = 1.0,
|
||||
ambientVehicleRange = 1.0,
|
||||
parkedVehicleDensity = 1.0,
|
||||
randomVehicleDensity = 1.0,
|
||||
vehicleDensity = 1.0
|
||||
}
|
||||
|
||||
-- Pattern string format
|
||||
--1 will lead to a random number from 0-9.
|
||||
--A will lead to a random letter from A-Z.
|
||||
-- . will lead to a random letter or number, with a 50% probability of being either.
|
||||
--^1 will lead to a literal 1 being emitted.
|
||||
--^A will lead to a literal A being emitted.
|
||||
--Any other character will lead to said character being emitted.
|
||||
-- A string shorter than 8 characters will be padded on the right.
|
||||
Config.CustomAIPlates = "........" -- Custom plates for AI vehicles
|
||||
|
||||
--[[
|
||||
PlaceHolders:
|
||||
{server_name} - Server Display Name
|
||||
{server_endpoint} - Server IP:Server Port
|
||||
{server_players} - Current Player Count
|
||||
{server_maxplayers} - Max Player Count
|
||||
|
||||
{player_name} - Player Name
|
||||
{player_rp_name} - Player RP Name
|
||||
{player_id} - Player ID
|
||||
{player_street} - Player Street Name
|
||||
]]
|
||||
|
||||
Config.DiscordActivity = {
|
||||
appId = 0, -- Discord Application ID,
|
||||
assetName = "LargeIcon", --image name for the "large" icon.
|
||||
assetText = "{server_name}", -- Text to display on the asset
|
||||
buttons = {
|
||||
{ label = "Join Server", url = "fivem://connect/{server_endpoint}" },
|
||||
{ label = "Discord", url = "https://discord.esx-framework.org" },
|
||||
},
|
||||
presence = "{player_name} [{player_id}] | {server_players}/{server_maxplayers}",
|
||||
refresh = 1 * 60 * 1000, -- 1 minute
|
||||
}
|
||||
23
resources/[core]/es_extended/shared/config/logs.lua
Normal file
23
resources/[core]/es_extended/shared/config/logs.lua
Normal file
@@ -0,0 +1,23 @@
|
||||
Config.DiscordLogs = {
|
||||
Webhooks = {
|
||||
default = "",
|
||||
test = "",
|
||||
Chat = "",
|
||||
UserActions = "",
|
||||
Resources = "",
|
||||
Paycheck = "",
|
||||
},
|
||||
|
||||
Colors = { -- https://www.spycolor.com/
|
||||
default = 14423100,
|
||||
blue = 255,
|
||||
red = 16711680,
|
||||
green = 65280,
|
||||
white = 16777215,
|
||||
black = 0,
|
||||
orange = 16744192,
|
||||
yellow = 16776960,
|
||||
pink = 16761035,
|
||||
lightgreen = 65309,
|
||||
},
|
||||
}
|
||||
75
resources/[core]/es_extended/shared/config/main.lua
Normal file
75
resources/[core]/es_extended/shared/config/main.lua
Normal file
@@ -0,0 +1,75 @@
|
||||
Config = {}
|
||||
|
||||
local txAdminLocale = GetConvar("txAdmin-locale", "en")
|
||||
local esxLocale = GetConvar("esx:locale", "invalid")
|
||||
Config.Locale = (esxLocale ~= "invalid") and esxLocale or (txAdminLocale ~= "custom" and txAdminLocale) or "en"
|
||||
|
||||
-- For ox inventory, this will automatically be adjusted, do not change! For other inventories, leave as false unless specifically instructed to change.
|
||||
Config.CustomInventory = false
|
||||
|
||||
Config.Accounts = {
|
||||
bank = {
|
||||
label = TranslateCap("account_bank"),
|
||||
round = true,
|
||||
},
|
||||
black_money = {
|
||||
label = TranslateCap("account_black_money"),
|
||||
round = true,
|
||||
},
|
||||
money = {
|
||||
label = TranslateCap("account_money"),
|
||||
round = true,
|
||||
},
|
||||
}
|
||||
|
||||
Config.StartingAccountMoney = { bank = 50000 }
|
||||
|
||||
Config.StartingInventoryItems = false -- table/false
|
||||
|
||||
Config.DefaultSpawns = { -- If you want to have more spawn positions and select them randomly uncomment commented code or add more locations
|
||||
{ x = 222.2027, y = -864.0162, z = 30.2922, heading = 1.0 },
|
||||
--{x = 224.9865, y = -865.0871, z = 30.2922, heading = 1.0},
|
||||
--{x = 227.8436, y = -866.0400, z = 30.2922, heading = 1.0},
|
||||
--{x = 230.6051, y = -867.1450, z = 30.2922, heading = 1.0},
|
||||
--{x = 233.5459, y = -868.2626, z = 30.2922, heading = 1.0}
|
||||
}
|
||||
|
||||
Config.AdminGroups = {
|
||||
["owner"] = true,
|
||||
["admin"] = true,
|
||||
}
|
||||
|
||||
Config.ValidCharacterSets = { -- Only enable additional charsets if your server is multilingual. By default everything is false.
|
||||
['el'] = false, -- Greek
|
||||
['sr'] = false, -- Cyrillic
|
||||
['he'] = false, -- Hebrew
|
||||
['ar'] = false, -- Arabic
|
||||
['zh-cn'] = false -- Chinese, Japanese, Korean
|
||||
}
|
||||
|
||||
Config.EnablePaycheck = true -- enable paycheck
|
||||
Config.LogPaycheck = false -- Logs paychecks to a nominated Discord channel via webhook (default is false)
|
||||
Config.EnableSocietyPayouts = false -- pay from the society account that the player is employed at? Requirement: esx_society
|
||||
Config.MaxWeight = 24 -- the max inventory weight without a backpack
|
||||
Config.PaycheckInterval = 7 * 60000 -- how often to receive paychecks in milliseconds
|
||||
Config.SaveDeathStatus = true -- Save the death status of a player
|
||||
Config.EnableDebug = false -- Use Debug options?
|
||||
|
||||
Config.DefaultJobDuty = true -- A players default duty status when changing jobs
|
||||
Config.OffDutyPaycheckMultiplier = 0.5 -- The multiplier for off duty paychecks. 0.5 = 50% of the on duty paycheck
|
||||
|
||||
Config.Multichar = GetResourceState("esx_multicharacter") ~= "missing"
|
||||
Config.Identity = true -- Select a character identity data before they have loaded in (this happens by default with multichar)
|
||||
Config.DistanceGive = 4.0 -- Max distance when giving items, weapons etc.
|
||||
|
||||
Config.AdminLogging = false -- Logs the usage of certain commands by those with group.admin ace permissions (default is false)
|
||||
|
||||
-------------------------------------
|
||||
-- DO NOT CHANGE BELOW THIS LINE !!!
|
||||
-------------------------------------
|
||||
if GetResourceState("ox_inventory") ~= "missing" then
|
||||
Config.CustomInventory = "ox"
|
||||
end
|
||||
|
||||
Config.EnableDefaultInventory = Config.CustomInventory == false -- Display the default Inventory ( F2 )
|
||||
Config.Identifier = GetConvar("esx:identifier", "license")
|
||||
1108
resources/[core]/es_extended/shared/config/weapons.lua
Normal file
1108
resources/[core]/es_extended/shared/config/weapons.lua
Normal file
File diff suppressed because it is too large
Load Diff
293
resources/[core]/es_extended/shared/functions.lua
Normal file
293
resources/[core]/es_extended/shared/functions.lua
Normal file
@@ -0,0 +1,293 @@
|
||||
local Charset = {}
|
||||
|
||||
for i = 48, 57 do
|
||||
table.insert(Charset, string.char(i))
|
||||
end
|
||||
for i = 65, 90 do
|
||||
table.insert(Charset, string.char(i))
|
||||
end
|
||||
for i = 97, 122 do
|
||||
table.insert(Charset, string.char(i))
|
||||
end
|
||||
|
||||
local weaponsByName = {}
|
||||
local weaponsByHash = {}
|
||||
|
||||
CreateThread(function()
|
||||
for index, weapon in pairs(Config.Weapons) do
|
||||
weaponsByName[weapon.name] = index
|
||||
weaponsByHash[joaat(weapon.name)] = weapon
|
||||
end
|
||||
end)
|
||||
|
||||
---@param length number
|
||||
---@return string
|
||||
function ESX.GetRandomString(length)
|
||||
math.randomseed(GetGameTimer())
|
||||
|
||||
return length > 0 and ESX.GetRandomString(length - 1) .. Charset[math.random(1, #Charset)] or ""
|
||||
end
|
||||
|
||||
---@param key? string Key pair to get specific value of config
|
||||
---@return unknown Returns the whole config if no key is passed, or a specific value
|
||||
function ESX.GetConfig(key)
|
||||
if key then
|
||||
return Config[key]
|
||||
end
|
||||
|
||||
return Config
|
||||
end
|
||||
|
||||
---@param weaponName string
|
||||
---@return number, table
|
||||
function ESX.GetWeapon(weaponName)
|
||||
weaponName = string.upper(weaponName)
|
||||
|
||||
assert(weaponsByName[weaponName], "Invalid weapon name!")
|
||||
|
||||
local index = weaponsByName[weaponName]
|
||||
return index, Config.Weapons[index]
|
||||
end
|
||||
|
||||
---@param weaponHash number
|
||||
---@return table
|
||||
function ESX.GetWeaponFromHash(weaponHash)
|
||||
weaponHash = type(weaponHash) == "string" and joaat(weaponHash) or weaponHash
|
||||
|
||||
return weaponsByHash[weaponHash]
|
||||
end
|
||||
|
||||
---@param byHash boolean
|
||||
---@return table
|
||||
function ESX.GetWeaponList(byHash)
|
||||
return byHash and weaponsByHash or Config.Weapons
|
||||
end
|
||||
|
||||
---@param weaponName string
|
||||
---@return string
|
||||
function ESX.GetWeaponLabel(weaponName)
|
||||
weaponName = string.upper(weaponName)
|
||||
|
||||
assert(weaponsByName[weaponName], "Invalid weapon name!")
|
||||
|
||||
local index = weaponsByName[weaponName]
|
||||
return Config.Weapons[index].label or ""
|
||||
end
|
||||
|
||||
---@param weaponName string
|
||||
---@param weaponComponent string
|
||||
---@return ESXWeaponComponent?
|
||||
function ESX.GetWeaponComponent(weaponName, weaponComponent)
|
||||
weaponName = string.upper(weaponName)
|
||||
|
||||
assert(weaponsByName[weaponName], "Invalid weapon name!")
|
||||
local weapon = Config.Weapons[weaponsByName[weaponName]]
|
||||
|
||||
for _, component in ipairs(weapon.components) do
|
||||
---@cast component ESXWeaponComponent
|
||||
if component.name == weaponComponent then
|
||||
return component
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param table table
|
||||
---@param nb? number
|
||||
---@return string
|
||||
function ESX.DumpTable(table, nb)
|
||||
if nb == nil then
|
||||
nb = 0
|
||||
end
|
||||
|
||||
if type(table) == "table" then
|
||||
local s = ""
|
||||
for _ = 1, nb + 1, 1 do
|
||||
s = s .. " "
|
||||
end
|
||||
|
||||
s = "{\n"
|
||||
for k, v in pairs(table) do
|
||||
if type(k) ~= "number" then
|
||||
k = '"' .. k .. '"'
|
||||
end
|
||||
for _ = 1, nb, 1 do
|
||||
s = s .. " "
|
||||
end
|
||||
s = s .. "[" .. k .. "] = " .. ESX.DumpTable(v, nb + 1) .. ",\n"
|
||||
end
|
||||
|
||||
for _ = 1, nb, 1 do
|
||||
s = s .. " "
|
||||
end
|
||||
|
||||
return s .. "}"
|
||||
else
|
||||
return tostring(table)
|
||||
end
|
||||
end
|
||||
|
||||
---@param value any
|
||||
---@param numDecimalPlaces? number
|
||||
---@return number
|
||||
function ESX.Round(value, numDecimalPlaces)
|
||||
return ESX.Math.Round(value, numDecimalPlaces)
|
||||
end
|
||||
|
||||
---@param value string
|
||||
---@param ... any
|
||||
---@return boolean, string?
|
||||
function ESX.ValidateType(value, ...)
|
||||
local types = { ... }
|
||||
if #types == 0 then return true end
|
||||
|
||||
local mapType = {}
|
||||
for i = 1, #types, 1 do
|
||||
local validateType = types[i]
|
||||
assert(type(validateType) == "string", "bad argument types, only expected string") -- should never use anyhing else than string
|
||||
mapType[validateType] = true
|
||||
end
|
||||
|
||||
local valueType = type(value)
|
||||
|
||||
local matches = mapType[valueType] ~= nil
|
||||
|
||||
if not matches then
|
||||
local requireTypes = table.concat(types, " or ")
|
||||
local errorMessage = ("bad value (%s expected, got %s)"):format(requireTypes, valueType)
|
||||
|
||||
return false, errorMessage
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
---@param ... any
|
||||
---@return boolean
|
||||
function ESX.AssertType(...)
|
||||
local matches, errorMessage = ESX.ValidateType(...)
|
||||
|
||||
assert(matches, errorMessage)
|
||||
|
||||
return matches
|
||||
end
|
||||
|
||||
---@param val unknown
|
||||
function ESX.IsFunctionReference(val)
|
||||
local typeVal = type(val)
|
||||
|
||||
return typeVal == "function" or (typeVal == "table" and type(getmetatable(val)?.__call) == "function")
|
||||
end
|
||||
|
||||
---@param conditionFunc function A function that is repeatedly called until it returns a truthy value or the timeout is exceeded.
|
||||
---@param errorMessage? string Optional. If set, an error will be thrown with this message if the condition is not met within the timeout. If not set, no error will be thrown.
|
||||
---@param timeoutMs? number Optional. The maximum time to wait (in milliseconds) for the condition to be met. Defaults to 1000ms.
|
||||
---@return boolean, any: Returns success status and the returned value of the condition function.
|
||||
function ESX.Await(conditionFunc, errorMessage, timeoutMs)
|
||||
timeoutMs = timeoutMs or 1000
|
||||
|
||||
if timeoutMs < 0 then
|
||||
error("Timeout should be a positive number.")
|
||||
end
|
||||
|
||||
if not ESX.IsFunctionReference(conditionFunc) then
|
||||
error("Condition Function should be a function reference.")
|
||||
end
|
||||
|
||||
-- since errorMessage is optional, we only validate it if the user provided it.
|
||||
if errorMessage then
|
||||
ESX.AssertType(errorMessage, "string", "errorMessage should be a string.")
|
||||
end
|
||||
|
||||
local invokingResource = GetInvokingResource()
|
||||
local startTimeMs = GetGameTimer()
|
||||
while GetGameTimer() - startTimeMs < timeoutMs do
|
||||
local result = conditionFunc()
|
||||
|
||||
if result then
|
||||
return true, result
|
||||
end
|
||||
|
||||
Wait(0)
|
||||
end
|
||||
|
||||
if errorMessage then
|
||||
error(("[%s] -> %s"):format(invokingResource, errorMessage))
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
---@param str string
|
||||
---@param allowDigits boolean? Allow numbers if necessary
|
||||
---@return boolean
|
||||
function ESX.IsValidLocaleString(str, allowDigits)
|
||||
if not ESX.ValidateType(str, 'string') then
|
||||
return false
|
||||
end
|
||||
|
||||
local locale = string.lower(Config.Locale)
|
||||
|
||||
local defaultRanges ={
|
||||
{0x0041, 0x005A}, -- Basic Latin uppercase
|
||||
{0x0061, 0x007A}, -- Basic Latin lowercase
|
||||
{0x0020, 0x0020}, -- Space
|
||||
{0x002D, 0x002D}, -- Dash
|
||||
{0x00C0, 0x02AF} -- Latin Extended
|
||||
}
|
||||
|
||||
if allowDigits then
|
||||
defaultRanges[#defaultRanges + 1] = {0x0030, 0x0039} -- 0-9 Numbers
|
||||
end
|
||||
|
||||
local localeRanges = {
|
||||
["el"] = { {0x0370, 0x03FF} }, -- Greek
|
||||
["sr"] ={ {0x0400, 0x04FF} }, -- Cyrillic
|
||||
["he"] ={ {0x05D0, 0x05EA} }, -- Hebrew letters
|
||||
["ar"] = {
|
||||
{0x0620, 0x063F}, -- Arabic
|
||||
{0x0641, 0x064A},
|
||||
{0x066E, 0x066F},
|
||||
{0x0671, 0x06D3},
|
||||
{0x06D5, 0x06D5},
|
||||
{0x0750, 0x077F},
|
||||
{0x08A0, 0x08BD}
|
||||
},
|
||||
["zh-cn"] ={ {0x4E00, 0x9FFF} } -- CJK
|
||||
}
|
||||
|
||||
local validRanges = { table.unpack(defaultRanges) }
|
||||
|
||||
if localeRanges[locale] then
|
||||
for i = 1, #localeRanges[locale] do
|
||||
validRanges[#validRanges + 1] = localeRanges[locale][i]
|
||||
end
|
||||
end
|
||||
|
||||
if Config.ValidCharacterSets then
|
||||
for charset, enabled in pairs(Config.ValidCharacterSets) do
|
||||
if enabled and charset ~= locale and localeRanges[charset] then
|
||||
for i = 1, #localeRanges[charset] do
|
||||
validRanges[#validRanges + 1] = localeRanges[charset][i]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, code in utf8.codes(str) do
|
||||
local isValid = false
|
||||
|
||||
for i = 1, #validRanges do
|
||||
local range = validRanges[i]
|
||||
if code >= range[1] and code <= range[2] then
|
||||
isValid = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not isValid then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
14
resources/[core]/es_extended/shared/main.lua
Normal file
14
resources/[core]/es_extended/shared/main.lua
Normal file
@@ -0,0 +1,14 @@
|
||||
ESX = {}
|
||||
|
||||
exports("getSharedObject", function()
|
||||
return ESX
|
||||
end)
|
||||
|
||||
AddEventHandler("esx:getSharedObject", function(cb)
|
||||
if ESX.IsFunctionReference(cb) then
|
||||
cb(ESX)
|
||||
end
|
||||
end)
|
||||
|
||||
-- backwards compatibility (DO NOT TOUCH !)
|
||||
Config.OxInventory = Config.CustomInventory == "ox"
|
||||
50
resources/[core]/es_extended/shared/modules/math.lua
Normal file
50
resources/[core]/es_extended/shared/modules/math.lua
Normal file
@@ -0,0 +1,50 @@
|
||||
ESX.Math = {}
|
||||
|
||||
---@param value number
|
||||
---@param numDecimalPlaces? number
|
||||
---@return number
|
||||
function ESX.Math.Round(value, numDecimalPlaces)
|
||||
if numDecimalPlaces then
|
||||
local power = 10 ^ numDecimalPlaces
|
||||
return math.floor((value * power) + 0.5) / power
|
||||
else
|
||||
return math.floor(value + 0.5)
|
||||
end
|
||||
end
|
||||
|
||||
-- credit http://richard.warburton.it
|
||||
---@param value number
|
||||
---@return string
|
||||
function ESX.Math.GroupDigits(value)
|
||||
local left, num, right = string.match(value, "^([^%d]*%d)(%d*)(.-)$")
|
||||
|
||||
return left .. (num:reverse():gsub("(%d%d%d)", "%1" .. TranslateCap("locale_digit_grouping_symbol")):reverse()) .. right
|
||||
end
|
||||
|
||||
---@param value string | number
|
||||
---@return string | nil
|
||||
function ESX.Math.Trim(value)
|
||||
value = tostring(value)
|
||||
return (string.gsub(value, "^%s*(.-)%s*$", "%1"))
|
||||
end
|
||||
|
||||
---@param minRange number
|
||||
---@param maxRange number
|
||||
---@return number
|
||||
function ESX.Math.Random(minRange, maxRange)
|
||||
math.randomseed(GetGameTimer())
|
||||
return math.random(minRange or 1, maxRange or 10)
|
||||
end
|
||||
|
||||
---@param origin vector
|
||||
---@param target vector
|
||||
---@return number
|
||||
function ESX.Math.GetHeadingFromCoords(origin, target)
|
||||
local dx = origin.x - target.x
|
||||
local dy = origin.y - target.y
|
||||
|
||||
local heading = math.deg(math.atan(dy, dx)) + 90
|
||||
|
||||
return (heading + 360) % 360
|
||||
end
|
||||
|
||||
241
resources/[core]/es_extended/shared/modules/table.lua
Normal file
241
resources/[core]/es_extended/shared/modules/table.lua
Normal file
@@ -0,0 +1,241 @@
|
||||
ESX.Table = {}
|
||||
|
||||
-- nil proof alternative to #table
|
||||
---@param t table
|
||||
---@return number
|
||||
function ESX.Table.SizeOf(t)
|
||||
local count = 0
|
||||
|
||||
for _, _ in pairs(t) do
|
||||
count = count + 1
|
||||
end
|
||||
|
||||
return count
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@return table
|
||||
function ESX.Table.Set(t)
|
||||
local set = {}
|
||||
for _, v in ipairs(t) do
|
||||
set[v] = true
|
||||
end
|
||||
return set
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@param value any
|
||||
---@return number
|
||||
function ESX.Table.IndexOf(t, value)
|
||||
for i = 1, #t, 1 do
|
||||
if t[i] == value then
|
||||
return i
|
||||
end
|
||||
end
|
||||
|
||||
return -1
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@param value any
|
||||
---@return number
|
||||
function ESX.Table.LastIndexOf(t, value)
|
||||
for i = #t, 1, -1 do
|
||||
if t[i] == value then
|
||||
return i
|
||||
end
|
||||
end
|
||||
|
||||
return -1
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@param cb function
|
||||
---@return any
|
||||
function ESX.Table.Find(t, cb)
|
||||
for i = 1, #t, 1 do
|
||||
if cb(t[i]) then
|
||||
return t[i]
|
||||
end
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@param cb function
|
||||
---@return number
|
||||
function ESX.Table.FindIndex(t, cb)
|
||||
for i = 1, #t, 1 do
|
||||
if cb(t[i]) then
|
||||
return i
|
||||
end
|
||||
end
|
||||
|
||||
return -1
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@param cb function
|
||||
---@return table
|
||||
function ESX.Table.Filter(t, cb)
|
||||
local newTable = {}
|
||||
|
||||
for i = 1, #t, 1 do
|
||||
if cb(t[i]) then
|
||||
newTable[#newTable + 1] = t[i]
|
||||
end
|
||||
end
|
||||
|
||||
return newTable
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@param cb function
|
||||
---@return table
|
||||
function ESX.Table.Map(t, cb)
|
||||
local newTable = {}
|
||||
|
||||
for i = 1, #t, 1 do
|
||||
newTable[i] = cb(t[i], i)
|
||||
end
|
||||
|
||||
return newTable
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@return table
|
||||
function ESX.Table.Reverse(t)
|
||||
local newTable = {}
|
||||
|
||||
for i = #t, 1, -1 do
|
||||
table.insert(newTable, t[i])
|
||||
end
|
||||
|
||||
return newTable
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@return table
|
||||
function ESX.Table.Clone(t)
|
||||
if type(t) ~= "table" then
|
||||
return t
|
||||
end
|
||||
|
||||
local meta = getmetatable(t)
|
||||
local target = {}
|
||||
|
||||
for k, v in pairs(t) do
|
||||
if type(v) == "table" then
|
||||
target[k] = ESX.Table.Clone(v)
|
||||
else
|
||||
target[k] = v
|
||||
end
|
||||
end
|
||||
|
||||
setmetatable(target, meta)
|
||||
|
||||
return target
|
||||
end
|
||||
|
||||
---@param t1 table
|
||||
---@param t2 table
|
||||
---@return table
|
||||
function ESX.Table.Concat(t1, t2)
|
||||
local t3 = ESX.Table.Clone(t1)
|
||||
|
||||
for i = 1, #t2, 1 do
|
||||
table.insert(t3, t2[i])
|
||||
end
|
||||
|
||||
return t3
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@param sep string
|
||||
---@return string
|
||||
function ESX.Table.Join(t, sep)
|
||||
local str = ""
|
||||
|
||||
for i = 1, #t, 1 do
|
||||
if i > 1 then
|
||||
str = str .. (sep or ",")
|
||||
end
|
||||
|
||||
str = str .. t[i]
|
||||
end
|
||||
|
||||
return str
|
||||
end
|
||||
|
||||
-- Credits: https://github.com/JonasDev99/qb-garages/blob/b0335d67cb72a6b9ac60f62a87fb3946f5c2f33d/server/main.lua#L5
|
||||
---@param tab table
|
||||
---@param val any
|
||||
---@return boolean
|
||||
function ESX.Table.TableContains(tab, val)
|
||||
if type(val) == "table" then
|
||||
for _, value in pairs(tab) do
|
||||
if ESX.Table.TableContains(val, value) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
else
|
||||
for _, value in pairs(tab) do
|
||||
if value == val then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Credit: https://stackoverflow.com/a/15706820
|
||||
-- Description: sort function for pairs
|
||||
---@param t table
|
||||
---@param order function
|
||||
---@return function
|
||||
function ESX.Table.Sort(t, order)
|
||||
-- collect the keys
|
||||
local keys = {}
|
||||
|
||||
for k, _ in pairs(t) do
|
||||
keys[#keys + 1] = k
|
||||
end
|
||||
|
||||
-- if order function given, sort by it by passing the table and keys a, b,
|
||||
-- otherwise just sort the keys
|
||||
if order then
|
||||
table.sort(keys, function(a, b)
|
||||
return order(t, a, b)
|
||||
end)
|
||||
else
|
||||
table.sort(keys)
|
||||
end
|
||||
|
||||
-- return the iterator function
|
||||
local i = 0
|
||||
|
||||
return function()
|
||||
i = i + 1
|
||||
if keys[i] then
|
||||
return keys[i], t[keys[i]]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@return Array
|
||||
function ESX.Table.ToArray(t)
|
||||
local array = {}
|
||||
for _, v in pairs(t) do
|
||||
array[#array + 1] = v
|
||||
end
|
||||
return array
|
||||
end
|
||||
|
||||
---@param t table
|
||||
---@return table
|
||||
function ESX.Table.Wipe(t)
|
||||
return table.wipe(t)
|
||||
end
|
||||
28
resources/[core]/es_extended/shared/modules/timeout.lua
Normal file
28
resources/[core]/es_extended/shared/modules/timeout.lua
Normal file
@@ -0,0 +1,28 @@
|
||||
local TimeoutCount = 0
|
||||
local CancelledTimeouts = {}
|
||||
|
||||
---@param msec number
|
||||
---@param cb function
|
||||
---@return number
|
||||
ESX.SetTimeout = function(msec, cb)
|
||||
local id <const> = TimeoutCount + 1
|
||||
|
||||
SetTimeout(msec, function()
|
||||
if CancelledTimeouts[id] then
|
||||
CancelledTimeouts[id] = nil
|
||||
return
|
||||
end
|
||||
|
||||
cb()
|
||||
end)
|
||||
|
||||
TimeoutCount = id
|
||||
|
||||
return id
|
||||
end
|
||||
|
||||
---@param id number
|
||||
---@return nil
|
||||
ESX.ClearTimeout = function(id)
|
||||
CancelledTimeouts[id] = true
|
||||
end
|
||||
Reference in New Issue
Block a user