[Java] [Java]Let’s create a Minecraft mod 1.14.4 [5. Add armor]

3 minute read

(This article is one of a series of commentary articles)

First article: Introduction Previous Article: 4. Add Tool Next article: 6. Add recipe

Add armor

4. Add tools added tools including sword. After you get a weapon, what you want next is armor! This time, let’s add armor.

ItemList.java


//...
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
        );
    }
}

This time, instead of creating a separate class, declare the ArmorItem class directly in the ItemList. The arguments are the material, slot location, and item property in order. ___ I will make detailed settings.

\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

en_us.json


{
  "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"
}

ja_jp.json


{
  "item.example_mod.example_helmet": "example helmet",
  "item.example_mod.example_chestplate": "Example chest plate",
  "item.example_mod.example_leggings": "Example leggings",
  "item.example_mod.example_boots": "Example boots"
}

example_helmet.json


{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_helmet"
  }
}

example_chestplate.json


{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_chestplate"
  }
}

example_leggings.json


{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_leggings"
  }
}

example_boots.json


{
  "parent": "item/generated",
  "textures": {
    "layer0": "example_mod:items/example_boots"
  }
}

Try to launch the game. Capture.PNG Capture2.PNG **I can add armor…not! ! ** When I wore it, it looked like iron armor (in addition to that, it’s supposed to look like iron armor in various ways such as status and repair materials). This is because we are passing ArmorMaterial.IRON as the material of the ArmorItem. ___ Let’s define our own new material, as we did with the tools.

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

ExampleArmorMaterial.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;
    }
}

name : 内部名 maxDamageFactor : 基礎耐久係数 MAX_DAMAGE_ARRAY : 部位ごとの耐久値 damageReductionAmountArray : 部位ごとの防御力 enchantability : エンチャント効率 soundEvent : 装着時の音 toughness : 防具強度1 repairMaterial : 修理素材

[各部位の耐久値] = [maxDamageFactor]×[MAX_DAMAGE_ARRAY[部位]]

ダイヤモンドより強い性能にしてみました。防具のマテリアルをこれに替えます。

ItemList.java


//...
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

\assets\example_mod\textures\models\armorフォルダを作り、[マテリアルの内部名]_layer_1.png[マテリアルの内部名]_layer_2.pngを配置します。バニラの防具のテクスチャなどを参考にしましょう。 キャプチャ.PNG ダイヤっぽい色合いでややわかりづらいですが、きちんとテクスチャが反映されました。また、耐久値が66*13=858になっていることが確認できます。

今度こそ防具の追加ができました!

参考

Minecraft 1.14.4 Forge Modの作成 その7 【防具の追加】

次の記事

6. レシピの追加

  1. ダメージ軽減にかかわる値。参考