Feel free to update and expand upon this.
- ProtocolLib
package your.package.here;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Collections;
import java.util.UUID;
public class Nametag {
PacketContainer packet;
public Nametag(Player player) {
this.packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_TEAM);
String name = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
this.packet.getIntegers().write(1, 0);
this.packet.getStrings().write(0, name);
this.packet.getChatComponents().write(0, WrappedChatComponent.fromText(player.toString()));
this.packet.getSpecificModifier(Collection.class).write(0, Collections.singletonList(player.getName()));
}
public Nametag setPrefix(String prefix) {
this.packet.getChatComponents().write(1, WrappedChatComponent.fromText(ChatColor.translateAlternateColorCodes('&', prefix) + " "));
return this;
}
public Nametag setSuffix(String suffix) {
this.packet.getChatComponents().write(2, WrappedChatComponent.fromText(" " + ChatColor.translateAlternateColorCodes('&', suffix)));
return this;
}
public void build() {
for (Player p : Bukkit.getOnlinePlayers()) {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet);
} catch (InvocationTargetException e) {
throw new RuntimeException("Cannot send packet " + packet, e);
}
}
}
}
new Nametag(player).setPrefix("&6Test").setSuffix("&aTest").build();
// 1.13+
new Nametag(player).setPrefix("&6Testttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt").setSuffix("&aTestttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt").build();
You can, just for sake of mind.
Team priorities work under the beggining identifiers:
00
01
02
03
11
15
20
00 shows up first, consider the priorities as if they were indexes in the tablist!
This gist is only lacking how to properly handle the team for the client too, so it removes the team later if player leaves etc