getting playtime / checking for certain time

Discussion in 'Spigot Plugin Development' started by TylerV, May 16, 2016.

  1. I'm making an achievement plugin and trying to get the players playtime and I have a task to check for the players playtime and my playtime reaches what the task looks for, but it doesn't work..

    I made a command to show my days played, which told me 6, and if i have at least 5 it should be completing the achievement. The code is below.

    Scheduler (in my onEnable)
    Code (Text):
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        if(!(AchManager.hasCompleted(p, Achievement.CloudyMemberI))) {
                            if(AchManager.getPlayTimeDays(p.getFirstPlayed()) >= TimeUnit.MILLISECONDS.toDays(604800000)) {
                                AchManager.setComplete(p, Achievement.CloudyMemberI);
                        if(!(AchManager.hasCompleted(p, Achievement.CloudyMemberII))) {
                            //change back to 7, its at 5 for testing.
                            if(AchManager.getPlayTimeDays(p.getFirstPlayed()) >= TimeUnit.MILLISECONDS.toDays(604800000 * 5)) {
                                AchManager.setComplete(p, Achievement.CloudyMemberII);
                        if(!(AchManager.hasCompleted(p, Achievement.CloudyMemberIII))) {
                            if(AchManager.getPlayTimeDays(p.getFirstPlayed()) >= TimeUnit.MILLISECONDS.toDays(604800000 * 30)) {
                                AchManager.setComplete(p, Achievement.CloudyMemberIII);
            }, 1L, TimeUnit.MINUTES.toSeconds(10) * 20L);
    the getPlayTimeDays() method:

    Code (Text):
        public static int getPlayTimeDays(Long playertime){
            long time = System.currentTimeMillis() - playertime;
            int day = (int) TimeUnit.MILLISECONDS.toDays(time);
            return day;
    any help would be appreciated, thanks.
  2. That Runnable looks pretty correct to me. Are you certain the AchManager methods for setComplete and hasCompleted are correct? Also, where you have a method to convert milis to days, why are you checking to see if it's greater than or equal to another conversion? Why not just use the Integer value for the days?
  3. setComplete and hasCompleted work for other achievements I've made in the plugin, so I'm certain they would work, and I'll try to use an integer instead of another conversion. Thanks
  4. I believe there is some new code to check a players statistics built into spigot. It gives accurate player online. I'll find it for you.

    EDIT: The code is
    Code (Text):
    int time = Player.getStatistic(Statistic.PLAY_ONE_TICK);
    This returns the amount of time the player has played on your server in ticks.
    #4 Leniency, May 16, 2016
    Last edited: May 16, 2016