aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/utils/TickDelay.java
blob: f467976537518dcd3cf6b73dfec98c61dd2a181e (plain)
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
package cc.polyfrost.oneconfig.utils;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

public class TickDelay {
    private int delay;
    private final Runnable function;

    public TickDelay(Runnable functionName, int ticks) {
        register();
        delay = ticks;
        function = functionName;
    }

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent event) {
        if (event.phase == TickEvent.Phase.START) {
            // Delay expired
            if (delay < 1) {
                run();
                destroy();
            }
            delay--;
        }
    }

    private void destroy() {
        MinecraftForge.EVENT_BUS.unregister(this);
    }

    private void register() {
        MinecraftForge.EVENT_BUS.register(this);
    }

    private void run() {
        function.run();
    }
}