Rename metahandler functions

Realised I named the player and item metadata functions too closely and made it confusing, this separates them better
This commit is contained in:
Jim Shield
2025-07-07 17:47:04 +01:00
parent d255034c02
commit b595453efe
4 changed files with 46 additions and 116 deletions

View File

@@ -21,12 +21,12 @@ This module provides access and control over player metadata, which is useful fo
print("Player stress level:", stress) print("Player stress level:", stress)
``` ```
- **SetMetadata(player, key, value)** - **setPlayerMetadata(player, key, value)**
⚠️ Server side only ⚠️ Server side only
- Updates or assigns a value to a specific metadata key for a player. - Updates or assigns a value to a specific metadata key for a player.
- The function updates the player's metadata using the active core export. - The function updates the player's metadata using the active core export.
- **Example:** - **Example:**
```lua ```lua
SetMetadata(player, "stress", 0) setPlayerMetadata(player, "stress", 0)
``` ```

View File

@@ -411,11 +411,11 @@ function makeItem(data)
-- handle metadata and experience in a single go -- handle metadata and experience in a single go
if data.craft["hasCrafted"] ~= nil then if data.craft["hasCrafted"] ~= nil then
data.craftable.craftedItems[data.item] = true data.craftable.craftedItems[data.item] = true
triggerCallback(getScript()..":server:SetMetadata", "craftedItems", data.craftable.craftedItems) triggerCallback(getScript()..":server:setPlayerMetadata", "craftedItems", data.craftable.craftedItems)
end end
if data.craft["exp"] ~= nil then if data.craft["exp"] ~= nil then
craftingLevel += data.craft["exp"].give * craftAmount craftingLevel += data.craft["exp"].give * craftAmount
triggerCallback(getScript()..":server:SetMetadata", "craftingLevel", craftingLevel) triggerCallback(getScript()..":server:setPlayerMetadata", "craftingLevel", craftingLevel)
end end
if data.craftable.Recipes[1].oneUse == true then if data.craftable.Recipes[1].oneUse == true then
removeItem("craftrecipe", 1, nil, data.craftable.Recipes[1].slot) removeItem("craftrecipe", 1, nil, data.craftable.Recipes[1].slot)
@@ -448,11 +448,11 @@ function makeItem(data)
currentToken = nil currentToken = nil
if data.craft["hasCrafted"] ~= nil then if data.craft["hasCrafted"] ~= nil then
data.craftable.craftedItems[data.item] = true data.craftable.craftedItems[data.item] = true
triggerCallback(getScript()..":server:SetMetadata", "craftedItems", data.craftable.craftedItems) triggerCallback(getScript()..":server:setPlayerMetadata", "craftedItems", data.craftable.craftedItems)
end end
if data.craft["exp"] ~= nil then if data.craft["exp"] ~= nil then
craftingLevel += data.craft["exp"].give craftingLevel += data.craft["exp"].give
triggerCallback(getScript()..":server:SetMetadata", "craftingLevel", craftingLevel) triggerCallback(getScript()..":server:setPlayerMetadata", "craftingLevel", craftingLevel)
end end
if data.craftable.Recipes[1].oneUse == true then if data.craftable.Recipes[1].oneUse == true then
removeItem("craftrecipe", 1, nil, data.craftable.Recipes[1].slot) removeItem("craftrecipe", 1, nil, data.craftable.Recipes[1].slot)

View File

