API reference Obsidian.API.Inventory
IItemBehavior interface
What a plugin's item does when a player uses it. Implement the methods the item needs; the others leave the item to Obsidian's default handling, which for an item without vanilla behaviour is nothing.
public interface IItemBehavior
In Obsidian.API.
Remarks
The behaviour belongs to the plugin and is dropped when the plugin unloads, while the item stays registered (stacks of it stay in inventories and keep their id), so keep state in the item stack's components or the plugin, not here.
Methods
InventoryTickAsync(ItemInventoryTickContext)
ValueTask InventoryTickAsync(ItemInventoryTickContext context);Called every tick, on the player's tick, for each stack of the item in a player's inventory: their hotbar and main inventory, then their off hand and armour, like vanilla's inventoryTick.
Remarks
It runs holding the player's inventory lock, which clicks take too, so that it never sees a click half done. Change the stack or the inventory through their own members (a slot changed this way is sent to the player after the tick). It may hurt or kill the holder, or any player: what that does to an inventory, like wearing armour, dropping a dead player's items or giving an advancement's loot, waits until the hooks let go of the lock and follows in the same tick, and once a hook kills the holder the rest of their stacks don't tick. Don't wait on anything else that clicks or the inventory lock wait on, such as another task's click. Players' inventories are only scanned for items whose behaviour implements this method, so leave it out when the item doesn't need it.
UseAsync(ItemUseContext)
ValueTask<bool> UseAsync(ItemUseContext context);Called when a player uses the item without aiming at a block (right-clicking the air).
Returns
Whether the behaviour handled the use, which skips Obsidian's own handling.
UseOnBlockAsync(ItemUseOnBlockContext)
ValueTask<bool> UseOnBlockAsync(ItemUseOnBlockContext context);Called when a player uses the item on a block (right-clicking it), before the block reacts or the item places a block.
Returns
Whether the behaviour handled the use, which skips Obsidian's own handling.
UseOnEntityAsync(ItemUseOnEntityContext)
ValueTask<bool> UseOnEntityAsync(ItemUseOnEntityContext context);Called when a player uses the item on a living entity (right-clicks it), on the entity's level tick, after the entity's own interaction (a villager's trading, feeding an animal, a plugin entity type's behaviour) and only when that did nothing, like vanilla's interactLivingEntity.
Returns
Whether the behaviour handled the use.
Remarks
Mobs that can be ridden (tamed horses, camels, adult happy ghasts and nautiluses) give the item its use during their own interaction, before the player mounts, as vanilla's do; so when it returns false and the mob does nothing either, it's called twice in one use. In creative mode the context's stack is a copy, so that changing it doesn't change the player's.