Main/resources/[carscripts]/slashtires/bridge/inventory/qb/server.lua

82 lines
2.7 KiB
Lua
Raw Normal View History

2025-06-07 08:51:21 +02:00
if not QBCore then
QBCore = exports['qb-core']:GetCoreObject()
end
---Checks if the player has the weapon item specified
---@param source string
---@param weaponHash integer
---@return boolean hasWeapon
function HasWeapon(source, weaponHash)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then
warn("Could not get player data checking for weapon item")
return false
end
local itemName = Config.AllowedWeapons[weaponHash].name
local itemCount = Player.Functions.GetItemByName(itemName)?.amount or 0
return itemCount > 0
end
---Removes the weapon from the player
---@param source string
---@param weaponHash integer
---@param playerPed integer
---@return boolean success
function RemoveWeapon(source, weaponHash, playerPed)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then
warn("Could not get player data when removing a weapon")
return false
end
local itemName = Config.AllowedWeapons[weaponHash]?.name
if not itemName then
warn(string.format("Could not get weapon name when removing a weapon with hash %s", weaponHash))
return false
end
local removedWeapon = Player.Functions.RemoveItem(itemName, 1)
if not removedWeapon then
warn(string.format("QBCore could not remove weapon item %s from the players inventory", itemName))
return false
end
TriggerClientEvent('inventory:client:ItemBox', source, QBCore.Shared.Items[itemName], 'remove')
RemoveWeaponFromPed(playerPed, weaponHash) -- qb-inventory does not remove the current weapon when it's associated item is removed, so we just force remove it
return true
end
---Reduces durability for the weapon
---@param source string
---@param weaponHash integer
---@param playerPed integer
---@return boolean removedWeapon If the weapon was removed due to durability going below 0
function ReduceDurabilityForWeapon(source, weaponHash, playerPed)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then
warn("Could not get player data when reducing weapon durability")
return false
end
local weapon = Config.AllowedWeapons[weaponHash]
if not weapon?.durability then
return false
end
local slot = QBCore.Player.GetFirstSlotByItem(Player.PlayerData.items, weapon.name)
local weaponSlot = Player.PlayerData.items[slot]
local durability = (weaponSlot.info.quality or 100) - weapon.durability
if durability <= 0.0 then
return RemoveWeapon(source, weaponHash, playerPed)
else
weaponSlot.info.quality = durability
Player.Functions.SetInventory(Player.PlayerData.items, true)
end
return false
end