How to display offline player name in a team scoreboard?

Discussion in 'Plugin Development' started by darknoneee, Apr 26, 2023.

  1. Offline

    darknoneee

    Hello everyone,

    I'm doing a team system and it have a scoreboard that displays all the teams members, but, when a player logs out, it will return a null value, so i can't display the player name in the scoreboard. How could i do it?

    Sorry for my bad english.
     
  2. Offline

    mehboss

    Hello. You can simply use OfflinePlayer instead of Player for when they are offline. OfflinePlayer#getName will return their name. When online, you can convert OfflinePlayer to Player using OfflinePlayer#getPlayer. Hope this helped!

    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/OfflinePlayer.html
     
  3. Offline

    darknoneee

    I tried it, but still not working.

    Here's my code:


    Code:
                    Objective.setDisplayName(Team.getTeam(players) + " (HP%)");
                    Objective.setDisplaySlot(DisplaySlot.SIDEBAR);
                    for (UUID team : Team.getMembers()) {
                        if (Team.members.get(team).equalsIgnoreCase(Team.getTeam(players))) {
                            OfflinePlayer mates = Bukkit.getPlayer(team);
                            if (mates!=null) {
                                double hp = ((CraftPlayer) mates).getHealth() / 2.0D * 10.0D;
                                Score Var = Objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + mates.getName()));
                                Var.setScore((int) hp);
                            } else {
                                Score Var = Objective.getScore(Bukkit.getOfflinePlayer("" + mates.getName()));
                                Var.setScore(-1);
                            }
    
                        }
                    }


    }

    }​
     
  4. Offline

    mehboss

    Hello again,

    The reason it is not working is because you are checking if "mates" is NULL, and then you are getting the Player by the Null variable.. getPlayer# returns null if they are offline.
    This should be:
    Code:
    Objective.getScore(Bukkit.getOfflinePlayer(team).getName());
     
    Last edited: Apr 27, 2023
    darknoneee likes this.
  5. Offline

    darknoneee

    It works! Thank you so much :D
     
    mehboss likes this.

Share This Page