blob: 890865abdbe61fb9ddc53791698618fc99ee8b36 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
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;
}
|