Trying to detect AFK players (without listening to PlayerMoveEvents)

Discussion in 'Plugin Development' started by ams2990, Apr 30, 2012.

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


    So I'm working on an AFK-kicking plugin for a fairly large server (average load of 70-100 people), so I'm trying to keep things as performant as possible. Despite a lot of thought on the subject, I don't know how to do it without listening to a bunch of events. Basically, every time one of the specified events happens, I find out what Player caused it, and update the entry corresponding to that player in a HashMap with the current time.

    The only problem is, PlayerMoveEvents are thrown hundreds of times a second...per player. Assuming they're moving, of course. Multiply that by 75 people, I'm doing a lot of put operations per second. Unfortunately, I don't see how to get around using PlayerMoveEvents, because people could be walking around the world not clicking on anything and get booted. Is there some way to figure out what the last event that happened to player was? Is there a better way of doing this altogether? Please help!
  2. Offline

    Father Of Time

    I would personally use a single scheduled event on a one minute timer. Have a hashmap that stores the player as the key and a custom class that stores location, Date and violation counter. When a player logs in add them to this hashmap, and remove them when they log out.

    Then on the scheduler tick you you loop through your hash map (which is limited to your online player count, so the loops are fairly resource lite) and look to see if each players current position is the same as their last known location, if so then give them a violation point and set the date and location to the current date and location.

    Every time the player is in the same location they earn a point, then you can just put a cap on points for the amount of minutes you want people allowed to be afk, so if you want them to be booted after 10 mins of not moving you could make it auto kick them once the violation hits 10; and if the players location has changed since the last check you simply reset their violation points to zero and reset the date and location again.

    This will at most do 100 location and date comparisons every 1 minute, which is massively less resource intensive than performing the exact same check every time someone moves (keep in mind the player movement event is triggered by something as small as turning your head, not even just physically moving from location to location).

    And of coarse you can adjust the timer frequency and violation points to meet your exact needs.

    I hope this explanation helps, Good luck with your project!
    zanda268, Crast and ams2990 like this.
  3. Offline


    Yes, this is brilliant.
  4. Offline

    Father Of Time

    Fantastic, I'm glad this suggestion could be of use to you.

    Good luck with your project!
    fromgate likes this.
Thread Status:
Not open for further replies.

Share This Page