Search code examples
javaminecraftbukkit

Modifying playerlist scoreboard


I want to change the list that is displayed when a player presses the tab key. I have used player.setPlayerListName(ChatColor.GREEN +" new Name") to change a player name and player.setPlayerListHeaderFooter("header", "footer"); to set the header/footer displayed in this list.

Now I want to make different people see different player names in the list, this would be used to hide moderators in the tab list and show different roles in minigames.

My example: I have a minigame that contains different roles, but not everyone should be able to see them on the table list, only players who are on the same team should be able to see their team members. Let's say I have 3 teams:

  • Team1 (2 players)
  • Team2 (2 players)
  • Team3 (2 players)

Everyone in Team1 should see their team members with their assigned team name in the table list. Everyone else would only be shown in white for Team1, so they cannot see which team they are on.

The same applies to Team2 and Team3.

This is an example, there is no need to lecture the players.

I've used ProtocolLib and Packets, in general, a few times so there shouldn't be a problem with it. What should I use? Thanks for your help :D


Solution

  • To remove a player from the TAB list, you should send PacketPlayOutPlayerInfo packet, like that :

    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, entityPlayerToAdd));
    

    In your case, you will have something like that :

    EntityPlayer ep = ((CraftPlayer) p).getHandle(); // the player to hide
    PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.REMOVE_PLAYER, ep);
    for(Player all : Bukkit.getOnlinePlayers()) {
        if(all != p && !Team.isSameTeam(all, p)) {
            ((CraftPlayer) all).getHandle().playerConnection.sendPacket(packet);
        }
    });
    

    Then, if you want to re-add them in TAB list, use :

    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, entityPlayerToAdd));
    

    If you want to have a complete wait to make a very great TAB, without take lot of time, there TAB plugin.

    Then, if you want to hide completly a player (even when playing, not only tab), use this :

    To hide/show player for others, you can use Player.hidePlayer (Javadoc) like that :

    MyPlugin pl = MyPlugin.getInstance();
    for(Player all : Bukkit.getOnlinePlayers()) {
       // here you can add filter to make mod showed to admin
       all.hidePlayer(pl, all);
    }
    

    Don't forget to hide them when someone join, like that :

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
       Player p = e.getPlayer();
       for(Player vanished : getVanished()) {
          p.hidePlayer(MyPlugin.getInstance(), vanished);
       }
    }
    

    And use the same to show player with Player.showPlayer (javadoc)