====== 添加盔甲 ====== ==== 介绍 ==== 盔甲是比一般的方块或者物品更复杂一点的实现,但是只要了解了,实现还是很简单的。如需添加盔甲,需要先实现 CustomArmorMaterial 类,然后注册物品。我们还需要看看如何为盔甲提供纹理。本文最后有一个解释如何添加击退的特殊章节,因为此方法只能通过Mixin访问(对于 1.16.3)。 本文档的一个例子可以在[[https://github.com/gdude2002/Gilded-Netherite|本模组GitHub仓库]]找到。 ==== 创建盔甲材料类 ==== 新的盔甲需要和一个新的名称(以及额外的一些内容,例如盔甲点和耐久度)一起设置,我们需要为我们的 CustomArmorMaterial 创建一个新的类。 本类实现 ,并以指定值为盔甲点(称为 PROTECTION_VALUES)开始。所有这些参数都会充分利用 @Override。 public class CustomArmorMaterial implements ArmorMaterial { private static final int[] BASE_DURABILITY = new int[] {13, 15, 16, 11}; private static final int[] PROTECTION_VALUES = new int[] {A, B, C, D}; // 其中A是头盔,B是护腿,C是胸甲,D是靴子。 // 例如,皮革使用{1, 2, 3, 1},钻石和下界合金使用{3, 6, 8, 3} } 接下来的变量如下定义(无需担心名称,下面你会看到如何实现): - : 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。** - : calls for the 'PROTECTION_VALUES' int we already wrote above. **调用以获取我们之前定义的'PROTECTION_VALUES'.** - : This will be how likely the armor can get high level or multiple enchantments in an enchantment book. **获取关于这件盔甲能够在一本附魔书中被附魔到多高等级、多少种类的附魔** - : The standard used by vanilla armor is ''.ITEM_ARMOR_EQUIP_X'', X being the type of armor. **在原版盔甲中,这个标准是''.ITEM_ARMOR_EQUIP_X'',其中X是你的盔甲类型** - : 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. **获取我们应该在铁砧中用什么来修复这件盔甲,材料可以是原版的,也可以是你自己的模组中的物品** - String : what the parent item of the armor is. In Diamond armor, it'd be "diamond". - : 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引入的新值 - : 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。加上这些参数之后,效果应该如下: public class CustomArmorMaterial implements class_1741 { private static final int[] BASE_DURABILITY = new int[] {13, 15, 16, 11}; private static final int[] PROTECTION_VALUES = new int[] {A, B, C, D}; @Override public int method_7696(class_1304 slot) { return BASE_DURABILITY[slot.method_5927()] * X; } @Override public int method_7697(class_1304 slot) { return PROTECTION_VALUES[slot.method_5927()]; } @Override public int method_7699() { return X; } @Override public class_3414 method_7698() { return class_3417.ITEM_ARMOR_EQUIP_X; } @Override public class_1856 method_7695() { return class_1856.method_8091(RegisterItems.X); } @Override public String method_7694() { // Must be all lowercase return "name"; } @Override public float method_7700() { return X.0F; } @Override public float method_24355() { return 0.XF; } } 注意你有盔甲材料类的基础了,所以在新的类中注册你的盔甲物品,即 RegisterItems。 ==== 创建盔甲物品 ==== 我们准备创建一个叫做 RegisterItems 的新的类以实现新的盔甲物件。这也会是注册工具的地方,如果你准备制作像锭这样的新物品(我们简单称为 Custom_Material)。 public class RegisterItems { public static final class_1741 CUSTOM_ARMOR_MATERIAL = new CustomArmorMaterial(); public static final class_1792 CUSTOM_MATERIAL = new CustomMaterialItem(new class_1792.class_1793()); // 如果创建了新的材料,则你需要注意这里。 public static final class_1792 CUSTOM_MATERIAL_HELMET = new class_1738(CUSTOM_ARMOR_MATERIAL, class_1304.field_6169, new class_1792.class_1793()); public static final class_1792 CUSTOM_MATERIAL_CHESTPLATE = new class_1738(CUSTOM_ARMOR_MATERIAL, class_1304.field_6174, new class_1792.class_1793()); public static final class_1792 CUSTOM_MATERIAL_LEGGINGS = new class_1738(CUSTOM_ARMOR_MATERIAL, class_1304.field_6172, new class_1792.class_1793()); public static final class_1792 CUSTOM_MATERIAL_BOOTS = new class_1738(CUSTOM_ARMOR_MATERIAL, class_1304.field_6166, new class_1792.class_1793()); } 现在物品创建好了,将其注册并给予适当的名称,你的第一个参数是名字空间,也就是你的模组 ID,第二个是你需要给予你的物品的名称。 我们会在最后一个ArmorItem的下面写这些。 public static void register() { class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_material"), CUSTOM_MATERIAL); class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_material_helmet"), CUSTOM_MATERIAL_HELMET); class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_material_chestplate"), CUSTOM_MATERIAL_CHESTPLATE); class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_material_leggings"), CUSTOM_MATERIAL_LEGGINGS); class_2378.method_10230(class_7923.field_41178, new class_2960("tutorial", "custom_material_boots"), CUSTOM_MATERIAL_BOOTS); } 你的盔甲物品已完成。现在我们在主类中调用 Registry。 public static final ItemGroup EXAMPLE_MOD_GROUP = FabricItemGroupBuilder.create( new Identifier("examplemod", "example_mod_group")) .icon(() -> new ItemStack(RegisterItems.CUSTOM_MATERIAL)) // 这里将你创建的新的材料的模型用作图标,但是你也可以随时使用你喜欢的 .build(); @Override public void onInitialize() { RegisterItems.register(); } 好了!你的盔甲现在应该存在于游戏中,虽然还没有纹理,但是已经可以通过 /give 来获得了。 现在分配纹理。 ==== 提供纹理 ==== 假定你已经: * 有了每一个盔甲物品的纹理(x_helmet.png、x_chestplate.png等) * 有了穿着的每个盔甲的纹理(x_layer_1.png和x_layer_2.png) 将其分配到每一个盔甲物品。 下列过程对于所有盔甲物品都是一样的,只需要修改我们使用的部分。这里以头盔为例。 { "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'。 做完上述步骤,你应该会有一套完整的盔甲!