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

(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

In 1. Add Item, I added a simple item that has no function. Now let's add tools that have roles such as swords and ice axes.

\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 a ʻitems` folder and create a class for each item. At this point, it is almost the same as the inherited class, so there is almost no need to create a new class, but I decided to make it a separate class assuming that it will be customized in various ways later.

ItemExampleAxe.java


package jp.koteko.example_mod.items;

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

public class ItemExampleAxe extends AxeItem {
    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 in order. Only Properties is passed at the time of instantiation, and the rest is decided here. See also the "Development" section below.

ʻI Item Tier tier: Item tier (so-called rarity / grade). ʻInt attackDamageIn or float attackDamageIn: Attack power. For some reason, int and float are different depending on the tool. It does not exist in Hoe. float attackSpeedIn: Attack speed. Properties builder: Item properties. The one I've used to set up creative tabs.


We 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 has become a name like a weapon with a lot of excitement ...) ~~

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 will be the type of item you have in your hand.

After placing the texture, start the game. キャプチャ.PNG キャプチャ.PNG (Since it was taken creatively, the durability has not decreased)

** Tools have been added! ** **

development

Q. Is the attack power and attack speed settings different from the values? Q. Why is the attack speed negative? ** A. Attack power seems to be calculated by [ʻIItemTiervalue] + [value in each tool] +1, and attack speed is 4.0 + [value in each tool]. ** ** Let's take a look at one of the inheritance source classes of the tool class added this time,SwordItem.java`.

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 (). (Note that I couldn't find any code to understand why the display on the GUI is actually +1. However, the vanilla item also matched the value calculated by such an expression. It is +1 on the display. It is unknown whether it is or is it +1 in the internal calculation.) (Similarly, the part calculated from the offset of 4.0 has not been found for the attack speed.)

Next, let's take a 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 fixed for each tier.

For example, in the ʻExample Sword added this time, it is ʻItem Tier.IRON, so Offensive power [IItemTier value] + [Value for each tool] +1 = 2 + 3 + 1 = 6 Attack speed 4.0+ [value for each tool] = 4.0 --2.4 = 1.6 It will be.


Q. I want to make it a unique material, not stone or iron. Q. Where is the durability value set? ** A. Let's create a new ʻItem Tier. ** ** Define a new Enum for the ʻItem Tier 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, collection level, durability, efficiency, attack power, enchantment efficiency, and (list of) repair materials. I tried to make it better than the diamond 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);
    }
}

キャプチャ.PNG Attack power and durability value are increasing properly. (F3 can be used for debugging and F3 + H can be used to display durability, etc.)


Q. I want to make a sword that I think of when thunder and flames come out! ** A. I'm still difficult to understand, so let's study together. ** (No commentary planned)

reference

Minecraft 1.14.4 Forge Mod Creation Part 6 [Addition of 5 Tools]

Next article

5. Add armor

Recommended Posts

[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]
[Java] Let's create a mod for Minecraft 1.16.1 [Add block]
[Java] Let's create a mod for Minecraft 1.14.4 [3. Add creative tab]
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[Java] Let's create a mod for Minecraft 1.16.1 [Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [9. Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [8. Add and generate ore]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
Let's create a Java development environment (updating)
Let's create a timed process with Java Timer! !!
Let's create a super-simple web framework in Java
[Java] Create a filter
[Java basics] Let's make a triangle with a for statement
Create a java method [Memo] [java11]
[Java] Create a temporary file
How to create a lightweight container image for Java apps
[Java twig] Create a parser combinator for recursive descent parsing 2
Create a MOB using the Minecraft Java Mythicmobs plugin | Preparation 1
Let's create a TODO application in Java 4 Implementation of posting function
How to sign a Minecraft MOD
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 8 Implementation of editing function
Create a Java project using Eclipse
[Java] How to create a folder
Spring Framework tools for Java developer
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 5 Switch the display of TODO
Create a fluentd server for testing
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
[Java twig] Create a parser combinator for recursive descent parsing (also memoize)
Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
Create a java web application development environment with docker for mac part2
[Java] Create and apply a slide master
How to create a Maven repository for 2020
Create a TODO app in Java 7 Create Header
[Rails] Let's create a super simple Rails API
[Java] Let's make a DB access library!
Let's make a calculator application with Java ~ Create a display area in the window
Java (add2)
Java (add)
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
How to create a database for H2 Database anywhere
A story about Java 11 support for Web services
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
Let's create a REST API using WildFly Swarm.
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Create a Lambda Container Image based on Java 15
[Java] Create something like a product search API
Let's create a RESTful email sending service + client
Try to create a bulletin board in Java