User Tools

Site Tools


tutorial:armor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorial:armor [2019/07/19 05:33]
ggtylerr created (Feel free to edit if I missed anything, this was all done by looking at the vanilla code so it's pretty likely I missed something :P)
tutorial:armor [2020/10/09 21:20] (current)
haykam Improve some code block formatting
Line 3: Line 3:
 ==== Introduction ==== ==== 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.+While armor is a bit more complicated to implement than a normal block or item, once you understand it, it becomes simple to implement. To add armor, we'll first make a CustomArmorMaterial ​class, then register the items. We'll also take a look at how to texture them. There'​s a special chapter at the end of this document that explains how to add knockback to the armor, since the method is only accessible through a mixin (as of 1.16.3). 
 + 
 +An example for this document can be found in [[https://​github.com/​CumulusMC/​Gilded-Netherite|this mod GitHub repository]]
  
 ==== Creating an Armor Material class ==== ==== 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+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 our CustomArmorMaterial
  
-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.+This class will implement ArmorMaterialand it'​ll ​start by assigning values to armor points (called PROTECTION_VALUES). All its following arguments will make use of @Override.
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
-public ​enum CustomArmorMaterial implements ArmorMaterial { +public ​class CustomArmorMaterial implements ArmorMaterial { 
-    ​CustomArmorMaterial() ​+ private static final int[] BASE_DURABILITY = new int[] {13, 15, 16, 11}; 
-         + private static final int[] PROTECTION_VALUES = new int[] {A, B, C, D};  
-    }+ 
 + // In which A is helmet, B chestplate, C leggings and D boots.  
 + // For reference, Leather uses {1, 2, 3, 1}, and Diamond/​Netherite {3, 6, 8, 3}
 } }
 </​code>​ </​code>​
  
-Since there'​s a lot of arguments ​neededhere's a list explaining each one of them.+The next arguments ​are defined as follows (don't worry about the namesyou'll see how we implement it below them):
  
-  - A String name. This will be used as a sort of "armor tag" for later. +  - getDurability:​ how many hits can armor take before breakingUses the int we wrote on '​BASE_DURABILITY' ​to calculate. Leather uses 5, Diamond 33, Netherite 37
-  - A durability multiplier. This will be the number that will be used to determine the durability based on the base values+  - getPretectionAmount:​ calls for the '​PROTECTION_VALUES' ​int we already wrote above
-  - Armor values, or "​Protection Amounts"​ in the vanilla code. This will be an int array+  - getEnchantability: ​This will be how likely the armor can get high level or multiple enchantments in an enchantment book. 
-  - Enchantability. ​This will be how likely the armor can get high level or multiple enchantments in an enchantment book. +  - SoundEvent getEquipSound: ​The standard used by vanilla armor is ''​SoundEvents.ITEM_ARMOR_EQUIP_X'',​ X being the type of armor. 
-  - A sound event. ​The standard used by vanilla armor is ''​SoundEvents.ITEM.EQUIP.ARMOR.X'',​ X being the type of armor. +  - Ingredient getRepairIngridient:​ what item are we gonna be using to repair the armor on an anvilIt can be either a vanilla item or one of your own. 
-  - Toughness. This is a second protection value where the armor is more durable against high value attacks. +  - String getName: what the parent item of the armor is. In Diamond armor, it'd be "​diamond"​. 
-  - A repair ingredientThis will be a ''​Supplier<​Ingredient>''​ instance instead of an ''​Item'',​ which will go over in a bit.+  - getToughness: ​This is a second protection value where the armor is more durable against high value attacks. ​Value goes as 'X.0F'
  
-With those arguments, it should now look something like this:+And the new value introduced on 1.16 
 +  - getKnockbackResistance:​ leave this value at 0. If you want to implement itwrite '​0.XF'​ (in which X is how much knockback protection you want), and I'll teach you how to make it work later on.
  
-<code java [enable_line_numbers="​true"​]>​ 
-public enum CustomArmorMaterial implements ArmorMaterial { 
-    CustomArmorMaterial(String name, int durabilityMultiplier,​ int[] armorValueArr,​ int enchantability,​ SoundEvent soundEvent, float toughness, Supplier<​Ingredient>​ repairIngredient) { 
-        ​ 
-    } 
-} 
-</​code>​ 
  
-We'​ll ​also have to define ​those values and make it usableso now it'​ll ​look like this:+I'​ll ​leave all variables written as X or A, B, C, D. With those arguments, it should now look something ​like this:
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
-public ​enum CustomArmorMaterial implements ArmorMaterial { +public ​class CustomArmorMaterial implements ArmorMaterial { 
-    ​private final String name; + private ​static ​final int[] BASE_DURABILITY = new int[] {13, 15, 16, 11}
-    ​private final int durabilityMultiplier;​ + private ​static ​final int[] PROTECTION_VALUES = new int[] {ABCD};
-    private final int[] armorValues+
-    private final int enchantability;​ +
-    private final SoundEvent equipSound;​ +
-    private final float toughness;​ +
-    private final Lazy<​Ingredient>​ repairIngredient;​ +
-     +
-    CustomArmorMaterial(String name, int durabilityMultiplier, ​int[] armorValueArrint enchantabilitySoundEvent soundEventfloat toughness, Supplier<​Ingredient>​ repairIngredient) { +
-        this.name = name; +
-        this.durabilityMultiplier = durabilityMultiplier;​ +
-        this.armorValues = armorValueArr;​ +
-        this.enchantability = enchantability;​ +
-        this.equipSound = soundEvent;​ +
-        this.toughness = toughness;​ +
-        this.repairIngredient = new Lazy(repairIngredient);​ // We'll need this to be a Lazy type for later. +
-    } +
-+
-</​code>​+
  
-''​ArmorMaterial''​ also needs several other methods, so we'll add them real quick here.+ @Override 
 + public int getDurability(EquipmentSlot slot) { 
 + return BASE_DURABILITY[slot.getEntitySlotId()] * X; 
 + }
  
-We'll also have to add our base durability values, so for now we'll use the vanilla values ''​[13, 15, 16, 11]''​+ @Override 
 + public int getProtectionAmount(EquipmentSlot slot) { 
 + return PROTECTION_VALUES[slot.getEntitySlotId()]
 + }
  
-<code java [enable_line_numbers="​true"​]>​ + @Override 
-public ​enum CustomArmorMaterial implements ArmorMaterial { + public int getEnchantability() { 
-    private static final int[] baseDurability = {13, 15, 16, 11}; + return ​X
-    private final String name; + }
-    private final int durabilityMultiplier;​ +
-    private final int[] armorValues;​ +
-    private final int enchantability;​ +
-    private final SoundEvent equipSound;​ +
-    private final float toughness;​ +
-    private final Lazy<​Ingredient>​ repairIngredient;​ +
-     +
-    CustomArmorMaterial(String name, int durabilityMultiplier,​ int[] armorValueArr,​ int enchantability,​ SoundEvent soundEvent, float toughness, Supplier<​Ingredient>​ repairIngredient) { +
-        ​this.name = name; +
-        this.durabilityMultiplier = durabilityMultiplier;​ +
-        this.armorValues = armorValueArr;​ +
-        this.enchantability = enchantability;​ +
-        this.equipSound = soundEvent;​ +
-        this.toughness = toughness;​ +
-        this.repairIngredient = new Lazy(repairIngredient);​ +
-    } +
-     +
-    public int getDurability(EquipmentSlot equipmentSlot_1) { +
-        ​return ​BASE_DURABILITY[equipmentSlot_1.getEntitySlotId()] * this.durabilityMultiplier+
-    }+
  
-    ​public ​int getProtectionAmount(EquipmentSlot equipmentSlot_1) { + @Override 
-        return ​this.protectionAmounts[equipmentSlot_1.getEntitySlotId()]+ public ​SoundEvent getEquipSound() { 
-    }+ return ​SoundEvents.ITEM_ARMOR_EQUIP_X
 + }
  
-    ​public ​int getEnchantability() { + @Override 
-        return ​this.enchantability+ public ​Ingredient getRepairIngredient() { 
-    }+ return ​Ingredient.ofItems(RegisterItems.X)
 + }
  
-    ​public ​SoundEvent getEquipSound() { + @Override 
-        return ​this.equipSound+ public ​String getName() { 
-    }+ return ​"​name"​
 + }
  
-    ​public ​Ingredient getRepairIngredient() { + @Override 
-        // We needed to make it a Lazy type so we can actually get the Ingredient from the Supplier. + public ​float getToughness() { 
-        ​return ​this.repairIngredientSupplier.get()+ return ​X.0F
-    }+ }
  
-    ​@Environment(EnvType.CLIENT) + @Override 
-    public String getName() { + public float getKnockbackResistance() { 
-        return this.name;​ + return ​0.XF
-    } + }
- +
-    public float getToughness() { +
-        return ​this.toughness+
-    }+
 } }
 </​code>​ </​code>​
  
-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:+ 
 +Now that you have the basics of the armor material class, ​let's register your armor items in a new class we'll simply call RegisterItems. 
 + 
 +==== Creating Armor Items ==== 
 + 
 +We're gonna make a new class called RegisterItems to implement ​your new armor pieces. This will also be the place to, for example, register tools, if you're making a new item like an ingot (We'll refer to this as a "​Custom_Material"​). This setup will also put the items on a new Creative tab, but you're free to delete that part.  
 + 
 +The syntax of groups is //​.group(YourModName.YOUR_MOD_NAME_BUT_IN_CAPS_GROUP)//​. I'll be referring to it as ExampleMod:
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
-public ​enum CustomArmorMaterial implements ArmorMaterial ​+public ​class RegisterItems ​
-    WOOL("​wool",​ 5, new int[]{1,3,2,1}15, SoundEvents.BLOCK_WOOL_PLACE0.0F, () -> { + 
-        return Ingredient.ofItems(Items.WHITE_WOOL); +   ​public static final ArmorMaterial customArmorMaterial = new CustomArmorMaterial(); 
-    ​}); +   ​public static final Item CUSTOM_MATERIAL = new CustomMaterialItem(new Item.Settings().group(ExampleMod.EXAMPLE_MOD_GROUP));​ 
-    ​[...]+    // If you made a new materialthis is where you would note it. 
 +    public static final Item CUSTOM_MATERIAL_HELMET = new ArmorItem(CustomArmorMaterialEquipmentSlot.HEADnew Item.Settings().group(ExampleMod.EXAMPLE_MOD_GROUP));​ 
 +    public static final Item CUSTOM_MATERIAL_CHESTPLATE = new ArmorItem(CustomArmorMaterialEquipmentSlot.CHESTnew Item.Settings().group(ExampleMod.EXAMPLE_MOD_GROUP)); 
 +    ​public static final Item CUSTOM_MATERIAL_LEGGINGS = new ArmorItem(CustomArmorMaterial,​ EquipmentSlot.LEGS,​ new Item.Settings().group(ExampleMod.EXAMPLE_MOD_GROUP)); 
 +    ​public static final Item CUSTOM_MATERIAL_BOOTS = new ArmorItem(CustomArmorMaterial,​ EquipmentSlot.FEET, new Item.Settings().group(ExampleMod.EXAMPLE_MOD_GROUP));​ 
 } }
 </​code>​ </​code>​
  
-Feel free to change any values. +Now that your items are properly created, lets register them and give them proper names. Your first parameter is going to be your namespace, which is your ModID, and then next one the name you want to give to your item.
- +
-==== Creating Armor Items ====+
  
-Back in the main class, you can now create it like so:+We'll be writing this right below your last ArmorItem.
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
-public ​class ExampleMod implements ModInitializer ​+public ​static void register() ​
-    ​public static final Item WOOL_HELMET = new ArmorItem(CustomArmorMaterial.WOOLEquipmentSlot.HEAD,​ (new Item.Settings().group(ItemGroup.COMBAT))); + Registry.register(Registry.ITEM, new Identifier("​examplemod",​ "​custom_material"​), CUSTOM_MATERIAL);​ 
-    ​public static final Item WOOL_CHESTPLATE = new ArmorItem(CustomArmorMaterial.WOOLEquipmentSlot.CHEST,​ (new Item.Settings().group(ItemGroup.COMBAT))); + Registry.register(Registry.ITEM, new Identifier("​examplemod",​ "​custom_material_helmet"​), CUSTOM_MATERIAL_HELMET); 
-    ​public static final Item WOOL_LEGGINGS = new ArmorItem(CustomArmorMaterial.WOOLEquipmentSlot.LEGS,​ (new Item.Settings().group(ItemGroup.COMBAT))); + Registry.register(Registry.ITEM, new Identifier("​examplemod",​ "​custom_material_chestplate"​), CUSTOM_MATERIAL_CHESTPLATE); 
-    ​public static final Item WOOL_BOOTS = new ArmorItem(CustomArmorMaterial.WOOLEquipmentSlot.FEET,​ (new Item.Settings().group(ItemGroup.COMBAT)));+ Registry.register(Registry.ITEM, new Identifier("​examplemod",​ "​custom_material_leggings"​), CUSTOM_MATERIAL_LEGGINGS); 
 + Registry.register(Registry.ITEM, new Identifier("​examplemod",​ "​custom_material_boots"​), CUSTOM_MATERIAL_BOOTS);
 } }
 </​code>​ </​code>​
  
