In response to the numerous posts I see on this all the time and, since I'm feeling too lazy to track them all down, I'll just go ahead and post a super easy method - no TickHandler, no EventHandler, just a simple override and your armor is doing all kinds of magic:
// 1.7.2: method name changed to "onArmorTick"
// 1.7.2: addChatMessage changed to require a ChatComponentText
@Override
public void onArmorTickUpdate(World world, EntityPlayer player, ItemStack itemstack)
{
if (itemstack.itemID == TutorialMain.WizardHat.itemID)
{
player.addChatMessage("[ARMOR TICK] You're wearing a wizard hat!!! Yippee!");
// 1.7.2 usage:
player.addChatMessage(new ChatComponentText("[ARMOR TICK] You're wearing a wizard hat!!! Yippee!"));
}
}
That goes in your custom armor Item class and is only called when a piece of your armor is actually equipped. You can make it do anything here that you could do in a Tick or Event Handler, such as flying, breathing underwater, walking on water, spawning particles, you get the idea.
EDIT: There is also one called 'onUsingItemTick' (or just 'onUsingTick', in 1.7.2), which is called every tick so long as you are using an item (i.e. you are continuing to hold right click AND in onItemRightClick you setItemInUse for the player AND you've been holding right click for a length of time less than the Item's getMaxUseDuration). Handy for the same reasons as above.
And no, before someone says otherwise, this doesn't belong in the tutorial section; it's for "Assistance in the creation and development of Minecraft modifications." People are more likely to read it here.
Anyway, just thought I'd share this as I just stumbled upon it myself recently. Pretty awesome method built right into the code
1.7.2 Note about Chat:
I prefer to create a helper method, because it's annoying having to create a new ChatComponentText object everywhere:
/** Sends the pre-translated message to the player as a chat message */
public static void sendChat(EntityPlayer player, String message) {
player.addChatMessage(new ChatComponentText(message));
}
// use like this:
ClassWithChatMethod.sendChat(player, "Some chat message");
// I usually call my class "PlayerUtils" or some such, and use it for player-related helper methods.
Thanks for the help, I was going crazy trying to impliment this. Only one thing, I have an if statement that is layed out like this if( var && var && var && var ) { <print statement> } but it does'nt work but if( var ) { <print statement> } works
It was nice of you to post that coolAlias, seeing as this is a question that is asked very often. I'm sure this will become one of those commonly used references for new modders (or modders who haven't done much with armor).
On Topic:
Thanks for sharing this here coolAlias, it's a good idea to keep it around here and I'll be sure to link ppl here when I see the question again for the N'th time
PS: To everyone, Reading what methods you can Override from Items and Blocks can lead to A LOT of cool stuff
There's a method called "onItemuse" inside every Item.
It gets called when you try to "use" the item.
Check if there's an entity infront of you (ray trace?) and kill it.
Or you could use the interactWithEntity or whatever it's called to do it upon r-clicking an entity, just see how the buckets work on cows for the method name and way to do it
Good to see some people are finding this useful. I added a summary of a similar method, 'onUsingItemTick', which was added by the awesome Forge team I do believe (as was onArmorTickUpdate, I think).
As Mazetar suggests, reading through the code is the best way to find what you need. Nearly everything you can think of that you'd want to do is already implemented in some fashion, even more so if you are using Forge. I keep hearing Mojang is in the process of developing a modding API and I can't help wondering, 'What for? We already have Forge!'
In response to the numerous posts I see on this all the time and, since I'm feeling too lazy to track them all down, I'll just go ahead and post a super easy method - no TickHandler, no EventHandler, just a simple override and your armor is doing all kinds of magic:
@Override
public void onArmorTickUpdate(World world, EntityPlayer player, ItemStack itemstack)
{
if (itemstack.itemID == TutorialMain.WizardHat.itemID)
{
player.addChatMessage("[ARMOR TICK] You're wearing a wizard hat!!! Yippee!");
}
}
That goes in your custom armor Item class and is only called when a piece of your armor is actually equipped. You can make it do anything here that you could do in a Tick or Event Handler, such as flying, breathing underwater, walking on water, spawning particles, you get the idea.
EDIT: There is also one called 'onUsingItemTick', which is called every tick so long as you are using an item (i.e. you are continuing to hold right click AND in onItemRightClick you setItemInUse for the player AND you've been holding right click for a length of time less than the Item's getMaxUseDuration). Handy for the same reasons as above.
And no, before someone says otherwise, this doesn't belong in the tutorial section; it's for "Assistance in the creation and development of Minecraft modifications." People are more likely to read it here.
Anyway, just thought I'd share this as I just stumbled upon it myself recently. Pretty awesome method built right into the code
Thanks a bunch, but how would one go about adding effects?
Hello, I want to do something very similar to this, but I want a potion effect to be added only when all four armor pieces are on. The code below does not work, it applies no potion effect at all even when everything is worn. Can anyone help me on this matter?
Hello, I want to do something very similar to this, but I want a potion effect to be added only when all four armor pieces are on. The code below does not work, it applies no potion effect at all even when everything is worn. Can anyone help me on this matter?
You do realize that the itemstack given in the method parameters is just ONE itemstack, right? It can't possibly be 4 different items at once. You need to get all currently equipped armor stacks and check those. You can use player.getCurrentArmor(0) up to (3), where 0 is boots, working your way up the body to the helm at 3.
You do realize that the itemstack given in the method parameters is just ONE itemstack, right? It can't possibly be 4 different items at once. You need to get all currently equipped armor stacks and check those. You can use player.getCurrentArmor(0) up to (3), where 0 is boots, working your way up the body to the helm at 3.
Thank you so much. I am extremely new at modding (even my level of understanding for Java itself is not on par with the complexities of Minecraft) so I apologize if my issue was a no-brainer. Looking into what you said, I managed to get it working. It's probably inefficient compared to what someone more apt at this would do, but it works, and right now that's all I care about. If you care to see it I'll put it here:
The comments were simply for debugging. Also I don't think an empty else at the end makes any difference, but I'd rather be safe than sorry. I'll do some tests tomorrow, but I'm pretty tired
Once again thanks a bunch. What you said was good enough for me to work it out.
No problem. In java, booleans are already true or false, so you don't usually compare them like 'if (boolean == true)', just 'if (boolean)'.
Here's perhaps a more conventional way of writing what you are after:
@Override
public void onArmorTickUpdate(World world, EntityPlayer player, ItemStack itemstack) {
// create local variables within the method, since Item class fields are technically static... which is not good for you ;P
ItemStack boots = player.getCurrentArmor(0);
ItemStack legs = player.getCurrentArmor(1);
ItemStack chest = player.getCurrentArmor(2);
ItemStack helm = player.getCurrentArmor(3);
// do quick null checks, then compare items
if (boots != null && legs != null && chest != null && helm != null && boots.getItem() == YourMod.yourBoots
&& legs.getItem() == YourMod.yourLegs && chest.getItem() == YourMod.yourChest && helm.getItem() == YourMod.yourHelm)
{
// everything is on and the right piece, so add your potion effect or whatever
}
}
I've never done this, but I'll give this a shot, try this:
[quote]
@Override
public void onArmorTickUpdate(World world, EntityPlayer player, ItemStack itemstack) {
// create local variables within the method, since Item class fields are technically static... which is not good for you ;P
ItemStack chest = player.getCurrentArmor(2);
// do quick null checks, then compare items
if (chest != null && chest.getItem() == YourMod.yourChest)
{
// everything is on and the right piece, so add your potion effect or whatever
player.capabilities.allowFlying = true;
}
}
what would i do if i wanted a chestpiece to make you fly?
Doing a quick search for 'armor fly' or something similar here in the Modification Development section will turn up dozens if not hundreds of similar questions with answers.
@wildbill22 That will get the player flying, but the armor tick doesn't tick when the armor isn't worn, and the item tick doesn't tick when the item isn't in the player's inventory, so how do you propose to stop the player from being able to fly? The trick is to use the player's own update, usually via PlayerTickEvent, so that you can both add AND remove flight, but you've got the basic idea
Doing a quick search for 'armor fly' or something similar here in the Modification Development section will turn up dozens if not hundreds of similar questions with answers.
@wildbill22 That will get the player flying, but the armor tick doesn't tick when the armor isn't worn, and the item tick doesn't tick when the item isn't in the player's inventory, so how do you propose to stop the player from being able to fly? The trick is to use the player's own update, usually via PlayerTickEvent, so that you can both add AND remove flight, but you've got the basic idea
In this particular case, not really. Sure, you could add the flight at that point, but there is no way to remove the flight once you are no longer wearing the armor, so you may as well do it all from one place, e.g. PlayerTickEvent.
Also, if you're going to be checking all of the armor slots (i.e. you need the full set), then EVERY single one of those pieces is going to be looking for all the other pieces every tick, at least the way most people go about it, so you may as well only check each slot ONCE per tick, which again is what would happen were you to implement it using PlayerTickEvent.
In this particular case, not really. Sure, you could add the flight at that point, but there is no way to remove the flight once you are no longer wearing the armor, so you may as well do it all from one place, e.g. PlayerTickEvent.
Also, if you're going to be checking all of the armor slots (i.e. you need the full set), then EVERY single one of those pieces is going to be looking for all the other pieces every tick, at least the way most people go about it, so you may as well only check each slot ONCE per tick, which again is what would happen were you to implement it using PlayerTickEvent.
Ah I see. You could add an else statement removing flight, but that might cause issues with other methods of flying. Anyways, that doesn't really matter.
That goes in your custom armor Item class and is only called when a piece of your armor is actually equipped. You can make it do anything here that you could do in a Tick or Event Handler, such as flying, breathing underwater, walking on water, spawning particles, you get the idea.
EDIT: There is also one called 'onUsingItemTick' (or just 'onUsingTick', in 1.7.2), which is called every tick so long as you are using an item (i.e. you are continuing to hold right click AND in onItemRightClick you setItemInUse for the player AND you've been holding right click for a length of time less than the Item's getMaxUseDuration). Handy for the same reasons as above.
And no, before someone says otherwise, this doesn't belong in the tutorial section; it's for "Assistance in the creation and development of Minecraft modifications." People are more likely to read it here.
Anyway, just thought I'd share this as I just stumbled upon it myself recently. Pretty awesome method built right into the code
1.7.2 Note about Chat:
I prefer to create a helper method, because it's annoying having to create a new ChatComponentText object everywhere:
Be sure to quote my post if you want a reply.
lol @ Cameronazzi copy-pasting my sig into his
Thanks for sharing this here coolAlias, it's a good idea to keep it around here and I'll be sure to link ppl here when I see the question again for the N'th time
PS: To everyone, Reading what methods you can Override from Items and Blocks can lead to A LOT of cool stuff
Off-Topic, but okay:
There's a method called "onItemuse" inside every Item.
It gets called when you try to "use" the item.
Check if there's an entity infront of you (ray trace?) and kill it.
Or you could use the interactWithEntity or whatever it's called to do it upon r-clicking an entity, just see how the buckets work on cows for the method name and way to do it
As Mazetar suggests, reading through the code is the best way to find what you need. Nearly everything you can think of that you'd want to do is already implemented in some fashion, even more so if you are using Forge. I keep hearing Mojang is in the process of developing a modding API and I can't help wondering, 'What for? We already have Forge!'
For resolving ID conflicts once and for all.
If all works out as intended by mojang, id conflicts are gone in 1.7 ^^
Thanks a bunch, but how would one go about adding effects?
Just an example, don't know if it works exactly like that.
Yep, that's pretty much it, though the parameters are off a bit ;P
One other thing to note is that you should always create a new potion effect when adding them:
Many potions use an amplifier of 0 or 1 (poison, jump, etc), some use 1 or 2 (e.g. heal and harm), so be aware of that when deciding on the amplifier.
I'm still using 1.6.4, by the way.
Thanks
NYou do realize that the itemstack given in the method parameters is just ONE itemstack, right? It can't possibly be 4 different items at once. You need to get all currently equipped armor stacks and check those. You can use player.getCurrentArmor(0) up to (3), where 0 is boots, working your way up the body to the helm at 3.
Thank you so much. I am extremely new at modding (even my level of understanding for Java itself is not on par with the complexities of Minecraft) so I apologize if my issue was a no-brainer. Looking into what you said, I managed to get it working. It's probably inefficient compared to what someone more apt at this would do, but it works, and right now that's all I care about. If you care to see it I'll put it here:
boolean helm, chest, leg, boot;
@Override
public void onArmorTickUpdate(World world, EntityPlayer player, ItemStack itemstack)
{
if (player.getCurrentArmor(3)!=null)
{
ItemStack helmet=player.getCurrentArmor(3);
if(helmet.getItem()==TestMod.DiaNuggetHelm){
helm = true;
//System.out.println("Helm: TRUE");
}
else
{
helm = false;
//System.out.println("Helm: TRUE");
}
}
if (player.getCurrentArmor(2)!=null)
{
ItemStack chestplate=player.getCurrentArmor(2);
if(chestplate.getItem()==TestMod.DiaNuggetChestplate){
chest = true;
//System.out.println("Chest: TRUE");
}
else
{
chest = false;
//System.out.println("Chest: TRUE");
}
}
if (player.getCurrentArmor(1)!=null)
{
ItemStack leggings=player.getCurrentArmor(1);
if(leggings.getItem()==TestMod.DiaNuggetLeggings){
leg = true;
//System.out.println("Leg: TRUE");
}
else
{
leg = false;
//System.out.println("Leg: TRUE");
}
}
if (player.getCurrentArmor(0)!=null)
{
ItemStack boots=player.getCurrentArmor(0);
if(boots.getItem()==TestMod.DiaNuggetBoots){
boot = true;
//System.out.println("Boot: TRUE");
}
else
{
boot = false;
//System.out.println("Boot: FALSE");
}
}
if((helm == true) && (chest == true) && (leg == true) && (boot == true))
{
//System.out.println("SUCESS");
player.addPotionEffect(new PotionEffect(Potion.moveSpeed.id, 14, 0));
helm = false;
chest = false;
leg = false;
boot = false;
}
else
{
//System.out.println("ELSE HIT");
}
}
The comments were simply for debugging. Also I don't think an empty else at the end makes any difference, but I'd rather be safe than sorry. I'll do some tests tomorrow, but I'm pretty tired
NHere's perhaps a more conventional way of writing what you are after:
[quote]
There, I hope that works for you
[url=2482915-wip-arkcraft-survival-evolved-dinos-taming]
Doing a quick search for 'armor fly' or something similar here in the Modification Development section will turn up dozens if not hundreds of similar questions with answers.
@wildbill22 That will get the player flying, but the armor tick doesn't tick when the armor isn't worn, and the item tick doesn't tick when the item isn't in the player's inventory, so how do you propose to stop the player from being able to fly? The trick is to use the player's own update, usually via PlayerTickEvent, so that you can both add AND remove flight, but you've got the basic idea
Isn't the correct method Item#onArmorTick though?
In this particular case, not really. Sure, you could add the flight at that point, but there is no way to remove the flight once you are no longer wearing the armor, so you may as well do it all from one place, e.g. PlayerTickEvent.
Also, if you're going to be checking all of the armor slots (i.e. you need the full set), then EVERY single one of those pieces is going to be looking for all the other pieces every tick, at least the way most people go about it, so you may as well only check each slot ONCE per tick, which again is what would happen were you to implement it using PlayerTickEvent.
Ah I see. You could add an else statement removing flight, but that might cause issues with other methods of flying. Anyways, that doesn't really matter.