mirror of
https://github.com/jimathy/jim_bridge.git
synced 2026-08-19 14:56:02 +01:00
make breaktool() smarter and add useToolDegrade()
This commit is contained in:
@@ -376,22 +376,46 @@ end
|
|||||||
--- ```lua
|
--- ```lua
|
||||||
--- breakTool({ item = "drill", damage = 10 })
|
--- breakTool({ item = "drill", damage = 10 })
|
||||||
--- ```
|
--- ```
|
||||||
function breakTool(data) -- WIP
|
function breakTool(data)
|
||||||
local durability, slot = getDurability(data.item)
|
local metadata, slot = getItemMetadata(data.item)
|
||||||
if not durability then durability = 100 end
|
metadata = metadata or {}
|
||||||
durability -= data.damage
|
if not metadata.durability then metadata.durability = 100 end
|
||||||
if durability <= 0 then
|
metadata.durability -= data.damage
|
||||||
removeItem(data.item, 1)
|
if metadata.durability <= 0 then
|
||||||
|
removeItem(data.item, 1, nil, slot)
|
||||||
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:setItemMetaData", { item = data.item, slot = slot, metadata = { durability = durability } })
|
TriggerServerEvent(getScript()..":server:setItemMetaData", { item = data.item, slot = slot, metadata = metadata })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- local metadata = getItemMetadata("item", 1, 1)
|
|
||||||
--- print(json.encode(metadata))
|
|
||||||
|
|
||||||
|
--- Reduces the uses of a tool by a specified damage amount.
|
||||||
|
---
|
||||||
|
--- If uses reaches zero or below, the tool is removed and a break sound is played.
|
||||||
|
---
|
||||||
|
--- @param data table Contains:
|
||||||
|
--- - item (string): The tool's name.
|
||||||
|
--- - damage (number): The damage % to apply.
|
||||||
|
---
|
||||||
|
--- @usage
|
||||||
|
--- ```lua
|
||||||
|
--- useToolDegrade({ item = "drill", maxUse = 10 })
|
||||||
|
--- ```
|
||||||
|
function useToolDegrade(data) -- WIP
|
||||||
|
local metadata, slot = getItemMetadata(data.item)
|
||||||
|
metadata = metadata or {}
|
||||||
|
if not metadata["Uses Left"] then metadata["Uses Left"] = data.maxUse end
|
||||||
|
metadata["Uses Left"] -= 1
|
||||||
|
if metadata["Uses Left"] <= 0 then
|
||||||
|
removeItem(data.item, 1, nil, slot)
|
||||||
|
local breakId = GetSoundId()
|
||||||
|
PlaySoundFromEntity(breakId, "Drill_Pin_Break", PlayerPedId(), "DLC_HEIST_FLEECA_SOUNDSET", 1, 0)
|
||||||
|
else
|
||||||
|
TriggerServerEvent(getScript()..":server:setItemMetaData", { item = data.item, slot = slot, metadata = metadata })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function getItemMetadata(item, slot, src)
|
function getItemMetadata(item, slot, src)
|
||||||
local lowestSlot = 100
|
local lowestSlot = 100
|
||||||
@@ -421,17 +445,6 @@ function getItemMetadata(item, slot, src)
|
|||||||
return metadata, lowestSlot
|
return metadata, lowestSlot
|
||||||
end
|
end
|
||||||
|
|
||||||
function getDurability(item, slot, src)
|
|
||||||
local metadata, slot = getItemMetadata(item, slot, src)
|
|
||||||
local durability = nil
|
|
||||||
|
|
||||||
if next(metadata) then
|
|
||||||
durability = metadata.durability or nil
|
|
||||||
end
|
|
||||||
|
|
||||||
return durability, slot
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Server event handler to set metadata for an item.
|
--- Server event handler to set metadata for an item.
|
||||||
---
|
---
|
||||||
--- Updates item metadata (e.g. durability) for the player's inventory based on slot.
|
--- Updates item metadata (e.g. durability) for the player's inventory based on slot.
|
||||||
|
|||||||
Reference in New Issue
Block a user