detecting player inactivity

Discussion in 'Plugin Development' started by matter123, May 4, 2011.

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

    matter123

    how would i determine if a players has not fired
    OnPlayerMove
    OnPlayerInteract
    OnPlayerChat
    OnPlayerCommandPreProcess

    in the last x minuites
     
  2. Offline

    Kuuichi

    I would set a boolean for each event like move, interact, chat or something like that and make "true" the default setting. If any of those events occur, it would set the respective boolean to false. To check if the player is truly "inactive", make an if statement seeing if all the booleans are still true. Also, set a delay for like 5 minutes, and then have the boolean return to its original "true" setting.

    That was me just giving ideas, I'm still a newbie in Java and I'm pretty sure what I wrote doesn't make sense :p If it does work though, then yay me!
     
  3. Offline

    Raphfrk

    You can use AtomicBoolean for that and then set up the timer as an async task.
     
  4. Offline

    Kuuichi

    Hahahahahahaha I just got owned in one sentence :) I don't even know what that is, TIME FOR GOOGLEZ.
     
  5. Offline

    matter123

    Can you give me a example for the timer part
     
  6. Offline

    Raphfrk

    Actually, it is probably easier to just check if he is in the exact same position :).

    You need a Runnable. This checks if any player has changed position since the last check.

    Code:
    class PlayerIdleTracker extends Runnable {
    
        final Server server;
    
        PlayerIdleTracker(Server server) {
            this.server = server;
        }
    
        private HashMap<String,Long> lastActiveMap = new HashMap<String,Long>();
        private HashMap<String,Location> lastLocationMap = new HashMap<String,Location>();
    
        public void run() {
            for(Player player : server.getOnlinePlayers()) {
                long currentTime = System.currentTimeMillis();
                String playerName = player.getName();
                Location loc = player.getLocation();
                Location lastLocation = lastLocationMap.get(playerName);
                if(lastActive == null || lastLocation == null || !lastLocation.equals(player.getLocation()) {
                    lastActiveMap.put(playerName, currentTime);
                    lastLocation.put(playerName, loc);
                }
                Long lastActive = lastActiveMap.get(lastActive);
            }
    
        public playerTookAnAction(Player player) {
            lastActiveMap.put(player.getName, System.currentTimeMillis());
        }
    
        public Long getIdleTime(Player player) {
            Long lastActive = lastActiveMap(player.getName());
            if(lastActive == null) {
                return null;
            } else {
                return System.currentTimeMillis() - lastActive;
        }
    }
    
    You then need to submit it.

    Code:
    PlayerIdleTracker playerIdleTracker = new PlayerIdleTracker(getServer();
    
    int taskId =  getServer().getScheduler().scheduleSyncDelayedTask(your-plugin, playerIdleTracker, 600L);
    
    This will repeat the task every 600 clock ticks (30 second @ 20 ticks per second).

    You can also ping the class from another method with

    playerIdleTracker.playerTookAnAction(player)

    This will also reset the idle timer for that player.

    You can check the idle time for a player with

    Long idle = playerIdleTracker.getIdleTime(player);

    It will return null if the player wasn't online since the last check.

    This all happens in the main thread, so you don't need to worry about syncing.
     
  7. Offline

    matter123

    wait also how would i use the get idle time when an event isnt fired
     
Thread Status:
Not open for further replies.

Share This Page