User Tools

Site Tools


Sidebar

Setup

Basics

Items

Blocks and Block Entities

Fluids

Entities

World Generation

Miscellaneous

Events

Mixins

Dynamic Data Generation

Advanced

Tutorials for Minecraft 1.15

Tutorials for Minecraft 1.14

Documentation

tutorial:armor

This is an old revision of the document!


Adding Armor

Introduction

While Armor is a bit more complicated to add then a normal block/item, once you can understand it, it becomes simple to make. To add Armor, we'll first make a custom material class, then register the items. We'll also take a look at how to texture them.

Creating an Armor Material class

Since new armor needs to be set with a new name (as well as extra things like armor points and durability), we'll have to create a new class for custom ArmorMaterial.

This class will implement ArmorMaterial and will be an enum type. It'll need a lot of arguments, mainly the name, durability, etc., so for now we'll just leave it empty. Don't worry about any errors for now.

  1. public enum CustomArmorMaterial implements ArmorMaterial {
  2. CustomArmorMaterial() {
  3.  
  4. }
  5. }

Since there's a lot of arguments needed, here's a list explaining each one of them.

  1. A String name. This will be used as a sort of “armor tag” for later.
  2. A durability multiplier. This will be the number that will be used to determine the durability based on the base values.
  3. Armor values, or “Protection Amounts” in the vanilla code. This will be an int array.
  4. Enchantability. This will be how likely the armor can get high level or multiple enchantments in an enchantment book.
  5. A sound event. The standard used by vanilla armor is SoundEvents.ITEM_ARMOR_EQUIP_X, X being the type of armor.
  6. Toughness. This is a second protection value where the armor is more durable against high value attacks.
  7. A repair ingredient. This will be a Supplier<Ingredient> instance instead of an Item, which will go over in a bit.

If you're on 1.16+, they also have a new argument:

  1. Knockback Resistance. This is the scale of the amount of knockback resisted from attacks, explosions, and projectiles. 1.0 is 100%.

With those arguments, it should now look something like this:

  1. public enum CustomArmorMaterial implements ArmorMaterial {
  2. CustomArmorMaterial(String name, int durabilityMultiplier, int[] armorValueArr, int enchantability, SoundEvent soundEvent, float toughness, float knockbackResistance, Supplier<Ingredient> repairIngredient) {
  3.  
  4. }
  5. }

We'll also have to define those values and make it usable, so now it'll look like this:

  1. public enum CustomArmorMaterial implements ArmorMaterial {
  2. private final String name;
  3. private final int durabilityMultiplier;
  4. private final int[] armorValues;
  5. private final int enchantability;
  6. private final SoundEvent equipSound;
  7. private final float toughness;
  8. private final float knockbackResistance;
  9. private final Lazy<Ingredient> repairIngredient;
  10.  
  11. CustomArmorMaterial(String name, int durabilityMultiplier, int[] armorValueArr, int enchantability, SoundEvent soundEvent, float toughness, float knockbackResistance, Supplier<Ingredient> repairIngredient) {
  12. this.name = name;
  13. this.durabilityMultiplier = durabilityMultiplier;
  14. this.armorValues = armorValueArr;
  15. this.enchantability = enchantability;
  16. this.equipSound = soundEvent;
  17. this.toughness = toughness;
  18. this.knockbackResistance = knockbackResistance;
  19. this.repairIngredient = new Lazy(repairIngredient); // We'll need this to be a Lazy type for later.
  20. }
  21. }

ArmorMaterial also needs several other methods, so we'll add them real quick here.

