MSpace-Dev Posted December 19, 2017 Posted December 19, 2017 (edited) Hey everyone, I am trying to get the held item of a player when an entity dies, using the LivingDropsEvent. But the current code I have doesn't seem to work. My message never comes through, and I really have no idea why. I'm guessing I'm not checking the held item correctly, but not sure. public void interceptMobDeath(LivingDropsEvent event) { Entity killerEntity = event.getSource().getSourceOfDamage(); if(killerEntity instanceof EntityLivingBase) { EntityLivingBase killer = (EntityLivingBase) killerEntity; if(killer.getHeldItemMainhand() != null) { if (killer.getHeldItem(EnumHand.MAIN_HAND) == new ItemStack(ModItems.spirit_knife)) { Utils.getLogger().info(killer); } } } } Edited December 19, 2017 by MSpace-Dev Fixed code up a bit Quote
MSpace-Dev Posted December 19, 2017 Author Posted December 19, 2017 Also, my IDE is telling me that 7 minutes ago, MSpace-Dev said: if(killer.getHeldItemMainhand() != null) { will always return true. Is that correct? Quote
MSpace-Dev Posted December 19, 2017 Author Posted December 19, 2017 Haha, funny Well, did some research on the difference, and have an understanding now, between '==' VS '.equals()'. So, I've managed to get to this so far, still fails. New if statement: if(killerEntity instanceof EntityLivingBase) { EntityLivingBase killer = (EntityLivingBase) killerEntity; Utils.getLogger().info("Player is holding " + killer.getHeldItemMainhand()); Utils.getLogger().info("Player SHOULD BE holding " + (new ItemStack(ModItems.spirit_knife))); if (killer.getHeldItemMainhand().equals(new ItemStack(ModItems.spirit_knife))) { Utils.getLogger().info(killer + " has knife"); } } The above outputs this when I kill something: [21:38:05] [Server thread/INFO]: Player is holding 1xitem.spirit_knife@0 [21:38:05] [Server thread/INFO]: Player SHOULD BE holding 1xitem.spirit_knife@0 So, they're both the same. I'm using .equals(). Still not sure why it isn't working. (Still not outputting ' killer has knife ') Quote
loordgek Posted December 19, 2017 Posted December 19, 2017 no you need to == the item in the stack to your item Quote
MSpace-Dev Posted December 19, 2017 Author Posted December 19, 2017 Ohhhh, haha. Thanks. Got it working now. (I thought the problem was that they were pointing at different locations in memory, or something like that, thus I needed to use .equals()) But yeah, still not the best. I think I should probably do more research on the difference between Items and ItemStacks are too. Still pretty fuzzy on what each's purpose is, and the difference between them. Quote
MSpace-Dev Posted December 19, 2017 Author Posted December 19, 2017 Could you tell me why comparing the two ItemStacks would fail though, compared to comparing two Items? (Again, fuzzy about the difference between the two) Quote
jabelar Posted December 19, 2017 Posted December 19, 2017 32 minutes ago, MSpace-Dev said: (Again, fuzzy about the difference between the two) == ALWAUYS looks to see if the things being compared are the actual same instance. Not just equivalent but actually the same thing stored in same memory. So when you compared to a new itemstack it will always be false because that is a different instance located in different place in memory. equals() MAY do something more and is intended to be used to test for equivalence. But needs to be overridden as apppropriate for the class. Usually an implementation will check each field in the instance to see if each of them are equivalent (which may require further calls to sub fields depending until you get down to comparing primitives. You can use == for singletons. For example, in Minecraft Item instances are singletons (there is only one instance of each in the game ). But Itemstack is not a singleton (each stack is its own instance) so you would only use == in very specific circumstances. When using equals() it is good idea to check out the implementation to confirm it does what you expect. However, in the case of ItemStacks there is actually a separate set of methods for comparing because you might only be interested in certain aspects -- like do you care if the durability is the same, or the NBT tags, etc. So you should consider the following methods: areItemStacksEqual() areItemStacksEqualUsingNBTShareTag() areItemStackShareTagsEqual() areItemStackTagsEqual() areItemsEqual() areItemsEqualIgnoreDurability() Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/
MSpace-Dev Posted December 19, 2017 Author Posted December 19, 2017 Ah, @jabelar That makes a TON of sense. Thanks for the in-depth explanation there, really helps me understand what went wrong in the beginning. Also, the knowledge of those ItemStack methods will definitely help in the future, I'm sure! Also, I completely understand how trying to compare a new instance of an item stack would never work, and has also increased my understanding of the difference between '==' and .equals(). Super grateful! Quote
Recommended Posts
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.