> For the complete documentation index, see [llms.txt](https://gumeng.gitbook.io/ce-shi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gumeng.gitbook.io/ce-shi/kubejsbasic/customs/itemtype/itemuse.md).

# 右键使用物品

```js
StartupEvents.registry('item', event => {
    event.create('use_item')
        /**
         * 物品的使用动画（会根据使用动画的类型播放对应的声音）
         * |'spear'（三叉戟）|'crossbow'（弩）|'eat'（吃）|
         * |'spyglass'（望远镜）|'block'（方块）|'none'（无）|
         * |'bow'（弓）|'drink'（喝）|
         */
        .useAnimation('drink')
        /**
         * 物品的最大使用刻（右键时间超过最大值时将执行.finishUsing()中的函数）
         * 返回值为0时物品将被标记为无法使用use
         * - 关联的函数：
         * > 玩家对象（$Player）的"useItemRemainingTicks"属性为玩家当前手持物品使用的剩余时间
         */
        .useDuration(itemstack => 64)
        /**
         * 是否可以使用
         * 当该值为true且useDuration>0时，该物品将被使用并播放对应的动画和声音
         */
        .use((level, player, hand) => true)
        /**
         * 当物品成功使用后（持续右键经过一个完整的useDuration）后的行为
         */
        .finishUsing((itemstack, level, entity) => {
            entity.potionEffects.add('minecraft:haste', 120 * 20)
            itemstack.shrink(1)
            if (entity.player) {
                /**
                 * @type {$Player_} - 判断是否为玩家，通过后重申entity的类型
                 */
                let player = entity
                player.addItem(Item.of('minecraft:glass_bottle').itemStack)
            }
            return itemstack
        })
        /**
         * 当物品未完成useDuration的时间刻就被释放后的行为
         * tick为距离完整的使用刻还有多少刻
         */
        .releaseUsing((itemstack, level, entity, tick) => {
            itemstack.shrink(1)
            level.createExplosion(entity.x, entity.y, entity.z).explode()
        })
})
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gumeng.gitbook.io/ce-shi/kubejsbasic/customs/itemtype/itemuse.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
