Solved Using ProtocolLib for Custom Payload Packets

Discussion in 'Spigot Plugin Development' started by Swedz, Feb 3, 2017.

  1. How would I go about reading incoming custom payload packets? I'm very lost on how to read the channel, and the data included in the packet. I've been looking around for ages on how to do this, but I can't find anything...

    I have this so far:
    Code (Text):
    public Client(JavaPlugin plugin) {
            ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.CUSTOM_PAYLOAD) {
                @Override
                public void onPacketSending(PacketEvent e) {
                    recievedPacket(e);
                }
            });
        }
       
        private void recievedPacket(PacketEvent e) {
            Player player = e.getPlayer();
            PacketContainer packet = e.getPacket();
            String channel = packet.getStrings().read(0);
           
            List<Byte> bytes = new ArrayList<Byte>();
            for(byte lb : packet.getBytes().getValues())
                bytes.add(lb);
            Byte[] b = bytes.toArray(new Byte[] {});
         }
    However I need to get the String data from the Byte[]... I can't find anything on this!

    Thank you for taking the time to help.
     
  2. I have used a ByteBuffer in the past for this exact thing.
     
  3. Am I doing everything else correctly though? I'm still not fully understanding the ProtocolLib stuff fully, and I don't even fully understand Custom Payload packets...

    I did look around a lot yesterday, and I still don't fully understand.
     
  4. To get the actual bytes I had to use reflection as the payload packet only contains a PacketDataSerializer (which at this time extends ByteBuf) for data:
    [​IMG]
    This allows us to call ByteBuf#array() to get the byte[] from the second object (first index when getting with protocollib). Here is an example.
     
    #4 WesJD, Feb 4, 2017
    Last edited: Feb 4, 2017
    • Like Like x 1
  5. Making a bytearray into string can be done like


    String string = new String(array)

    Sendt fra min ALE-L21 med Tapatalk
     
    • Agree Agree x 1
  6. Thanks a ton, in your handleInternal(PacketEvent event) method, the byte[] is the information in the custom payload, correct?
     
    #6 Swedz, Feb 4, 2017
    Last edited: Feb 4, 2017
  7. Yes.
     
    • Like Like x 1
  8. I haven't tested it until now, and I found that I get an IllegalArgumentException (ClassCastException) at this line:
    Code (Text):
    byte[] bytes = (byte[]) ARRAY_METHOD.invoke(packet.getModifier().readSafely(1));
    I find this odd, since in your code, you do the exact same thing, except that your argument is stated as "final".
     
  9. Check the class of the object from readSafely(1).
     
  10. It is net.minecraft.server.v1_8_R3.PacketDataSerializer
     
  11. I don't have a 1.8 jar, but it should extend or implement (not looking at the jar rn) ByteBuf, so use that in the reflection for the method.
     
  12. Did the trick, thanks! :)
     

Share This Page