[Java] [Java]Let’s create a Minecraft mod 1.14.4 [4. Add tools]

6 minute read

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

First article: Introduction Previous article: 3. Add Creative Tab Next article: 5. Add armor

Add tools

1. Add item added a simple item that has no function. Now let’s add a tool with a role such as a sword or ice ax.

\src\main\java\jp\koteko\example_mod\
   ├ items
   │ ├ ItemExampleAxe.java
   │ ├ ItemExampleHoe.java
   │ ├ ItemExamplePickaxe.java
   │ ├ ItemExampleShovel.java
   │ └ ItemExampleSword.java
   ├ lists
   │ └ ItemList.java
   ├ ExampleItemGroup.java
   └ ExampleMod.java

Create an items folder and create a class for each item. At this point, it is almost the same as the inheritance class, so there is almost no need to create a new class, but I decided to make it a different class, assuming that it will be customized later.

ItemExampleAxe.java


package jp.koteko.example_mod.items;

import net.minecraft.item.AxeItem;
import net.minecraft.item.ItemTier;

public class ItemExampleAxe extends Axe Item {
    public ItemExampleAxe(Properties properties) {
        super(ItemTier.IRON, 5, -3.0F, properties);
    }
}

ItemExampleHoe.java


package jp.koteko.example_mod.items;

import net.minecraft.item.HoeItem;
import net.minecraft.item.ItemTier;

public class ItemExampleHoe extends HoeItem {
    public ItemExampleHoe(Properties properties) {
        super(ItemTier.IRON, 0, properties);
    }
}

ItemExamplePickaxe.java


package jp.koteko.example_mod.items;

import net.minecraft.item.ItemTier;
import net.minecraft.item.PickaxeItem;

public class ItemExamplePickaxe extends PickaxeItem {
    public ItemExamplePickaxe(Properties properties) {
        super(ItemTier.IRON, 1, -2.8F, properties);
    }
}

ItemExampleShovel.java


package jp.koteko.example_mod.items;

import net.minecraft.item.ItemTier;
import net.minecraft.item.ShovelItem;

public class ItemExampleShovel extends ShovelItem {
    public ItemExampleShovel(Properties properties) {
        super(ItemTier.IRON, 1.5F, -3.0F, properties);
    }
}

ItemExampleSword.java


package jp.koteko.example_mod.items;

import net.minecraft.item.ItemTier;
import net.minecraft.item.SwordItem;

public class ItemExampleSword extends SwordItem {
    public ItemExampleSword(Properties properties) {
        super(ItemTier.IRON, 3, -2.4F, properties);
    }
}

The arguments passed to the inheritance source constructor are as follows. I passed only Properties at the time of instantiation, and I decided to decide here afterwards. See also the “Development” section below.

IItemTier tier: Item tier (so-called rareness/grade). int attackDamageIn or float attackDamageIn: Attack power. For some reason, int and float are different depending on the tool. Not in Hoe. float attackSpeedIn: Attack speed. Properties builder: Item properties. The one I used to set up the creative tab so far.


I will add it to the item list and register it.

ItemList.java


package jp.koteko.example_mod.lists;

