shadowfacts.net/site/tutorials/forge-modding-112/armor.md

239 lines
7.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

```
metadata.title = "Armor"
metadata.date = "2016-09-17 16:43:00 -0400"
metadata.series = "forge-modding-112"
metadata.seriesName = "Forge Mods for 1.12"
```
Before we can create armor, we'll need to create an armor material to use for our copper armor.
We'll add a new field to our main mod class.
```java
// ...
public class TutorialMod {
// ...
public static final ItemArmor.ArmorMaterial copperArmorMaterial = EnumHelper.addArmorMaterial("COPPER", modId + ":copper", 15, new int[]{2, 5, 6, 2}, 9, SoundEvents.ITEM_ARMOR_EQUIP_IRON, 0.0F);
// ...
}
```
`EnumHelper.addArmorMaterial` takes a number of parameters:
- `"COPPER"`: The name of the new enum value, this is completely capitalized, following the enum naming convention.
- `modId + ":copper"`: This is the texture that will be used for our armor. We prefix it with our mod ID to use our mod's domain instead of the default `minecraft` domain.
- `15`: The maximum damage factor.
- `new int[]{2, 5, 6, 2}`: The damage reduction factors for each armor piece.
- `9`: The enchantibility of the armor.
- `SoundEvents.ITEM_ARMOR_EQUIP_IRON`: The sound event that is played when the armor is equipped.
- `0.0F`: The toughness of the armor.
Next we'll need the textures for the armor material that are used to render the on-player overlay.
Download the layer 1 texture [here](https://raw.githubusercontent.com/shadowfacts/TutorialMod/1.12/src/main/resources/assets/tutorial/textures/models/armor/copper_layer_1.png) and save it to `src/main/resources/assets/tutorial/textures/models/armor/copper_layer_1.png`. Download the layer 2 texture [here](https://raw.githubusercontent.com/shadowfacts/TutorialMod/1.12/src/main/resources/assets/tutorial/textures/models/armor/copper_layer_2.png) and save it to `src/main/resources/assets/tutorial/textures/models/armor/copper_layer_2.png`.
## Armor Item Base Class
Before we can begin creating armor items, we'll need to create a base class that implements our `ItemModelProvider` interface so it can be used with our registration helper method.
We'll create a class called `ItemArmor` in our `item` package that extends the Vanilla `ItemArmor` class.
```java
package net.shadowfacts.tutorial.item;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.shadowfacts.tutorial.TutorialMod;
public class ItemArmor extends net.minecraft.item.ItemArmor {
private String name;
public ItemArmor(ArmorMaterial material, EntityEquipmentSlot slot, String name) {
super(material, 0, slot);
setRegistryName(name);
setUnlocalizedName(name);
this.name = name;
}
public void registerItemModel(Item item) {
TutorialMod.proxy.registerItemRenderer(this, 0, name);
}
}
```
## Copper Helmet
Firstly, we'll create a field for our copper helmet item and register it and its item model.
```java
public class ModItems {
// ...
public static ItemArmor copperHelmet = new ItemArmor(TutorialMod.copperArmorMaterial, EntityEquipmentSlot.HEAD, "copper_helmet");
public static void register(IForgeRegistry<Item> registry) {
registry.registerAll(
// ...
copperHelemet
);
}
public static void registerModels() {
// ...
copperHelmet.registerItemModel();
}
}
```
Next, we'll need to create a JSON model for our item. The file will be at `src/main/resources/assets/tutorial/models/item/copper_helmet.json`.
```json
{
"parent": "item/generated",
"textures": {
"layer0": "tutorial:items/copper_helmet"
}
}
```
You can download the copper helmet texture from [here](https://raw.githubusercontent.com/shadowfacts/TutorialMod/1.12/src/main/resources/assets/tutorial/textures/items/copper_helmet.png) and save it to `src/main/resources/assets/tutorial/textures/items/copper_helmet.png`.
Lastly, we'll need to add a localization entry for the helmet.
```properties
# ...
item.copper_helmet.name=Copper Helmet
```
## Copper Chestplate
First, we'll create a field for our copper chestplate item and register it and its item model.
```java
public class ModItems {
// ...
public static ItemArmor copperChestplate = new ItemArmor(TutorialMod.copperArmorMaterial, EntityEquipmentSlot.CHEST, "copper_chestplate");
public static void register(IForgeRegistry<Item> registry) {
registry.registerAll(
// ...
copperChestplate
);
}
public static void registerModels() {
// ...
copperChestplate.registerItemModel();
}
}
```
Next, we'll need to create a JSON model for our item. The file will be at `src/main/resources/assets/tutorial/models/item/copper_chestplate.json`.
```json
{
"parent": "item/generated",
"textures": {
"layer0": "tutorial:items/copper_chestplate"
}
}
```
You can download the copper helmet texture from [here](https://raw.githubusercontent.com/shadowfacts/TutorialMod/1.12/src/main/resources/assets/tutorial/textures/items/copper_chestplate.png) and save it to `src/main/resources/assets/tutorial/textures/items/copper_chestplate.png`.
Lastly, we'll need to add a localization entry for the helmet.
```properties
# ...
item.copper_chestplate.name=Copper Chestplate
```
## Copper Leggings
First, we'll create a field for our copper leggings item and register it and its item model.
```java
public class ModItems {
// ...
public static ItemArmor copperLeggings = new ItemArmor(TutorialMod.copperArmorMaterial, EntityEquipmentSlot.LEGS, "copper_leggings");
public static void register(IForgeRegistry<Item> registry) {
registry.registerAll(
// ...
copperLeggings
);
}
public static void registerModels() {
// ...
copperLeggings.registerItemModel();
}
}
```
Next, we'll need to create a JSON model for our item. The file will be at `src/main/resources/assets/tutorial/models/item/copper_leggings.json`.
```json
{
"parent": "item/generated",
"textures": {
"layer0": "tutorial:items/copper_leggings"
}
}
```
You can download the copper helmet texture from [here](https://raw.githubusercontent.com/shadowfacts/TutorialMod/1.12/src/main/resources/assets/tutorial/textures/items/copper_leggings.png) and save it to `src/main/resources/assets/tutorial/textures/items/copper_leggings.png`.
Lastly, we'll need to add a localization entry for the helmet.
```properties
# ...
item.copper_leggings.name=Copper Leggings
```
## Copper Boots
First, we'll create a field for our copper boots item and register it in our `ModItems.init` method.
```java
public class ModItems {
// ...
public static ItemArmor copperBoots = new ItemArmor(TutorialMod.copperArmorMaterial, EntityEquipmentSlot.FEET, "copper_boots");
public static void register(IForgeRegistry<Item> registry) {
registry.registerAll(
// ...
copperBoots
);
}
public static void registerModels() {
// ...
copperBoots.registerItemModel();
}
}
```
Next, we'll need to create a JSON model for our item. The file will be at `src/main/resources/assets/tutorial/models/item/copper_boots.json`.
```json
{
"parent": "item/generated",
"textures": {
"layer0": "tutorial:items/copper_boots"
}
}
```
You can download the copper boots texture from [here](https://raw.githubusercontent.com/shadowfacts/TutorialMod/1.12/src/main/resources/assets/tutorial/textures/items/copper_boots.png) and save it to `src/main/resources/assets/tutorial/textures/items/copper_boots.png`.
Lastly, we'll need to add a localization entry for the boots.
```properties
# ...
item.copper_boots.name=Copper Boots
```
## Done!
Now, when we run the game, we can obtain our copper armor from the Combat creative tab, and when we equip it, we can see the player overlay being rendered and the armor value being show on the HUD:
![copper armor screenshot](https://i.imgur.com/Vv8Qzne.png)