Jump to content

[Solved][1.7.10]Why is this check for armor always returning true?


jabelar

Recommended Posts

Okay, I simply want to check if certain armor is being worn, but for some reason this statement is returning true even with nothing equipped at all.  I'm not sure why the null check doesn't return false in the first place, and even more confused why it considers the unlocalized name matched.  I know there is nothing equipped because I check but also am doing fresh world where I don't even have the armor anywhere in my inventory (equipped or not).

 

    		if (thePlayer.getCurrentArmor(0) != null && thePlayer.getCurrentArmor(0).getUnlocalizedName().equals(MagicBeans.bootsOfSafeFalling.getUnlocalizedName()));
    		{
    			// DEBUG
    			System.out.println(MagicBeans.bootsOfSafeFalling.getUnlocalizedName()+" equipped.");
                }

 

The console prints out: item.boots_of_safe_falling equipped.

 

Maybe I'm just going cross-eyed from 3 days straight programming, but shouldn't the the null check return false if nothing is equipped?  And how could the string equals return true with nothing in the armor slot?

 

Another weird thing is that if I change the println to thePlayer.getCurrentArmor(0).getUnlocalizedName()+" equipped." I get a null pointer exception on that line.  But that seems strange because the expression in the if expression evaluated without an exception.

 

Note I've tried many other variations on this check.  I've tried checking if the item itself was equals (using both == and .equals() method).  I tried comparing methods from items and itemstacks.

 

Help!

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

For Items, it is much more efficient to check identity:

ItemStack stack = thePlayer.getCurrentArmor(0);
if (stack != null && stack.getItem() == MagicBeans.bootsOfSafeFalling) {
System.out.println(stack.getDisplayName() + " equipped.");
}

 