import jp.koteko.example_mod.ExampleItemGroup;
import jp.koteko.example_mod.ExampleMod;
import jp.koteko.example_mod.items.*;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ItemList {
    public static Item ExampleIngot = new Item(new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_ingot"));
    public static Item ExampleAxe = new ItemExampleAxe(new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_axe"));
    public static Item ExampleHoe = new ItemExampleHoe(new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_hoe"));
    public static Item ExamplePickaxe = new ItemExamplePickaxe(new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_pickaxe"));
    public static Item ExampleShovel = new ItemExampleShovel(new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_shovel"));
    public static Item ExampleSword = new ItemExampleSword(new Item.Properties().group(ExampleItemGroup.DEFAULT))
            .setRegistryName(new ResourceLocation(ExampleMod.MOD_ID, "example_sword"));

    @SubscribeEvent
    public static void registerItems(RegistryEvent.Register<Item> event) {
        event.getRegistry().registerAll(
                ExampleIngot,
                ExampleAxe,
                ExampleHoe,
                ExamplePickaxe,
                ExampleShovel,
                ExampleSword
        );
    }
}


Next, we will make settings.

\src\main\resources\assets\example_mod
   ├ blockstates
   ├ lang
   │ └ en_us.json
   │ └ ja_jp.json
   ├ models
   │ ├ block
   │ └ item
   │ ├ example_axe.json
   │ ├ example_hoe.json
   │ ├ example_pickaxe.json│ ├ example_shovel.json
   │ └ example_sword.json
   └ textures
      ├ blocks
      └ items
         ├ example_axe.png
         ├ example_hoe.png
         ├ example_pickaxe.png
         ├ example_shovel.png
         └ example_sword.png

en_us.json


{
  "item.example_mod.example_axe": "Example Axe",
  "item.example_mod.example_hoe": "Example Hoe",
  "item.example_mod.example_pickaxe": "Example Pickaxe",
  "item.example_mod.example_shovel": "Example Shovel",
  "item.example_mod.example_sword": "Example Sword"
}

ja_jp.json


{
  "item.example_mod.example_axe": "example ax",
  "item.example_mod.example_hoe": "Example hoe",
  "item.example_mod.example_pickaxe": "Example ice ax",
  "item.example_mod.example_shovel": "Example scoop",
  "item.example_mod.example_sword": "example sword",
}

~~ (It’s kind of like a weapon that’s kind of scared…)~~

example_axe.json


{
  "parent": "item/handheld",
  "textures": {
    "layer0": "example_mod:items/example_axe"
  }
}

example_hoe.json


{
  "parent": "item/handheld",
  "textures": {
    "layer0": "example_mod:items/example_hoe"
  }
}

example_pickaxe.json


{
  "parent": "item/handheld",
  "textures": {
    "layer0": "example_mod:items/example_pickaxe"
  }
}

example_shovel.json


{
  "parent": "item/handheld",
  "textures": {
    "layer0": "example_mod:items/example_shovel"
  }
}

example_sword.json


{
  "parent": "item/handheld",
  "textures": {
    "layer0": "example_mod:items/example_sword"
  }
}

By specifying item/handheld for parent, it becomes an item of the type you have in your hand.

After placing the textures, start the game. Capture.PNG Capture.PNG (Durability has not decreased because I took it creatively)

** Tools have been added! **

Development

Q. Why are the attack power and attack speed settings different from the values? Q. Why is the attack speed negative? **A. It seems that the attack power can be found by [the value of ``IItemTier]+[the value of each tool]+1 and the attack speed is 4.0+[the value of each tool]. ** Let's take a look at SwordItem.java`, one of the inherited classes of the tool class added this time.

SwordItem.java


//...
public class SwordItem extends TieredItem {
   private final float attackDamage;
   private final float attackSpeed;

   public SwordItem(IItemTier tier, int attackDamageIn, float attackSpeedIn, Item.Properties builder) {
      super(tier, builder);
      this.attackSpeed = attackSpeedIn;
      this.attackDamage = (float)attackDamageIn + tier.getAttackDamage();
   }
   //...
}

You can see that it is calculated as (float)attackDamageIn + tier.getAttackDamage(). (Although I couldn’t find the code that shows the reason why the display on the GUI is +1, the vanilla item also matched the value calculated by such an expression. It is unclear whether or not it is +1 even if calculated internally.) (Similarly, for the attack speed, the part calculated from the offset of 4.0 has not been found.)

Next, let’s look at Item Tier.

ItemTier.java


//...
public enum ItemTier implements IItemTier {
   WOOD(0, 59, 2.0F, 0.0F, 15, () -> {
      return Ingredient.fromTag(ItemTags.PLANKS);
   }),
   STONE(1, 131, 4.0F, 1.0F, 5, () -> {
      return Ingredient.fromItems(Blocks.COBBLESTONE);
   }),
   IRON(2, 250, 6.0F, 2.0F, 14, () -> {
      return Ingredient.fromItems(Items.IRON_INGOT);
   }),
   DIAMOND(3, 1561, 8.0F, 3.0F, 10, () -> {
      return Ingredient.fromItems(Items.DIAMOND);
   }),
   GOLD(0, 32, 12.0F, 0.0F, 22, () -> {
      return Ingredient.fromItems(Items.GOLD_INGOT);
   });
   //...
   private ItemTier(int harvestLevelIn, int maxUsesIn, float efficiencyIn, float attackDamageIn, int enchantabilityIn, Supplier<Ingredient> repairMaterialIn) {
      this.harvestLevel = harvestLevelIn;
      this.maxUses = maxUsesIn;
      this.efficiency = efficiencyIn;
      this.attackDamage = attackDamageIn;
      this.enchantability = enchantabilityIn;
      this.repairMaterial = new LazyLoadBase<>(repairMaterialIn);
   }
   //...
}

In this way, the value is determined for each Tier.

For example, the Example Sword added this time is ItemTier.IRON, so Offensive power [Value of IItemTier] + [Value of each tool] +1 = 2 + 3 + 1 = 6 Attack speed 4.0+[value for each tool] = 4.0-2.4 = 1.6 Will be. ___ Q. I want to use a proper material, not stone or iron. Q. Where is the durability setting? **A. Create a new ItemTier. ** Define a new Enum for the ItemTier mentioned above.

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

ExampleItemTier.java


package jp.koteko.example_mod.items;

import jp.koteko.example_mod.lists.ItemList;
import net.minecraft.item.IItemTier;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.LazyLoadBase;

import java.util.function.Supplier;

public enum ExampleItemTier implements IItemTier {
    EXAMPLE(4, 3122, 10.0F, 4.0F, 25, () -> {
        return Ingredient.fromItems(ItemList.ExampleIngot);
    });

    private final int harvestLevel;
    private final int maxUses;
    private final float efficiency;
    private final float attackDamage;
    private final int enchantability;
    private final LazyLoadBase<Ingredient> repairMaterial;

    private ExampleItemTier(int harvestLevelIn, int maxUsesIn, float efficiencyIn, float attackDamageIn, int enchantabilityIn, Supplier<Ingredient> repairMaterialIn) {this.harvestLevel = harvestLevelIn;
        this.maxUses = maxUsesIn;
        this.efficiency = efficiencyIn;
        this.attackDamage = attackDamageIn;
        this.enchantability = enchantabilityIn;
        this.repairMaterial = new LazyLoadBase<>(repairMaterialIn);
    }

    public int getMaxUses() {
        return this.maxUses;
    }

    public float getEfficiency() {
        return this.efficiency;
    }

    public float getAttackDamage() {
        return this.attackDamage;
    }

    public int getHarvestLevel() {
        return this.harvestLevel;
    }

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

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

The arguments are, in order, the collection level, durability, efficiency, attack power, enchantment efficiency, and repair material (as a list). I made it a better value than the diamond’s DIAMOND (3, 1561, 8.0F, 3.0F, 10, …)`.

Change the Tier of the tool class.

ItemExampleSword.java


package jp.koteko.example_mod.items;

import net.minecraft.item.SwordItem;

public class ItemExampleSword extends SwordItem {
    public ItemExampleSword(Properties properties) {
        super(ExampleItemTier.EXAMPLE, 3, -2.4F, properties);
    }
}

Capture.PNG The attack power and durability are increasing properly. (The debug value can be displayed with F3 and the durability value can be displayed with F3+H.) ___ Q. I want to make a squid that looks like lightning and fire! **A. I still don’t know because it’s difficult so let’s study together. ** (no commentary planned)

Reference

Minecraft 1.14.4 Forge Mod Part 6 [Add 5 kinds of tools]

Next article

5. Add armor