Basic Modding - Custom Armor

Goal

I want to show you how to create a custom Armor set and how to add potion effects to it.

Difficulty

3/10 - Relatively Easy

Prerequisites

Forge Version

This Tutorial was created with Forge 11.14.1.1334 for Minecraft 1.8. If anything doesn't work with other versions, please contact me!


The Armor Material

As well as we had to add a ToolMaterial for custom tools, we need to add an ArmorMaterial for custom armor. To do this, we'll use the EnumHelper again. This is the code for adding an ArmorMaterial:

ModItems.class:
public static ArmorMaterial ARMOR = EnumHelper.addArmorMaterial("NAME", texture, durability, damageReduction[], enchantability);

This method takes the following arguments:

 

  • A unique name for the material. This should be the same as the variable's name, but you can choose another one.
  • A texture name for the armor. This name is used to get the right armor model texture. You need to write your modid, a colon and a chosen name. I'll come back to this value in a minute.
  • The durability of the armor item. This value is multiplicated with the following list to generate the different durabilities for each armor item:
    durability * 11 for the helmet
    durability * 16 for the chestplate
    durability * 15 for the leggings
    durability * 13 for the boots
    Common values for the durability are:
    • 5 for leather armor
    • 15 for chain and iron armor
    • 7 for gold armor
    • 33 for diamond armor
  • The damageReductionArray of the armor. This is a list of integer values that define the number of armor points for helmet, chestplate, leggings and boots that are added to the player's hotbar when the armor is worn. These values should be adjusted carefully because if you use higher values than the diamond armor has, the armor bar is overfilled. The additional armor points are not rendered but show their effect: If a player has got 25 armor points, he becomes invulnerable. A feature like this sould not be added by any mod!
    Common values for the damage reduction array are:
    • new int[]{1, 3, 2, 1} for leather armor (7 at all)
    • new int[]{2, 5, 4, 1} for chain armor (12 at all)
    • new int[]{2, 6, 5, 2} for iron armor (15 at all)
    • new int[]{2, 5, 3, 1} for gold armor (11 at all)
    • new int[]{3, 8, 6, 3} for diamond armor (20 at all - do not use more!)
  • The enchantability of the armor. This value determines the enchantments you get on average per level. The higher this value, the better the enchantments you get.
    Common values for the enchantability are:
    • 15 for leather armor
    • 12 for chain armor
    • 9 for iron armor
    • 25 for gold armor
    • 10 for diamond armor

For this tutorial I'll use the following values:

  • The texture name "tutorial"
  • A durability of 16 (a bit better than iron)
  • A damage reduction array like this: new int[] {3, 8, 6, 3} (like diamond)
  • A enchantability of 30 (better than everything else)
ModItems.class:
public static ArmorMaterial TUTORIALA = EnumHelper.addArmorMaterial("TUTORIALA", "tutorial:tutorial", 16, new int[] {3, 8, 6, 3}, 30);

(Note that I use the variable name TUTORIALA, because I already have a variable named TUTORIAL in the file)

The item class

If we want to add potion effects to our armor items, we need to create a new class for the armor. If you don't want to use potion effects, you can use ItemArmor instead.

The new class itself extends ItemArmor. You should override the constructor as follows:

ItemModArmor.class:
package com.bedrockminer.tutorial.items;

import [...];

public class ItemModArmor extends ItemArmor {

    public ItemModArmor(String unlocalizedName, ArmorMaterial material, int renderIndex, int armorType) {
        super(material, renderIndex, armorType);

        this.setUnlocalizedName(unlocalizedName);
    }
}

This class now is enough to create simple armor items, so let's do this.

Of course, we want to create a full set of armor, so we need four items.

ModItems.class:
GameRegistry.registerItem(tutorialHelmet = new ItemModArmor("tutorial_helmet", TUTORIALA, 1, 0), "tutorial_helmet");
GameRegistry.registerItem(tutorialChestplate = new ItemModArmor("tutorial_chestplate", TUTORIALA, 1, 1), "tutorial_chestplate");
GameRegistry.registerItem(tutorialLeggings = new ItemModArmor("tutorial_leggings", TUTORIALA, 2, 2), "tutorial_leggings");
GameRegistry.registerItem(tutorialBoots = new ItemModArmor("tutorial_boots", TUTORIALA, 1, 3), "tutorial_boots");       

Pay special attention to the numbers at the end of each constructor. The first number here defines the texture layer. This value should be 1 for helmet, chestplate and boots and 2 for leggings. I'll explain this in a minute.

