Skip to content

Instantly share code, notes, and snippets.

@MooseElkingtons
Created August 24, 2012 05:05
Show Gist options
  • Select an option

  • Save MooseElkingtons/3445631 to your computer and use it in GitHub Desktop.

Select an option

Save MooseElkingtons/3445631 to your computer and use it in GitHub Desktop.
package com.Marneus.Bot.API.Methods;
import java.util.Random;
/**
* Provides various methods for time management.
*
* @author MooseElkingtons
*/
public class TimeManager {
/**
* Sleeps for the time given.
*
* @param millis the amount of time in milliseconds to sleep.
*/
public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch(Exception e) {}
}
/**
* Sleeps for the time given.
*
* @param millis the amount of time in milliseconds to sleep.
*/
public static void sleep(long millis) {
sleep((int) millis);
}
/**
* Will sleep for x milliseconds when the time is met.
*
* @param time the time to sleep at.
* @param millis the amount of time to sleep for.
*/
public static void sleepAt(long time, int millis) {
try {
long init = System.currentTimeMillis();
while(true) {
if(init + time <= System.currentTimeMillis()) {
Thread.sleep(millis);
return;
}
}
} catch(Exception e) {}
}
/**
* Sleeps for a random amount of time within the given parameters.
*
* @param minTime the minimal amount of time before it can sleep.
* @param maxTime the limit of how much time can be slept for.
*/
public static void sleepBetween(int minTime, int maxTime) {
sleep(minTime + new Random().nextInt(maxTime - minTime));
}
/**
* Sleeps until the time given is met.
*
* @param time the time given.
*/
public static void sleepUntil(long time) {
try {
long init = System.currentTimeMillis();
while(init + time > System.currentTimeMillis())
Thread.sleep(init + time - System.currentTimeMillis());
} catch(Exception e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment