I often see requests for tutorials on custom armor that adds potion effects or is resistant to a certain type of damage, so I figured I'd make one. This tutorial assumes you know the basics of Java, Minecraft modding, and have your mod setup already. To begin, create a new class that extends ItemArmor and implements ISpecialArmor. *Note* I am not going to hold your hand and write this line by line for you. I am going to show you code from a mod I have been working on and will explain it in commented lines. Also, I have noticed a lot of off topic requests in this post. No, I am not going to make a bukkit plugin or mod specifically for you, nor am I going to help you with things that this title does not suggest.
If you just want armor that adds a potion effect, skip this part and read the next section!
A very important part of this is going to be some called "ArmorProperties", this allows us to modify that damage dealt to a player that is wearing this armor. It's constructor has three parts. Priority, AbsorbRatio, and AbsorbMax, in that order. Priority determines which piece of armor gets damaged first, I usually set it to 1 so all pieces get damaged at the same time. I'm not entirely sure what AbsorbRatio does(Sorry ), and AbsorbMax is the maximum amount of damage one piece of armor can absorb. *Note* this method gets called PER PIECE of armor, so if you want a full suit of armor to absorb all fire damage dealt to it do not just have the armor check for fire damage then return an instance of ArmorProperties that has AbsorbMax set to Integer.MAX_VALUE or else the user can just craft some boots then be fire proof.
For this example we are going to use a armor set I wrote called ItemPyrusArmor, that is very resistant to fire damage:
package me.tyler15555.metalworking.item;
import me.tyler15555.metalworking.util.Resources;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraftforge.common.ISpecialArmor;
public class ItemPyrusArmor extends ItemArmor implements ISpecialArmor {
public ItemPyrusArmor(ArmorMaterial p_i45325_1_, int p_i45325_2_, int p_i45325_3_) {
super(p_i45325_1_, p_i45325_2_, p_i45325_3_);
}
//This is the MOST important method in this class! This gets called every time a player wearing your armor gets damaged, however it is called before the damage is applied so you can modify it.
@Override
public ArmorProperties getProperties(EntityLivingBase player, ItemStack armor, DamageSource source, double damage, int slot) {
if(source == source.inFire || source == source.lava || source == source.onFire) { //Check for fire damage, you can use other types of damage too.
return new ArmorProperties(1, 1, MathHelper.floor_double(damage * .25D));
}
return new ArmorProperties(0, 0, 0);
}
@Override
public int getArmorDisplay(EntityPlayer player, ItemStack armor, int slot) {
return 4; //How many half shields each piece of armor will fill up on the armor bar
}
@Override
public void damageArmor(EntityLivingBase entity, ItemStack stack, DamageSource source, int damage, int slot) {
stack.damageItem(damage * 2, entity); //Allows you to control the amount of damage done to each piece of armor, useful for nerfing some suits of armor.
}
@Override //This is pretty self explanatory
public String getArmorTexture(ItemStack armor, Entity entity, int slot, String type) {
if(armor.getItem() == MWItems.pyrusLegs) {
return "metalworking:textures/armor/pyrus_2.png";
} else {
return "metalworking:textures/armor/pyrus_1.png";
}
}
@Override
public CreativeTabs[] getCreativeTabs() {
return new CreativeTabs[] {CreativeTabs.tabCombat, Resources.tabMW}; //This lets me put my armor in as many create tabs as I want, pretty cool right?
}
@Override
public boolean getIsRepairable(ItemStack armor, ItemStack stack) {
return stack.getItem() == MWItems.ingotPyrus; //Alllows certain items to repair this armor.
}
}
Potion Effects and Armor
The armor above is pretty complex to make and requires implementing new classes, however adding potion effects is super easy and you only need to override one method!
For this example, we are going to use a class I created called ItemThoriteArmor, which slows the player down while it is worn
It is really that easy! However, you SHOULD NOT do anything with the player that would be disabled once the armor is taken off. For example, do not set player.capabilities.canFly to true then try to set it to false when the player is not wearing the armor because the method will no longer be called when the player is not wearing the armor. Potion effects are ok because the game will remove them once the timer runs out.
I hope this tutorial was able to help you guys out! If you need any help, feel free to post in the comments. Happy modding!
Hey, how do you add the potion effect only after all your the armor is on you ?
(it only activate once all the custom armor I made is wear)
Here is something you can try:
if(player.getActivePotionEffects().contains(Your potion effect) && this instanceof YourHelmet) {
ItemStack[] armor = new ItemStack[] {}; //Create a new itemstack with each piece of your armor
int gearCounter = 0;
for(int i = 1; i < 5; i++) {
if(player.getEquipmentInSlot(i) == armor[i - 1] {
gearCounter++;
}
}
if(gearCounter == 4) {
player.addPotionEffect();
gearCounter = 0;
}
EDIT: Changed the checks a bit so this should cause less stress on a server. Now the armor is only checked when the potion effect isn't active and only the helmet will do the check.
Just found a tutorial here that uses the onUpdate method. Works great for me
@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) {
super.onUpdate(stack, world, entity, par4, par5);
EntityPlayer player = (EntityPlayer) entity;
ItemStack equipped = player.getCurrentEquippedItem();
if(equipped == stack) {
player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 5, 0));
}
}
Credit goes to
You're going to want to make sure the player is actually holding something before you compare it to the items stack. Replace your current if() statement with this:
That potion effect code you created is such a game crasher, because every time my player receives falldamage the game crashes . Anyone else getting this problem ?
Thats the vanilla Minecraft method to add a potion effect... Post your code.
The Meaning of Life, the Universe, and Everything.
Join Date:
6/2/2014
Posts:
100
Minecraft:
teh_herper
Member Details
Would it be possible to change the way a mob behaves while wearing armor, like instead of having endermen ignore you while wearing a pumpkin, but having a creeper ignore you while wearing armor?
And what would the DamageSource be if it were an explosion? I tried using setExplosion() but it doesn't seem to be working correctly, as it affects all damage not including explosions too.
I apologize if any questions are dumb, but I only recently began modding.
Rollback Post to RevisionRollBack
Fact: Using flint and steel on a creeper causes them to stop in place and blow up.
Would it be possible to change the way a mob behaves while wearing armor, like instead of having endermen ignore you while wearing a pumpkin, but having a creeper ignore you while wearing armor?
And what would the DamageSource be if it were an explosion? I tried using setExplosion() but it doesn't seem to be working correctly, as it affects all damage not including explosions too.
I apologize if any questions are dumb, but I only recently began modding.
Using setExplosion() would set the damage source to an explosion, which may not be accurate. I believe there is a method or field that determines whether or not is an explosion, check around DamageSource.class
If you just want armor that adds a potion effect, skip this part and read the next section!
A very important part of this is going to be some called "ArmorProperties", this allows us to modify that damage dealt to a player that is wearing this armor. It's constructor has three parts. Priority, AbsorbRatio, and AbsorbMax, in that order. Priority determines which piece of armor gets damaged first, I usually set it to 1 so all pieces get damaged at the same time. I'm not entirely sure what AbsorbRatio does(Sorry ), and AbsorbMax is the maximum amount of damage one piece of armor can absorb. *Note* this method gets called PER PIECE of armor, so if you want a full suit of armor to absorb all fire damage dealt to it do not just have the armor check for fire damage then return an instance of ArmorProperties that has AbsorbMax set to Integer.MAX_VALUE or else the user can just craft some boots then be fire proof.
For this example we are going to use a armor set I wrote called ItemPyrusArmor, that is very resistant to fire damage:
For this example, we are going to use a class I created called ItemThoriteArmor, which slows the player down while it is worn
It is really that easy! However, you SHOULD NOT do anything with the player that would be disabled once the armor is taken off. For example, do not set player.capabilities.canFly to true then try to set it to false when the player is not wearing the armor because the method will no longer be called when the player is not wearing the armor. Potion effects are ok because the game will remove them once the timer runs out.
I hope this tutorial was able to help you guys out! If you need any help, feel free to post in the comments. Happy modding!
Add this to your ItemSword class:
public boolean hitEntity(ItemStack stack, EntityLivingBase hitEntity, EntityLivingBase attackingEntity) {
hitEntity.setFire(4);
return true;
}
(it only activate once all the custom armor I made is wear)
Here is something you can try:
EDIT: Changed the checks a bit so this should cause less stress on a server. Now the armor is only checked when the potion effect isn't active and only the helmet will do the check.
You're going to want to make sure the player is actually holding something before you compare it to the items stack. Replace your current if() statement with this:
EDIT: Tweaked the if() statement a bit
Just use world.spawnParticleEffect()
Can you elaborate a little bit or give an example to remove player particle effects.
Thanks!
@Override
public boolean hitEntity(ItemStack stack, EntityLivingBase hitEntity, EntityLivingBase attackingEntity) {
hitEntity.knockBack(hitEntity, 0, 0, 0);
return true;
}
edit: removed commented out code
Whoops, I misread your question and I can't say that I have an answer for you
This is not at all related to the tutorial and I cannot say that I have an answer for you.
Thats the vanilla Minecraft method to add a potion effect... Post your code.
Would it be possible to change the way a mob behaves while wearing armor, like instead of having endermen ignore you while wearing a pumpkin, but having a creeper ignore you while wearing armor?And what would the DamageSource be if it were an explosion? I tried using setExplosion() but it doesn't seem to be working correctly, as it affects all damage not including explosions too.
I apologize if any questions are dumb, but I only recently began modding.
Fact: Using flint and steel on a creeper causes them to stop in place and blow up.
Using setExplosion() would set the damage source to an explosion, which may not be accurate. I believe there is a method or field that determines whether or not is an explosion, check around DamageSource.class