Jump to content

[1.12.2] On Item Click not working


ogrekpl

Recommended Posts

Hello guys!

I'm not sure what am I doing wrong :/ 
What I want to do:

- if player got in inventory specify item do:
   - deal dmg to player
   - add new intem to inventory

   - delete item from hand

 

public class ItemFireDiamondFireing extends ItemBase implements IHasModel
{

	public ItemFireDiamondFireing(String name) 
	{
		super(name);
	}
	
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) 
	{
		ItemStack item = playerIn.getHeldItem(handIn);
		
		ItemStack itemStackIn = new ItemStack(ItemInit.ITEM_GRINDER_BOWL);
		
		if (playerIn.inventory.hasItemStack(itemStackIn))
		{
			float dmg = 20.0f; // x dmg
			playerIn.setHealth(playerIn.getHealth() - dmg); //Deal x DMG
			
			playerIn.addItemStackToInventory(itemStackIn); // some item 
			
		}
		
		return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
	}	

 

Link to comment
Share on other sites

1 hour ago, ogrekpl said:

itemStackIn

This is a bad name for this variable. The "in" portion of the parameters indicate that it was passed in to the function.

 

1 hour ago, ogrekpl said:

return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);

If you want to delete the item from their hand, don't return the item.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1. Define "not working".

2. Currently, your code triggers twice when the player right clicks (fires once on both side). Check if the world is on the desired side before executing your code.

3. Stop using ItemBase and IHasModel. They are bad practices.

4.

float dmg = 20.0f; // x dmg
playerIn.setHealth(playerIn.getHealth() - dmg); //Deal x DMG

Unless you are intentionally setting the player's health to a lower value instead of damaging the player, use Entity#attackEntityFrom instead.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Ok i got it. Some my bad implementations in inicialization items. 

Thanks DavidM for Entity#attackEntityFrom and Draco18sdiesieben07  too

 

Additional, I wounder, in what way I can damage item from inventory (decrese durability from 20 to 19 - by 1 point)
 

	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) 
	{
		ItemStack item = playerIn.getHeldItem(handIn);
		ItemStack itemStack = new ItemStack(ItemInit.ITEM_GRINDER_BOWL); // item with durability
		ItemStack itemStackReturn = new ItemStack(ItemInit.ITEM_FIRE_DIAMOND);
		
		if (playerIn.inventory.hasItemStack(itemStack))
		{
			playerIn.attackEntityFrom(DamageSource.ON_FIRE, 3.0f);
			playerIn.addItemStackToInventory(itemStackReturn);	
			int slotNr = playerIn.inventory.findSlotMatchingUnusedItem(itemStack); // find slot nr where is this item i want decrese durability
			
			ItemStack damageItemStack = new ItemStack(***); // here i want to copy item from inventory
			//someItemStack.setItemDamage(someItemStack.getItemDamage() - 1); // decrease durabilit of item that i copied 
			playerIn.inventory.removeStackFromSlot(slotNr); //remove original item
			//add this copied item with 1 less durability
			
			return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, ItemStack.EMPTY);
		}
		else 
		{
			return new ActionResult<ItemStack>(EnumActionResult.FAIL, item);
		}
		
	}	

 

This is my idea but i cant find methods for taking ItemStack from item in inventory(e.g by slot nr)
I hope you know what I need :) 

Edited by ogrekpl
Link to comment
Share on other sites

12 minutes ago, ogrekpl said:

Additional, I wounder, in what way I can damage item from inventory (decrese durability from 20 to 19 - by 1 point)

ItemStack#damageItem.

 

Also, you still haven't fixed the "firing on both sides" problem.

14 hours ago, DavidM said:

Currently, your code triggers twice when the player right clicks (fires once on both side). Check if the world is on the desired side before executing your code.

 

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

19 minutes ago, DavidM said:

ItemStack#damageItem.

Yes, but still, I probably need to take specific item, 'coz player might have 10 similar items. I thought I have find one, copy it, damage this copy, remove original and add copied one. Am I correct?

 

19 minutes ago, DavidM said:

Also, you still haven't fixed the "firing on both sides" problem.

Probably I'm too nooby. Why it's triggering twice not once? Item I got in hand is fireing one(ITEM_FIREDIAMOND_FIREING), and by clicking it I get dmg as player and receive stable one (ITEM_FIRE_DIAMOND)

Edited by ogrekpl
Link to comment
Share on other sites

16 hours ago, diesieben07 said:

No. It is a workaround for the deficiencies in the deobfuscation toolchain which cannot reliably handle parameters with the same name as a field in the class. That is the sole reason for the "in" suffix.

Oh for fuck's sake diesieben07, you know that's what I meant. I was trying to point out that the name was bad because it was copying a naming schema based on the passed in parameters without thinking about what that schema meant.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.