User Tools

Site Tools


zh_cn:tutorial:armor

添加盔甲

介绍

盔甲是比一般的方块或者物品更复杂一点的实现,但是只要了解了,实现还是很简单的。如需添加盔甲,需要先实现 CustomArmorMaterial 类,然后注册物品。我们还需要看看如何为盔甲提供纹理。本文最后有一个解释如何添加击退的特殊章节,因为此方法只能通过Mixin访问(对于 1.16.3)。

本文档的一个例子可以在本模组GitHub仓库找到。

创建盔甲材料类

新的盔甲需要和一个新的名称(以及额外的一些内容,例如盔甲点和耐久度)一起设置,我们需要为我们的 CustomArmorMaterial 创建一个新的类。

本类实现 ArmorMaterial,并以指定值为盔甲点(称为 PROTECTION_VALUES)开始。所有这些参数都会充分利用 @Override。

  1. public class CustomArmorMaterial implements ArmorMaterial {
  2. private static final int[] BASE_DURABILITY = new int[] {13, 15, 16, 11};
  3. private static final int[] PROTECTION_VALUES = new int[] {A, B, C, D};
  4.  
  5. // 其中A是头盔,B是护腿,C是胸甲,D是靴子。
  6. // 例如,皮革使用{1, 2, 3, 1},钻石和下界合金使用{3, 6, 8, 3}
  7. }

接下来的变量如下定义(无需担心名称,下面你会看到如何实现):

  1. method_7696: how many hits can armor take before breaking. Uses the int we wrote on 'BASE_DURABILITY' to calculate. Leather uses 5, Diamond 33, Netherite 37.

计算你的盔甲在损坏之前能承受多少次打击,利用的是你在'BASE_DURABILITY'里所写的int值。皮革是5,钻石33,下届合金的是37。

  1. method_7697: calls for the 'PROTECTION_VALUES' int we already wrote above.

调用以获取我们之前定义的'PROTECTION_VALUES'.

  1. getEnchantability: This will be how likely the armor can get high level or multiple enchantments in an enchantment book.

获取关于这件盔甲能够在一本附魔书中被附魔到多高等级、多少种类的附魔

  1. SoundEvent getEquipSound: The standard used by vanilla armor is SoundEvents.ITEM_ARMOR_EQUIP_X, X being the type of armor.

在原版盔甲中,这个标准是SoundEvents.ITEM_ARMOR_EQUIP_X,其中X是你的盔甲类型

  1. Ingredient getRepairIngredient: what item are we gonna be using to repair the armor on an anvil. It can be either a vanilla item or one of your own.

获取我们应该在铁砧中用什么来修复这件盔甲,材料可以是原版的,也可以是你自己的模组中的物品

  1. String getName: what the parent item of the armor is. In Diamond armor, it'd be “diamond”.
  2. getToughness: This is a second protection value where the armor is more durable against high value attacks. Value goes as 'X.0F'

这是第二个保护数值,表示装甲在面对高伤害攻击时更耐用。数值为'X.0F'。 在1.16引入的新值

  1. getKnockbackResistance: leave this value at 0. If you want to implement it, write '0.XF' (in which X is how much knockback protection you want), and I'll teach you how to make it work later on.

将这个值保留为0。如果你想实现它,写上'0.XF'(其中X代表你想要的击退保护量),我会稍后告诉你如何让它生效。

接下来所有的参数都会写成 X 或者 A、B、C、D。加上这些参数之后,效果应该如下:

  1. public class CustomArmorMaterial implements ArmorMaterial {
  2. private static final int[] BASE_DURABILITY = new int[] {13, 15, 16, 11};
  3. private static final int[] PROTECTION_VALUES = new int[] {A, B, C, D};
  4.  
  5. @Override
  6. public int method_7696(EquipmentSlot slot) {
  7. return BASE_DURABILITY[slot.getEntitySlotId()] * X;
  8. }
  9.  
  10. @Override
  11. public int method_7697(EquipmentSlot slot) {
  12. return PROTECTION_VALUES[slot.getEntitySlotId()];
  13. }
  14.  
  15. @Override
  16. public int getEnchantability() {
  17. return X;
  18. }
  19.  
  20. @Override
  21. public SoundEvent getEquipSound() {
  22. return SoundEvents.ITEM_ARMOR_EQUIP_X;
  23. }
  24.  
  25. @Override
  26. public Ingredient getRepairIngredient() {
  27. return Ingredient.ofItems(RegisterItems.X);
  28. }
  29.  
  30. @Override
  31. public String getName() {
  32. // Must be all lowercase
  33. return "name";
  34. }
  35.  
  36. @Override
  37. public float getToughness() {
  38. return X.0F;
  39. }
  40.  
  41. @Override
  42. public float getKnockbackResistance() {
  43. return 0.XF;
  44. }
  45. }

