1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to detect when player not moving?

Discussion in 'Development' started by LewBr, Apr 12, 2018.

  1. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Hello,

    How could i detect when a player is not using PlayerMoveEvent? whitout using some coordinates and higher, lower etc..
     
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Why do you need this? Check the yaw and/or the pitch of the player on a task.
     
  3. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    What you mean? I just want to detect when a player is not moving.. like NOTHING..
     
  4. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Make a task that checks the yaw or the pitch of the player.
     
  5. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    PHP:
    private $movePlayers = [];

    public function 
    onMove(PlayerMoveEvent $event) : void{
             
    $player $event->getPlayer();
             
    $this->movePlayers[$player->getName()] = time();
    }

    public function 
    isMoveExecuted(Player $player) : bool{
         if(!isset(
    $this->movePlayers[$player->getName()])){
                return 
    false;
         }
     
        
    $timeDiff time() - $this->movePlayers[$player->getName()];
        if(
    $timeDiff 0.1){
          return 
    false;
        }

        return 
    true;
    }
     
    Mohagames205, RyanShaw and LewBr like this.
  6. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    You use PlayerMoveEvent
     
  7. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    And what is problem?
     
  8. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    I believe this is the best option. There is no easy isMoving function
     
    LewBr likes this.
  9. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    This I would also like to know, I was looking for a function like isMoving but how complicated it is, I do not know.. ty
     
  10. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    Theres no function isMoving()... You can save old pos, and check new pos and use lot of hacks lol
     
  11. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
     
  12. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    sorry my english is not so good.. I asked if there was any way to detect if a player is not moving but I used the event as the word, I'm sorry
     
  13. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    *cough* Player::$speed *cough*
     
  14. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Yeah but what about if player moves his head?
     
  15. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Use Entity::$lastYaw and Entity::$lastPitch and test them against the current yaw and pitch via a task. Pocketmine certainly isn't rocket science.
     
  16. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Can you tell us why you want to check that? The answer can differ based on time difference between two movements.
     
  17. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    What I'd suggest is have an array to store the player's ticksLived during a movement (and don't forget to remove the entries on player quit).
    PHP:
    /** @var int[] */
    private $playerMovements = [];
    Then update the property whenever player moves:
    PHP:
    public function onPlayerMove(PlayerMoveEvent $event) : void{
        if(!
    $event->getFrom()->equals($event->getTo())){
            
    $player $event->getPlayer();
            
    $this->playerMovements[$player->getId()] = $player->ticksLived;
        }
    }
    And have a function that returns whether the player moved during the last tick.
    PHP:
    public function isMoving(Player $playerint $tick_diff 1) : bool{
        
    $id $player->getId();
        return isset(
    $this->playerMovements[$id]) &&
            
    $player->ticksLived $this->playerMovements[$id] <= $tick_diff;
    }
    Reverse the retval to check whether the player is not moving.
     
  18. NTT

    NTT Zombie

    Messages:
    311
    GitHub:
    NTT1906
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.