ProtocolLib

3M Downloads

Im trying to change the team's prefix in spigot with protocollib api

italianfelipe opened this issue · 2 comments

commented

Make sure you're doing the following

  • [✅ ] You're using the latest build for your server version
  • [✅ ] This isn't an issue caused by another plugin
  • [✅ ] You've checked for duplicate issues
  • [✅ ] You didn't use /reload

Hello! im doing a TNTTag plugin and im trying to set the nametag and tablist prefix of the player like this BUT its not like updating when i want to

for example when a player HITS another player

it only updates if a player leaves and rejoins
and since im doing a competitive gamemode i cant really do
p.hidePlayer
p.ShowPlayer
for everybody

i tried with protocol lib but it seems there is still something im lacking any1 can tell me?

API method(s) used
`// Create a new packet
PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);

    // Set the team name
    packet.getStrings().write(0, "TNT");

    // Set the team display name
    packet.getStrings().write(1, ChatColor.translateAlternateColorCodes('&', "&c[TNT] "));

    // Set the team prefix
    packet.getStrings().write(2, ChatColor.translateAlternateColorCodes('&', "&c[TNT] "));

    // Set the team suffix
    packet.getStrings().write(3, ChatColor.RESET + " ");

    Set<String> teamMembers = t.getEntries();

    // Set the team members
    packet.getSpecificModifier(Collection.class).write(0, teamMembers);

    // Send the packet to the player
    for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
        ProtocolLibrary.getProtocolManager().sendServerPacket(onlinePlayer, packet);
    }`

Expected behavior
Basically i need it to change the tablist and nametag of the player whenever i want
for example whenever a player gets hit

Code
`public static void getTNTTeam(Player player) {
Scoreboard sb = Bukkit.getScoreboardManager().getMainScoreboard();
String prefix = ChatColor.translateAlternateColorCodes('&', "&c[TNT] ");
Team t = sb.getTeam("TNT");
if (t == null)
t = sb.registerNewTeam("TNT");
t.setPrefix(prefix);
t.setSuffix(ChatColor.RESET + " ");
if(t.hasEntry(player.getName())) {
t.removeEntry(player.getName());
}
t.addEntry(player.getName());

    // Create a new packet
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);

    // Set the team name
    packet.getStrings().write(0, "TNT");

    // Set the team display name
    packet.getStrings().write(1, ChatColor.translateAlternateColorCodes('&', "&c[TNT] "));

    // Set the team prefix
    packet.getStrings().write(2, ChatColor.translateAlternateColorCodes('&', "&c[TNT] "));

    // Set the team suffix
    packet.getStrings().write(3, ChatColor.RESET + " ");

    Set<String> teamMembers = t.getEntries();

    // Set the team members
    packet.getSpecificModifier(Collection.class).write(0, teamMembers);

    // Send the packet to the player
    for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
        ProtocolLibrary.getProtocolManager().sendServerPacket(onlinePlayer, packet);
    }
}

public static void getNormalTeam(Player player) {
    Scoreboard sb = Bukkit.getScoreboardManager().getMainScoreboard();
    String prefix = ChatColor.translateAlternateColorCodes('&', "&7");
    Team t = sb.getTeam("Players");
    if (t == null)
        t = sb.registerNewTeam("Players");
    t.setPrefix(prefix);
    t.setSuffix(ChatColor.RESET + " ");
    if(t.hasEntry(player.getName())) {
        t.removeEntry(player.getName());
    }
    t.addEntry(player.getName());

    // Create a new packet
    PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);

    // Set the team name
    packet.getStrings().write(0, "Players");

    // Set the team display name
    packet.getStrings().write(1, ChatColor.translateAlternateColorCodes('&', "&7"));

    // Set the team prefix
    packet.getStrings().write(2, ChatColor.translateAlternateColorCodes('&', "&7"));

    // Set the team suffix
    packet.getStrings().write(3, ChatColor.RESET + " ");

    Set<String> teamMembers = t.getEntries();

    // Set the team members
    packet.getSpecificModifier(Collection.class).write(0, teamMembers);

    // Send the packet to the player
    for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
        ProtocolLibrary.getProtocolManager().sendServerPacket(onlinePlayer, packet);
    }
}`
commented

Which Minecraft Version are you targeting? It looks like you write code for MC 1.8.
If you are targeting MC 1.20 have a look on how to correctly implement packet accesses for ProtocolLib for scoreboard teams here: https://github.com/lukalt/PacketWrapper/blob/master/src/main/java/com/comphenix/packetwrapper/wrappers/play/clientbound/WrapperPlayServerScoreboardTeam.java

commented

Sorry i found out the issue i could've just updated the scoreboard of the teams without protocollib