I'm creating a mod for me and my friends to play. I created an item called Chemtech Injector and basically what it does is on right-click it, it adds two potion effects. I am trying to get it to delete the item afterwards by setting the itemstack in your main hand to empty. The problem is I only want this to happen if the Chemtech Injector is in the hand of the player. To do that I wanted to use an If statement. This is the code I have so far,
Basically what I need help with is figuring out how to get my item instead of ItemStack. I'm not very good at java coding thus why I'm here. The version is 1.16.5.
ItemStack has a method that returns the item it holds, namely, getItem(), so you use "getHeldItemMainhand().getItem()", although you need to consider that "getHeldItemMainhand()" can return null, which will cause a NullPointerException when you try to call "getItem()", but this is a simple matter of adding "getHeldItemMainhand() != null" prior to the check for the item:
if (playerIn.getHeldItemMainhand() != null && playerIn.getHeldItemMainhand().getItem() == yourItem)
{
// do stuff
}
(note that this refers to how things would be written in 1.6.4, using MCP deobfuscated names which can change from version to version; I assume that there is still a method called "getItem()" though. Also, I have the impression that you don't need to check if the item is in your hand at all, methods like "onItemUse()" or "onItemRightClick()" are only called when you right-click/use an item in your hand, unless you mean that you want it to only work in the main hand, not off-hand, which is something that isn't in 1.6.4 so I assume that 1.9+ doesn't have a way to easily tell which hand an item is used in)
To do this you would create an entity that extends Entitythrowable and then create an entity that extends the entity that extends Entitythrowable and an item that uses a function or something else to reduce the item in stack when used. Optionally you could extend Snowball which would also work.
In the following you can change the number 1 to a greater number and it will reduce the number of the items in the stack by that number when right clicking.
Also in the same code is whether or not the player is sneaking or not when using the item and it summons in two different entities for each way that the player uses the item.
The item code for 1.11.2-1.16.5 uses the word shrink to reduce the number of items in a stack of items, being introduced in the 1.12.2 version:
I'm creating a mod for me and my friends to play. I created an item called Chemtech Injector and basically what it does is on right-click it, it adds two potion effects. I am trying to get it to delete the item afterwards by setting the itemstack in your main hand to empty. The problem is I only want this to happen if the Chemtech Injector is in the hand of the player. To do that I wanted to use an If statement. This is the code I have so far,
if(playerIn.getHeldItemMainhand() == ItemStack.) {
}
Basically what I need help with is figuring out how to get my item instead of ItemStack. I'm not very good at java coding thus why I'm here. The version is 1.16.5.
ItemStack has a method that returns the item it holds, namely, getItem(), so you use "getHeldItemMainhand().getItem()", although you need to consider that "getHeldItemMainhand()" can return null, which will cause a NullPointerException when you try to call "getItem()", but this is a simple matter of adding "getHeldItemMainhand() != null" prior to the check for the item:
(note that this refers to how things would be written in 1.6.4, using MCP deobfuscated names which can change from version to version; I assume that there is still a method called "getItem()" though. Also, I have the impression that you don't need to check if the item is in your hand at all, methods like "onItemUse()" or "onItemRightClick()" are only called when you right-click/use an item in your hand, unless you mean that you want it to only work in the main hand, not off-hand, which is something that isn't in 1.6.4 so I assume that 1.9+ doesn't have a way to easily tell which hand an item is used in)
TheMasterCaver's First World - possibly the most caved-out world in Minecraft history - includes world download.
TheMasterCaver's World - my own version of Minecraft largely based on my views of how the game should have evolved since 1.6.4.
Why do I still play in 1.6.4?
To do this you would create an entity that extends Entitythrowable and then create an entity that extends the entity that extends Entitythrowable and an item that uses a function or something else to reduce the item in stack when used. Optionally you could extend Snowball which would also work.
In the following you can change the number 1 to a greater number and it will reduce the number of the items in the stack by that number when right clicking.
Also in the same code is whether or not the player is sneaking or not when using the item and it summons in two different entities for each way that the player uses the item.
The item code for 1.11.2-1.16.5 uses the word shrink to reduce the number of items in a stack of items, being introduced in the 1.12.2 version:
In 1.10.2 and previous versions however this was a function to reduce the number of an item in a stack of items for example:
For the entity you would extend your entity that's extending Entitythrowable so you can right click it and use or throw the object.
I have an entity that applies potion effects to players and entities which the code can be adjusted for (1.10.2-1.16.5) as well:
This could also be used to reduce items in itemstack for earlier versions of minecraft 1.8.9-1.9.4:
So like this?
if (playerIn.getHeldItemMainhand() != null && playerIn.getHeldItemMainhand().getItem() == ItemInit.CHEMTECH_INJECTOR)
{
// do stuff
}
When I tried that I got an error saying Incompatible operand types Item and RegistryObject<ChemtechInjector>