1.8.8 Create Tab Teams but Each Player Has a Specific Prefix

Discussion in 'Spigot Plugin Development' started by Chronuak, Jul 29, 2021.

  1. Hi,
    I want to create a tablist that every player gets their rank prefix and if the player is in their team they get a suffix. Like this:
    [Admin] Chronuak [TEAM]
    And if the player is not in the team there should not be a suffix:
    [Admin] Chronuak


    However, if I work with teams all players who are in that specific team have the same prefix and the same suffix what I don't want.
    Do you have any idea how I can solve this?
     
  2. You can set their custom Tablist name with the function
    Code (Java):
    org.bukkit.entity.Player.setPlayerListName(java.lang.String);
    Therefore, you don't have to rely on the default Minecraft mechanics, if that is what you mean.

    For instance:
    Code (Java):
    player.setPlayerListName(String.format("[%s] %s [%s]", "ADMIN", player.getName(), "TEAM"));
     
  3. But then the suffix is visible to every player and not only the ones who are in the team
     
  4. Code (Java):
    Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers();

    if(onlinePlayers.size() > 1) {

        EntityPlayer ep = ((CraftPlayer)player).getHandle();

        PacketPlayOutPlayerInfo packet = new PacketPlayOutPlayerInfo();
        Field typeField = packet.getClass().getDeclaredField("a");
        typeField.setAccessible(true);
        typeField.set(packet, PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_DISPLAY_NAME);

        Field listField = packet.getClass().getDeclaredField("b");
        listField.setAccessible(true);
        List<PacketPlayOutPlayerInfo.PlayerInfoData> b = (List<PacketPlayOutPlayerInfo.PlayerInfoData>) listField.get(packet);


        onlinePlayers.forEach(target -> {
            EntityPlayer entityTarget = ((CraftPlayer)target).getHandle();
            if(player.canSee(target)) {

                String tabListName = String.format("[%s] %s", "ADMIN", target.getName());;

                if(sameTeam(player, target)) {
                    tabListName += String.format(" [%s]", "TEAM");
                }
                b.add(packet.new PlayerInfoData(entityTarget.getProfile(), entityTarget.ping, entityTarget.playerInteractManager.getGameMode(), CraftChatMessage.fromString(tabListName)));
            }
        });

        ep.playerConnection.sendPacket(packet);
    }
    Thank me later.