If-Check doesn't work

Discussion in 'Plugin Development' started by ThrustLP, May 24, 2016.

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

    ThrustLP

    Hey guys! I've coded a plugin that changes the tablistname of a player. If it is changed by a pex-command (premission) it should also be changed in the tablist. So I check if it got changed in a PlayerMoveEvent:

    Code:
    @EventHandler
        public void onMv(PlayerMoveEvent e){
        Player p = e.getPlayer();
        plugin.tablist(p, plugin.theader, plugin.tfooter);
        //plugin.reloadConfig();
           
            for(Map.Entry<String, String> grps : plugin.tabprefixes.entrySet()){
                String perm = "cunchat.tablist." + grps.getKey();
               
                if(p.hasPermission(perm)){
                    String name = p.getDisplayName();
                    if(p.getDisplayName().length() > 16-(grps.getValue().length())){
                        name = name.substring(0, 16-(grps.getValue().length()));
                        String l = grps.getValue() + p.getDisplayName();
                       
                        if(p.getDisplayName() != l){
                        p.setPlayerListName(grps.getValue() + name);
                        }
                       
                       
                       
                        break;
                       
                    }else{
                       
                        String l = grps.getValue() + p.getDisplayName();
                       
    
                        if(p.getDisplayName() != l){
                            p.setPlayerListName(grps.getValue() + p.getDisplayName());
                        }
                       
                        break;
                    }
                   
                   
                   
                }
               
            }
            //TagAPI.refreshPlayer(p);
           
           
        }
    The problem is: For some reason if(p.getDisplayName() != l) doesn't work (p.setPlayerListName(grps.getValue() + p.getDisplayName()); is used every time!

    Why?

    Thank you!
     
  2. Offline

    DoggyCode™

    l is a string, surround it with quotation marks: "l".

    And use equals(String#) or equalsIgnoreCase(String#) when working with strings.

    Code:
    if (p.getDisplayName().equalsIgnoreCase("l")) {
      //do stuff
    }
    And please learn java
     
  3. Offline

    Zombie_Striker

    @DoggyCode™
    First, if you want to tell someone to learn Java, you should show them where to learn Java:
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/

    Also, he wants to check to make sure the string is not equal to the variable 'l'.

    Please do not post broken code or code that does not do what the OP wanted. If you do, the OP will not understand why the code will not work.


    @ThrustLP
    You cannot compare Strings using '=='. You use "==" when you want to compare memory space. Use '.equals' to compare values.
     
Thread Status:
Not open for further replies.

Share This Page