(This article is one of a series of commentary articles)
First article: Introduction Previous article: 3. Add Creative Tab Next article: 5. Add armor
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. (Since it was taken creatively, the durability has not decreased)
** Tools have been added! ** **
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);
}
}
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)
Minecraft 1.14.4 Forge Mod Creation Part 6 [Addition of 5 Tools]