Search code examples
javapluginsbukkit

How can i sort the Minecraft Bukkit Tablist with Packets


I have created a Plugin which shows the Rank of the Player and his Name in the Tablist the code looks like:

groupPlayer.setPlayerListName(SchnitzelTab.getInstance().groupHandler.getGroups().get(i).getPrefix() + groupPlayer.getName());
PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, ((EntityPlayer) ((CraftPlayer) groupPlayer).getHandle()));

But my Question is now how I can sort the Tablist?


Solution

  • As far as I know you can't sort your tablist like that. But there is a much simpler way to add prefixes to players and sort them by adding the players to teams.

    So as to do this you had to create a Scoreboard, create some teams and set their prefixes.

    public Scoreboard sb;
    public Team t_admin;
    public Team t_mod;
    public Team t_player;
    
    public void onEnable() {
        sb = Bukkit.getScoreboardManager().getNewScoreboard();
        t_admin = sb.registerNewTeam("a");
        t_mod = sb.registerNewTeam("b");
        t_player = sb.registerNewTeam("c");
        t_admin.setPrefix(groupHandler.getGroups().get(/*your int */).getPrefix());
        t_mod.setPrefix(groupHandler.getGroups().get(/*your int */).getPrefix());
        t_player.setPrefix(groupHandler.getGroups().get(/*your int */).getPrefix());
        //you can add as many as you want
    }
    

    players in the tablist now get sorted by the teamname. In this case the rank admin gets "a", mod gets "b" and player gets "c" which means that admin is over mod and mod over player.

    To add a player to a team you simply have to add do the following:

    t_player.addEntry(p.getName());
    

    Don't forget to set the players scoreboard to our created sb. Maybe you want to do this as the player joins. You final code could look somehow like this:

    public Scoreboard sb;
    public Team t_admin;
    public Team t_mod;
    public Team t_player;
    
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        sb = Bukkit.getScoreboardManager().getNewScoreboard();
        t_admin = sb.registerNewTeam("a");
        t_mod = sb.registerNewTeam("b");
        t_player = sb.registerNewTeam("c");
        t_admin.setPrefix(groupHandler.getGroups().get(/*your int */).getPrefix());
        t_mod.setPrefix(groupHandler.getGroups().get(/*your int */).getPrefix());
        t_player.setPrefix(groupHandler.getGroups().get(/*your int */).getPrefix());
        //you can add as many as you want
    }
    
    @EventHandler
    public void on(PlayerJoinEvent e) {
        Player p = e.getPlayer();
        p.setScoreboard(sb);
        int i = getTeam(p);
        switch (i) {
            case 0: //or whatever player is
                t_player.addEntry(p.getName());
                break;
            case 1:
                //for all the other groups
                break;
            default:
                break;
        }
    }