1.12.2 Send "fake food level" to prevent sprinting

Discussion in 'Spigot Plugin Development' started by CabbageRoll_, Nov 2, 2020.

  1. It is known that sprinting is client side so it has to be prevented on server side. But for me cancelling the event would be insufficient because the player will still go some distance before cancellation. If i could somehow send fake food bar to the client that would solve my problem. There is no api method for such thing and i do not know how to manipulate packets. Any help?
    For example server side will have normal food level but client will constantly have lets say 2 food.
    I already am considering just storing the real food level in a separate variable but i am still wondering if if the way with packets is possible.
     
  2. Check it out: https://wiki.vg/Protocol#Update_Health
    You also can use ProtocolLib with ProtocolWrapper to work with packets.
     
  3. I want to stay away from most external plugins as much as i can. ( i need it only about once and never again, also still learning the basics). How would i send this packet manually?
    I found the packet and how to send it, but already getting the error "craftplayer cannot be resolved to a type", and everyone uses it so im kind of stuck


    PacketPlayOutUpdateHealth test;
    test=new PacketPlayOutUpdateHealth((float)player.getHealth(), 2, player.getSaturation());//why does packet need float and gethealth returns double??
    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(test);

    By the way i am using server jar as resource so i dont know what is the problem
    Eclipse was dumb and didnt give me a recommenedation to import CraftPlayer. This code does indeed work.
    Code (Text):


                PacketPlayOutUpdateHealth test;
                test=new PacketPlayOutUpdateHealth((float)player.getHealth(), 2, player.getSaturation());
                ((CraftPlayer) player).getHandle().playerConnection.sendPacket(test);
     
    Food will gradually seem to go up for some reason but it does what i want it to. Ill experiment with it to not have the annoying blinking but it does what i want (being in a loop that runs every tick), stops sprinting.
     
    #3 CabbageRoll_, Nov 2, 2020
    Last edited: Nov 2, 2020
  4. Might wanna check if cancelling FoodLevelChangeEvent also causes a flicker, if it does, its probably client side and not much you can do about it. If it doesn't, there is probably a packet going out that causes the flicker, meaning you can cancel it :)
     
  5. You want to set the saturation to 0, that should stop the client from increasing the food level.

    You might also want to use ProtocolLib (and maybe PacketWrapper) to edit the sent food level in the packets sent by the server as well, instead of just overwriting the food level until the server decides to send the next food level packet.
     
  6. afaik the food is entirely server-side, meaning that this probably the server updating the player food level.
    So you should definitely cancel the event and not use the packet
     
  7. That would also stop things like health regeneration, the OP doesn't seem to want that, but instead just fake the food level to prevent the client from sprinting.
     
  8. Well the actual reason it was flickering is because of peceful mode because the food bar tries to increase itself client side while constantly going back due to packet being spammed. Easy and anything above has no visible flicker so ill leave it as it is.
    Now this together with combination of cancelling the sprint event would further empower it to stop the sprinting (or doing the necessary action to see if a player is hacking/lagging if they manage to sprint)