The second value defines the armor type. 0 is a helmet, 1 a chestplate and so on.

Texturing the armor

For the items, you need to create a model as always, but we also need to create the special armor texture which is used as an overlay over the player's skin when the armor is worn.

For the Items I used these textures:

The armor texture mostly consists aut of two images. One for helmet, chestplate and boots and the other one for the leggings. This is done because the texture for the leggings overlaps with the ones for chestplate and boots.

The texture layer number we used for the construction of the items defines, which layer the armor texture uses.

The textures itself are created similar to a player skin. Here are the two textures I used:

Here you can see that the left image contains the textures for helmet, chestplate and boots, while the left image only contains the leggings.

These images need to be named in a special way:

texture_layer_x.png

texture is the name you defined in the armor material.

x is the layer number you defined in the armor constructor.

For my helmet the texture name is:

tutorial_layer_1.png.


The textures need to be placed in assets/modid/textures/models/armor.

Once we have placed them there, our armor is finished.

Armor Screenshot

Potion effects for armor

If you have created your own armor class, you are able to add potion effects to it.

Therefore, we need to override onArmorTick().

ItemModArmor.class:
@Override
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {

}   

In this method we can apply potion effects to the player if he wears the armor. The itemStack argument is the armor stack itself, so we can retrieve the item from it by calling itemStack.getItem() and compare it to our registered items.

ItemModArmor.class:
if (itemStack.getItem() == ModItems.tutorialHelmet) {

} 

Now we can apply the potion effect. One thing is important: If we continuously apply the effect every single tick, the duration of the effect stays the same. This could cause problems with some effects like poison or regeneration because they calculate when to heal or damage the player using the duration of the effect. If the duration is always the same this can cause some errors.

To fix this, I created a method which checks if the player has the effect already. If so, the effect is only applied if the duration is one tick or less. With this method, the effects always work correctly.

ItemModArmor.class:
@Override
public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {
    if (itemStack.getItem() == ModItems.tutorialHelmet) {
        effectPlayer(player, Potion.nightVision, 0);
    }
    if (itemStack.getItem() == ModItems.tutorialChestplate) {
        effectPlayer(player, Potion.digSpeed, 0);
    }
    if (itemStack.getItem() == ModItems.tutorialLeggings) {
        effectPlayer(player, Potion.moveSpeed, 0);
    }
    if (itemStack.getItem() == ModItems.tutorialBoots) {
        effectPlayer(player, Potion.jump, 1);
    }
} 

private void effectPlayer(EntityPlayer player, Potion potion, int amplifier) {
    //Always effect for 8 seconds, then refresh
    if (player.getActivePotionEffect(potion) == null || player.getActivePotionEffect(potion).getDuration() <= 1)
        player.addPotionEffect(new PotionEffect(potion.id, 159, amplifier, true, true));
}

Note that the amplifier 0 means that the potion effect has level 1.

Also note the two true at the end of the construtor of the potion effect. They make the effect's particles invisible. If you don't want this, just remove them.


It's also possible to apply an effect only if the player wears the complete armor set. Then, you need to check the items directly from the player's inventory.

ItemModArmor.class:
if (player.inventory.armorItemInSlot(3) != null && player.inventory.armorItemInSlot(3).getItem() == ModItems.tutorialHelmet
        && player.inventory.armorItemInSlot(2) != null && player.inventory.armorItemInSlot(2).getItem() == ModItems.tutorialChestplate
        && player.inventory.armorItemInSlot(1) != null && player.inventory.armorItemInSlot(1).getItem() == ModItems.tutorialLeggings
        && player.inventory.armorItemInSlot(0) != null && player.inventory.armorItemInSlot(0).getItem() == ModItems.tutorialBoots) {
        this.effectPlayer(player, Potion.regeneration, 1);
}

Note that you need to change the armor piece ids here for some reason: 3 is helmet, 2 is chestplate and so on.

Looking at the length of the if statement it's probably useful to create a small method to do the check here.


Now you have an armor set that gives you night vision, haste, swiftness, jump boost and regeneration when worn completely.


You can download the code used in this tutorial as a .zip file from here.


Recommended tutorials to continue with

Take a look at the tutorial list and decide what you want to do next.


Comments and Questions:

If you want to report modding problems, please make sure to include the code in a pastebin link or something else! Don't just write "It doesn't work", otherwise your post will be deleted. For more complicated problems, please use the troubleshooter form.