User Tools

Site Tools


zh_cn:tutorial:items_docs

This is an old revision of the document!


物品

Items are the pieces of content that appear in your inventory. They can perform actions when you click, act as food, or spawn entities. The following documentation will give you a rundown of the entire Item class and everything related to it. For a tutorial you can follow along, visit Practical Example: Adding an Item.

物品设置

Item(物品类)的构造方法(constructor)需要一个Item.Settings(物品设置类)的实例。该建造者类(builder class)定义物品的诸多行为,例如堆叠数量、耐久度、是否可食用、等等。以下是所有可用的建造者方法:

方法 参数类型 简介
food FoodComponent 设置该物品的FoodComponent(食品部分)为参数值,并标记该物品可食用。
maxCount int 设置该物品的最大堆叠数量为参数值。与大于0的耐久度冲突。
maxDamageIfAbsent int 若该物品未设置最大耐久度以及堆叠数量不大于1,则设置最大耐久度为参数值。
maxDamage int 设置该物品的最大耐久度。(与大于1的最大堆叠数量冲突,并且会在注册时报错!
recipeRemainder Item 设置该物品的合成剩余物(即合成后偿还给玩家的物品)。
group ItemGroup 设置该物品的物品组(即创造物品栏中的标签页)。
rarity Rarity 设置该物品的稀有度,而其也决定了物品名称的默认显示颜色。

Fabric物品设置

Fabric的物品API提供了一些额外的方法来设置一些其他属性。要使用这些新方法,只需要将new Item.Settings()替换成new FabricItemSettings()即可。 下列是所有FabricItemSettings提供的额外功能:

方法 参数类型 简介
equipmentSlot EquipmentSlotProvider 设置物品如何提供并指定装备槽。
customDamage CustomDamageHandler 设置物品如何自定义处理伤害变化。

食物

public Item.Settings food(FoodComponent foodComponent)

foodComponent - 一个FoodComponent对象。设置时物品的可食属性会根据FoodComponent的值而变化。更深一步的讲解请见FoodComponent的简介页。


最大堆叠数量

public Item.Settings maxCount(int maxCount)

maxCount - 物品在物品堆中可堆叠的最大数量。如果之前已经调用过maxDamage(),游戏会抛出一个RuntimeException(运行时异常),因为一个物品无法同时拥有数量与耐久度。推荐将最大堆叠数量设为64以下的值,更高的值可能会引发许多问题。


若未设置则设置最大耐久度

public Item.Settings maxDamageIfAbsent(int maxDamage)

maxDamage - 物品在物品堆中的最大耐久度。

如果之前还没有调用过maxDamage(),则会将最大耐久度设成参数的值。工具和盔甲在没有设置耐久度时会使用此方法来将物品的最大耐久度设置为工具材料(ToolMaterial)的最大耐久度。


最大耐久度

public Item.Settings maxDamage(int maxDamage)

maxDamage - 物品在物品堆中的最大耐久度。


合成剩余物

public Item.Settings recipeRemainder(Item recipeRemainder)

recipeRemainder - 原物品在合成完成之后返回的物品

有设置了合成剩余物的物品在合成后会将剩余物返回给玩家。设置了该属性的物品有桶(岩浆、水、牛奶)和瓶子(龙息、蜂蜜),合成过后则会返回对应的空物品。


物品组

public Item.Settings group(ItemGroup group)

group - 该物品加入的ItemGroup(物品组)

每个物品组都会被显示为创造模式物品栏中的一个标签页,将物品添加进该组则会将该物品加入标签页中,而显示的顺序由注册的顺序决定。若要添加一个新物品组请见这里


稀有度

public Item.Settings rarity(Rarity rarity)

rarity - 该物品的稀有度。(默认为常见)

有自定义的稀有度的物品名称会变成不同的颜色。

稀有度 字体颜色
常见 白色
少见 黄色
稀有 青色
史诗 淡紫色
zh_cn/tutorial/items_docs.1604293596.txt.gz · Last modified: 2020/11/02 05:06 by leocth2