homes196 Posted September 25, 2018 Share Posted September 25, 2018 I've defined my player's display name in a String variable (changed actual player's name for this example): String MULE_NAME = "displayname"; And a method that checks their name: public void getJob() { if (getPlayers().myPlayer().getName() == MULE_NAME) { CURRENT_JOB = "Mule"; } else { CURRENT_JOB = "Other job"; } } It ALWAYS returns as "Other job". I've double checked that I've entered the right display name into MULE_NAME and even had the log output the variable and my player's name every loop. They match in the log yet apparently are not the same. What gives? Quote Link to comment Share on other sites More sharing options...
Explv Posted September 25, 2018 Share Posted September 25, 2018 (edited) 9 hours ago, homes196 said: I've defined my player's display name in a String variable (changed actual player's name for this example): String MULE_NAME = "displayname"; And a method that checks their name: public void getJob() { if (getPlayers().myPlayer().getName() == MULE_NAME) { CURRENT_JOB = "Mule"; } else { CURRENT_JOB = "Other job"; } } It ALWAYS returns as "Other job". I've double checked that I've entered the right display name into MULE_NAME and even had the log output the variable and my player's name every loop. They match in the log yet apparently are not the same. What gives? First of all it should be: myPlayer().getName() Not: getPlayers().myPlayer().getName() Secondly you don't compare Strings using ==. == Checks that the references are equal, not the values. To check the values of Strings for equality you should use .equals(): myPlayer().getName().equals(MULE_NAME) Example: String string1 = new String("hi"); String string2 = new String("hi"); string1 == string2 // false, string1 is a different instance to string2 string1 == string1 // true, string1 is the same instance as string1 string1.equals(string2) // true, the value of string1 is equal to the value of string2 If there is a space in the username you _may_ need to do this to replace non-breaking spaces: (I cant remember if you actually need to do this or not in this case) myPlayer().getName().replace('\u00A0', ' ').equals(MULE_NAME) Also you have named the mule name variable as MULE_NAME, yet the value is not a constant. It should be: private static final String MULE_NAME = "displayname"; This will not affect functionality, but it is best practice. Finally you say that you are using this getJob() function every loop. I would recommend you only call it once in onStart() because the username isn't going to change throughout the script's lifetime. (Unless the script changes accounts) Edited September 26, 2018 by Explv 4 Quote Link to comment Share on other sites More sharing options...
homes196 Posted September 25, 2018 Author Share Posted September 25, 2018 @Explv Thank you for the detailed response, using myPlayer().getName().equals fixed my issue. I mix up other practices from other programming languages (Lua) sometimes and I'm not that great at Java yet. 1 Quote Link to comment Share on other sites More sharing options...
Alek Posted September 25, 2018 Share Posted September 25, 2018 1 hour ago, homes196 said: @Explv Thank you for the detailed response, using myPlayer().getName().equals fixed my issue. I mix up other practices from other programming languages (Lua) sometimes and I'm not that great at Java yet. Scripting languages teach bad convention practices. 1 Quote Link to comment Share on other sites More sharing options...
FrostBug Posted September 25, 2018 Share Posted September 25, 2018 9 hours ago, Explv said: String string1 = "hi"; String string2 = "hi"; string1 == string2 // false, string1 is a different instance to string2 string1 == string1 // true, string1 is the same instance as string1 string1.equals(string2) // true, the value of string1 is equal to the value of string2 Actually, string1 == string2 IS true, but only in case of Strings, because of Javas String constant pool 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted September 26, 2018 Share Posted September 26, 2018 (edited) 12 minutes ago, FrostBug said: Actually, string1 == string2 IS true, but only in case of Strings, because of Javas String constant pool Yeah you right, thanks for the correction, I should have written new String() instead. Will updaterino Edited September 26, 2018 by Explv 1 Quote Link to comment Share on other sites More sharing options...
extatus Posted February 10, 2019 Share Posted February 10, 2019 On 9/25/2018 at 5:29 PM, Explv said: myPlayer().getName().replace('\u00A0', ' ').equals(MULE_NAME) thx Quote Link to comment Share on other sites More sharing options...