User Tools

Site Tools


zh_cn:tutorial:tools

This is an old revision of the document!


添加工具

创建工具材料

工具需要ToolMaterial来定义以下行为:

  • 耐久
  • 挖掘速度
  • 攻击伤害
  • 挖掘等级
  • 附魔级别
  • 修复原料

换句话说,工具材料定义了该类工具的基本功能,工具可以选择使用由材料提供的值,或者使用其自己的。

原版的工具材料在ToolMaterials中。我们为我们的材料创建单独的类:

  1. public class PotatoToolMaterial implements ToolMaterial {
  2.  
  3. [...]
  4. }

ToolMaterial需要实现一系列方法:

耐久

getDurability定义了工具在使用此材料时的耐久。所有相同类型的原版工具都有相同的耐久。

  1. @Override
  2. public int getDurability() {
  3. return 500;
  4. }

挖掘速度

getMiningSpeedMultiplier定义了工具破坏方块的速度。木制工具的速度为2.0F,钻石工具的速度为8.0F。

  1. @Override
  2. public float getMiningSpeedMultiplier() {
  3. return 5.0F;
  4. }

攻击伤害

getAttackDamage返回工具的基本伤害。注意大多数工具在其构造器中都需要一个整数,这意味着最终的攻击伤害为(float) materialDamage + (int) toolDamage + 1。如果你需要工具在其构造器中完全控制其伤害数量,你可以让材料返回0F的攻击伤害。

  1. @Override
  2. public float getAttackDamage() {
  3. return 3.0F;
  4. }

挖掘等级

getMiningLevel设置了工具的挖掘等级。钻石的挖掘等级为3,黑曜石需要3+的挖掘等级来挖掘。

  1. @Override
  2. public int getMiningLevel() {
  3. return 2;
  4. }

附魔能力

getEnchantability定义了工具可以如何附魔。金有22的附魔能力,钻石的附魔能力为10。更高的附魔能力意味着更好(更高等级)的附魔。

  1. @Override
  2. public int getEnchantability() {
  3. return 15;
  4. }

修复原料

getRepairIngredient返回在铁砧中修复物品所需要的Ingredient

  1. @Override
  2. public Ingredient getRepairIngredient() {
  3. return Ingredient.ofItems(Items.POTATO);
  4. }

ToolMaterial不需要注册。A good way to pass them out to tools that require them is by keeping an instance somewhere (and then referencing it when you need it). In this case, we will put our instance at the top of the Tool Material class:

  1. public class PotatoToolMaterial implements ToolMaterial {
  2.  
  3. public static final PotatoToolMaterial INSTANCE = new PotatoToolMaterial();
  4.  
  5. [...]
  6. }

PotatoToolMaterial can now be referenced with PotatoToolMaterial.INSTANCE.

创建工具

所有的基本工具类(PickaxeItemShovelItemHoeItemAxeItemSwordItem)都需要一个ToolMaterial、攻击速度(浮点)、额外攻击伤害数量(整数)、Item.Settings实例。

  1. public static ToolItem POTATO_SHOVEL = new ShovelItem(PotatoToolMaterial.INSTANCE, 1.5F, -3.0F, new Item.Settings().group(ItemGroup.TOOLS));
  2. public static ToolItem POTATO_SWORD = new SwordItem(PotatoToolMaterial.INSTANCE, 3, -2.4F, new Item.Settings().group(ItemGroup.COMBAT));

`PickaxeItem`、`HoeItem`和`AxeItem`都有受保护的构造器,这意味着你需要用公开的构造器创建子类:

  1. public class CustomPickaxeItem extends PickaxeItem {
  2. public CustomPickaxeItem(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) {
  3. super(material, attackDamage, attackSpeed, settings);
  4. }
  5. }

使用自定义的子类:

public static ToolItem POTATO_PICKAXE = new CustomPickaxeItem(PotatoToolMaterial.INSTANCE, 1, -2.8F, new Item.Settings().group(ItemGroup.TOOLS));
public static ToolItem POTATO_AXE = new CustomAxeItem(PotatoToolMaterial.INSTANCE, 7.0F, -3.2F, new Item.Settings().group(ItemGroup.TOOLS));
public static ToolItem POTATO_HOE = new CustomHoeItem(PotatoToolMaterial.INSTANCE, 7, -3.2F, new Item.Settings().group(ItemGroup.TOOLS));

If you want to add any special attributes or behaviors to your tool, create a subclass that extends one of the base tool classes, and override any required methods.

注册物品

注册物品可以参考此教程

让你的工具对非原版方块起作用

请参考mining_levels的最后一段。

zh_cn/tutorial/tools.1627176560.txt.gz · Last modified: 2021/07/25 01:29 by solidblock