forked from Simnation/Main
red
This commit is contained in:
parent
9178871ecd
commit
640cdd069b
9 changed files with 583 additions and 269 deletions
|
@ -30,23 +30,23 @@
|
|||
|
||||
--// Player //--
|
||||
-- Item
|
||||
AddItem = function(source, ...)
|
||||
AddItem = function(source, item, amount, metadata, slot)
|
||||
if ESX then
|
||||
xPlayer = ESX.GetPlayerFromId(source)
|
||||
xPlayer.addInventoryItem(...)
|
||||
xPlayer.addInventoryItem(item, amount, metadata, slot)
|
||||
else
|
||||
xPlayer = QBCore.Functions.GetPlayer(source)
|
||||
xPlayer.Functions.AddItem(...)
|
||||
xPlayer.Functions.AddItem(item, amount, slot, metadata)
|
||||
end
|
||||
end
|
||||
|
||||
RemoveItem = function(source, ...)
|
||||
RemoveItem = function(source, item, amount, metadata, slot)
|
||||
if ESX then
|
||||
xPlayer = ESX.GetPlayerFromId(source)
|
||||
xPlayer.removeInventoryItem(...)
|
||||
xPlayer.removeInventoryItem(item, amount, metadata, slot)
|
||||
else
|
||||
xPlayer = QBCore.Functions.GetPlayer(source)
|
||||
xPlayer.Functions.RemoveItem(...)
|
||||
xPlayer.Functions.RemoveItem(item, amount, slot, metadata)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -616,6 +616,28 @@
|
|||
return values
|
||||
end
|
||||
|
||||
-- Uses table.clone for fast shallow copying (memcpy) before checking and doing actual deepcopy for nested tables
|
||||
-- Handles circular references via seen table
|
||||
-- Significantly faster (~50%) than doing actual deepcopy for flat or lightly-nested structures
|
||||
---@param orig table
|
||||
---@return table
|
||||
table.deepcopy = function(orig, seen)
|
||||
if type(orig) ~= "table" then return orig end
|
||||
seen = seen or {}
|
||||
if seen[orig] then return seen[orig] end
|
||||
|
||||
local copy = table.clone(orig)
|
||||
seen[orig] = copy
|
||||
|
||||
for k, v in next, orig do
|
||||
if type(v) == "table" then
|
||||
copy[k] = table.deepcopy(v, seen)
|
||||
end
|
||||
end
|
||||
|
||||
return copy
|
||||
end
|
||||
|
||||
math.round = function(number, decimals)
|
||||
local _ = 10 ^ decimals
|
||||
return math.floor((number * _) + 0.5) / (_)
|
||||
|
@ -744,32 +766,52 @@
|
|||
local CreatedEntities = {}
|
||||
UtilityNet = {}
|
||||
|
||||
UtilityNet.ForEachEntity = function(fn, slice)
|
||||
if slice then
|
||||
if not GlobalState.Entities[slice] then
|
||||
return
|
||||
end
|
||||
UtilityNet.ForEachEntity = function(fn, slices)
|
||||
if slices then
|
||||
local entities = UtilityNet.GetEntities(slices)
|
||||
|
||||
for k,v in pairs(GlobalState.Entities[slice]) do
|
||||
local ret = fn(v, k)
|
||||
for i = 1, #slices do
|
||||
local _entities = entities[slices[i]]
|
||||
local n = 0
|
||||
|
||||
if _entities then
|
||||
-- Manual pairs loop for performance
|
||||
local k,v = next(_entities)
|
||||
|
||||
if ret ~= nil then
|
||||
return ret
|
||||
while k do
|
||||
n = n + 1
|
||||
local ret = fn(v, k)
|
||||
|
||||
if ret ~= nil then
|
||||
return ret
|
||||
end
|
||||
k,v = next(_entities, k)
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
if not GlobalState.Entities then
|
||||
local entities = UtilityNet.GetEntities()
|
||||
|
||||
if not entities then
|
||||
return
|
||||
end
|
||||
|
||||
for sliceI,slice in pairs(GlobalState.Entities) do
|
||||
for k2, v in pairs(slice) do
|
||||
-- Manual pairs loop for performance
|
||||
local sliceI,slice = next(entities)
|
||||
|
||||
while sliceI do
|
||||
local k2, v = next(slice)
|
||||
while k2 do
|
||||
local ret = fn(v, k2)
|
||||
|
||||
if ret ~= nil then
|
||||
return ret
|
||||
end
|
||||
|
||||
k2,v = next(slice, k2)
|
||||
end
|
||||
|
||||
sliceI, slice = next(entities, sliceI)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -798,11 +840,7 @@ end
|
|||
|
||||
-- Returns the slice the entity is in
|
||||
UtilityNet.InternalFindFromNetId = function(uNetId)
|
||||
for sliceI, slice in pairs(GlobalState.Entities) do
|
||||
if slice[uNetId] then
|
||||
return slice[uNetId], sliceI
|
||||
end
|
||||
end
|
||||
return exports["utility_lib"]:InternalFindFromNetId(uNetId)
|
||||
end
|
||||
|
||||
UtilityNet.DoesUNetIdExist = function(uNetId)
|
||||
|
@ -835,16 +873,20 @@ UtilityNet.GetEntityModel = function(uNetId)
|
|||
end
|
||||
end
|
||||
|
||||
UtilityNet.GetEntities = function()
|
||||
return exports["utility_lib"]:GetEntities()
|
||||
end
|
||||
|
||||
UtilityNet.SetModelRenderDistance = function(model, distance)
|
||||
return exports["utility_lib"]:SetModelRenderDistance(model, distance)
|
||||
end
|
||||
|
||||
UtilityNet.SetEntityCoords = function(uNetId, newCoords)
|
||||
return exports["utility_lib"]:SetEntityCoords(uNetId, newCoords)
|
||||
UtilityNet.SetEntityCoords = function(uNetId, newCoords, skipPositionUpdate)
|
||||
return exports["utility_lib"]:SetEntityCoords(uNetId, newCoords, skipPositionUpdate)
|
||||
end
|
||||
|
||||
UtilityNet.SetEntityRotation = function(uNetId, newRotation)
|
||||
return exports["utility_lib"]:SetEntityRotation(uNetId, newRotation)
|
||||
UtilityNet.SetEntityRotation = function(uNetId, newRotation, skipRotationUpdate)
|
||||
return exports["utility_lib"]:SetEntityRotation(uNetId, newRotation, skipRotationUpdate)
|
||||
end
|
||||
|
||||
UtilityNet.DetachEntity = function(uNetId)
|
||||
|
@ -917,7 +959,25 @@ getValueAsStateTable = function(id, baseKey, depth)
|
|||
})
|
||||
end
|
||||
|
||||
UtilityNet.AddStateBagChangeHandler = function(uNetId, func)
|
||||
return AddEventHandler("Utility:Net:UpdateStateValue", function(s_uNetId, key, value)
|
||||
if uNetId == s_uNetId then
|
||||
func(key, value)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
UtilityNet.RemoveStateBagChangeHandler = function(eventData)
|
||||
if eventData and eventData.key and eventData.name then
|
||||
RemoveEventHandler(eventData)
|
||||
end
|
||||
end
|
||||
|
||||
UtilityNet.State = function(id)
|
||||
if not id then
|
||||
error("UtilityNet.State: id is required, got nil", 2)
|
||||
end
|
||||
|
||||
local state = setmetatable({
|
||||
raw = function(self)
|
||||
return exports["utility_lib"]:GetEntityStateValue(id)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue