1

Working on a plugin in Java. everything works. I come to this one if statement and it just fails. I've tried changing the values from int to string and vice versa, it simply won't budge. I logged the coordinates. I try to compare them as strings.
Code:

String locationx = plugin.getConfig().getString("locationx");
String eventlocationx = Integer.toString(event.getClickedBlock().getX());

String locationy = plugin.getConfig().getString("locationy");
String eventlocationy = Integer.toString(event.getClickedBlock().getY());

String locationz = plugin.getConfig().getString("locationz");
String eventlocationz = Integer.toString(event.getClickedBlock().getZ());

if (locationx == eventlocationx && locationy == eventlocationy && locationz == eventlocationz)
{
    System.out.println("SUCCESS: Passed coordinate check on the right click.");
    return true;
}
else
{
    System.out.println(locationx);
    System.out.println(eventlocationx);

    System.out.println(locationy);
    System.out.println(eventlocationy);

    System.out.println(locationz);
    System.out.println(eventlocationz);

    System.out.println("ERROR: Failed on the right click coordinate check.");
    return false;
}


config:

locationx: '-226'

locationy: '64'

locationz: '266'

console log:

-226
[11:31:47 INFO]: -226
[11:31:47 INFO]: 64
[11:31:47 INFO]: 64
[11:31:47 INFO]: 266
[11:31:47 INFO]: 266
[11:31:47 INFO]: ERROR: Failed on the right click coordinate check.

I hope anyone can help me out.

woepertjes
  • 13
  • 2
  • It would be better for you to convert the strings you get from the config to integer and then compare, than what you are trying to do now, which is convert integer to string and compare. Your string comparison, as the duplicate says, is wrong, but it would be better to compare as ints to begin with. – RealSkeptic Apr 15 '19 at 10:10

1 Answers1

1

You cant compare 2 strings with == since strings are classes, == checks for the same instance, you have to use String.equals(OtherString) or covert them to ints like this:

int locationx = Integer.parsInt(plugin.getConfig().getString("locationx"));
int eventlocationx = event.getClickedBlock().getX();

int locationy = Integer.parsInt(plugin.getConfig().getString("locationy"));
int eventlocationy = event.getClickedBlock().getY();

int locationz = Integer.parsInt(plugin.getConfig().getString("locationz"));
int eventlocationz = event.getClickedBlock().getZ();

if (locationx == eventlocationx && locationy == eventlocationy && locationz == 
eventlocationz)
{
System.out.println("SUCCESS: Passed coordinate check on the right click.");
 return true;
}
else
{
System.out.println(locationx);
System.out.println(eventlocationx);

System.out.println(locationy);
System.out.println(eventlocationy);

System.out.println(locationz);
System.out.println(eventlocationz);

System.out.println("ERROR: Failed on the right click coordinate check.");
return false;
}
Starmixcraft
  • 389
  • 1
  • 14