ProtocolLib

3M Downloads

How to update display name

abcdef-007 opened this issue ยท 1 comments

commented

Make sure you're doing the following

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

Describe the question
I am trying to change the user's display name but I am getting an error: FieldAccessException: Field index 0 is out of bounds for length 0

Expected behavior
Not getting errors

Code

PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
packet.getPlayerInfoActions().write(0, EnumSet.of(EnumWrappers.PlayerInfoAction.UPDATE_DISPLAY_NAME));

WrappedGameProfile wgp = new WrappedGameProfile(uuid, orgName);
packet.getPlayerInfoDataLists().write(1,
    Collections.singletonList(
        new PlayerInfoData(wgp, 50, NativeGameMode.ADVENTURE, WrappedChatComponent.fromLegacyText(newName))
    )
);

ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);

Additional context
Using server version 1.19 and plugin version 5.1.0

commented

Hi,

the code you sent is compatible with 1.19.3 or later. If you are using 1.19, you need to change your code:

PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_DISPLAY_NAME);

WrappedGameProfile wgp = new WrappedGameProfile(uuid, orgName);
packet.getPlayerInfoDataLists().write(0,
    Collections.singletonList(
        new PlayerInfoData(wgp, 50, NativeGameMode.ADVENTURE, WrappedChatComponent.fromLegacyText(newName))
    )
);

ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);