Skip to main content

ItemPart

Trait ItemPart 

pub trait ItemPart:
    Send
    + Sync
    + 'static {
Show 22 methods // Provided methods fn tooltip(&self, host: &dyn ClientHost, event: &mut TooltipEvent<'_>) { ... } fn use_on( &self, host: &mut dyn ServerHost, event: &mut UseOnEvent<'_>, ) -> Flow { ... } fn use_item( &self, host: HostSide<'_>, event: &mut UseItemEvent<'_>, ) -> Option<InteractionResult> { ... } fn hurt_enemy( &self, host: &mut dyn ServerHost, event: &mut HurtEnemyEvent<'_>, ) { ... } fn inventory_tick( &self, host: &mut dyn ServerHost, event: &mut InventoryTickEvent<'_>, ) { ... } fn item_bar(&self, host: &dyn ClientHost, event: &mut ItemBarEvent<'_>) { ... } fn dispense_item( &self, host: &mut dyn ServerHost, event: &mut DispenseItemEvent<'_>, ) -> Option<(Option<ItemStack>, i32)> { ... } fn tooltip_component( &self, host: &dyn ClientHost, event: &mut TooltipComponentEvent<'_>, ) -> Option<[f32; 2]> { ... } fn tooltip_component_draw( &self, host: &dyn ClientHost, event: &mut TooltipComponentDrawEvent<'_>, ) { ... } fn item_decorations( &self, host: &dyn ClientHost, event: &mut ItemDecorationsEvent<'_>, ) { ... } fn use_duration(&self, host: &dyn Host, event: &mut UseDurationEvent<'_>) { ... } fn use_animation( &self, host: &dyn ClientHost, event: &mut UseAnimationEvent<'_>, ) { ... } fn on_use_tick(&self, host: HostSide<'_>, event: &mut OnUseTickEvent<'_>) { ... } fn release_using( &self, host: HostSide<'_>, event: &mut ReleaseUsingEvent<'_>, ) { ... } fn can_perform_action( &self, host: &dyn Host, event: &mut CanPerformActionEvent<'_>, ) { ... } fn crafting_remainder( &self, host: &dyn Host, event: &mut CraftingRemainderEvent<'_>, ) { ... } fn damage_item( &self, host: &mut dyn ServerHost, event: &mut DamageItemEvent<'_>, ) -> Flow { ... } fn on_left_click_entity( &self, host: &mut dyn ServerHost, event: &mut OnLeftClickEntityEvent<'_>, ) -> Flow { ... } fn on_entity_swing( &self, host: HostSide<'_>, event: &mut OnEntitySwingEvent<'_>, ) -> Flow { ... } fn item_interact_mob( &self, host: &mut dyn ServerHost, event: &mut ItemInteractMobEvent<'_>, ) -> Option<bool> { ... } fn on_entity_item_update( &self, host: &mut dyn ServerHost, event: &mut OnEntityItemUpdateEvent<'_>, ) -> Flow { ... } fn on_item_use_first( &self, host: &mut dyn ServerHost, event: &mut OnItemUseFirstEvent<'_>, ) -> Option<InteractionResult> { ... }
}
Expand description

An item’s part.

Provided Methods§

