LoginSignup
4
1

More than 3 years have passed since last update.

Minecraft 1.14.4 Forge Modの作成 その6 【5種ツールの追加】

Last updated at Posted at 2019-10-31

はじめに

斧、鍬、ピッケル、スコップ、剣の5種類のツールを追加してみます。
2019-10-31_12.09.45.png

参考
Tools - Minecraft Modding Tutorial for MC 1.14/1.14.3

💻開発環境

ここでは環境は以下のようにします。

  • Windows 10
  • JDK 8u211
  • Minecraft 1.14.4
  • Minecraft Forge 1.14.4 (28.1.0)
  • IntelliJ IDEA 2019.2.3

アイテムの宣言

ItemList.java
package jp.yuyu.biwako_mod.lists;

import net.minecraft.item.Item;

public class ItemList {
    public static Item BiwakoIngot;
    public static Item BiwakoBlock;
    // 5種ツール
    public static Item BiwakoAxe;
    public static Item BiwakoHoe;
    public static Item BiwakoPickaxe;
    public static Item BiwakoShovel;
    public static Item BiwakoSword;
}

ツールマテリアルの作成

列挙型

ツールには、

  • 収穫レベル 木・金(0)、石(1)、鉄(2)、ダイヤ(3) 黒曜石を回収できるものにしたいなら3に設定
  • 耐久力
  • 採掘効率
  • 攻撃力
  • エンチャント能力 高レベルのエンチャントのつきやすさ

のパラメータを持つマテリアルの情報を与えることになります。
オリジナルのマテリアルは、今後いくつも増える予定があり、
こういった場合は管理しやすいようにEnumクラスを作成します。

Enumの解説は[Enum で振る舞う]が分かりやすかったです。

新規Javaクラスを作成する際に列挙型を指定し、ToolMaterialList.java作成します。
image.png
image.png

ToolMaterialList.java

とりあえず、ソースコード全体を記載します。

ToolMaterialList.java
package jp.yuyu.biwako_mod.lists;

import net.minecraft.item.IItemTier;
import net.minecraft.item.Item;
import net.minecraft.item.crafting.Ingredient;

public enum ToolMaterialList implements IItemTier {
    // Enumの宣言
    MATERIAL_BIWAKO(3, 1000, 15.0f, 3.0f,25, ItemList.BiwakoIngot);

    // メンバ変数
    private float attackDamage, efficiency;
    private int maxUses, harvestLevel, enchantability;
    private Item repairMaterial;

    // コンストラクタ
    private ToolMaterialList(int harvestLevelIn, int maxUsesIn, float efficiencyIn, float attackDamageIn, int enchantabilityIn, Item repairMaterialIn) {
        this.harvestLevel = harvestLevelIn;
        this.maxUses = maxUsesIn;
        this.efficiency = efficiencyIn;
        this.attackDamage = attackDamageIn;
        this.enchantability = enchantabilityIn;
        this.repairMaterial = repairMaterialIn;
    }

    // メソッド
    @Override
    public float getAttackDamage() {
        return attackDamage;
    }

    @Override
    public float getEfficiency() {
        return efficiency;
    }

    @Override
    public int getMaxUses() {
        return maxUses;
    }

    @Override
    public int getHarvestLevel() {
        return harvestLevel;
    }

    @Override
    public int getEnchantability() {
        return enchantability;
    }

    @Override
    public Ingredient getRepairMaterial() {
        return Ingredient.fromItems(this.repairMaterial);
    }
}

構造はEnumの宣言、メンバ変数、コンストラクタ、メソッドの4つ。
解説記事にも書かれているように、

オブジェクトの列挙になるので、メソッドを宣言したり、メンバ変数を宣言したりと、振る舞いを持たせることが出来る。
内部的には、列挙したオブジェクトは定数として扱われるので、列挙の数だけオブジェクトが予め生成されることになる。

ため、オリジナルのマテリアルを増やしたければ、このソースコードにEnumの宣言を新たに記述するだけでOKとなる。
今回は、MATERIAL_BIWAKOを追加してみた。

ツール追加用クラス

各ツールの追加用クラスを用意します。

image.png

それぞれ、クラスを継承しコンストラクタを用意します。
スコップは今までItemSpadeクラスを継承していましたが、このバージョンからは
名前がShovelItemとなっているようです。

ItemCustomAxe.java
package jp.yuyu.biwako_mod.items;

import net.minecraft.item.AxeItem;
import net.minecraft.item.IItemTier;

public class ItemCustomAxe extends AxeItem {
    public ItemCustomAxe(IItemTier tier, float attackDamageIn, float attackSpeedIn, Properties builder) {
        super(tier, attackDamageIn, attackSpeedIn, builder);
    }
}
ItemCustomHoe.java
package jp.yuyu.biwako_mod.items;

import net.minecraft.item.HoeItem;
import net.minecraft.item.IItemTier;

public class ItemCustomHoe extends HoeItem {
    public ItemCustomHoe(IItemTier tier, float attackSpeedIn, Properties builder) {
        super(tier, attackSpeedIn, builder);
    }
}
ItemCustomPickaxe.java
package jp.yuyu.biwako_mod.items;

import net.minecraft.item.PickaxeItem;
import net.minecraft.item.IItemTier;

