Posted August 3, 201411 yr Hello everyone, I've made a block which should do something when right-clicked on if the player currently has a diamond in their hand. If they don't, it should do nothing. However, the if statement always says that there is no diamond in the player's hand, even if there is. Here is my code: public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9) { if (player.getHeldItem()!=null) { String theitem = player.getHeldItem().getUnlocalizedName().substring(5); if (theitem == "diamond") { System.out.println("Is a diamond!"); } else { System.out.println("Not a diamond"); } } return true; } In the above code, Not a diamond is always printed. I have tried putting "diamond" in a variable and then comparing the two variables, I used System.out.println(player.getHeldItem().getUnlocalizedName().substring(5)); to output whatever item the player had in their hand and it said "diamond" in the console. So does that mean that "diamond" != "diamond"? I even put this in before the if statement: System.out.println(player.getHeldItem().getUnlocalizedName().substring(5)=="diamond"); It always returned false. So, could someone please help / tell me what I'm doing wrong? Thanks in advance, -Toastrackenigma
August 3, 201411 yr Why grab the name? just use the item itself like if(player.getHeldItem().equals(new ItemStack(Items.diamond))) Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
August 3, 201411 yr Author Thanks for your reply, however that a) gives me an error (diamond cannot be resolved or is not a field) and b) it still just returns "Not a diamond".
August 3, 201411 yr have you tried outputting the name to the console so you know what value to use? String theitem = player.getHeldItem().getUnlocalizedName().substring(5); System.out.println(theitem);? also remember you are using "substring(5)" so you probably want if (theitem.toLowerCase().equals("diamo")) Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
August 3, 201411 yr Author I used System.out.println(player.getHeldItem().getUnlocalizedName().substring(5)); to output whatever item the player had in their hand and it said "diamond" in the console. So does that mean that "diamond" != "diamond"? I do know what value to use Also, toLowerCase() doesn't seem to change anything
August 3, 201411 yr in java, you cannot use == operator for strings, it checks for references, not the hash code of the string. Instead, use: theitem.equals("diamond"); also remember you are using "substring(5)" so you probably want if (theitem.toLowerCase().equals("diamo")) No! substring(5) gives you the string starting from index 5. he used that because getUnlocalizedName prefixes "tile." to blocks' names and "item." to items' names
August 3, 201411 yr No! substring(5) gives you the string starting from index 5. he used that because getUnlocalizedName prefixes "tile." to blocks' names and "item." to items' names Ah right derp, I haven't used subString for a long time now so forgot how it actually works. My bad. And the reason why I tossed .toLowerCase() in is because some of my custom items I use camel case like itemRelic and if I wanted to compare "itemRelic" to "itemrelic" it won't work (or would it?) so I just make them both lowercase to be safe. Currently updating my Mod to 1.10.2 https://bitbucket.org/hugo_the_dwarf/riseoftristram2016/src?at=master
August 3, 201411 yr And the reason why I tossed .toLowerCase() in is because some of my custom items I use camel case like itemRelic and if I wanted to compare "itemRelic" to "itemrelic" it won't work (or would it?) so I just make them both lowercase to be safe. You can use .equalsIgnoreCase if you're worried about cases
August 3, 201411 yr Or you could try the proper method if(player.getHeldItem().getItem() == Items.diamond) { //do stuff here } Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods
August 3, 201411 yr Author Thanks for all of your help, it works now! That's interesting about == not working in Java, I'm better at python / javascript so hence why I used it as an operator Thanks again, -Toastrackenigma
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.