Solved Prefixes in Tab using scoreboards

Discussion in 'Plugin Development' started by TheFl4me, Feb 4, 2016.

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

    TheFl4me

    Currently I am just using the typical player.setDisplayName() method, but I was wondering how I can do this with socreboards so that I can then arrange the player names in the TabList.

    Example: http://prntscr.com/9z1d3c

    Any idea on how to do this?

    NOTE:

    I am not very experienced with scoreboards.
     
  2. Trying the same ;)

    Just create a simple scoreboard & create the teams / ranks.

    Code:
            //Teams
            Team admin = board.registerNewTeam("admin");
            Team user = board.registerNewTeam("user");
           
            admin.setPrefix("§c§l");
            user.setPrefix("§7§l");
    I set the prefix just to color.... You could also do §cAdmin §r
    After that..

    Code:
            for(Player pl : Bukkit.getOnlinePlayers()) {
                if(pl.isOp()) {
                    admin.addPlayer(pl);
                } else {
                    user.addPlayer(pl);
                }
            }
    You add the players online with the permission to the team ;)

    Don't forget to make an update method becuase when someone joins the old players won't have the color or prefix of the new player ;/
     
    Flaps likes this.
  3. Offline

    TheFl4me

    Thanks a lot, I will try this later.

    But do you also know how to organise the tab list like in the screenshot?

    EDIT:

    I dont totally understand what you are trying to say here.

    When a player joins will all others have a white name in his POV or will he have a white name for all others POV?

    EDIT:

    Ok everything works fine now, the only thing that I still need to find out how to do is how to organise the playernames in Tab.
     
    Last edited: Feb 5, 2016
  4. https://www.spigotmc.org/resources/tablistorganizer-beta.15713/
     
  5. Offline

    TheFl4me

    No need, I just found out how to do it without API. Its really easy, it does it automatically for you, it organizes the players by their team name (alphabetically) in the tab.

    On another note. I have now encountered another issue. Since I also have another scoreboard on the side of the display which shows you your stats etc... I can no longer set one scoreboard without removing the other.

    Is there anyway to have multiple scoreboards? 1 for teams and 1 for the sidebar? I searched google but didnt find anything I could work with. (I also dont want to use an api).

    EDIT:

    After testing around a lot I am now stuck with this: (Testing with 2 accounts)

    When account 1 joins, everythign works fine.
    As soon as account 2 joins the tags stil work normal but they both see the stats of account 2 in the sidebar.

    Code:
    public static final Scoreboard SCOREBOARD = Bukkit.getScoreboardManager().getNewScoreboard();
    Code:
    //This method is in onEnable()
        private void loadScoreboards() {
            createTeam("A-admin");
            createTeam("B-modplus");
            createTeam("C-mod");
            createTeam("D-trialmod");
            createTeam("E-builder");
            createTeam("F-youtuber");
            createTeam("G-pro");
            createTeam("H-mvp");
            createTeam("I-vip");
            createTeam("J-normal");
        }
      
        private void createTeam(String teamName) {
            Scoreboard board = SCOREBOARD;
            if(board.getTeam(teamName) != null)
                board.getTeam(teamName).unregister();
            board.registerNewTeam(teamName);
            board.getTeam(teamName).setPrefix(getPrefix(teamName.substring(2)));
        }
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            ePlayer p = ePlayer.get(e.getPlayer());
            TagListener.setTag(p);
        }
    Code:
    @SuppressWarnings("deprecation")
        public static void setTag(final ePlayer p, String tag) {
            final Scoreboard board = Admin.SCOREBOARD;
            OfflinePlayer offp = Bukkit.getOfflinePlayer(p.getUniqueId());
            for(Team team : board.getTeams()) {
                team.removePlayer(offp);
            }
            board.getTeam(Admin.getRankLetter(tag) + tag).addPlayer(offp);
            p.setChatName(Admin.getPrefix(tag) + p.getName());
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Admin.getPlugin(), new Runnable() {                          
                public void run() {   
                    p.getPlayer().setScoreboard(board);
                }
            }, 5);
        }
    This sets the sidebar scoreboard:
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            ePlayer p = ePlayer.get(e.getPlayer());
            KitPvP.getUtils().setScoreboard(p);
        }
    Code:
    public void setScoreboard(ePlayer p) {  
            Scoreboard board = Admin.SCOREBOARD;
            if(board.getObjective(DisplaySlot.SIDEBAR) != null)
                board.getObjective(DisplaySlot.SIDEBAR).unregister();
            Objective obj = board.registerNewObjective("stats", "player");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            obj.setDisplayName(ChatColor.GOLD + "Your stats");
          
            Score rank = obj.getScore(ChatColor.GREEN + "Rank:");
            rank.setScore(7);
            Score rank_value = obj.getScore(p.getRank());
            rank_value.setScore(6);
          
            Score kit = obj.getScore(ChatColor.GREEN + "Kit:");
            kit.setScore(5);
            Score kit_value = obj.getScore(ChatColor.WHITE + KitManager.getPlayerKit(p));
            kit_value.setScore(4);
          
            Score kills = obj.getScore(ChatColor.GREEN + "Kills:");
            kills.setScore(3);
            Score kills_value = obj.getScore(ChatColor.WHITE + Integer.toString(p.getKills()) + " (" + Integer.toString(p.getKillStreak()) + ")");
            kills_value.setScore(2);
          
            Score deaths = obj.getScore(ChatColor.GREEN + "Deaths:");
            deaths.setScore(1);
            Score deaths_value = obj.getScore(ChatColor.WHITE + Integer.toString(p.getDeaths()));
            deaths_value.setScore(0);
          
            p.getPlayer().setScoreboard(board);
        }
     
    Last edited: Feb 5, 2016
  6. If you want to set a score you need to create a scoreboard for every player or use packets ;)
     
  7. Offline

    TheFl4me

    How would I set a board for every player?

    Because I tried this in the setScoreboard(ePlayer p) void

    Code:
    Scoreboard board = Bukkit.getScoreboardManager().getNewScoreBoard();
    and It still had the exact same effect.
     
  8. Offline

    Konato_K

    @TheFl4me You need to call #getNewScoreboard for every player, the downside of this is that by having a different scoreboard, they aren't also count in teams and you would have to make the teams on every scoreboard so they appear properly to every player


    Another way is to have an empty board and fill it with packets just for the players (so the scoreboard is really empty)
     
  9. Offline

    TheFl4me

    This is infact how I was doing it before, but the issue was/is that these scoreboards are in 2 different plugins (none changeable) and since both have .setScoreboard(board); at the end it always removed the other scoreboard.
     
  10. Offline

    Flaps

    I made a method for someone needing help on Spigot awhile ago, here, try this:

    Code:
    public void setNick(Player player, String nick) {
    for (Player online : Bukkit.getOnlinePlayers()) {
        Scoreboard scoreboard = online.getScoreboard();
        if (scoreboard == Bukkit.getScoreboardManager().getMainScoreboard()) {
          scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
        }
        Team team = scoreboard.getTeam(player.getName());
        if (team == null) {
            team = scoreboard.registerNewTeam(player.getName());
        }
        team.setPrefix(nick);
        team.addEntry(player.getName());
        online.setScoreboard(scoreboard);
      }
    }
    You'll probably have to modify it a bit to what you need to do, but the rough idea is there.
     
  11. Offline

    TheFl4me

    Thank you but my issue is no longer how to set prefixes using scoreboards, but how to be able to set 2 scoreboards. (One for the teams and one for the stats on the sidebar)
     
  12. Offline

    Konato_K

    @TheFl4me Players can't have two scoreboards, you'll have to work with 1 for each player or fake the scoreboard
     
  13. Offline

    Flaps

    You can create the sidebar and then call my method to set a nickname/prefix. It should still work as long as you're doing 1 scoreboard per player.
     
Thread Status:
Not open for further replies.

Share This Page