Jump to content

Recommended Posts

Posted

Concerned code inside my eventhandler:

@SubscribeEvent
public void onEntityRightClicked(EntityInteractEvent event)
{	
	//Current item in hand
	ItemStack itemstack = event.entityPlayer.inventory.getCurrentItem();

	//Cow
	if(event.target instanceof EntityCow)
	{
		if (itemstack != null && itemstack.getItem() == LifeMod.getItem("lm_item_food_mugempty") && !event.entityPlayer.capabilities.isCreativeMode)
        {
            if (--itemstack.stackSize == 0)
            {
            	event.entityPlayer.inventory.setInventorySlotContents(event.entityPlayer.inventory.currentItem, new ItemStack(LifeMod.getItem("lm_food_otherdrinks_mugmilk")));
            }
            else if(event.entityPlayer.inventory.addItemStackToInventory(new ItemStack(LifeMod.getItem("lm_food_otherdrinks_mugmilk"))) == false);
            {
            	event.entityPlayer.dropPlayerItemWithRandomChoice(new ItemStack(LifeMod.getItem("lm_food_otherdrinks_mugmilk")), false);
            }
        }
	}
}

What works fine is when I right click a cow with 1 mug, that slot is replaced with 1 mug of milk. Also, when I have a full inventory, the mug of milk is dropped.

 

What does NOT work fine is that when I have an empty or semi-empty inventory, the milk mug is added (or if a stack of that sort exists, merges to that), BUT a milk mug is ALSO dropped, instead of ONLY just being added to the inventory. Does anyone know how to fix this?

Posted

I ran into something interesting on interact events recently. 

 

Put a log or print method at the top of your mod and see how many times it is running when you right click.  I am betting that it is running twice.

 

I had this occur when i was working with anti-greifing code for buckets. 

 

If it is that things are runnign twice, some of the variables were null on the 2nd run and I had to play with it to detect this and act only on the pass of interest.

 

This was not a client/server duplicity, it was actually running twice on the server.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

I ran into something interesting on interact events recently. 

 

Put a log or print method at the top of your mod and see how many times it is running when you right click.  I am betting that it is running twice.

 

I had this occur when i was working with anti-greifing code for buckets. 

 

If it is that things are runnign twice, some of the variables were null on the 2nd run and I had to play with it to detect this and act only on the pass of interest.

 

This was not a client/server duplicity, it was actually running twice on the server.

 

Hm, I see what you mean. I ran loads of tests and can confirm the code is running twice, but I'm not sure how to find out which pass of the code causes some of the variables to be null and how to fix this. What did you do when you had your issue?

Posted

In my case, I was trying to stop from dropping or picking up lava/water in certain areas.

 

I found the first fire of the event was clicking on a block.  The 2nd was the bucket.  On the first fire, it has the block location, on the 2nd, it was 0,0,0 or null in some fields.

 

If I had to guess the pattern, I bet the 2nd event is the right one on yours. 

 

How to detect, you need to look for some fields to be null or empty.  Again, from what you reported, I bet that event.entityPlayer (or its inventory) is null on the one you do not want so you can ignore it and just use the other one.

 

Let me know if that works. 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

How to detect, you need to look for some fields to be null or empty.  Again, from what you reported, I bet that event.entityPlayer (or its inventory) is null on the one you do not want so you can ignore it and just use the other one.

 

Or, as I've told you already in your thread about that matter, check if the action of right-clicking is RIGHT_CLICK_AIR (event.action == RIGHT_CLICK_AIR)

if you want "those with the 0's" or RIGHT_CLICK_BLOCK for the other call.

There's another one called LEFT_CLICK_BLOCK, which is called when a block is left-clicked (obviously)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

I wasn't for sure in his case it would be the same with a custom bucket so I gave him a more general approach, but by all means try that as well.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

The EntityInteractEvent (which Pardeep is using) actually runs twice because of server/client (1x server-call, 1x client-call) so for that you just check if worldObj.isRemote == true/false (true if you want client-side, grab the worldObj from the entity shipped by the event parameter)

 

For any kind of block/item interaction (for blocks there are left/right-click, for items only right-click, this would be your case, delpi) there is AFAIK only the serverside interaction. For blocks, the event will always trigger 1x (either if you right-click or left-click the block). To check with which mouse button the player interacts with the block, use the event.action variable (either RIGHT_CLICK_BLOCK or LEFT_CLICK_BLOCK).

Item interactions (right-click only here) is a special case where the event seems to fire either 1x or 2x. The 1x is when the player right-clicks with the item when he doesn't face a block (either no block in sight or block too far away). The 2x is if he does.

In either case, the event will trigger with the event RIGHT_CLICK_AIR. the 2nd one will also fire with RIGHT_CLICK_BLOCK.

 

I hope I could clarify some things :)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

Fantastic explanation!! Thanks.

 

I got in the habit with events of having different event handlers for the Client/Common(server) proxies to avoid any confusion over which side it is.  Found it easier later on to troubleshoot on changes.  Might be a usefull strategies for others.

 

Did you help with the Forge Coding or just reason this out? 

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

Fantastic explanation!! Thanks.

 

I got in the habit with events of having different event handlers for the Client/Common(server) proxies to avoid any confusion over which side it is.  Found it easier later on to troubleshoot on changes.  Might be a usefull strategies for others.

 

Did you help with the Forge Coding or just reason this out?

 

Just reasoning and looking up. Eclipse (or any other reasonably good IDE really) has great features for this. I first open the Event class, right-click on the constructor and choose "Open Call hierarchy"), then I see when the event is exactly fired. I even see there which event bus to use (there are following event busses existing: for Minecraft Forge EVENT_BUS, TERRAIN_GEN_BUS and ORE_GEN_BUS; for FML it's only FMLCommonHandler.instance().bus() (and actually the mod loading events, which call your preInit, init etc.))

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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.