If I may ask, where are you getting 'thePlayer' from, and in what context is your code being run (e.g. Item#onUpdate, packet, etc.) ?

Link to comment
Share on other sites

Why do you compare unlocalized names?!

And that line you said causes an exception does not exist in the snippet you posted.

 

unlocalized names was the last thing I tried.  I first tried to equate the items with:

if (thePlayer.getCurrentArmor(0) != null && thePlayer.getCurrentArmor(0).getItem() != null && thePlayer.getCurrentArmor(0).getItem() == MagicBeans.bootsOfSafeFalling);

And also using .equals

if (thePlayer.getCurrentArmor(0) != null && thePlayer.getCurrentArmor(0).getItem() != null && thePlayer.getCurrentArmor(0).getItem().equals(MagicBeans.bootsOfSafeFalling));

 

I guess I can try instanceof but I have several similar items using same class so not really cleanest way.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

For Items, it is much more efficient to check identity:

ItemStack stack = thePlayer.getCurrentArmor(0);
if (stack != null && stack.getItem() == MagicBeans.bootsOfSafeFalling) {
System.out.println(stack.getDisplayName() + " equipped.");
}

 

If I may ask, where are you getting 'thePlayer' from, and in what context is your code being run (e.g. Item#onUpdate, packet, etc.) ?

 

Yeah, I actually started with using == as i just replied to diesieben07.

 

This is in LivingFallEvent so thePlayer is from the event parameter.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

You should never need to check if an ItemStack's getItem() method returns null - if the stack itself is not null, it is guaranteed to have a valid Item.

 

Can you show your full LivingFallEvent code? (another btw, LivingFallEvent is not posted in creative mode - for that, you must also subscribe to PlayerFlyableFallEvent).

Link to comment
Share on other sites

You should never need to check if an ItemStack's getItem() method returns null - if the stack itself is not null, it is guaranteed to have a valid Item.

 

I added the additional check while debugging.  Initially I simply had

if (thePlayer.getCurrentArmor(0) != null && thePlayer.getCurrentArmor(0) == MagicBeans.bootsOfSafeFalling);

 

And to prove that my armor is not actually equipped, here is screenshot during testing just now:

http://s13.postimg.org/8r6wpp8tz/Capture4.png

 

Regarding the creative mode, yeah the way I'm testing the fall event is to go to creative and then fly up kinda high then switch game mode to 0 which causes me to drop.

 

My full method for the fall event is:

 @SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
    public void onEvent(LivingFallEvent event)
    {
    	if (!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityPlayer)
    	{
    		EntityPlayer thePlayer = (EntityPlayer) event.entityLiving;
    		if (thePlayer.getCurrentArmor(0) != null && thePlayer.getCurrentArmor(0).getItem() == MagicBeans.bootsOfSafeFalling);
    		{
    			// DEBUG
    			System.out.println("LivingFallEvent handled due to having safe falling armor equipped");
    			event.distance = 0.0F ;
    		}
    	}
        
    }

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

That's pretty bizarre.

 

What do you get if you put 'System.out.println("CurrentArmor(0): " + thePlayer.getCurrentArmor(0));' ?

 

EDIT: Just tried it myself, and it is completely inexplicable:

if (!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityPlayer)
    	{
    		EntityPlayer thePlayer = (EntityPlayer) event.entityLiving;
    		ItemStack stack = thePlayer.getCurrentArmor(0);
    		System.out.println("Stack = " + stack);
    		if (stack != null && stack.getItem() == ZSSItems.bootsHeavy);
    		{
    			// DEBUG
    			System.out.println("LivingFallEvent handled due to having safe falling armor equipped: " + stack);
    			event.distance = 0.0F ;
    		}
    	}

 

Output:

[14:59:58] [server thread/INFO] [sTDOUT]: [zeldaswordskills.handler.ZSSEntityEvents:onFall:78]: Stack = null
[14:59:58] [server thread/INFO] [sTDOUT]: [zeldaswordskills.handler.ZSSEntityEvents:onFall:82]: LivingFallEvent handled due to having safe falling armor equipped: null

Link to comment
Share on other sites

Hmm, I went back to my original code (from github, so is same as I started with exactly) but it seems to be working now for some reason!  If I have nothing in boot slot then there is no console output, if I put vanilla boots in slot there is no console output, and if I put in my boots it has console output.  So, yeah, working...

 

Maybe I overlooked something when testing originally.  I'll do a bit more testing before I consider it solved.

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

That's pretty bizarre.

 

What do you get if you put 'System.out.println("CurrentArmor(0): " + thePlayer.getCurrentArmor(0));' ?

 

EDIT: Just tried it myself, and it is completely inexplicable:

if (!event.entityLiving.worldObj.isRemote && event.entityLiving instanceof EntityPlayer)
    	{
    		EntityPlayer thePlayer = (EntityPlayer) event.entityLiving;
    		ItemStack stack = thePlayer.getCurrentArmor(0);
    		System.out.println("Stack = " + stack);
    		if (stack != null && stack.getItem() == ZSSItems.bootsHeavy);
    		{
    			// DEBUG
    			System.out.println("LivingFallEvent handled due to having safe falling armor equipped: " + stack);
    			event.distance = 0.0F ;
    		}
    	}

 

Output:

[14:59:58] [server thread/INFO] [sTDOUT]: [zeldaswordskills.handler.ZSSEntityEvents:onFall:78]: Stack = null
[14:59:58] [server thread/INFO] [sTDOUT]: [zeldaswordskills.handler.ZSSEntityEvents:onFall:82]: LivingFallEvent handled due to having safe falling armor equipped: null

 

Doh, I figured it out.  There is actually a typo in the code.  You want me to tell you what it is, or do you want the satisfaction of finding it yourself?  It is kinda a silly mistake but also the type of thing that could be dangerous if you miss it...

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I see the typo :D i had a similar problem a few weeks ago.

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

[me=diesieben07]mumbles something about braces on newlines being stupid[/me]

 

Yeah well, I've never understood the aversion to it though.  I find code much more readable if it is spaced out generally (I'll even put whitespace in my code).  I'm a touch typist so it takes me no extra time to go to next line.  In the old days, vertical screen resolution was low so it was important to compress the vertical space, and I know some people have problem with understanding code that goes more than one screen, but I find that space helps me more.  Anyway, this is one argument that has been around since dawn of languages like C and I can't find any compelling argument for either way.

 

Anyway, back to the thread topic: the error is of course that there is semicolon after the if expression thereby terminating it and letting code execution fall through to next block always.  This happened because I cut and past from another line where I had previously been assigning it to a boolean.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

[me=diesieben07]mumbles something about braces on newlines being stupid[/me]

 

I agree and so does (at least one of) my friends.

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

[me=diesieben07]mumbles something about braces on newlines being stupid[/me]

LOL!!! Nice one. Well, I DID copy / paste your code to make sure I could replicate the problem, and... <smacks forehead>.

 

Personally, I usually only use a newline before a brace in my class declarations; everywhere else they get inlined.

Link to comment
Share on other sites

CUDDLE THOSE BRACES! *Shakes a fist*

Then again, the place I had it happen had the semi-colon hiding next to the curly brace anyway.  Took a while to find. ^..^;

Fortunately I knew where the code was going wrong, so it made the problem easier to locate (specifically, I knew a loop was running forever because the content was never executing and if it did, even once, it was guaranteed to cause an exit condition).

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.