package los.numeros.utils; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; /** * class for dealing with minecraft ticks * */ public class MinecraftTick { /* interface for callbacks */ public interface Callback { public void run(long delay); } /** * run callback on delay * * @param ticks minecraft ticks to wait before calling delay * @param callback callback to run after ticks delay */ public void delayCallback(long ticks, MinecraftTick.Callback callback) { Delay delay = new Delay(); delay.tickCallback = callback; /* register event to run on every server tick */ /* TEST: I'm pretty sure this creates a new event every time this method is * called, but I could be wrong */ ServerTickEvents.END_SERVER_TICK.register(event -> { /* check if we're done */ if (delay.tickCounter == ticks) { /* run callback */ delay.tickCallback.run(ticks); delay.tickCallbackRan = true; /* reset for next run */ delay.tickCounter = 0L; return; } /* increment counter every tick */ if (!delay.tickCallbackRan) { delay.tickCounter++; } }); return; } /** * convert seconds to ticks * * @param seconds seconds * @return ticks */ static public long secondToTick(int seconds) { return seconds * 20; } /** * convert ticks to seconds * * @param ticks minecraft ticks * @return seconds */ static public int tickToSecond(long ticks) { return (int)(ticks / 20); } } /** * small class to deal with tick delay callbacks * */ class Delay { public long tickCounter = 0L; public boolean tickCallbackRan = false; public MinecraftTick.Callback tickCallback; }