We'll also have to add our base durability values, so for now we'll use the vanilla values [13, 15, 16, 11]

  1. public enum CustomArmorMaterial implements ArmorMaterial {
  2. private static final int[] baseDurability = {13, 15, 16, 11};
  3. private final String name;
  4. private final int durabilityMultiplier;
  5. private final int[] armorValues;
  6. private final int enchantability;
  7. private final SoundEvent equipSound;
  8. private final float toughness;
  9. private final float knockbackResistance;
  10. private final Lazy<Ingredient> repairIngredient;
  11.  
  12. CustomArmorMaterial(String name, int durabilityMultiplier, int[] armorValueArr, int enchantability, SoundEvent soundEvent, float toughness, float knockbackResistance, Supplier<Ingredient> repairIngredient) {
  13. this.name = name;
  14. this.durabilityMultiplier = durabilityMultiplier;
  15. this.armorValues = armorValueArr;
  16. this.enchantability = enchantability;
  17. this.equipSound = soundEvent;
  18. this.toughness = toughness;
  19. this.repairIngredient = new Lazy(repairIngredient);
  20. }
  21.  
  22. public int getDurability(EquipmentSlot equipmentSlot_1) {
  23. return BASE_DURABILITY[equipmentSlot_1.getEntitySlotId()] * this.durabilityMultiplier;
  24. }
  25.  
  26. public int getProtectionAmount(EquipmentSlot equipmentSlot_1) {
  27. return this.protectionAmounts[equipmentSlot_1.getEntitySlotId()];
  28. }
  29.  
  30. public int getEnchantability() {
  31. return this.enchantability;
  32. }
  33.  
  34. public SoundEvent getEquipSound() {
  35. return this.equipSound;
  36. }
  37.  
  38. public Ingredient getRepairIngredient() {
  39. // We needed to make it a Lazy type so we can actually get the Ingredient from the Supplier.
  40. return this.repairIngredientSupplier.get();
  41. }
  42.  
  43. @Environment(EnvType.CLIENT)
  44. public String getName() {
  45. return this.name;
  46. }
  47.  
  48. public float getToughness() {
  49. return this.toughness;
  50. }
  51.  
  52. public float getKnockbackResistance() {
  53. return this.knockbackResistance;
  54. }
  55. }

Now that you have the basics of the armor material class, you can now make your own material for armor. This can be done at the top of the code like so:

  1. public enum CustomArmorMaterial implements ArmorMaterial {
  2. WOOL("wool", 5, new int[]{1,3,2,1}, 15, SoundEvents.BLOCK_WOOL_PLACE, 0.0F, 0.0F, () -> {
  3. return Ingredient.ofItems(Items.WHITE_WOOL);
  4. });
  5. [...]
  6. }

Feel free to change any values.

Creating Armor Items

Back in the main class, you can now create it like so:

  1. public class ExampleMod implements ModInitializer {
  2. public static final Item WOOL_HELMET = new ArmorItem(CustomArmorMaterial.WOOL, EquipmentSlot.HEAD, (new Item.Settings().group(ItemGroup.COMBAT)));
  3. public static final Item WOOL_CHESTPLATE = new ArmorItem(CustomArmorMaterial.WOOL, EquipmentSlot.CHEST, (new Item.Settings().group(ItemGroup.COMBAT)));
  4. public static final Item WOOL_LEGGINGS = new ArmorItem(CustomArmorMaterial.WOOL, EquipmentSlot.LEGS, (new Item.Settings().group(ItemGroup.COMBAT)));
  5. public static final Item WOOL_BOOTS = new ArmorItem(CustomArmorMaterial.WOOL, EquipmentSlot.FEET, (new Item.Settings().group(ItemGroup.COMBAT)));
  6. }

Registering Armor Items

Register them the same way you'd register a normal item.

  1. [...]
  2. public void onInitialize() {
  3. Registry.register(Registry.ITEM,new Identifier("tutorial","wool_helmet"), WOOL_HELMET);
  4. Registry.register(Registry.ITEM,new Identifier("tutorial","wool_chestplate"), WOOL_CHESTPLATE);
  5. Registry.register(Registry.ITEM,new Identifier("tutorial","wool_leggings"), WOOL_LEGGINGS);
  6. Registry.register(Registry.ITEM,new Identifier("tutorial","wool_boots"), WOOL_BOOTS);
  7. }

Texturing

Since you already know how to make item models and textures, we won't go over them here. (They're done exactly the same as items.) Armor textures are done a little differently since Minecraft thinks it's a vanilla armor item. For this, we'll make a pack.mcmeta file so our resources can act like a resource pack.

src/main/resources/pack.mcmeta
{
    "pack":{
        "pack_format": 5,
        "description": "Tutorial Mod"
    }
}

Now you can finally place your textures here in src/main/resources/assets/minecraft/textures/models/armor/. Keep in mind that they're separated in 2 pictures. (Use vanilla textures for reference.)

If you followed everything, you should now be able to have a full armor set!

tutorial/armor.1596243547.txt.gz · Last modified: 2020/08/01 00:59 by ggtylerr