-==== Registering Armor Items ====+Your armor items are done. Now we'll just call the Registry on our main class (and annotate the new group).
  
-Register them the same way you'd register a normal item.+<code java [enable_line_numbers="​true"​]>​ 
 +public static final ItemGroup EXAMPLE_MOD_GROUP = FabricItemGroupBuilder.create( 
 +            new Identifier("​examplemod",​ "​example_mod_group"​)) 
 +            .icon(() -> new ItemStack(RegisterItems.CUSTOM_MATERIAL)) // This uses the model of the new material you created as an icon, but you can reference to whatever ​you like 
 +            ​.build();
  
-<code java [enable_line_numbers=true]>​ +@Override
-    [...]+
     public void onInitialize() {     public void onInitialize() {
-        ​Registry.register(Registry.ITEM,​new Identifier("​tutorial","​wool_helmet"​),​ WOOL_HELMET);​ +        ​RegisterItems.register();​
- Registry.register(Registry.ITEM,​new Identifier("​tutorial","​wool_chestplate"​),​ WOOL_CHESTPLATE);​ +
- Registry.register(Registry.ITEM,​new Identifier("​tutorial","​wool_leggings"​),​ WOOL_LEGGINGS);​ +
- Registry.register(Registry.ITEM,​new Identifier("​tutorial","​wool_boots"​),​ WOOL_BOOTS);+
     }     }
 </​code>​ </​code>​
 +
 +That's it! Your armor should now exist in game, untextured still, but present and able to be given with /give.
 +
 +Now we'll be assigning the textures to each piece.
 +
 +
  
 ==== Texturing ==== ==== Texturing ====
  