注意你有盔甲材料类的基础了,所以在新的类中注册你的盔甲物品,即 RegisterItems。

创建盔甲物品

我们准备创建一个叫做 RegisterItems 的新的类以实现新的盔甲物件。这也会是注册工具的地方,如果你准备制作像锭这样的新物品(我们简单称为 Custom_Material)。

  1. public class RegisterItems {
  2.  
  3. public static final ArmorMaterial CUSTOM_ARMOR_MATERIAL = new CustomArmorMaterial();
  4. public static final Item CUSTOM_MATERIAL = new CustomMaterialItem(new Item.Settings());
  5. // 如果创建了新的材料,则你需要注意这里。
  6. public static final Item CUSTOM_MATERIAL_HELMET = new ArmorItem(CUSTOM_ARMOR_MATERIAL, EquipmentSlot.HEAD, new Item.Settings());
  7. public static final Item CUSTOM_MATERIAL_CHESTPLATE = new ArmorItem(CUSTOM_ARMOR_MATERIAL, EquipmentSlot.CHEST, new Item.Settings());
  8. public static final Item CUSTOM_MATERIAL_LEGGINGS = new ArmorItem(CUSTOM_ARMOR_MATERIAL, EquipmentSlot.LEGS, new Item.Settings());
  9. public static final Item CUSTOM_MATERIAL_BOOTS = new ArmorItem(CUSTOM_ARMOR_MATERIAL, EquipmentSlot.FEET, new Item.Settings());
  10.  
  11. }

现在物品创建好了,将其注册并给予适当的名称,你的第一个参数是名字空间,也就是你的模组 ID,第二个是你需要给予你的物品的名称。

我们会在最后一个ArmorItem的下面写这些。

  1. public static void register() {
  2. Registry.register(Registries.ITEM, new Identifier("tutorial", "custom_material"), CUSTOM_MATERIAL);
  3. Registry.register(Registries.ITEM, new Identifier("tutorial", "custom_material_helmet"), CUSTOM_MATERIAL_HELMET);
  4. Registry.register(Registries.ITEM, new Identifier("tutorial", "custom_material_chestplate"), CUSTOM_MATERIAL_CHESTPLATE);
  5. Registry.register(Registries.ITEM, new Identifier("tutorial", "custom_material_leggings"), CUSTOM_MATERIAL_LEGGINGS);
  6. Registry.register(Registries.ITEM, new Identifier("tutorial", "custom_material_boots"), CUSTOM_MATERIAL_BOOTS);
  7. }

你的盔甲物品已完成。现在我们在主类中调用 Registry。

  1. public static final ItemGroup EXAMPLE_MOD_GROUP = FabricItemGroupBuilder.create(
  2. new Identifier("examplemod", "example_mod_group"))
  3. .icon(() -> new ItemStack(RegisterItems.CUSTOM_MATERIAL)) // 这里将你创建的新的材料的模型用作图标,但是你也可以随时使用你喜欢的
  4. .build();
  5.  
  6. @Override
  7. public void onInitialize() {
  8. RegisterItems.register();
  9. }

好了!你的盔甲现在应该存在于游戏中,虽然还没有纹理,但是已经可以通过 /give 来获得了。

现在分配纹理。

提供纹理

假定你已经:

  • 有了每一个盔甲物品的纹理(x_helmet.png、x_chestplate.png等)
  • 有了穿着的每个盔甲的纹理(x_layer_1.png和x_layer_2.png)

将其分配到每一个盔甲物品。

下列过程对于所有盔甲物品都是一样的,只需要修改我们使用的部分。这里以头盔为例。

resources/assets/tutorial/models/item/custom_material_helmet.json
{
	"parent": "item/generated",
	"textures": {
		"layer0": "tutorial:item/custom_material_helmet"
	}
}

重复上述过程,完成其他物品。

要给予穿着的盔甲的纹理,只需要将X_layer_1.png和X_layer_2.png(其中X是你在你的盔甲材料类中重载的getName方法的返回值)放到 'resources/assets/minecraft/textures/models/armor'。

做完上述步骤,你应该会有一套完整的盔甲!

zh_cn/tutorial/armor.txt · Last modified: 2023/08/20 10:19 by wjz_p