Solved [HELP] Listening WASD, VehicleSteer with ProtocolLib

Discussion in 'Spigot Plugin Development' started by Gober, Jun 28, 2018.

Thread Status:
Not open for further replies.
  1. Hello!
    I'm currently working on a plugin rightnow, I've just started trying to work with ProtocolLib, but I couldn't get it done.
    What i'm confused:
    - How to get the Player's WASD, Jumping, and sort of things with ProtocolLib.

    Code (Java):
        @EventHandler
        public void onMove(PacketEvent event) {
            if (event.getPacketType() == PacketType.Play.Client.STEER_VEHICLE) {
                PacketContainer packet = event.getPacket();
                Player p = event.getPlayer();
                Field jump = packet.g
                if (p.getPassengers() == null) {
                    return;
                }
                Entity b = p.getVehicle();
                if (b instanceof Player) {
                    return;
                }
                           // How to check the player's wasd / jump / dismount?
             
            }
        }
    NOTE: I'm actually new to NMS & other sort of Packets things.

    Any helps are welcome, feedbacks too..
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Ah yes, my area of expertise.

    If you're good with a fairly long explanation of packets in general but specifically this packet, I would really recommend you read this: http://www.scarabcoder.com/blog/2018/2/27/getting-wasd-movement-control-input-from-the-client

    Anyway, here are the fields in the Steer Vehicle packet (also known as the input packet):

    [​IMG]
    (Taken from http://wiki.vg/Protocol#Steer_Vehicle, always check that wiki when dealing with packets).

    There are two ways to get the data from the packet. The first way is to use ProtocolLib's packet containers, which make it easy to get data that's listed the exact same way as in the wiki. The second is to use the NMS classes from CraftBukkit, which is usually in a similar but different format and has all the data parsed. It does require you to use reflection and will break with every version, so PL's containers are a better solution.

    You just need to get the first two floats for WASD input (the last byte you will have to deal with bitwise flags, a somewhat complicated topic at first glance). For the first field - as stated in the description for it - if the float is more than 0 they're holding "A" (left key), less than zero and it'll be the "D" key, if it's 0 neither. Same for the second field, > 0 is W/forward < 0 is S/backward.

    To read these floats, you can use PacketContainer#getFloat(), then get them by their field numbers (starting at 0, index for each field *type*):

    Code (Text):
    packet.getFloat().read(0); //First field, left/right
    packet.getFloat().read(1); //Second field, forward/backward
    Note: For some odd reason, it's PacketContainer#getFloat instead of PacketContainer#getFloats. Seems inconsistent with the other functions (getBytes, getBooleans, etc).
     
    • Winner Winner x 4
  3. This right here is a winner explanation xD!
    Thank you very much
     
  4. But is there a way to check if it jumped?

    EDIT: Found it, Simply Add a do ThePacketContainer.getBooleans().read(0);
     
    #4 Gober, Jun 28, 2018
    Last edited: Jun 28, 2018
    • Like Like x 1
Thread Status:
Not open for further replies.