p.getName().equals("") doesn't work

Discussion in 'Plugin Development' started by WetriZ, Oct 25, 2021.

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

    WetriZ

    I am trying to set mine and my friends prefixes to DEVELOPER, OWNER and HELPER and for others that are opped just A-TEAM. For some reason, we all have A-TEAM prefixes. It seems that p.getName().equals() are false but why? I also tried equalsIgnoreCase() method... Same result. I also tried p.equals(Bukkit.getPlayer(""))... same result. I even compared UUIDs. Nothing worked. I am not sure why is this happening. I did before some jobs with Teams and I have to admit that I dont know how to properly work with Scoreboards and teams. But it worked so I didn't worry about it. I still think that this cannot affect my problem but just in case I have attached what I did with scoreboards and Teams. I also removed some code that is 100% unnecesary in this issue. Any help would be appreciated.

    my code:

    Code:
        Scoreboard scoreboard = Bukkit.getServer().getScoreboardManager().getMainScoreboard();
        Team team = null;
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
    
            Player p = e.getPlayer();
    
            if(p.isOp()){
    
                String name = p.getName();
               
                if(p.getName().equals("Finchos")){
                    p.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD+ "DEVELOPER " + ChatColor.RESET + name);
                    p.setPlayerListName(ChatColor.AQUA + "" + ChatColor.BOLD+ "DEVELOPER " + ChatColor.RESET + name);
    
                    if(scoreboard.getTeam(p.getName()) == null){
                        team = scoreboard.registerNewTeam(p.getName());
                    }
                    else{
                        team = scoreboard.getTeam(p.getName());
                    }
    
                    team.setPrefix("§b§lDEVELOPER §r");
                    team.addPlayer(p);
                }
    
                if(name.equalsIgnoreCase("Wartyx")){
                    p.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD+ "OWNER " + ChatColor.RESET + name);
                    p.setPlayerListName(ChatColor.DARK_RED + "" + ChatColor.BOLD+ "OWNER " + ChatColor.RESET + name);
    
                    if(scoreboard.getTeam(p.getName()) == null){
                        team = scoreboard.registerNewTeam(p.getName());
                    }
                    else{
                        team = scoreboard.getTeam(p.getName());
                    }
    
                    team.setPrefix("§4§lOWNER §r");
                    team.addPlayer(p);
                }
    
                if(name.equals("KureciiRizek")){
                    p.setDisplayName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD+ "BUILDER " + ChatColor.RESET + name);
                    p.setPlayerListName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD+ "BUILDER " + ChatColor.RESET + name);
    
                    if(scoreboard.getTeam(p.getName()) == null){
                        team = scoreboard.registerNewTeam(p.getName());
                    }
                    else{
                        team = scoreboard.getTeam(p.getName());
                    }
    
                    team.setPrefix("§d§lBUILDER §r");
                    team.addPlayer(p);
                }
            else{
                    p.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD+ "A-TEAM " + ChatColor.RESET + name);
                    p.setPlayerListName(ChatColor.AQUA + "" + ChatColor.BOLD+ "A-TEAM " + ChatColor.RESET + name);
    
                    if(scoreboard.getTeam(p.getName()) == null){
                        team = scoreboard.registerNewTeam(p.getName());
                    }
                    else{
                        team = scoreboard.getTeam(p.getName());
                    }
    
                    team.setPrefix("§b§lA-TEAM §r");
                    team.addPlayer(p);
            }
    
    
            }
            else{
                if(scoreboard.getTeam(p.getName()) == null){
                    team = scoreboard.registerNewTeam(p.getName());
                }
                else{
                    team = scoreboard.getTeam(p.getName());
                }
            }
        }
       
     
  2. Online

    timtower Administrator Administrator Moderator

    @WetriZ Considered printing the names?
     
  3. Offline

    WetriZ

    Yes I did. It seems that it should work. However, I have some news. My friend that should have BUILDER prefix actually has his prefix. I have no idea whats wrong with the upper ones if{}
     
    Last edited: Oct 26, 2021
  4. Offline

    sugonmad

    You forgot to put an else after each if-case. It changes the players' prefixes to what they should be, but then if their name isnt Kurecii it just puts them back to "A-Team" ( That's why the builders prefix was right the entire time). Just put an else behind every if-case that compares the name and it should work.

    General small tip: You seem to not be consistent with your code, like e.g. with the name checking. Try to make everything more uniform or else you might run into issues when your plugins get more complicated.
    Also you don't need to write how the name should be affected every time. You could write a method that generally changes the display name, playerlist name and sets them in a new team, and then give that method the prefix and name as a String parameter.

    Hope I could help, you probably already solved this one!
     
Thread Status:
Not open for further replies.

Share This Page