ProtocolLib

3M Downloads

Player's nametag not updating in-game

mybe opened this issue ยท 0 comments

commented

Make sure you're doing the following

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

Describe the question
Hello, I am inquiring to see what is wrong with my code in regards to changing a player's nametag.

API method(s) used
Creating packets (Entity Metadata), sending packets, etc.

Expected behavior
I need my method to change the nametag above a player's head. After the method is run, the nametag does not change. No errors are printed to console. To ensure the method is run I have a statement that broadcasts the player attempting to receive the packet:
Bukkit.broadcastMessage("Sending packet to: " + onlinePlayer.getName());

Code
Here is the code of my method:

public void updateNametag(Player pl, String nametag) {
        WrappedDataWatcher dataWatcher = WrappedDataWatcher.getEntityWatcher(pl).deepClone();
        WrappedDataWatcher.Serializer chatSerializer = WrappedDataWatcher.Registry.getChatComponentSerializer(true);
        WrappedDataWatcher.WrappedDataWatcherObject watcherObject = new WrappedDataWatcher.WrappedDataWatcherObject(2, chatSerializer);
        Optional<Object> optional = Optional.of(WrappedChatComponent.fromChatMessage(nametag)[0].getHandle());
        dataWatcher.setObject(watcherObject, optional);
        dataWatcher.setObject(3, true);
        PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA);
        packet.getWatchableCollectionModifier().write(0, dataWatcher.getWatchableObjects());
        packet.getIntegers().write(0, pl.getEntityId());

        for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
            try {
                Bukkit.broadcastMessage("Sending packet to: " + onlinePlayer.getName());
                ProtocolLibrary.getProtocolManager().sendServerPacket(onlinePlayer, packet);
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
                return;
            }
        }
    }

And here is how I am calling my method:
updateNametag(pl, "test123"); where pl is a Player.

Additional context
I am on the latest version of ProtocolLib. My plugin is made for 1.17 and my server is on 1.17.1. Thanks in advance!