-Since you already know how to make item models and textureswe 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 itemFor this, we'll make a ''​pack.mcmeta''​ file so our resources can act like a resource pack.+We're going to assume ​you 
 +  * Have the textures for each armor item (x_helmet.pngx_chestplate.png etc.) 
 +  * Have the textures ​for the armor in body (x_layer_1.png and x_layer_2.png)
  
-<​code ​JavaScript src/main/resources/pack.mcmeta>+And assign them to each armor item.  
 + 
 +The following should be the same with all armor items, only changing which part are we using. We'll use helmet for our example. 
 + 
 +<​code ​JSON resources/assets/examplemod/​models/​item/custom_material_helmet.json>
 { {
-    ​"pack":{ + "parent": ​"​item/​generated",​ 
-        "pack_format":4, + "textures": ​{ 
-        "description":"​Tutorial Mod+ "layer0": "examplemod:​item/​custom_material_helmet
-    }+ }
 } }
 </​code>​ </​code>​
  
-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.)+Repeat with all armor items. 
 + 
 +To give your on-body armor a texture, ​you'll simply put the layer_1.png and layer_2.png on '​resources/​assets/​minecraft/​textures/​models/​armor'​.
  
 If you followed everything, you should now be able to have a full armor set! If you followed everything, you should now be able to have a full armor set!
 +
 +====Adding Knockback Protection====
 +
 +And here comes the so very cursed!
 +
 +Mojang decided that they were not only going to hardcode getKnockbackResistance,​ but they were also gonna make it immutable! Fun stuff.
 +
 +To get around this, we're gonna make a mixin that goes into ArmorItem. If this is your first time, [[tutorial:​mixin_registration|here'​s how to register mixins on your fabric.mod.json]]
 +
 +We'll make a class called ArmorItemMixin,​ and write:
 +
 +<code java [enable_line_numbers:"​true"​]>​
 +@Mixin (ArmorItem.class)
 +public abstract class ArmorItemMixin {
 +
 +}
 +</​code>​
 +
 +Now we have to make a @Shadow to modify knockbackResistance,​ which is and EntityAttribute
 +
 +<code java [enable_line_numbers:"​true"​]>​
 +@Mixin (ArmorItem.class)
 +public abstract class ArmorItemMixin {
 + @Shadow @Final private static UUID[] MODIFIERS;
 + @Shadow @Final @Mutable private Multimap<​EntityAttribute,​ EntityAttributeModifier>​ attributeModifiers;​
 + @Shadow @Final protected float knockbackResistance;​
 +}
 +</​code>​
 +
 +Next we @Inject our GENERIC_KNOCKBACK_RESISTANCE into the ArmorMaterial constructor.
 +
 +<code java [enable_line_numbers:"​true"​]>​
 +@Mixin (ArmorItem.class)
 +public abstract class ArmorItemMixin {
 +
 +    @Shadow @Final private static UUID[] MODIFIERS;
 +    @Shadow @Final @Mutable private Multimap<​EntityAttribute,​ EntityAttributeModifier>​ attributeModifiers;​
 +    @Shadow @Final protected float knockbackResistance;​
 +    ​
 +    @Inject(method = "<​init>",​ at = @At(value = "​RETURN"​))
 +    private void constructor(ArmorMaterial material, EquipmentSlot slot, Item.Settings settings, CallbackInfo ci) {
 +        UUID uUID = MODIFIERS[slot.getEntitySlotId()];​
 +
 +        if (material == RegisterItems.customArmorMaterial) {
 +            ImmutableMultimap.Builder<​EntityAttribute,​ EntityAttributeModifier>​ builder = ImmutableMultimap.builder();​
 +
 +            this.attributeModifiers.forEach(builder::​put);​
 +
 +            builder.put(
 +                    EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE,​
 +                    new EntityAttributeModifier(uUID,​
 +                            "Armor knockback resistance",​
 +                            this.knockbackResistance,​
 +                            EntityAttributeModifier.Operation.ADDITION
 +                    )
 +            );
 +
 +            this.attributeModifiers = builder.build();​
 +        }
 +    }
 +    ​
 +}
 +</​code>​
 +
 +Now your armor has the knockback resistance value you assigned to it back on CustomArmorMaterial.
tutorial/armor.1563514436.txt.gz · Last modified: 2019/07/19 05:33 by ggtylerr