Skip to content

Commit

Permalink
chore: type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Dec 20, 2023
1 parent 0911681 commit 58b0bbc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ local Animations = lib.load('data.animations')
local Items = require 'modules.items.client'
local usingItem = false

---@param data { name: string, label: string, count: number, slot: number, metadata: table<string, any>, weight: number }
lib.callback.register('ox_inventory:usingItem', function(data)
local item = Items[data.name]

Expand Down Expand Up @@ -411,17 +412,18 @@ local function canUseItem(isAmmo)
end

---@param data table
---@param cb function?
---@param cb fun(response: SlotWithItem | false)?
local function useItem(data, cb)
local slotData, result = PlayerData.inventory[data.slot]

if not canUseItem(data.ammo and true) then return end
if not slotData or not canUseItem(data.ammo and true) then return end

if currentWeapon?.timer and currentWeapon.timer > 100 then return end

if invOpen and data.close then client.closeInventory() end

usingItem = true
---@type boolean?
result = lib.callback.await('ox_inventory:useItem', 200, data.name, data.slot, slotData.metadata)

if result and cb then
Expand Down Expand Up @@ -601,6 +603,9 @@ local function useSlot(slot)
end)
elseif data.component then
local components = data.client.component

if not components then return end

local componentType = data.type
local weaponComponents = PlayerData.inventory[currentWeapon.slot].metadata.components

Expand Down Expand Up @@ -879,7 +884,7 @@ local function updateInventory(data, weight)

local curItem = PlayerData.inventory[item.slot]

if curItem?.name then
if curItem and curItem.name then
itemCount[curItem.name] = (itemCount[curItem.name] or 0) - curItem.count
end

Expand Down Expand Up @@ -1129,6 +1134,9 @@ end)
RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inventory, weight, player)
if source == '' then return end

---@class PlayerData
---@field inventory table<number, SlotWithItem?>
---@field weight number
PlayerData = player
PlayerData.id = cache.playerId
PlayerData.source = cache.serverId
Expand Down Expand Up @@ -1550,6 +1558,8 @@ RegisterNUICallback('removeComponent', function(data, cb)

local itemSlot = PlayerData.inventory[currentWeapon.slot]

if not itemSlot then return end

for _, component in pairs(Items[data.component].client.component) do
if HasPedGotWeaponComponent(playerPed, currentWeapon.hash, component) then
for k, v in pairs(itemSlot.metadata.components) do
Expand Down
1 change: 1 addition & 0 deletions server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ lib.callback.register('ox_inventory:useItem', function(source, itemName, slot, m

data.consume = consume

---@type boolean
local success = lib.callback.await('ox_inventory:usingItem', source, data)

if item.weapon then
Expand Down

0 comments on commit 58b0bbc

Please sign in to comment.