ProtocolLib

3M Downloads

Unable to set value of field private final error when send PLAYER_INFO

Avzti opened this issue ยท 1 comments

commented

Hello, i want to set displayname at tablist for player(s) but i got this error:

[15:53:51 INFO]: UUID of player oneyz_ is 80c59900-77b6-4bf0-8eba-60f13c8b5399
[15:53:51 INFO]: oneyz_ joined the game
[15:53:51 ERROR]: [TabOZ] Unhandled exception occurred in onPacketSending(PacketEvent) for TabOZ
java.lang.IllegalStateException: Unable to set value of field private final java.util.EnumSet net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket.a

ProtocolLib dependency in maven: 5.1.0
ProtocolLib plugin version on server: 5.2.0 (also tested on 5.1.0 for 1.20.1)
Server version: 1.20.1

code:

    `protocolManager.addPacketListener(new PacketAdapter(this, PacketType.Play.Server.PLAYER_INFO) {
        @Override
        public void onPacketSending(PacketEvent e) {
            if (e.getPacketType() == PacketType.Play.Server.PLAYER_INFO) {
                PacketContainer packet = e.getPacket();
                List<PlayerInfoData> playerInfoDataList = packet.getPlayerInfoDataLists().read(0);


                List<PlayerInfoData> modifiedLists = playerInfoDataList.stream()
                        .filter(data -> data != null)
                        .map(data -> {
                            WrappedGameProfile profile = data.getProfile();
                            Player player = getServer().getPlayer(profile.getUUID());

                            if (player != null && player.getName().equals("oneyz_")) {
                                WrappedGameProfile newProfile = new WrappedGameProfile(profile.getUUID(), "newName");
                                return new PlayerInfoData(newProfile, data.getLatency(), data.getGameMode(), data.getDisplayName());
                            }
                            return data;
                        })
                        .collect(Collectors.toList());

                packet.getPlayerInfoDataLists().write(0, modifiedLists);
            }
        }
    });
}`
commented

I don't know why, but you have to write to field index 1. See example code below. This code has been tested on 1.20.6 and 1.21.

var packet = newPacket(PacketType.Play.Server.PLAYER_INFO);
packet.getPlayerInfoActions().write(0, EnumSet.of(EnumWrappers.PlayerInfoAction.ADD_PLAYER,
        EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE, EnumWrappers.PlayerInfoAction.UPDATE_LATENCY,
        EnumWrappers.PlayerInfoAction.UPDATE_LISTED));

packet.getPlayerInfoDataLists().write(1,
        List.of(new PlayerInfoData(this.profile.getId(), 1, false,
                EnumWrappers.NativeGameMode.SURVIVAL, WrappedGameProfile.fromHandle(this.profile), null)));

See also: #2760 (comment)