Jump to content

Fetching my player name can't match with string variable


Recommended Posts

Posted

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?

Posted (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 by Explv
  • Like 4
Posted
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

  • Like 1
  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...