Way to track a players movement?

Discussion in 'Spigot Plugin Development' started by yhl, Apr 24, 2017.

  1. yhl

    yhl

    I just want an int or double or something of the number of blocks that the player has moved.
     
  2. I would recommend to make use of Player#getStatistic.

    If you don´t want to get the whole amout, i would use the PlayerMoveEvent in Combination with Event.getFrom() and Event.getTo()
     
  3. yhl

    yhl

    Could you give me an example or something?
     
  4. WAS

    WAS

    Not too hard. As ExoBiTe said, there is a total, but if you want it to be from a triggered point something like

    Code (Java):
      private Map<UUID, Integer> blocksTraveled = new HashMap<UUID, Integer>();

       @EventHandler
       public void onPlayerMove(PlayerMoveEvent e) {
         if ( someTriggerIsTrue ) {
           int traveled = 0;
           if ( blocksTraveled.containsKey(e.getPlayer().getUniqueId()) )
             traveled = blocksTraveled.get(e.getPlayer().getUniqueId());
           if ( ! ( e.getTo().equals(e.getFrom()) ) ) // Note: May want to check block locations
             blocksTraveled.put(e.getPlayer().getUniqueId(), ( traveled + 1 ) );
         }
       }
    Additionally, you could add a method to clear blocks traveled from outside the class if some condition is met

    Code (Java):
      public Integer removeTraveled(UUID uuid) {
         return blocksTraveled.remove(uuid);
       }
    provided you handle a instance. If not static could work...
     
  5. Mas

    Mas

    PlayerMoveEvent is fired when a player sends a movement/look packet, not when a player moves a block, so that is unreliable. You should instead use Location#distance(Location) with PlayerMoveEvent#getFrom() and PlayerMoveEvent#getTo(). Also, use primitive type 'int' over 'Integer'.
     
  6. WAS

    WAS

    As noted in the code, you can actually check the block. Distance is actually horribly unreliable, and is using math if I am not mistaken, adding a layer of computation.. I tried using distance for PlayerAirEvents spigot api extension and it was not a true number, though I was checking Y.
     
  7. yhl

    yhl

    I don't understand how to use getTo and getFrom (Like how would I use it?)
     
  8. Mas

    Mas

  9. yhl

    yhl

  10. yhl

    yhl

  11. Mas

    Mas

    ... You use the method #distance on the getFrom() location, using the getTo() location as the argument.
    If you can't make sense of this you need to learn more Java before attempting to learn the Spigot API.
     
  12. yhl

    yhl

    It says that it can't convert double to boolean
     
  13. Mas

    Mas

     
  14. yhl

    yhl

    Well then how would i use it in an if statement? I can't fingure it out
     
  15. Mas

    Mas

    You don't need one?
    You said you are trying to find how far a player has moved.
    To do this, use location.distance(otherLocation) where 'location' is event.getFrom() and 'otherLocation' is event.getTo().
    https://docs.oracle.com/javase/tutorial/
     
  16. yhl

    yhl

    Well I just got that :p but how would I use that to track distance
     
  17. yhl

    yhl

    Yeah, I want to make it so an int goes up every time a player walks a block, so how would you not need an if statement
     
  18. WAS

    WAS

    Code (Java):
      private Map<UUID, Integer> blocksTraveled = new HashMap<UUID, Integer>();

       @EventHandler
       public void onPlayerMove(PlayerMoveEvent e) {
         if ( someTriggerIsTrue ) {
           int traveled = 0;
           if ( blocksTraveled.containsKey(e.getPlayer().getUniqueId()) )
             traveled = blocksTraveled.get(e.getPlayer().getUniqueId());
           if ( ! ( e.getTo().getBlock().getLocation().equals(e.getFrom().getBlock().getLocation()) ) )
             blocksTraveled.put(e.getPlayer().getUniqueId(), ( traveled + 1 ) );
         }
       }
    Notice the edit here. It's checking the actual blocks location. If they don't match, the player is moving out of a blocks location and it will add to that players movement count.

    Too much computation for PlayerMoveEvent.... It's also redundant as they'll always be moving a block (or technically half a block if walking) and this method is not really meant for that < You might as well just check the relative block location, which is what the PlayerMoveEvent gives you with getTo and getFrom.
     
    #18 WAS, Apr 24, 2017
    Last edited: Apr 24, 2017
  19. yhl

    yhl

    omg sorry everyone, I forgot the implement Listener and register the events ://///////////////////////
     
    • Funny Funny x 1
  20. Mas

    Mas

    PlayerMoveEvent is not called when the player moves a block, as I previously stated. Also by spoonfeeding like that they're never going to learn.