ProtocolLib

3M Downloads

Failed to encode packet

xeRicker opened this issue ยท 2 comments

commented

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

Describe the question
I'm encountering an issue when trying to set an entity's custom name using ProtocolLib. Despite following what seems to be the correct procedure, I consistently receive a "failed to encode packet 'clientbound/minecraft'" error. I'd greatly appreciate any guidance on resolving this issue or identifying what might be causing it. I'm using the latest version of Paper 1.21.

API method(s) used
ProtocolLibrary.getProtocolManager().sendServerPacket()
PacketContainer creation and manipulation
WrappedDataWatcher for entity metadata modification

Expected behavior
The code should successfully create a packet to set the custom name of an entity and send it to the player without any encoding errors.

Here's the relevant code I'm using:

    public static void sendCustomName(Player player, Entity entity, String name) {
        PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA);
        packet.getIntegers().write(0, entity.getEntityId());
        WrappedDataWatcher watcher = new WrappedDataWatcher();

        Component nameComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(name);
        Optional<Component> optionalName = Optional.of(nameComponent);
        WrappedDataWatcher.WrappedDataWatcherObject nameObject = new WrappedDataWatcher.WrappedDataWatcherObject(2, WrappedDataWatcher.Registry.getChatComponentSerializer(true));
        watcher.setObject(nameObject, optionalName);

        WrappedDataWatcher.WrappedDataWatcherObject visibilityObject = new WrappedDataWatcher.WrappedDataWatcherObject(3, WrappedDataWatcher.Registry.get(Boolean.class));
        watcher.setObject(visibilityObject, true);

        packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
        ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
    }
commented

Contribution
I also have a similar problem: I am sending a ScoreboardTeamPacket through ProtocolLib but keep getting a failed-to-encode packet error.

Details
X - Switched from MC Version 1.20.2 to 1.20.6
X - I'm using the latest Dev Build version of ProtocolLib
x - The code I'm using to send the packet works in 1.20.2 but not 1.20.6

Error Message
https://pastebin.com/adEkZi1w

Code
`
//Set mode to create team
getPacketContainer().getModifier().write(0, action);

    // SETS UNiQUE NAME FOR TEAM
    getPacketContainer().getStrings().write(0, scoreboardName);

    var teamDisplayName = WrappedChatComponent.fromText("");//Get team display name

    //Create optional internal structure
    var optStruct = getPacketContainer().getOptionalStructures().read(0);

    //Check if is present and write data
    if (optStruct.isPresent()) { //IS ALWAYS PRESENT

        //Get internal structures
        var struct = optStruct.get();

        //Write data for internal structure
        struct.getChatComponents().write(0, teamDisplayName); //Sets team display name || CRUCIAL for team to work
        struct.getIntegers().write(0, 1); //Sets amount of entities the team can accept || Only the corpse
        struct.getStrings().write(0, "never"); //Entity name tag visibility || NEVER
        struct.getStrings().write(1, "never"); //Entity Collision rule || NEVER

        //Write internal structure to packet
        getPacketContainer().getOptionalStructures().write(0, Optional.of(struct));

    }

    //Add entity to team
    getPacketContainer().getModifier().write(2, Lists.newArrayList(entityName));

`

commented

Managed to fix it. Apparently, ENTITY_METADATA takes in an ArrayList of WrappedDataValue.

    public static void sendCustomName(Player player, Entity target, String name) {
        PacketContainer packet =  ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.ENTITY_METADATA);
        packet.getIntegers().write(0, target.getEntityId());

        List<WrappedDataValue> metadata = new ArrayList<>();
        Optional<?> optionalName = Optional.of(WrappedChatComponent.fromChatMessage(name)[0].getHandle());

        WrappedDataValue customName = new WrappedDataValue(2, WrappedDataWatcher.Registry.getChatComponentSerializer(true), optionalName);
        WrappedDataValue customNameVisible = new WrappedDataValue(3, WrappedDataWatcher.Registry.get(Boolean.class), true);

        metadata.add(customName);
        metadata.add(customNameVisible);

        packet.getDataValueCollectionModifier().write(0, metadata);
        ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
    }