mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-30 01:38:52 +00:00
Bandwith optimization + Add ESX.SetTimeout / ESX.ClearTimeout serverside
This commit is contained in:
@@ -5,6 +5,7 @@ for i = 65, 90 do table.insert(Charset, string.char(i)) end
|
||||
for i = 97, 122 do table.insert(Charset, string.char(i)) end
|
||||
|
||||
ESX = {}
|
||||
ESX.PlayerData = {}
|
||||
ESX.CurrentRequestId = 0
|
||||
ESX.ServerCallbacks = {}
|
||||
ESX.TimeoutCallbacks = {}
|
||||
@@ -996,210 +997,206 @@ ESX.Game.Utils.DrawText3D = function(coords, text, size)
|
||||
end
|
||||
|
||||
ESX.ShowInventory = function()
|
||||
|
||||
ESX.TriggerServerCallback('esx:getPlayerData', function(data)
|
||||
|
||||
local playerPed = GetPlayerPed(-1)
|
||||
local elements = {}
|
||||
local playerPed = GetPlayerPed(-1)
|
||||
local elements = {}
|
||||
|
||||
table.insert(elements, {
|
||||
label = '[Cash] $' .. ESX.PlayerData.money,
|
||||
count = ESX.PlayerData.money,
|
||||
type = 'item_money',
|
||||
value = 'money',
|
||||
usable = false,
|
||||
rare = false,
|
||||
canRemove = true
|
||||
})
|
||||
|
||||
for i=1, #ESX.PlayerData.accounts, 1 do
|
||||
table.insert(elements, {
|
||||
label = '[Cash] $' .. data.money,
|
||||
count = data.money,
|
||||
type = 'item_money',
|
||||
value = 'money',
|
||||
label = '[' .. ESX.PlayerData.accounts[i].label .. '] $' .. ESX.PlayerData.accounts[i].money,
|
||||
count = ESX.PlayerData.accounts[i].money,
|
||||
type = 'item_account',
|
||||
value = ESX.PlayerData.accounts[i].name,
|
||||
usable = false,
|
||||
rare = false,
|
||||
canRemove = true
|
||||
})
|
||||
end
|
||||
|
||||
for i=1, #data.accounts, 1 do
|
||||
for i=1, #ESX.PlayerData.inventory, 1 do
|
||||
|
||||
if ESX.PlayerData.inventory[i].count > 0 then
|
||||
table.insert(elements, {
|
||||
label = '[' .. data.accounts[i].label .. '] $' .. data.accounts[i].money,
|
||||
count = data.accounts[i].money,
|
||||
type = 'item_account',
|
||||
value = data.accounts[i].name,
|
||||
label = ESX.PlayerData.inventory[i].label .. ' x' .. ESX.PlayerData.inventory[i].count,
|
||||
count = ESX.PlayerData.inventory[i].count,
|
||||
type = 'item_standard',
|
||||
value = ESX.PlayerData.inventory[i].name,
|
||||
usable = ESX.PlayerData.inventory[i].usable,
|
||||
rare = ESX.PlayerData.inventory[i].rare,
|
||||
canRemove = ESX.PlayerData.inventory[i].canRemove,
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
for i=1, #Config.Weapons, 1 do
|
||||
|
||||
local weaponHash = GetHashKey(Config.Weapons[i].name)
|
||||
|
||||
if HasPedGotWeapon(playerPed, weaponHash, false) and Config.Weapons[i].name ~= 'WEAPON_UNARMED' then
|
||||
|
||||
local ammo = GetAmmoInPedWeapon(playerPed, weaponHash)
|
||||
|
||||
table.insert(elements, {
|
||||
label = Config.Weapons[i].label .. ' x1 [' .. ammo .. ']',
|
||||
count = 1,
|
||||
type = 'item_weapon',
|
||||
value = Config.Weapons[i].name,
|
||||
usable = false,
|
||||
rare = false,
|
||||
canRemove = true
|
||||
})
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
for i=1, #data.inventory, 1 do
|
||||
ESX.UI.Menu.CloseAll()
|
||||
|
||||
if data.inventory[i].count > 0 then
|
||||
table.insert(elements, {
|
||||
label = data.inventory[i].label .. ' x' .. data.inventory[i].count,
|
||||
count = data.inventory[i].count,
|
||||
type = 'item_standard',
|
||||
value = data.inventory[i].name,
|
||||
usable = data.inventory[i].usable,
|
||||
rare = data.inventory[i].rare,
|
||||
canRemove = data.inventory[i].canRemove,
|
||||
})
|
||||
ESX.UI.Menu.Open(
|
||||
'default', GetCurrentResourceName(), 'inventory',
|
||||
{
|
||||
title = _U('inventory'),
|
||||
align = 'bottom-right',
|
||||
elements = elements,
|
||||
},
|
||||
function(data, menu)
|
||||
|
||||
menu.close()
|
||||
|
||||
local elements = {}
|
||||
|
||||
if data.current.usable then
|
||||
table.insert(elements, {label = _U('use'), action = 'use', type = data.current.type, value = data.current.value})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
for i=1, #Config.Weapons, 1 do
|
||||
|
||||
local weaponHash = GetHashKey(Config.Weapons[i].name)
|
||||
|
||||
if HasPedGotWeapon(playerPed, weaponHash, false) and Config.Weapons[i].name ~= 'WEAPON_UNARMED' then
|
||||
|
||||
local ammo = GetAmmoInPedWeapon(playerPed, weaponHash)
|
||||
|
||||
table.insert(elements, {
|
||||
label = Config.Weapons[i].label .. ' x1 [' .. ammo .. ']',
|
||||
count = 1,
|
||||
type = 'item_weapon',
|
||||
value = Config.Weapons[i].name,
|
||||
usable = false,
|
||||
rare = false,
|
||||
canRemove = true
|
||||
})
|
||||
|
||||
if data.current.canRemove then
|
||||
table.insert(elements, {label = _U('give'), action = 'give', type = data.current.type, value = data.current.value})
|
||||
table.insert(elements, {label = _U('remove'), action = 'remove', type = data.current.type, value = data.current.value})
|
||||
end
|
||||
end
|
||||
|
||||
ESX.UI.Menu.CloseAll()
|
||||
table.insert(elements, {label = _U('return'), action = 'return'})
|
||||
|
||||
ESX.UI.Menu.Open(
|
||||
'default', GetCurrentResourceName(), 'inventory',
|
||||
{
|
||||
title = _U('inventory'),
|
||||
align = 'bottom-right',
|
||||
elements = elements,
|
||||
},
|
||||
function(data, menu)
|
||||
ESX.UI.Menu.Open(
|
||||
'default', GetCurrentResourceName(), 'inventory_item',
|
||||
{
|
||||
title = _U('inventory'),
|
||||
align = 'bottom-right',
|
||||
elements = elements,
|
||||
},
|
||||
function(data, menu)
|
||||
|
||||
menu.close()
|
||||
local item = data.current.value
|
||||
local type = data.current.type
|
||||
|
||||
local elements = {}
|
||||
if data.current.action == 'give' then
|
||||
|
||||
if data.current.usable then
|
||||
table.insert(elements, {label = _U('use'), action = 'use', type = data.current.type, value = data.current.value})
|
||||
end
|
||||
if type == 'item_weapon' then
|
||||
|
||||
if data.current.canRemove then
|
||||
table.insert(elements, {label = _U('give'), action = 'give', type = data.current.type, value = data.current.value})
|
||||
table.insert(elements, {label = _U('remove'), action = 'remove', type = data.current.type, value = data.current.value})
|
||||
end
|
||||
local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
|
||||
|
||||
table.insert(elements, {label = _U('return'), action = 'return'})
|
||||
if closestPlayer == -1 or closestDistance > 3.0 then
|
||||
ESX.ShowNotification(_U('players_nearby'))
|
||||
else
|
||||
TriggerServerEvent('esx:giveInventoryItem', GetPlayerServerId(closestPlayer), type, item, 1)
|
||||
end
|
||||
|
||||
ESX.UI.Menu.Open(
|
||||
'default', GetCurrentResourceName(), 'inventory_item',
|
||||
{
|
||||
title = _U('inventory'),
|
||||
align = 'bottom-right',
|
||||
elements = elements,
|
||||
},
|
||||
function(data, menu)
|
||||
else
|
||||
|
||||
local item = data.current.value
|
||||
local type = data.current.type
|
||||
ESX.UI.Menu.Open(
|
||||
'dialog', GetCurrentResourceName(), 'inventory_item_count_give',
|
||||
{
|
||||
title = _U('amount')
|
||||
},
|
||||
function(data2, menu2)
|
||||
|
||||
if data.current.action == 'give' then
|
||||
local quantity = tonumber(data2.value)
|
||||
local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
|
||||
|
||||
if type == 'item_weapon' then
|
||||
if closestPlayer == -1 or closestDistance > 3.0 then
|
||||
ESX.ShowNotification(_U('players_nearby'))
|
||||
else
|
||||
TriggerServerEvent('esx:giveInventoryItem', GetPlayerServerId(closestPlayer), type, item, quantity)
|
||||
end
|
||||
|
||||
local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
|
||||
menu2.close()
|
||||
menu.close()
|
||||
|
||||
if closestPlayer == -1 or closestDistance > 3.0 then
|
||||
ESX.ShowNotification(_U('players_nearby'))
|
||||
else
|
||||
TriggerServerEvent('esx:giveInventoryItem', GetPlayerServerId(closestPlayer), type, item, 1)
|
||||
end,
|
||||
function(data2, menu2)
|
||||
menu2.close()
|
||||
end
|
||||
)
|
||||
|
||||
else
|
||||
|
||||
ESX.UI.Menu.Open(
|
||||
'dialog', GetCurrentResourceName(), 'inventory_item_count_give',
|
||||
{
|
||||
title = _U('amount')
|
||||
},
|
||||
function(data2, menu2)
|
||||
|
||||
local quantity = tonumber(data2.value)
|
||||
local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
|
||||
|
||||
if closestPlayer == -1 or closestDistance > 3.0 then
|
||||
ESX.ShowNotification(_U('players_nearby'))
|
||||
else
|
||||
TriggerServerEvent('esx:giveInventoryItem', GetPlayerServerId(closestPlayer), type, item, quantity)
|
||||
end
|
||||
|
||||
menu2.close()
|
||||
menu.close()
|
||||
|
||||
end,
|
||||
function(data2, menu2)
|
||||
menu2.close()
|
||||
end
|
||||
)
|
||||
|
||||
end
|
||||
|
||||
elseif data.current.action == 'remove' then
|
||||
|
||||
if type == 'item_weapon' then
|
||||
|
||||
TriggerServerEvent('esx:removeInventoryItem', type, item, 1)
|
||||
menu.close()
|
||||
|
||||
else
|
||||
|
||||
ESX.UI.Menu.Open(
|
||||
'dialog', GetCurrentResourceName(), 'inventory_item_count_remove',
|
||||
{
|
||||
title = _U('amount')
|
||||
},
|
||||
function(data2, menu2)
|
||||
|
||||
local quantity = tonumber(data2.value)
|
||||
|
||||
if quantity == nil then
|
||||
ESX.ShowNotification(_U('amount_invalid'))
|
||||
else
|
||||
TriggerServerEvent('esx:removeInventoryItem', type, item, quantity)
|
||||
end
|
||||
|
||||
menu2.close()
|
||||
menu.close()
|
||||
|
||||
end,
|
||||
function(data2, menu2)
|
||||
menu2.close()
|
||||
end
|
||||
)
|
||||
|
||||
end
|
||||
|
||||
elseif data.current.action == 'use' then
|
||||
|
||||
TriggerServerEvent('esx:useItem', data.current.value)
|
||||
|
||||
elseif data.current.action == 'return' then
|
||||
|
||||
ESX.UI.Menu.CloseAll()
|
||||
ESX.ShowInventory()
|
||||
|
||||
end
|
||||
|
||||
elseif data.current.action == 'remove' then
|
||||
|
||||
if type == 'item_weapon' then
|
||||
|
||||
TriggerServerEvent('esx:removeInventoryItem', type, item, 1)
|
||||
menu.close()
|
||||
|
||||
else
|
||||
|
||||
ESX.UI.Menu.Open(
|
||||
'dialog', GetCurrentResourceName(), 'inventory_item_count_remove',
|
||||
{
|
||||
title = _U('amount')
|
||||
},
|
||||
function(data2, menu2)
|
||||
|
||||
local quantity = tonumber(data2.value)
|
||||
|
||||
if quantity == nil then
|
||||
ESX.ShowNotification(_U('amount_invalid'))
|
||||
else
|
||||
TriggerServerEvent('esx:removeInventoryItem', type, item, quantity)
|
||||
end
|
||||
|
||||
menu2.close()
|
||||
menu.close()
|
||||
|
||||
end,
|
||||
function(data2, menu2)
|
||||
menu2.close()
|
||||
end
|
||||
)
|
||||
|
||||
end
|
||||
|
||||
elseif data.current.action == 'use' then
|
||||
|
||||
TriggerServerEvent('esx:useItem', data.current.value)
|
||||
|
||||
elseif data.current.action == 'return' then
|
||||
|
||||
end,
|
||||
function(data, menu)
|
||||
ESX.UI.Menu.CloseAll()
|
||||
ESX.ShowInventory()
|
||||
|
||||
end
|
||||
)
|
||||
|
||||
end,
|
||||
function(data, menu)
|
||||
ESX.UI.Menu.CloseAll()
|
||||
ESX.ShowInventory()
|
||||
end
|
||||
)
|
||||
|
||||
end,
|
||||
function(data, menu)
|
||||
menu.close()
|
||||
end
|
||||
)
|
||||
|
||||
end)
|
||||
end,
|
||||
function(data, menu)
|
||||
menu.close()
|
||||
end
|
||||
)
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -21,7 +21,8 @@ local Pickups = {}
|
||||
RegisterNetEvent('esx:playerLoaded')
|
||||
AddEventHandler('esx:playerLoaded', function(xPlayer)
|
||||
|
||||
PlayerLoaded = true
|
||||
PlayerLoaded = true
|
||||
ESX.PlayerData = xPlayer
|
||||
|
||||
for i=1, #xPlayer.accounts, 1 do
|
||||
|
||||
@@ -63,24 +64,20 @@ AddEventHandler('playerSpawned', function()
|
||||
Citizen.Wait(0)
|
||||
end
|
||||
|
||||
ESX.TriggerServerCallback('esx:getPlayerData', function(data)
|
||||
|
||||
local playerPed = GetPlayerPed(-1)
|
||||
local playerPed = GetPlayerPed(-1)
|
||||
|
||||
-- Restore position
|
||||
if data.lastPosition ~= nil then
|
||||
SetEntityCoords(playerPed, data.lastPosition.x, data.lastPosition.y, data.lastPosition.z)
|
||||
end
|
||||
-- Restore position
|
||||
if ESX.PlayerData.lastPosition ~= nil then
|
||||
SetEntityCoords(playerPed, ESX.PlayerData.lastPosition.x, ESX.PlayerData.lastPosition.y, ESX.PlayerData.lastPosition.z)
|
||||
end
|
||||
|
||||
-- Restore loadout
|
||||
for i=1, #data.loadout, 1 do
|
||||
local weaponHash = GetHashKey(data.loadout[i].name)
|
||||
GiveWeaponToPed(playerPed, weaponHash, data.loadout[i].ammo, false, false)
|
||||
end
|
||||
-- Restore loadout
|
||||
for i=1, #ESX.PlayerData.loadout, 1 do
|
||||
local weaponHash = GetHashKey(ESX.PlayerData.loadout[i].name)
|
||||
GiveWeaponToPed(playerPed, weaponHash, ESX.PlayerData.loadout[i].ammo, false, false)
|
||||
end
|
||||
|
||||
LoadoutLoaded = true
|
||||
|
||||
end)
|
||||
LoadoutLoaded = true
|
||||
|
||||
end)
|
||||
|
||||
@@ -97,31 +94,46 @@ AddEventHandler('skinchanger:modelLoaded', function()
|
||||
end
|
||||
|
||||
-- Restore loadout
|
||||
ESX.TriggerServerCallback('esx:getPlayerData', function(data)
|
||||
|
||||
local playerPed = GetPlayerPed(-1)
|
||||
local playerPed = GetPlayerPed(-1)
|
||||
|
||||
for i=1, #data.loadout, 1 do
|
||||
local weaponHash = GetHashKey(data.loadout[i].name)
|
||||
GiveWeaponToPed(playerPed, weaponHash, data.loadout[i].ammo, false, false)
|
||||
end
|
||||
for i=1, #ESX.PlayerData.loadout, 1 do
|
||||
local weaponHash = GetHashKey(ESX.PlayerData.loadout[i].name)
|
||||
GiveWeaponToPed(playerPed, weaponHash, ESX.PlayerData.loadout[i].ammo, false, false)
|
||||
end
|
||||
|
||||
LoadoutLoaded = true
|
||||
|
||||
end)
|
||||
LoadoutLoaded = true
|
||||
|
||||
end)
|
||||
|
||||
RegisterNetEvent('esx:setAccountMoney')
|
||||
AddEventHandler('esx:setAccountMoney', function(account)
|
||||
|
||||
for i=1, #ESX.PlayerData.accounts, 1 do
|
||||
if ESX.PlayerData.accounts[i].name == account.name then
|
||||
ESX.PlayerData.accounts[i] = account
|
||||
end
|
||||
end
|
||||
|
||||
ESX.UI.HUD.UpdateElement('account_' .. account.name, {
|
||||
money = account.money
|
||||
})
|
||||
end)
|
||||
|
||||
RegisterNetEvent('es:activateMoney')
|
||||
AddEventHandler('es:activateMoney', function(money)
|
||||
ESX.PlayerData.money = money
|
||||
end)
|
||||
|
||||
RegisterNetEvent('esx:addInventoryItem')
|
||||
AddEventHandler('esx:addInventoryItem', function(item, count)
|
||||
|
||||
for i=1, #ESX.PlayerData.inventory, 1 do
|
||||
if ESX.PlayerData.inventory[i].name == item.name then
|
||||
ESX.PlayerData.inventory[i] = item
|
||||
end
|
||||
end
|
||||
|
||||
ESX.UI.ShowInventoryItemNotification(true, item, count)
|
||||
|
||||
if ESX.UI.Menu.IsOpen('default', 'es_extended', 'inventory') then
|
||||
@@ -133,6 +145,12 @@ end)
|
||||
RegisterNetEvent('esx:removeInventoryItem')
|
||||
AddEventHandler('esx:removeInventoryItem', function(item, count)
|
||||
|
||||
for i=1, #ESX.PlayerData.inventory, 1 do
|
||||
if ESX.PlayerData.inventory[i].name == item.name then
|
||||
ESX.PlayerData.inventory[i] = item
|
||||
end
|
||||
end
|
||||
|
||||
ESX.UI.ShowInventoryItemNotification(false, item, count)
|
||||
|
||||
if ESX.UI.Menu.IsOpen('default', 'es_extended', 'inventory') then
|
||||
@@ -141,6 +159,11 @@ AddEventHandler('esx:removeInventoryItem', function(item, count)
|
||||
|
||||
end)
|
||||
|
||||
RegisterNetEvent('esx:setJob')
|
||||
AddEventHandler('esx:setJob', function(job)
|
||||
ESX.PlayerData.job = job
|
||||
end)
|
||||
|
||||
RegisterNetEvent('esx:addWeapon')
|
||||
AddEventHandler('esx:addWeapon', function(weaponName, ammo)
|
||||
local playerPed = GetPlayerPed(-1)
|
||||
@@ -431,6 +454,7 @@ Citizen.CreateThread(function()
|
||||
end
|
||||
|
||||
if loadoutChanged and LoadoutLoaded then
|
||||
ESX.PlayerData.loadout = loadout
|
||||
TriggerServerEvent('esx:updateLoadout', loadout)
|
||||
end
|
||||
|
||||
|
||||
@@ -7,5 +7,5 @@ Config.PaycheckInterval = 7 * 60000
|
||||
Config.ShowDotAbovePlayer = false
|
||||
Config.DisableWantedLevel = true
|
||||
Config.RemoveInventoryItemDelay = 5 * 60000
|
||||
Config.EnableWeaponPickup = false
|
||||
Config.EnableWeaponPickup = true
|
||||
Config.Locale = 'fr'
|
||||
|
||||
@@ -3,6 +3,7 @@ ESX.Players = {}
|
||||
ESX.UsableItemsCallbacks = {}
|
||||
ESX.Items = {}
|
||||
ESX.ServerCallbacks = {}
|
||||
ESX.TimeoutCallbacks = {}
|
||||
ESX.LastPlayerData = {}
|
||||
ESX.Pickups = {}
|
||||
ESX.PickupId = 0
|
||||
@@ -73,4 +74,28 @@ AddEventHandler('esx:triggerServerCallback', function(name, requestId, ...)
|
||||
TriggerClientEvent('esx:serverCallback', _source, requestId, ...)
|
||||
end, ...)
|
||||
|
||||
end)
|
||||
end)
|
||||
|
||||
-- SetTimeout
|
||||
CreateThread(function()
|
||||
while true do
|
||||
|
||||
Wait(0)
|
||||
|
||||
local currTime = os.clock() * 1000
|
||||
|
||||
for i=1, #ESX.TimeoutCallbacks, 1 do
|
||||
|
||||
if ESX.TimeoutCallbacks[i] ~= nil then
|
||||
|
||||
if currTime >= ESX.TimeoutCallbacks[i].time then
|
||||
ESX.TimeoutCallbacks[i].cb()
|
||||
ESX.TimeoutCallbacks[i] = nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -20,6 +20,18 @@ ESX.GetRandomString = function(length)
|
||||
|
||||
end
|
||||
|
||||
ESX.SetTimeout = function(msec, cb)
|
||||
table.insert(ESX.TimeoutCallbacks, {
|
||||
time = os.clock() * 1000 + msec,
|
||||
cb = cb
|
||||
})
|
||||
return #ESX.TimeoutCallbacks
|
||||
end
|
||||
|
||||
ESX.ClearTimeout = function(i)
|
||||
ESX.TimeoutCallbacks[i] = nil
|
||||
end
|
||||
|
||||
ESX.RegisterServerCallback = function(name, cb)
|
||||
ESX.ServerCallbacks[name] = cb
|
||||
end
|
||||
@@ -249,4 +261,5 @@ ESX.CreatePickup = function(type, name, count, label, player)
|
||||
|
||||
ESX.PickupId = pickupId
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user