Posted December 19, 20177 yr 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, 20177 yr by MSpace-Dev Fixed code up a bit
December 19, 20177 yr Author Also, my IDE is telling me that 7 minutes ago, MSpace-Dev said: if(killer.getHeldItemMainhand() != null) { will always return true. Is that correct?
December 19, 20177 yr Author 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 ')
December 19, 20177 yr Author 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.
December 19, 20177 yr Author 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)
December 19, 20177 yr 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() Check out my tutorials here: http://jabelarminecraft.blogspot.com/
December 19, 20177 yr Author 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!
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.