Delete data after a player's period of inactivity on server

Discussion in 'Spigot Plugin Development' started by iDoomfull, Aug 13, 2017 at 9:38 AM.

  1. Hello, I am interested in a method which can delete player's data if he didn't joined the server for a certain amount of time. This way, useless data can be deleted and light the memory a bit. I though of a method which implies schedule tasks, but keeping them running for a long period might cause lag, so.. your opinions?
     
  2. 1. Check if they are offline with Bukkit#getOfflinePlayers and loop through that list
    2. Start a delayed task that will run after an interval (10mins) for 10mins of inactivity but you will need some booleans to make sure the player has not logged on then logged off or if they are online
    3. After that task has ran, bingo!

    [ MORE VISUAL ]
    Code (Text):

    // put this inside your method
    for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
         new BukkitRunnable() {
                @Override
                 public void run() {
                      if (!(isOnline || hasBeenOnline || justLoggedOff)) {
                            deleteData(op);
                      }
                 }
         }.runTaskLater(MyPlugin.getInstance(), 20 * 60 * 10);
    }
     
    • Funny Funny x 1
  3. What if I want to do it for a period of days?
     
  4. Wouldn’t you just get the last login time of the player, it’s bigger than x many days then purge?

    Something like,
    for(OfflinePlayer op : Bukkit.getOfflinePlayers()){
    if(op.getLastLogin() >= TimeUnit.DAYS.toMillis(5)){
    // purge
    }
    }

    On mobile so it won’t look the best sorry
     
  5. Code (Text):

    for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
                if ((System.currentTimeMillis() - op.getLastPlayed()) >= (time in seconds times 1000)) {

                    // remove their data

                }
            }
     
     
    • Agree Agree x 1

Share This Page