Feature: New hook: useItem
Alexr03 opened this issue · 6 comments
Is your feature request related to a problem? Please describe.
We have an arena system for players to do activities like 1v1s, we like to disable the players use of their inventory to prevent healing, putting armour on etc by using items in their inventory. Right now our only option is to disable use of the inventory all together. We tried to use invBusy
state on the player, this works but has a side effect of no longer being able to shoot their weapon
Line 1422 in bb8f029
We could disable this ourself but we're trying to keep ox_inventory
as vanilla as possible to make updates easier.
This has led to the idea of making a useItem hook where we can return false if we want to stop the item from being used, then we can disable all items and setup a "whitelist" of allowed items that can be used while in the arena.
Describe the solution you'd like
Ability to create a hook for useItem
that will allow us to cancel the request to use the item.
Example:
local hookId = exports.ox_inventory:registerHook('useItem', function(payload)
print(json.encode(payload, { indent = true }))
local playerState = Player(payload.source).state
if (not playerState.inArena) then
return
end
if (payload.itemName == 'water') then -- Only allow water to be used in arena, otherwise cancel the use.
return
end
return false
end, {
print = true,
})
Describe alternatives you've considered
(The alternatives below will prevent the inventory from opening and allowing weapon shooting, but won't allow us to dynamically cancel the useItem
)
- Remove/Comment code that disables shooting when
invBusy
is true. - Check every frame for if state
invOpen
is true, and then call export tocloseInventory
immediately.
Additional context
N/A
https://overextended.dev/ox_inventory/Functions/Client#weaponwheel
this will disable the inventory while allowing you to shoot still
exports.ox_inventory:weaponWheel(true)
GiveWeaponToPed(cache.ped, self.private.weapon, 9999, false, true)
SetCurrentPedWeapon(cache.ped, self.private.weapon, true)
SetPedInfiniteAmmo(cache.ped, true, self.private.weapon)
SetPedInfiniteAmmoClip(cache.ped, true)
here is an export from something ive useed it for. and then when the weaponwheel is disabled again, it will actually unequip the weapon given this way if they dont have it in their inventory. assuming you have the weapon mismatch checking on for the inventory
ultimately the hook here is not that useful since if you register item uses through the export callbacks you have access to all the different stages of item use already.
https://overextended.dev/ox_inventory/Guides/creatingItems#client-callbacks
https://overextended.dev/ox_inventory/Guides/creatingItems#server-callbacks
That is what we currently do to give a temporary weapon.
Its my understanding that it will disable using weapons from the inventory, but will allow other items like bandages? At least this is how it works for us. (https://aka.x3.dev/s/HRjXNJe/raw it wont let us use a weapon, when weaponWheel is enabled, but can still use other items)
I see what you're saying with the callbacks, but surely a hook is better to define the logic in one place of what can/cannot be used rather than in each callback?
Yes the docs imply it but code wise it only disable weapons being used as far as I can see.
Line 511 in 3f298a1
Regardless I still think having a hook for useItem
is a good addition.
Interesting. I guess i can see the convenience of the hook in some cases then.