[Java] 让我们来制作Minecraft的Mod吧,版本是1.14.4【5. 添加防具】

这篇文章是一系列解释性文章中的一篇。

首要新闻:入门篇
前一篇文章:4. 添加工具
后一篇文章:6. 添加菜谱

添加防具

4. 在工具的补充中,我们添加了包括剑在内的工具类别。在拿到武器之后,我们当然希望有防具!这一次,让我们开始添加防具类别吧。

//...
public class ItemList {
    public static Item ExampleHelmet = new ArmorItem(ArmorMaterial.IRON, EquipmentSlotType.HEAD, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_helmet"));
    public static Item ExampleChestplate = new ArmorItem(ArmorMaterial.IRON, EquipmentSlotType.CHEST, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_chestplate"));
    public static Item ExampleLeggings = new ArmorItem(ArmorMaterial.IRON, EquipmentSlotType.LEGS, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_leggings"));
    public static Item ExampleBoots = new ArmorItem(ArmorMaterial.IRON, EquipmentSlotType.FEET, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_boots"));

    @SubscribeEvent
    public static void registerItems(RegistryEvent.Register<Item> event) {
        event.getRegistry().registerAll(
                ExampleHelmet,
                ExampleChestplate,
                ExampleLeggings,
                ExampleBoots
        );
    }
}

这次我们不另外创建一个类,而是直接在ItemList中声明ArmorItem类。参数依次为材料、槽位和物品属性。


我們將進行細節設定。

\src\main\resources\assets\example_mod
   ├ blockstates
   ├ lang
   │  └ en_us.json
   │  └ ja_jp.json
   ├ models
   │  ├ block
   │  └ item
   │     ├ example_helmet.json
   │     ├ example_chestplate.json
   │     ├ example_leggings.json
   │     └ example_boots.json
   └ textures
      ├ blocks
      └ items
         ├ example_helmet.png
         ├ example_chestplate.png
         ├ example_leggings.png
         └ example_boots.png
{
  "item.example_mod.example_helmet": "Example Helmet",
  "item.example_mod.example_chestplate": "Example Chestplate",
  "item.example_mod.example_leggings": "Example Leggings",
  "item.example_mod.example_boots": "Example Boots"
}
{
  "item.example_mod.example_helmet": "例のヘルメット",
  "item.example_mod.example_chestplate": "例のチェストプレート",
  "item.example_mod.example_leggings": "例のレギンス",
  "item.example_mod.example_boots": "例のブーツ"
}
{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_helmet"
  }
}
{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_chestplate"
  }
}
{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_leggings"
  }
}
{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_boots"
  }
}
キャプチャ2.PNG

就像在工具时代一样,我们来定义新的独特材料。

\src\main\java\jp\koteko\example_mod\
   ├ items
   │   └ ExampleArmorMaterial.java
   ├ lists
   ├ ExampleItemGroup.java
   └ ExampleMod.java
package jp.koteko.example_mod.items;

import jp.koteko.example_mod.ExampleMod;
import jp.koteko.example_mod.lists.ItemList;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.IArmorMaterial;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.LazyLoadBase;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.SoundEvents;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import java.util.function.Supplier;

public enum ExampleArmorMaterial implements IArmorMaterial {
    EXAMPLE("example", 66, new int[]{3, 6, 8, 3}, 30, SoundEvents.ITEM_ARMOR_EQUIP_DIAMOND, 3.0F, () -> {
        return Ingredient.fromItems(ItemList.ExampleIngot);
    });

    private static final int[] MAX_DAMAGE_ARRAY = new int[]{13, 15, 16, 11};
    private final String name;
    private final int maxDamageFactor;
    private final int[] damageReductionAmountArray;
    private final int enchantability;
    private final SoundEvent soundEvent;
    private final float toughness;
    private final LazyLoadBase<Ingredient> repairMaterial;

    private ExampleArmorMaterial(String nameIn, int maxDamageFactorIn, int[] damageReductionAmountsIn, int enchantabilityIn, SoundEvent equipSoundIn, float toughnessIn, Supplier<Ingredient> repairMaterialSupplier) {
        this.name = nameIn;
        this.maxDamageFactor = maxDamageFactorIn;
        this.damageReductionAmountArray = damageReductionAmountsIn;
        this.enchantability = enchantabilityIn;
        this.soundEvent = equipSoundIn;
        this.toughness = toughnessIn;
        this.repairMaterial = new LazyLoadBase<>(repairMaterialSupplier);
    }

    public int getDurability(EquipmentSlotType slotIn) {
        return MAX_DAMAGE_ARRAY[slotIn.getIndex()] * this.maxDamageFactor;
    }

    public int getDamageReductionAmount(EquipmentSlotType slotIn) {
        return this.damageReductionAmountArray[slotIn.getIndex()];
    }

    public int getEnchantability() {
        return this.enchantability;
    }

    public SoundEvent getSoundEvent() {
        return this.soundEvent;
    }

    public Ingredient getRepairMaterial() {
        return this.repairMaterial.getValue();
    }

    @OnlyIn(Dist.CLIENT)
    public String getName() {
        return ExampleMod.MOD_ID + ":" + this.name;
    }

    public float getToughness() {
        return this.toughness;
    }
}

名称:内部名称
最大损伤系数:基础耐久系数
MAX_DAMAGE_ARRAY:每个部位的耐久值
damageReductionAmountArray:每个部位的防御力
附魔能力:附魔效率
soundEvent:装备时的音效
坚固度:防具强度1
修复材料:修复素材

[各部位的耐久值] = [最大受损系数] × [最大损坏值数组[部位]]

我将性能提升到比钻石更强大的水平。我将防具的材料更换为这个。

//...
public class ItemList {
    public static Item ExampleHelmet = new ArmorItem(ExampleArmorMaterial.EXAMPLE, EquipmentSlotType.HEAD, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_helmet"));
    public static Item ExampleChestplate = new ArmorItem(ExampleArmorMaterial.EXAMPLE, EquipmentSlotType.CHEST, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_chestplate"));
    public static Item ExampleLeggings = new ArmorItem(ExampleArmorMaterial.EXAMPLE, EquipmentSlotType.LEGS, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_leggings"));
    public static Item ExampleBoots = new ArmorItem(ExampleArmorMaterial.EXAMPLE, EquipmentSlotType.FEET, new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_boots"));
//...
}
キャプチャ.PNG
\src\main\resources\assets\example_mod
   ├ blockstates
   ├ lang
   ├ models
   └ textures
      ├ blocks
      ├ items
      └ models
         └ armor
            ├ example_layer_1.png
            └ example_layer_2.png
キャプチャ.PNG

这次终于成功增加了防具!

请你的参考

《Minecraft 1.14.4 Forge Mod的开发 第7部分-新增防具》

下一篇文章

6. 添加食谱

与减轻伤害有关的数值。参考。
bannerAds