public class ItemCustomPickaxe extends PickaxeItem {
    public ItemCustomPickaxe(IItemTier tier, float attackDamageIn, float attackSpeedIn, Properties builder) {
        super(tier, attackDamageIn, attackSpeedIn, builder);
    }
}
ItemCustomShovel.java
package jp.yuyu.biwako_mod.items;

import net.minecraft.item.IItemTier;
import net.minecraft.item.ShovelItem;

public class ItemCustomShovel extends ShovelItem {
    public ItemCustomShovel(IItemTier tier, float attackDamageIn, float attackSpeedIn, Properties builder) {
        super(tier, attackDamageIn, attackSpeedIn, builder);
    }
}
ItemCustomSword.java
package jp.yuyu.biwako_mod.items;

import net.minecraft.item.IItemTier;
import net.minecraft.item.SwordItem;

public class ItemCustomSword extends SwordItem {
    public ItemCustomSword(IItemTier tier, int attackDamageIn, float attackSpeedIn, Properties builder) {
        super(tier, attackDamageIn, attackSpeedIn, builder);
    }
}
  • IItemTier tier ツールマテリアル
  • Properties builder クリエイティブタブ

アイテムの登録

無機能アイテムを登録した時と同じように、アイテムの登録をしていきます。
今回は、ダイヤツールの攻撃力、攻撃速度と同じになるように設定してみました。

BiwakoMod.java
        public static void onItemsRegistry(final RegistryEvent.Register<Item> itemRegistryEvent) {
            LOGGER.info("HELLO from Register Item");
            itemRegistryEvent.getRegistry().registerAll(
                    ItemList.BiwakoAxe = new ItemCustomAxe(ToolMaterialList.MATERIAL_BIWAKO,5.0f,-3.0f,new Item.Properties().group(ItemGroup_Biwako))
                            .setRegistryName(new ResourceLocation(MOD_ID,"biwako_axe")),
                    ItemList.BiwakoHoe = new ItemCustomHoe(ToolMaterialList.MATERIAL_BIWAKO,0.0f,new Item.Properties().group(ItemGroup_Biwako))
                            .setRegistryName(new ResourceLocation(MOD_ID,"biwako_hoe")),
                    ItemList.BiwakoPickaxe = new ItemCustomPickaxe(ToolMaterialList.MATERIAL_BIWAKO,1,-2.8f,new Item.Properties().group(ItemGroup_Biwako))
                            .setRegistryName(new ResourceLocation(MOD_ID,"biwako_pickaxe")),
                    ItemList.BiwakoShovel = new ItemCustomShovel(ToolMaterialList.MATERIAL_BIWAKO,1.5f,-3.0f,new Item.Properties().group(ItemGroup_Biwako))
                            .setRegistryName(new ResourceLocation(MOD_ID,"biwako_shovel")),
                    ItemList.BiwakoSword = new ItemCustomSword(ToolMaterialList.MATERIAL_BIWAKO,3,-2.4f,new Item.Properties().group(ItemGroup_Biwako))
                            .setRegistryName(new ResourceLocation(MOD_ID,"biwako_sword"))
            );
        }

翻訳ファイル

en_us.json
{
    "item.biwako_mod.biwako_axe": "Biwako Axe",
    "item.biwako_mod.biwako_hoe": "Biwako Hoe",
    "item.biwako_mod.biwako_pickaxe": "Biwako Pickaxe",
    "item.biwako_mod.biwako_shovel": "Biwako Shovel",
    "item.biwako_mod.biwako_sword": "Biwako Sword"
}
ja_jp.json
{
    "item.biwako_mod.biwako_axe": "琵琶湖の斧",
    "item.biwako_mod.biwako_hoe": "琵琶湖のクワ",
    "item.biwako_mod.biwako_pickaxe": "琵琶湖のピッケル",
    "item.biwako_mod.biwako_shovel": "琵琶湖のスコップ",
    "item.biwako_mod.biwako_sword": "琵琶湖の剣"
}

モデルファイル

ツール用のモデルファイルの場合、parentにはitem/handheldを指定します。
それ以外は今まで通りです。

biwako_axe.json
{
    "parent": "item/handheld",
    "textures": {
        "layer0": "biwako_mod:items/biwako_axe"
    }
}
biwako_hoe.json
{
    "parent": "item/handheld",
    "textures": {
        "layer0": "biwako_mod:items/biwako_hoe"
    }
}
biwako_pickaxe.json
{
    "parent": "item/handheld",
    "textures": {
        "layer0": "biwako_mod:items/biwako_pickaxe"
    }
}
biwako_shovel.json
{
    "parent": "item/handheld",
    "textures": {
        "layer0": "biwako_mod:items/biwako_shovel"
    }
}
biwako_sword.json
{
    "parent": "item/handheld",
    "textures": {
        "layer0": "biwako_mod:items/biwako_sword"
    }
}

テクスチャファイル

テクスチャを作成し、配置します。

biwako_axe.png
biwako_hoe.png
biwako_pickaxe.png
biwako_shovel.png
biwako_sword.png

image.png

Minecraftの起動

2019-10-31_12.09.45.png


Githubにてプロジェクトを公開しています。

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1