hope this works
This commit is contained in:
1 parent
5b6641fc60
commit
e06e04a2c6
6 files changed
+138
-102
No files matched your search
@@ -1,61 +0,0 @@
|
|||||||
Behaviors = {
|
|
||||||
All = {},
|
|
||||||
isSetup = false,
|
|
||||||
}
|
|
||||||
|
|
||||||
function Behaviors.Setup()
|
|
||||||
if Behaviors.isSetup then return end
|
|
||||||
Behaviors.isSetup = true
|
|
||||||
local behaviors = Require("lib/entities/client/behaviors/init.lua")
|
|
||||||
for _, behavior in ipairs(behaviors) do
|
|
||||||
Behaviors.Create(behavior.tag, behavior)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Behaviors.Create(behaviorId, behavior)
|
|
||||||
if not behaviorId or not behavior then return end
|
|
||||||
if Behaviors.All[behaviorId] then
|
|
||||||
print(string.format("[ClientEntity] Behavior %s already exists", behaviorId))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
Behaviors.All[behaviorId] = behavior
|
|
||||||
end
|
|
||||||
|
|
||||||
function Behaviors.Get(behaviorId)
|
|
||||||
return Behaviors.All[behaviorId]
|
|
||||||
end
|
|
||||||
|
|
||||||
function Behaviors.Remove(behaviorId)
|
|
||||||
if not Behaviors.All[behaviorId] then return end
|
|
||||||
Behaviors.All[behaviorId] = nil
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function Behaviors.Trigger(actionName, clientEntityData, ...)
|
|
||||||
if not clientEntityData or not actionName then return end
|
|
||||||
for tag, behavior in pairs(Behaviors.All) do
|
|
||||||
local hasBehaviorArgs = Behaviors.Has(tag, clientEntityData) -- this is everything that's contained inside the object's individual tag
|
|
||||||
if hasBehaviorArgs and behavior[actionName] then
|
|
||||||
behavior[actionName](clientEntityData, hasBehaviorArgs, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Behaviors.Inherit(behaviorId, clientEntityData, defaultData)
|
|
||||||
if not Behaviors.All[behaviorId] then
|
|
||||||
print(string.format("[ClientEntity] Behavior %s does not exist", behaviorId))
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
if not clientEntityData or not clientEntityData.id then
|
|
||||||
print("[ClientEntity] Invalid client entity data provided for inheritance")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
clientEntityData[behaviorId] = defaultData or {} -- Mark the entity as having this behavior
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function Behaviors.Has(behaviorId, clientEntityData)
|
|
||||||
return clientEntityData and clientEntityData[behaviorId]
|
|
||||||
end
|
|
||||||
|
|
||||||
return Behaviors
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
local fileList = {
|
|
||||||
"particles",
|
|
||||||
"targets",
|
|
||||||
}
|
|
||||||
|
|
||||||
local path = "lib/entities/client/behaviors/"
|
|
||||||
|
|
||||||
local function loadAllResources()
|
|
||||||
local modules = {}
|
|
||||||
for _, fileName in ipairs(fileList) do
|
|
||||||
local filePath = path .. fileName .. ".lua"
|
|
||||||
local required = Require(filePath)
|
|
||||||
if required then
|
|
||||||
table.insert(modules, required)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return modules
|
|
||||||
end
|
|
||||||
|
|
||||||
return loadAllResources()
|
|
||||||
@@ -282,12 +282,21 @@ RegisterNetEvent("community_bridge:client:DeleteEntity", function(id)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNetEvent("community_bridge:client:UpdateEntity", function(id, data)
|
RegisterNetEvent("community_bridge:client:UpdateEntity", function(id, data)
|
||||||
ClientEntity.Update(id, data)
|
for k, v in pairs(data) do
|
||||||
|
ClientEntity.Set(id, k, v)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
local Loaded = false
|
||||||
|
AddEventHandler('community_bridge:Client:OnPlayerLoaded', function(resourceName)
|
||||||
|
if Loaded then return end
|
||||||
|
Loaded = true
|
||||||
end)
|
end)
|
||||||
|
|
||||||
AddEventHandler('onResourceStart', function(resourceName)
|
AddEventHandler('onResourceStart', function(resourceName)
|
||||||
if resourceName ~= GetCurrentResourceName() then return end
|
if resourceName ~= GetCurrentResourceName() then return end
|
||||||
Behaviors.Setup()
|
if Loaded then return end
|
||||||
|
Loaded = true
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Resource Stop Cleanup
|
-- Resource Stop Cleanup
|
||||||
|
|||||||
@@ -117,6 +117,19 @@ function ServerEntity.Remove(id)
|
|||||||
Entities[id] = nil
|
Entities[id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Updates data for a server-side entity and notifies clients.
|
||||||
|
-- @param id string|number The ID of the entity to update.
|
||||||
|
-- @param data table The data fields to update.
|
||||||
|
function ServerEntity.Set(id, data)
|
||||||
|
local entity = Entities[id]
|
||||||
|
if not entity then return false end
|
||||||
|
for key, value in pairs(data) do
|
||||||
|
entity[key] = value
|
||||||
|
end
|
||||||
|
TriggerClientEvent("community_bridge:client:UpdateEntity", -1, id, data)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
-- Clean up entities associated with a stopped resource
|
-- Clean up entities associated with a stopped resource
|
||||||
AddEventHandler('onResourceStop', function(resourceName)
|
AddEventHandler('onResourceStop', function(resourceName)
|
||||||
local toDelete = {}
|
local toDelete = {}
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
Behaviors = {
|
||||||
|
All = {},
|
||||||
|
isSetup = false,
|
||||||
|
invoked = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function Behaviors.Create(behaviorId, behavior)
|
||||||
|
if not behaviorId or not behavior then return end
|
||||||
|
if Behaviors.All[behaviorId] then return print(string.format("[ClientEntity] Behavior %s already exists", behaviorId)) end
|
||||||
|
local invoking = GetInvokingResource() or "community_bridge"
|
||||||
|
Behaviors.All[behaviorId] = behavior
|
||||||
|
Behaviors.invoked[invoking] = Behaviors.invoked[invoking] or {}
|
||||||
|
table.insert(Behaviors.invoked[invoking], behaviorId)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Behaviors.Get(behaviorId)
|
||||||
|
return Behaviors.All[behaviorId]
|
||||||
|
end
|
||||||
|
|
||||||
|
function Behaviors.Remove(behaviorId)
|
||||||
|
if not Behaviors.All[behaviorId] then return end
|
||||||
|
Behaviors.All[behaviorId] = nil
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function Behaviors.Trigger(actionName, clientEntityData, ...)
|
||||||
|
if not clientEntityData or not actionName then return end
|
||||||
|
for property, behavior in pairs(Behaviors.All) do
|
||||||
|
local hasBehaviorArgs = Behaviors.Has(property, clientEntityData) -- this is everything that's contained inside the object's individual property
|
||||||
|
if hasBehaviorArgs and behavior[actionName] then
|
||||||
|
local success, result = pcall(behavior[actionName], clientEntityData, hasBehaviorArgs, ...)
|
||||||
|
if not success then
|
||||||
|
print(string.format("[ClientEntity] Behavior %s failed: %s", property, result))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Behaviors.Inherit(behaviorId, clientEntityData, defaultData)
|
||||||
|
if not Behaviors.All[behaviorId] then
|
||||||
|
print(string.format("[ClientEntity] Behavior %s does not exist", behaviorId))
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
if not clientEntityData or not clientEntityData.id then
|
||||||
|
print("[ClientEntity] Invalid client entity data provided for inheritance")
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
clientEntityData[behaviorId] = defaultData or {} -- Mark the entity as having this behavior
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function Behaviors.Has(behaviorId, clientEntityData)
|
||||||
|
return clientEntityData and clientEntityData[behaviorId]
|
||||||
|
end
|
||||||
|
|
||||||
|
function Behaviors.Cleanup(resourceName)
|
||||||
|
if not resourceName or not Behaviors.invoked[resourceName] then return end
|
||||||
|
for _, behaviorId in ipairs(Behaviors.invoked[resourceName]) do
|
||||||
|
Behaviors.Remove(behaviorId)
|
||||||
|
end
|
||||||
|
Behaviors.invoked[resourceName] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
AddEventHandler("onResourceStop", function(resourceName)
|
||||||
|
Behaviors.Cleanup(resourceName)
|
||||||
|
if resourceName ~= GetCurrentResourceName() then return end
|
||||||
|
for behaviorId, _ in pairs(Behaviors.All) do
|
||||||
|
Behaviors.Remove(behaviorId)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
return Behaviors
|
||||||
@@ -131,17 +131,16 @@ Inventory.OpenStash = function(src, _type, id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---This will register a stash
|
---This will register a stash
|
||||||
---@param id number|string
|
---@param id string
|
||||||
---@param label string
|
---@param label string
|
||||||
---@param slots number
|
---@param slots number
|
||||||
---@param weight number
|
---@param weight number
|
||||||
---@param owner string
|
---@param owner string
|
||||||
---@param groups table
|
---@param groups table
|
||||||
---@param coords table
|
---@param coords table
|
||||||
---@return boolean
|
---@return string
|
||||||
---@return string|number
|
|
||||||
Inventory.RegisterStash = function(id, label, slots, weight, owner, groups, coords)
|
Inventory.RegisterStash = function(id, label, slots, weight, owner, groups, coords)
|
||||||
if Inventory.Stashes[id] then return true, id end
|
if Inventory.Stashes[id] then return id end
|
||||||
Inventory.Stashes[id] = {
|
Inventory.Stashes[id] = {
|
||||||
id = id,
|
id = id,
|
||||||
label = label,
|
label = label,
|
||||||
@@ -151,8 +150,8 @@ Inventory.RegisterStash = function(id, label, slots, weight, owner, groups, coor
|
|||||||
groups = groups,
|
groups = groups,
|
||||||
coords = coords
|
coords = coords
|
||||||
}
|
}
|
||||||
ox_inventory:RegisterStash(id, label, slots, weight, owner, groups)
|
ox_inventory:RegisterStash(id, label, slots, weight, owner, groups, coords)
|
||||||
return true, id
|
return id
|
||||||
end
|
end
|
||||||
|
|
||||||
---This will return a boolean if the player has the item.
|
---This will return a boolean if the player has the item.
|
||||||
@@ -194,24 +193,47 @@ end
|
|||||||
|
|
||||||
-- This will open the specified shop for the src passed.
|
-- This will open the specified shop for the src passed.
|
||||||
---@param src number
|
---@param src number
|
||||||
---@param shopTitle string
|
---@param id string
|
||||||
Inventory.OpenShop = function(src, shopTitle)
|
Inventory.OpenShop = function(src, id)
|
||||||
TriggerClientEvent('ox_inventory:openInventory', src, 'shop', {type = shopTitle})
|
local title = registeredShops[id]?.title or id
|
||||||
|
TriggerClientEvent('ox_inventory:openInventory', src, 'shop', {type = title})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- This will register a shop, if it already exists it will return true.
|
-- This will register a shop, if it already exists it will return true.
|
||||||
---@param shopTitle string
|
---@param title string
|
||||||
---@param shopInventory table
|
---@param items table
|
||||||
---@param shopCoords table
|
---@param coords table
|
||||||
---@param shopGroups table
|
---@param groups table
|
||||||
Inventory.RegisterShop = function(shopTitle, shopInventory, shopCoords, shopGroups)
|
Inventory.RegisterShop = function(title, items, coords, groups)
|
||||||
if registeredShops[shopTitle] then return true end
|
return Inventory.CreateShop({id = title, name = title, items = items, coords = coords, groups = groups})
|
||||||
registeredShops[shopTitle] = true
|
|
||||||
ox_inventory:RegisterShop(shopTitle, { name = shopTitle, inventory = shopInventory, groups = shopGroups, })
|
|
||||||
--return Inventory.OpenShop(src, shopTitle)
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Creates a new shop if it doesn't already exist.
|
||||||
|
---
|
||||||
|
--- @class ShopData
|
||||||
|
--- @field id string|nil -- Unique identifier for the shop (optional, falls back to title)
|
||||||
|
--- @field name string -- Display name of the shop
|
||||||
|
--- @field items table -- Table of items available in the shop
|
||||||
|
--- @field coords table|nil -- Optional coordinates for the shop location
|
||||||
|
--- @field groups table|nil -- Optional groups allowed to access the shop
|
||||||
|
---
|
||||||
|
--- @param data ShopData -- Table containing shop properties
|
||||||
|
--- @return string|boolean -- Returns the shop ID if the shop was created, false if it already exists
|
||||||
|
Inventory.CreateShop = function(data)
|
||||||
|
assert(data, "CreateShop: data is required")
|
||||||
|
local id = data.id or data.name
|
||||||
|
if registeredShops[id] then return id end
|
||||||
|
assert(data.name, "CreateShop: name is required")
|
||||||
|
assert(data.items and type(data.items) == "table", "CreateShop: items table is required")
|
||||||
|
registeredShops[id] = {
|
||||||
|
name = data.name,
|
||||||
|
items = data.items,
|
||||||
|
coords = data.coords,
|
||||||
|
groups = data.groups
|
||||||
|
}
|
||||||
|
ox_inventory:RegisterShop(id, data)
|
||||||
|
return id
|
||||||
|
end
|
||||||
|
|
||||||
---UNUSED:
|
---UNUSED:
|
||||||
---This will return generic item data from the specified inventory, with the items total count.
|
---This will return generic item data from the specified inventory, with the items total count.
|
||||||
|
|||||||
Reference in new issue
Block a user