Easiest way to make a 24 hour delay

Discussion in 'Plugin Development' started by sgavster, Mar 11, 2016.

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

    sgavster

    Hello! I'm trying to make a 24 hour delay for my plugin (not just kits) but I can't seem to figure it out:

    I know I use use System.currentTimeMillis() but everything I've tried doesn't work.

    My current code:


    Code:
    Long rt = System.currentTimeMillis() - LoginHandler.getThings(p).getLong("starterdelay");
                    if(rt == null || rt == 0) {
                        addKitStarter(p);
                        LoginHandler.getThings(p).set("starterdelay", System.currentTimeMillis());
                        LoginHandler.getThings(p).save();
                    } else if(rt >= 24 * 60 * 60 * 1000) {
                        addKitStarter(p);
                        LoginHandler.getThings(p).set("starterdelay", System.currentTimeMillis());
                        LoginHandler.getThings(p).save();
                    } else {
                        p.sendMessage("§4Error: §cYou still have §6" + getDurationBreakdown(System.currentTimeMillis() - rt));
                    }
                }
                }
    getDurationBreakdown:
    Code:
    public String getDurationBreakdown(long millis){
                if(millis < 0){
                    throw new IllegalArgumentException("Duration must be greater than zero!");
                }
    
                long days = TimeUnit.MILLISECONDS.toDays(millis);
                millis -= TimeUnit.DAYS.toMillis(days);
                long hours = TimeUnit.MILLISECONDS.toHours(millis);
                millis -= TimeUnit.HOURS.toMillis(hours);
                long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
                millis -= TimeUnit.MINUTES.toMillis(minutes);
                long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
    
                StringBuilder sb = new StringBuilder(64);
                sb.append(days);
                sb.append(" Days ");
                sb.append(hours);
                sb.append(" Hours ");
                sb.append(minutes);
                sb.append(" Minutes ");
                sb.append(seconds);
                sb.append(" Seconds");
    
                return(sb.toString());
            }
    It tells me in chat:

    [​IMG]

    Also, how could I update the time easily? Thanks for any help, sorry if this is confusing, I don't really know what I'm doing this with.
     
  2. Offline

    Zombie_Striker

    @sgavster
    Use the Date object to get the day, hour, minute, and second of when the delay starts. Then, use subtraction to get how many seconds/minutes/days are left until the "cooldown" is over.
     
  3. Offline

    sgavster

    @Zombie_Striker So I'm using the time object now but I'm a bit confused as to how I would do this?

    If I have the hour object and subract that from the current hour, it could be three days later and it'd still be wrong it it wasn't the exact time, so how could I do it?
     
  4. Offline

    Zombie_Striker

    Code:
    int timeInMinutes = (Days * 24 * 60 /*converts days to minutes*/) + (Hours * 60 /*Hours to minutes*/) + Minutes /**remaining minutes**/;
     
Thread Status:
Not open for further replies.

Share This Page