How to equal players name in if?

Discussion in 'Plugin Development' started by ruwen, Feb 16, 2012.

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

    ruwen

    I have been trying to make a specific command do different things depends on whos asking. but i cant get it accept any name.s

    my if looks like this:

    Player player = (Player) sender;
    if (player.GetDisplayName() == "myname"){
    player.sendMessage("You are so cool")
    }

    whats wrong, why do it not show the text when myname call the command?

    Hope someone can help me
     
  2. Offline

    Njol

    always use string.equals(anotherstring) for Strings (actually objects in general):
    if (player.GetDisplayName().equals("myname")){

    btw: you might want to use getName() instead of the display name, since the display name might contain colours and prefixes depending on the plugins on the server.
     
  3. Offline

    thehutch

    There is also : .equalsIgnoreCase("") , this will ignore the case of the string also.

    Another suggestion for your code would be a "instanceof" check to make sure that the commandsender is infact a player :D other you will get ClassCastExceptions throwing up and anger customers :D
     
  4. Offline

    Atomic Fusion

    Usually in Java, methods do not start with a capital. Would
    Code:
    if (player.getDisplayName() == "myname"){
    Fix the issue? But regardless, the other advice is sound.
     
  5. Offline

    ruwen

    Thanks for the help, it works perfectly
     
Thread Status:
Not open for further replies.

Share This Page