Solved Stopping Sprinting! HELP :(

Discussion in 'Plugin Development' started by GamingMeatheadHD, Mar 5, 2015.

Thread Status:
Not open for further replies.
  1. Hi there!

    Plain and simple, I'm trying to stop a player from sprinting. Sounds easy, right? Currently I have been able to stop the sprinting, if and only if, they double-tap "w" to sprint. If they use the default left-ctrl key to sprint, the action isn't canceled. I'm beyond confused why the two different methods of sprinting would be registered as different things, and not the same.

    Here's my code (in a PlayerMoveEvent listener class):

    Code:
    if(event.getPlayer().isSprinting())
                event.getPlayer().setSprinting(false);
    Like I said, this code doesn't stop left-ctrl sprinting, but it does stop double-tap "w" sprinting. Any thoughts or ideas?

    Thanks :)
     
  2. Offline

    Regablith

    PlayerToggleSprintEvent
     
  3. I feel stupid for not knowing that even existed. haha. But I gave it a go. Here's what I have so far:

    Code:
    @EventHandler
        public void onSprint(PlayerToggleSprintEvent event){
            event.setCancelled(true);
        }
    I also tried

    if(event.isSprinting())
    event.setCancelled(true);

    if(event.isSprinting())
    event.getPlayer().setSprinting(false);

    if(event.getPlayer().isSprinting())
    event.getPlayer().setSprinting(false);

    if(event.getPlayer().isSprinting())
    event.setCancelled(true);

    None of the above seem to work though.
     
  4. Offline

    ChipDev

    Try debugging.
    Also try
    Code:
    @EventHandler
        public void onSprint(PlayerToggleSprintEvent event){
            event.getPlayer().setSprinting(false);
        }
     
  5. I'm sorry to say that even event.getPlayer().setSprinting(false) didn't work either. It's almost like this event is useless.
     
  6. Offline

    ChipDev

    Did you register your events?
    Or else use PlayerMoveEvent :/
     
  7. That was the first thing I checked, having made this blind mistake before. Everything is registered and seems to be set up correctly. The only problem with PlayerMoveEvent is it can't seem to block the left-ctrl sprinting, so we're back to the original problem unfortunately. :/
     
  8. Offline

    Hex_27

  9. Offline

    teej107

    @GamingMeatheadHD I've heard that sprinting was handled by client only in 1.7 (or something like that, either way breaking sprint checks). Set the player's food level so they can't sprint is my only suggestion.
     
    Konato_K likes this.
  10. Both of these are probably possible ways to get around it. They just don't sound very clean, ya know? Kinda like just putting a bandaid over the problem rather than fixing it, yet these may be the only solutions unfortunately. Thanks for the ideas!
     
  11. Offline

    Gartenzaun

    You could try to create a scheduleSyncDelayedTask with 0L. This worked for me.
     
  12. Interesting. And simply just loop through all online players setting sprint to false? Good idea.
     
  13. Offline

    Gartenzaun

    @GamingMeatheadHD

    Try this :) :

    Code:
        @EventHandler
        public void sprintEvent(PlayerToggleSprintEvent sv) {
            final Player sprinter = sv.getPlayer();
            if (sv.isSprinting()) {
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(yourPluginName, new Runnable() {
                    public void run() {
                        sprinter.setSprinting(false);
                    }
                }, 0L);
            }
        }
     
  14. Offline

    MexMaster

    @GamingMeatheadHD
    I don't think there is a clean way of doing so.
    I'm also using the control key to sprint because normally when you hit somebody in combat you will stop sprinting but pressing the control key will override this and makes you sprint again. So I'm guessing with Player#setSprinting you just stop sprinting but it will instantly be overridden by the client. You can also notice this when holding control and running against a block. At first you will stop sprinting but then jump up the block and you should notice that you instantly begin to sprint when touching the ground again. This isn't the case when you sprint by pressing double w
     
  15. This works well with double-tap w sprinting, but left-ctrl still is unaffected.

    It looks like you may be right. I don't know if there's any way to stop this without manipulating hunger or adding slowness.

    Ultimately, I decided to go with just setting the food level to 1. It bugs me having to do that. It doesn't seem as professional as it could be, but it works. Thanks for everyone's input!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  16. Offline

    MexMaster

    @GamingMeatheadHD
    I just got the idea that you could decrease their walk speed if they begin to sprint. Also set it back to 1 when they stop sprinting.
    So the food level wouldn't be so annoying
     
  17. Offline

    teej107

    @MexMaster I don't think walk speed affects sprint speed.
     
  18. Offline

    MexMaster

    @teej107 You are sure? I thought Essentials does so with the command /speed <0-10> and it also changes sprint speeds
     
  19. Offline

    teej107

    @MexMaster I'm not certain but it wouldn't be hard to find out.
     
  20. I actually tried this method first. It didn't affect the sprinting unfortunately.
     
    MexMaster likes this.
Thread Status:
Not open for further replies.

Share This Page