fn tooltip(&self, host: &dyn ClientHost, event: &mut TooltipEvent<'_>)

Item.appendHoverText: an item’s own lines in its tooltip, after its name and before its components’ (enchantments, lore, attributes). Add lines to lines, which arrives empty, each with a colour as rgba packs it (u32::MAX keeps the text’s own); Text::legacy reads § codes. The stack has its components (stack.data()). Asked each frame the tooltip shows, never for one the stack hides or a crossbow’s loaded projectile. This is the scoped Item.appendHoverText moment, not NeoForge’s global ItemTooltipEvent: attached parts add their lines here before component lines are assembled. Afterwards Hooks::item_tooltip sees the finished list, including these parts’ lines. Side: client.

Every part gets event.lines in order and may add, change or remove lines.

fn use_on(&self, host: &mut dyn ServerHost, event: &mut UseOnEvent<'_>) -> Flow

A player uses stack (right click) with self.hand on the block at pos, face the face clicked ([0, 1, 0] is the top), once the block itself let the click through (a chest still opens): where ItemStack.useOn starts. Set claimed to take the use: the hand swings and neither the item’s useOn nor its use runs. Server-side, for every player of the world (the helpers read the acting one); an item on cooldown is never used. hand, hit (UseOnContext.getClickLocation, getClickedFace, isInside) and player (the acting player’s UUID) and player_entity_id (the server’s entity id),. Side: server.

Return Flow::Cancel to take it: this sets claimed, and later parts are skipped.

fn use_item( &self, host: HostSide<'_>, event: &mut UseItemEvent<'_>, ) -> Option<InteractionResult>

A player uses stack (right click) with self.hand: in the air, or on a block once native_use_on let it through, where Item.use starts. Runs on both logical sides after the global right-click event. Return an interaction result: Success swings on the client, SuccessServer swings on the server, Consume handles without swinging. Fail skips this item’s use without swinging, then the off hand may be tried. None lets later Parts answer; if none answers, the item’s own use runs. Some(Pass) skips later Parts and continues to the item’s own use. TryWithEmptyHand is block-phase-only and is treated as Pass here. Cooldown items are skipped. hand is the interaction hand of THIS click, including an instantaneous air use. Unlike this field, living_used_item_hand answers only during active item use. This is PlayerInteractEvent.RightClickItem.getHand. Side: both.

Return Some to set result; the first part returning Some wins.

fn hurt_enemy(&self, host: &mut dyn ServerHost, event: &mut HurtEnemyEvent<'_>)

A player’s hit landed on the mob entity of type kind (minecraft:zombie), holding stack: where Item.hurtEnemy is called, before the post-attack enchantments and the weapon’s wear. It only hears. Another player as the target isn’t told yet. Server-side, for every player of the world (the helpers read the acting one). Side: server.

fn inventory_tick( &self, host: &mut dyn ServerHost, event: &mut InventoryTickEvent<'_>, )

A server tick of stack, the stack a player carries in slot: where Inventory.tick calls Item.inventoryTick, for every stack, each tick, /tick freeze included (it stops the world, not the player). slot is as Inventory saves it (0 to 35; the armour 100 feet to 103 head; -106 the off hand), and selected says it is the held slot. ItemPlace::PlayerSlot { player, slot } reads and changes the stack, which stays in its slot: an emptied one empties the slot, and the player’s client gets the change. Only the ItemParts attached to stack’s item hear it. Server-side, for every living player of the world (the helpers read the acting one). Side: server.

fn item_bar(&self, host: &dyn ClientHost, event: &mut ItemBarEvent<'_>)

The small bar GuiGraphicsExtractor.itemDecorations draws under an item, wherever the game shows one: bar arrives holding vanilla’s value (the damage bar, or a bundle’s fullness already applied) and may be replaced (width 0..=13, colour rgb) or set to None to hide it. Asked each frame a stack is drawn. Client-side. Side: client.

fn dispense_item( &self, host: &mut dyn ServerHost, event: &mut DispenseItemEvent<'_>, ) -> Option<(Option<ItemStack>, i32)>

DispenserBlock.getDispenseMethod: choose a behaviour for any item, including vanilla items, on the authoritative server (not droppers). The block the dispenser faces is asked first (dispense_method), and its answer keeps this part from being asked. Leave answer None for vanilla. Otherwise answer (remainder, event): a remainder of None empties the slot, Some(stack) replaces it (the hook owns consumption: no extra item is removed afterwards). event is the level event the dispenser plays with its smoke: 1000 the click, 1001 the fail sound (OptionalDispenseItemBehavior without success), or 0 for nothing at all (DispenseItemBehavior.NOOP). To drop one normally, answer (native_dispense_default(..), 1000). Side: server.

Return Some to set answer; the first part returning Some wins.

fn tooltip_component( &self, host: &dyn ClientHost, event: &mut TooltipComponentEvent<'_>, ) -> Option<[f32; 2]>

Client-side image component for this stack’s tooltip. Answer its width/height in GUI pixels; finite positive dimensions reserve space after text and any vanilla bundle component. None keeps the vanilla tooltip unchanged. Side: client.

Return Some to set size; the first part returning Some wins.

fn tooltip_component_draw( &self, host: &dyn ClientHost, event: &mut TooltipComponentDrawEvent<'_>, )

Render the measured component at origin, inside the tooltip background. Stack is the component’s data. Use native_sprite/native_fill/native_text; Size is the whole tooltip content box: the widest component and the sum of component heights (minus 2 normally; no subtraction for an item tooltip with multiple components). Origin is this component’s running top and the box’s left edge. All text is drawn before any images. Side: client.

fn item_decorations( &self, host: &dyn ClientHost, event: &mut ItemDecorationsEvent<'_>, )

Append an item’s custom decorations after its default bar, cooldown and count. Coordinates are the icon’s top-left in GUI units, as the batch takes them; no item translation is active. Runs wherever the shared item-decoration path is used (hotbar, menus, tooltips, and the other screens that draw item icons: advancements, beacon, stats, trading). The NeoForge counterpart is IItemDecorator.render; registration through RegisterItemDecorationsEvent is a separate moment. Side: client.

fn use_duration(&self, host: &dyn Host, event: &mut UseDurationEvent<'_>)

Item.getUseDuration of stack, in ticks: duration arrives with vanilla’s (a consumable’s time, 72000 for a shield, a spear, a bow, 0 for an item with no use over time). A mod’s own item (outside minecraft:) with no consumable, blocks_attacks or kinetic weapon that answers above 0 is used over time on a right click, as an Item.use that calls startUsingItem, until it is let go or the time runs out (native_living_entity_use_item_finish). A consumable’s time changes too, potions and the other vanilla items keep their own. Asked when a use starts, each tick a mod’s item is in use, and each frame the hand draws one in use, on both sides. It has no user, as it is asked so often: answer from the stack alone. Side: both.

fn use_animation( &self, host: &dyn ClientHost, event: &mut UseAnimationEvent<'_>, )

Item.getUseAnimation of stack in use, for first- and third-person rendering. animation arrives with the item’s vanilla answer; mods may change it. A Spear override on an item without kinetic data is refused as None. Asked on the client when each render consumer updates; answer from the stack alone. Side: client.

fn on_use_tick(&self, host: HostSide<'_>, event: &mut OnUseTickEvent<'_>)

Item.onUseTick of stack, used by entity with remaining ticks left, always above 0: what a mod’s own item does each tick it is used (cook, charge, drain). It comes where ItemStack.onUseTick calls the item, once native_living_entity_use_item_tick let the use go on: after a consumable’s crumbs, for consumables, shields and mods’ items (vanilla’s other items do their own, and a kinetic weapon has none on the server). Both sides, as native_living_entity_use_item_tick: change the world only where this Game runs the world, not on a server’s client. Runs every tick of every use: check the item first. Side: both.

fn release_using(&self, host: HostSide<'_>, event: &mut ReleaseUsingEvent<'_>)

Item.releaseUsing of stack: entity let it go with time_left ticks of its native_use_duration left, so it was held for duration - time_left (a bow-like item shoots by that charge). Set used as vanilla’s return, and the stack’s use cooldown starts (ItemStack.releaseUsing). After the vanilla item’s own release, once native_living_entity_use_item_stop let it through. Both sides, as vanilla’s: change the world only where this Game runs the world, not on a server’s client. Side: both.

fn can_perform_action( &self, host: &dyn Host, event: &mut CanPerformActionEvent<'_>, )

NeoForge’s IItemExtension.canPerformAction: can stack perform the item ability ability, a namespaced id such as minecraft:shield_block, minecraft:shears_harvest or minecraft:shears_carve (a mod’s own abilities are its own, my_mod:chop)? can_perform arrives with vanilla’s answer for that ability: the item kind’s (shears shear) or the stack’s component (blocks_attacks, a shield’s, blocks); set it either way and the stack grants or loses the ability. Asked where vanilla checks the item for an action: a raised item blocking attacks (minecraft:shield_block: a granted stack blocks as a bare blocks_attacks component does, a denied one never blocks nor is raised); shearing a sheep, mooshroom, snow golem or bogged, a dispenser’s shears and a bee nest’s honeycombs (minecraft:shears_harvest); carving a pumpkin (minecraft:shears_carve). It decides whether the stack may do the action, not what the action does to the world or the tool (block_tool_modification is the block’s side of a tool action; item wear is its own). Only the ItemParts attached to stack’s item are asked, on both sides, so the client’s prediction agrees with the server: answer from the stack and ability alone, and fast. An item with no such part costs one table lookup. Side: both.

fn crafting_remainder( &self, host: &dyn Host, event: &mut CraftingRemainderEvent<'_>, )

NeoForge’s IItemExtension.getCraftingRemainder(ItemStack): what stack leaves when consumed by crafting, furnace fuel or brewing. remainder arrives holding the item’s data remainder. Replace it, or set it to None to leave nothing. A part that leaves it alone preserves the data remainder. Recipe-specific book and banner copies are handled by the recipe. Asked for each consumption on the authoritative server, with a read-only Host. Oversized counts, unknown items and invalid damage are refused by the stack boundary, retaining the original data remainder. API stacks cannot have count zero; use None to leave nothing. Native panics follow the normal Part crash path, attributed to the owning mod and Part. Side: server.

fn damage_item( &self, host: &mut dyn ServerHost, event: &mut DamageItemEvent<'_>, ) -> Flow

IItemExtension.damageItem: the wear stack is about to take (ItemStack.hurtAndBreak: a tool that mines or hits, armour that takes a hit, a shield, a rod, an elytra, a tool a dispenser uses, a mob’s armour), after Unbreaking has had its say, as NeoForge passes it. amount is the points about to be lost; change it (0 or less leaves the item whole) or set canceled for no wear at all. Not asked when Unbreaking already saved the item, nor for a creative player’s tools (they take no wear). Only the ItemParts attached to stack’s item hear it: an item with none costs one table lookup, and a game with no such part none at all. On the paths of a player the player’s state is swapped in, so host.players().acting_player() answers them. Once for each wear, on a server. Side: server.

Return Flow::Cancel to take it: this sets canceled, and later parts are skipped.

fn on_left_click_entity( &self, host: &mut dyn ServerHost, event: &mut OnLeftClickEntityEvent<'_>, ) -> Flow

IItemExtension.onLeftClickEntity: player (the acting player’s UUID), holding stack in the main hand, is about to attack the entity entity (Entity.getId) of type kind (minecraft:cow, minecraft:player, minecraft:item_frame): where Player.attack starts, once the global attack_entity let the blow through and before any damage, knockback, enchantment effect, hurt_enemy or the weapon’s wear. Set canceled and the attack ends there: the target is not hurt, hurt_enemy does not fire and the stack takes no wear. Only the ItemParts attached to stack’s item are asked. Server-side, for every player of the world (the helpers read the acting one). Side: server.

Return Flow::Cancel to take it: this sets canceled, and later parts are skipped.

fn on_entity_swing( &self, host: HostSide<'_>, event: &mut OnEntitySwingEvent<'_>, ) -> Flow

IItemExtension.onEntitySwing: the player in place (entity, its Entity.getId when known) is about to swing stack in hand: where LivingEntity.swing starts, for an attack, a use or a swing the server asked for. Set canceled and no swing starts: no animation and no swing packet. Only the ItemParts attached to stack’s item are asked. Both sides, as vanilla’s: answer the same on both, and change the world only where this Game runs the world, not on a server’s client. Side: both.

Return Flow::Cancel to take it: this sets canceled, and later parts are skipped.

fn item_interact_mob( &self, host: &mut dyn ServerHost, event: &mut ItemInteractMobEvent<'_>, ) -> Option<bool>

native_item_interact_mob(entity, kind, stack, swing) runs after ordinary entity interactions pass, for an alive native Mob selected by the exact packet ID or unobstructed crosshair. First Some(true) owns and swings once; Some(false) owns without a swing; None passes. The nonempty stack belongs to the acting hand. Armor stands, other real players and dragon-part tables are not covered. Bundles need “hooks”: @API:item_interact_mob@. Side: server.

Return Some to set swing; the first part returning Some wins.

fn on_entity_item_update( &self, host: &mut dyn ServerHost, event: &mut OnEntityItemUpdateEvent<'_>, ) -> Flow

IItemExtension.onEntityItemUpdate: each tick of a dropped item entity holding stack, before the entity’s own tick (ItemEntity.tick: its fluids, movement, pickup delay, merging and lifetime), and after entity_tick_pre. item is the entity, in the world: read and change it through the host, its attachments included. Set canceled to skip that tick’s vanilla work, as NeoForge’s true return does; the item skips movement and its own merge pass; another item can still merge into it.\n /// entity_tick_post still hears it. Only the ItemParts attached to stack’s item hear it: an item with none costs one table lookup, and a game with no such part none at all. Server-side, for every dropped item that ticks. Side: server.

Return Flow::Cancel to take it: this sets canceled, and later parts are skipped.

fn on_item_use_first( &self, host: &mut dyn ServerHost, event: &mut OnItemUseFirstEvent<'_>, ) -> Option<InteractionResult>

IItemExtension.onItemUseFirst: the held item’s first say on a click on a block, on the server for each player, after use_item_on_block (ItemBeforeBlock) let it through and before the block’s own useItemOn. An answer ends the click: neither the block nor the item’s useOn runs. Pass goes on to the block. Side: server.

Return Some to set answer; the first part returning Some wins.

Trait Implementations§

§

impl Wire for Box<dyn ItemPart>

§

const MIN: usize = 8

Minimum encoded size, used to reject impossible collection lengths.
§

fn put(&self, out: &mut Writer)

Encodes this value, recording failures on the writer.
§

fn put_owned(self, out: &mut Writer)

Consumes owned data when a resource’s ownership must cross the boundary.
§

fn take(input: &mut Reader<'_>) -> Result<Box<dyn ItemPart>, WireError>

Decodes one value, leaving any following bytes for the caller.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§