@@ -385,121 +385,51 @@ function breakTool(data) -- WIP
local breakId = GetSoundId() local breakId = GetSoundId()
PlaySoundFromEntity(breakId, "Drill_Pin_Break", PlayerPedId(), "DLC_HEIST_FLEECA_SOUNDSET", 1, 0) PlaySoundFromEntity(breakId, "Drill_Pin_Break", PlayerPedId(), "DLC_HEIST_FLEECA_SOUNDSET", 1, 0)
else else
TriggerServerEvent(getScript()..":server:setMetaData", { item = data.item, slot = slot, metadata = { durability = durability } }) TriggerServerEvent(getScript()..":server:setItemMetaData", { item = data.item, slot = slot, metadata = { durability = durability } })
end end
end end
--- Retrieves the durability and slot number of an item in a player's inventory. --- local metadata = getItemMetadata("item", 1, 1)
--- --- print(json.encode(metadata))
--- Searches through the player's inventory for the specified item.
---
--- @param item string The item name. function getItemMetadata(item, slot, src)
--- @return number|nil number The durability, or nil if not found.
--- @return number|nil number The slot number, or nil if not found.
---
--- @usage
--- ```lua
--- local durability, slot = getDurability("drill")
--- if durability then
--- print("Durability:", durability)
--- print("Slot:", slot)
--- end
--- ```
function getDurability(item)
local lowestSlot = 100 local lowestSlot = 100
local durability = nil local chosenSlot = slot
if isStarted(QBInv) or isStarted(PSInv) or isStarted(RSGInv) or isStarted(JPRInv) then local metadata = {}
local itemcheck = Core.Functions.GetPlayerData().items
local itemcheck = getPlayerInv(src)
if chosenSlot then
for k, v in pairs(itemcheck) do
if v.name == item and v.slot == chosenSlot then
lowestSlot = v.slot
metadata = itemcheck[k].info or itemcheck[k].metadata or {}
debugPrint("^6Bridge^7: ^2Found metadata for item ^3"..item.." ^2in slot ^3"..lowestSlot.."^7")
end
end
else
for k, v in pairs(itemcheck) do for k, v in pairs(itemcheck) do
if v.name == item then if v.name == item then
if v.slot <= lowestSlot then if v.slot <= lowestSlot then
lowestSlot = v.slot lowestSlot = v.slot
durability = itemcheck[k].info and itemcheck[k].info.durability or nil metadata = itemcheck[k].info or itemcheck[k].metadata or {}
end end
end end
end end
end end
return metadata, lowestSlot
end
if isStarted(OXInv) then function getDurability(item, slot, src)
local itemcheck = exports[OXInv]:Search('slots', item) local metadata, slot = getItemMetadata(item, slot, src)
for k, v in pairs(itemcheck) do local durability = nil
if v.slot <= lowestSlot then
debugPrint(v.slot, itemcheck[k].metadata.durability) if next(metadata) then
lowestSlot = v.slot durability = metadata.durability or nil
durability = v.metadata and v.metadata.durability or nil
end
end
end end
if isStarted(QSInv) then return durability, slot
local itemcheck = exports[QSInv]:getUserInventory()
for _, v in pairs(itemcheck) do
if v.name == item and v.slot <= lowestSlot then
lowestSlot = v.slot
durability = v.info and v.info.durability or nil
end
end
end
if isStarted(CoreInv) then
local itemcheck = exports[CoreInv]:getInventory()
for _, v in pairs(itemcheck) do
if v.name == item and v.slot <= lowestSlot then
lowestSlot = v.slot
durability = v.metadata and v.metadata.durability or nil
end
end
end
if isStarted(CodeMInv) then
local itemcheck = exports[CodeMInv]:GetClientPlayerInventory()
for _, v in pairs(itemcheck) do
if v.name == item and tonumber(v.slot) <= lowestSlot then
lowestSlot = tonumber(v.slot)
durability = v.info and v.info.durability or nil
end
end
end
if isStarted(OrigenInv) then
local itemcheck = exports[OrigenInv]:getPlayerInventory()
for _, v in pairs(itemcheck) do
if v.name == item and v.slot <= lowestSlot then
lowestSlot = v.slot
durability = v.metadata and v.metadata.durability or nil
end
end
end
if isStarted(TgiannInv) then
local itemcheck = exports[TgiannInv]:GetPlayerItems()
for _, v in pairs(itemcheck) do
if v.name == item and v.slot <= lowestSlot then
lowestSlot = v.slot
durability = v.metadata and v.metadata.durability or nil
end
end
end
-- For ESX default inventory (es_extended)
if ESX and isStarted(ESXExport) then
local xPlayer = ESX.GetPlayerData() or {}
if xPlayer.inventory then
for _, v in ipairs(xPlayer.inventory) do
if v.name == item then
-- Optionally use a slot field if available; otherwise, use the index
if v.slot and v.slot <= lowestSlot then
lowestSlot = v.slot
end
if v.metadata and v.metadata.durability then
durability = v.metadata.durability
end
end
end
end
end
return durability, lowestSlot
end end
--- Server event handler to set metadata for an item. --- Server event handler to set metadata for an item.
@@ -513,9 +443,9 @@ end
--- ---
--- @usage --- @usage
--- ```lua --- ```lua
--- TriggerServerEvent("script:server:setMetaData", { item = "drill", slot = 5, metadata = { durability = 80 } }) --- TriggerServerEvent("script:server:setItemMetaData", { item = "drill", slot = 5, metadata = { durability = 80 } })
--- ``` --- ```
RegisterNetEvent(getScript()..":server:setMetaData", function(data, src) RegisterNetEvent(getScript()..":server:setItemMetaData", function(data, src)
local src = src or source local src = src or source
if isStarted(QBInv) or isStarted(PSInv) or isStarted(RSGInv) or isStarted(JPRInv) then if isStarted(QBInv) or isStarted(PSInv) or isStarted(RSGInv) or isStarted(JPRInv) then
--debugPrint(src, data.item, 1, data.slot) --debugPrint(src, data.item, 1, data.slot)

View File

@@ -58,9 +58,9 @@ end
--- ---
--- @usage --- @usage
--- ```lua --- ```lua
--- local myMeta = GetMetadata(player, "myKey") --- local myMeta = getPlayerMetadata(player, "myKey")
--- ``` --- ```
function GetMetadata(player, key) function getPlayerMetadata(player, key)
if not player then if not player then
debugPrint("^6Bridge^7: ^3GetMetadata^7() calling server: "..key) debugPrint("^6Bridge^7: ^3GetMetadata^7() calling server: "..key)
return triggerCallback(getScript()..":server:GetMetadata", key) return triggerCallback(getScript()..":server:GetMetadata", key)
@@ -98,11 +98,11 @@ createCallback(getScript()..":server:GetMetadata", function(source, key)
if type(key) == "table" then if type(key) == "table" then
local Metadata = {} local Metadata = {}
for _, k in ipairs(key) do for _, k in ipairs(key) do
Metadata[k] = GetMetadata(player, k) Metadata[k] = getPlayerMetadata(player, k)
end end
return Metadata return Metadata
elseif type(key) == "string" then elseif type(key) == "string" then
return GetMetadata(player, key) return getPlayerMetadata(player, key)
end end
end) end)
@@ -120,9 +120,9 @@ end)
--- ---
--- @usage --- @usage
--- ```lua --- ```lua
--- SetMetadata(player, "myKey", "newValue") --- setPlayerMetadata(player, "myKey", "newValue")
--- ``` --- ```
function SetMetadata(player, key, value) function setPlayerMetadata(player, key, value)
debugPrint("^6Bridge^7: ^3SetMetadata^7() setting metadata for key: "..key) debugPrint("^6Bridge^7: ^3SetMetadata^7() setting metadata for key: "..key)
if isStarted(QBExport) or isStarted(QBXExport) then if isStarted(QBExport) or isStarted(QBXExport) then
debugPrint("^6Bridge^7: ^3SetMetadata^7() using QBExport/QBXExport") debugPrint("^6Bridge^7: ^3SetMetadata^7() using QBExport/QBXExport")
@@ -144,14 +144,14 @@ function SetMetadata(player, key, value)
end end
-- Register a server callback for setting metadata. -- Register a server callback for setting metadata.
createCallback(getScript()..":server:SetMetadata", function(source, key, value) createCallback(getScript()..":server:setPlayerMetadata", function(source, key, value)
debugPrint("SetMetadata callback triggered for source:", source, "key:", key, "value:", value) debugPrint("SetMetadata callback triggered for source:", source, "key:", key, "value:", value)
local player = GetPlayer(source) local player = GetPlayer(source)
--[[if not player then --[[if not player then
print("Error setting metadata: player not found for source "..tostring(source)) print("Error setting metadata: player not found for source "..tostring(source))
return false return false
end]] end]]
SetMetadata(player, key, value) setPlayerMetadata(player, key, value)
print("Metadata set successfully.", key) print("Metadata set successfully.", key)
return true return true
end) end)