I'm currently working on a plugin for my own Spigot server (1.8.x) and I'm encountering an issue with PacketPlayOutPlayerInfo.
I'm trying to make the player name (above the head) display a different color depending on the player who is seeing it. Basically, I've tested different methods to achieve this, but none have worked.
The best method I've tried, in my opinion, is the following:
(Into a PlayerJoinEvent Listener)
EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
GameProfile playerProfile = entityPlayer.getProfile();
Field nameField = playerProfile.getClass().getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(playerProfile, ChatColor.RED + player.getName());
PlayerConnection connection = ((CraftPlayer) Bukkit.getPlayer("HeartZone")).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER, entityPlayer));
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, entityPlayer));
It partly works because I see my name in red, but every online player can see my name in red, and that's not what I'm looking for.
I've tried removing the following code:
PlayerConnection connection = ((CraftPlayer) Bukkit.getPlayer("HeartZone")).getHandle().playerConnection;
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.REMOVE_PLAYER, entityPlayer));
connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, entityPlayer));
And the result is the same. So, is PacketPlayOutPlayerInfo really working and useful?
Finally, I tried adding:
nameField.set(playerProfile, ChatColor.stripColor(player.getName()));
But the color is normal (reseted) for all players seeing me, as if nameField.set(GameProfile, String)
was calling packets by itself.
Edit : I've been searching and testing code for 2 days after publishing, please be kind.
The issue here comes from you are changing the player name, but the original player name. So everyone will got this new information.
The idea would be to make your own GameProfile
object, or custom player data as showed in this post :
PacketPlayOutPlayerInfo info = new PacketPlayOutPlayerInfo(); setInfo(info, EnumPlayerInfoAction.ADD_PLAYER, info.new PlayerInfoData(prof, entity.ping, entity.playerInteractManager.getGameMode(), CraftChatMessage.fromString(name)[0]));