Dafuq is this value?

Discussion in 'Plugin Development' started by Lolmewn, Jul 18, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Lolmewn

    Hi there,

    I am making a sort-of stats plugin. You can see it here: http://mc.centrility.nl/stats/
    However, it seems sometimes values are.. not what they should be.
    In my plugin, I use the PlayerMoveEvent, and then calculate how far he's moved.
    Every 10 seconds, it writes that to the MySQL database.

    Anyway, what is weird, is that one of my players seems to have traveled 2.000.000 blocks by boat.
    Now, I asked him, and he certainly did not do that. What is a possible cause of this?
     
  2. Offline

    andf54

    Teleporting?
     
  3. Offline

    Lolmewn

    While in a boat? :confused:
     
  4. Offline

    andf54

    Maybe he hit /spawn or something while he was in a boat.

    You need to post the source if you want a detailed analysis.
     
  5. Offline

    Lolmewn

    Right, of course.
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    3. public void onPlayerMove(final PlayerMoveEvent event) {
    4. Player p = event.getPlayer();
    5. double distance = event.getFrom().distance(event.getTo());
    6. if (distance < 0.0001) {
    7. return;
    8. }
    9. int type = 0;
    10. if (p.isInsideVehicle()) {
    11. Entity vehicle = p.getVehicle();
    12. if (vehicle instanceof Boat) {
    13. type = 1;
    14. }
    15. if (vehicle instanceof Minecart) {
    16. type = 2;
    17. }
    18. if (vehicle instanceof Pig) {
    19. type = 3;
    20. }
    21. }
    22. MoveHolder h = this.getPlugin().getHolders().get(p.getName());
    23. if (h == null) {
    24. h = new MoveHolder();
    25. h.addDistance(distance, type);
    26. getPlugin().getHolders().put(p.getName(), h);
    27. } else {
    28. h.addDistance(distance, type);
    29. }
    30. }
     
  6. you sure the move event is called while in a boat, it sund only logic to my if its send when he moves its head
     
  7. Offline

    nisovin

    Checking distance in PlayerMoveEvent is horribly inefficient. You really should just use a scheduled task for this, it'd be better.
     
  8. Offline

    Lolmewn

    I know it is. It's accuracy vs performance really.
    If I'd use a task, how would I be doing that? Have the old locations of players in a HashMap, perform .distance() and use that?
     
  9. Offline

    Dreeass

    A repeating task every 10 seconds that saves the distance for each player, but when teleporting you have to cancel it. I'm not close to a computer, so I can't try it out.
     
  10. Offline

    andf54

    Added:
    Code:
            double distance = event.getFrom().distance(event.getTo());
            if (distance >= 0.0001) {
                System.out.println("moved " + distance);
            }
     
    
    Jumped in a boat and I got:
    ...
    03:44:28 [INFO] moved 0.1419950976946974
    03:44:28 [INFO] moved 0.14070763454861387
    03:44:28 [INFO] [PLAYER_COMMAND] andf54: /spawn
    03:44:28 [INFO] moved 123.75965081559839
    03:44:29 [INFO] moved 1.2105770304805719
    ...

    Teleporting is the cause. You just need check for teleports and ignore move when it happens.
     
  11. Offline

    Lolmewn

    andf54 Alright, thanks. I could also just check if the distance is bigger than say, 5. No way you go 5 blocks per 0.05 seconds :)
     
  12. Offline

    Dreeass

    It might be better cancelling it one time, but how? Meh, just check it :p
     
  13. I think the cause of that high value is that the PlayerTeleportEvent extends PlayerMoveEvent, so you methode is also catching teleport events
     
  14. Offline

    Lolmewn

    Wait a sec, doesn't that mean I can check if the event is an instanceof PlayerTeleportEvent? If so, I can just let it return and don't care :)
     
  15. if(event instanceof PlayerTeleportEvent)
    return;
     
  16. Offline

    Lolmewn

    Exactly :) That's much better :D

    Thanks all!
     
Thread Status:
Not open for further replies.

Share This Page