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:
<code> (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));
<code> (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));
</code>
(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:
<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));
<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));
</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:
<code> nameField.set(playerProfile, ChatColor.stripColor(player.getName()));
<code> nameField.set(playerProfile, ChatColor.stripColor(player.getName()));
</code>
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.
Thanks in advance, and feel free to ask me as many questions as necessary.
Neyticom