Jump to content

[1.11] Get Held Item from EntityLivingBase not working


MSpace-Dev

Recommended Posts

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 by MSpace-Dev
Fixed code up a bit
Link to comment
Share on other sites

Haha, funny :P 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 ')

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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!

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.