if(player.getLocation().getWorld() == world){

Discussion in 'Plugin Development' started by oran10majar, Nov 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    oran10majar

    Its doesnt work. What is the normal world name. (Its world, but its doesnt work)
     
  2. Offline

    Aperx

    Don't use ==
    That's for integers
    You have to use a string
     
  3. oran10majar The == method only works properly for primitive objects- int, float, double, char, etc.

    To compare objects, such as Worlds, you need to use the .equals() method.

    In this case:

    if (player.getLocation().getWorld().equals(world))
     
  4. Offline

    Jade

    Moved to correct section.
     
  5. Offline

    oran10majar

    I didnt understand the code, it doesnt work for me
     
  6. Offline

    Chinwe

    To check the world name is "world", use:
    if (player.getLocation().getWorld().getName().equals("world"))


    Someguyfromcrowd == compares if two objects are the same object (instead of checking if their contents are the equals using .equals()), not just for primitive data types :)
     
    Someguyfromcrowd likes this.
  7. Offline

    lycano

    Chinwe not quite correct. You need to be more precise on what == does.

    The term "same object" can be misinterpreted since new String("Hello") == new String("Hello") seem to be the same object but this will return false if checked.

    This is because it compares for the object reference and not its values.

    To be more precise: If == is used to compare two objects then it compares the object reference and not the values.

    So whenever you compare a value use equals.

    (Slightly offtopic)

    I also recommend using itemStack.getType().equals(Material.AIR) instead of getType() == Material.AIR since the first one is more fluid and better readable.

    You could use the latter since that is what equals would do for enums but as said its confusing to read == for many devs therefore should be avoided.
     
    Someguyfromcrowd likes this.
Thread Status:
Not open for further replies.

Share This Page