Skip to content

Instantly share code, notes, and snippets.

@minecrafter
Created November 10, 2015 02:29
Show Gist options
  • Save minecrafter/582814bcec50ee9ed0b1 to your computer and use it in GitHub Desktop.
Save minecrafter/582814bcec50ee9ed0b1 to your computer and use it in GitHub Desktop.
package net.thechunk.chunklib.util;
import com.google.common.base.Preconditions;
import java.util.concurrent.TimeUnit;
/**
* Time related utility class.
*/
public class TimeUtils {
/**
* One Minecraft tick in terms of real time (50 milliseconds).
*/
public static final long ONE_TICK_IN_MILLISECONDS = 50;
/**
* Converts a real time value into Minecraft ticks.
*
* @param value the amount of time
* @param from the unit of time
* @return the amount of time specified in ticks
*/
public static long toMinecraftTicks(long value, TimeUnit from) {
long inMs = from.toMillis(value);
Preconditions.checkArgument(inMs >= ONE_TICK_IN_MILLISECONDS, value + " " + from + " is smaller than 50 ms! (" + inMs + ")");
return inMs / ONE_TICK_IN_MILLISECONDS;
}
/**
* Converts a Minecraft tick time value into a real time value.
*
* @param value the amount of time
* @param to the desired amount of time
* @return the amount of ticks specified in real time
*/
public static long fromMinecraftTicks(long value, TimeUnit to) {
long minecraftInMs = value * ONE_TICK_IN_MILLISECONDS;
return to.convert(minecraftInMs, TimeUnit.MILLISECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment