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();
org.bukkit.command.CommandException: Unhandled exception executing command 'k' in plugin Kingdom v1.3-SNAPSHOT
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[server.jar:git-Spigot-642f6d2-57ab4cf]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[server.jar:git-Spigot-642f6d2-57ab4cf]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-642f6d2-57ab4cf]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [server.jar:git-Spigot-642f6d2-57ab4cf]
at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [server.jar:git-Spigot-642f6d2-57ab4cf]
at java.lang.Thread.run(Thread.java:832) [?:?]
Caused by: com.comphenix.protocol.reflect.FieldAccessException: No field with type net.minecraft.server.v1_12_R1.IChatBaseComponent exists in class PacketPlayOutScoreboardTeam.
at com.comphenix.protocol.reflect.StructureModifier.writeInternal(StructureModifier.java:348) ~[?:?]
at com.comphenix.protocol.reflect.StructureModifier.write(StructureModifier.java:328) ~[?:?]
at nl.snowpix.kingdom.Tab.NameTag.(NameTag.java:24) ~[?:?]
at nl.snowpix.kingdom.commands.KingdomCommand.onCommand(KingdomCommand.java:784) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Spigot-642f6d2-57ab4cf]
... 15 more
This error!