aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/utils/TickDelay.java
blob: 33b02fe92908310c67201dc1b188b60d54278f7f (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
41
42
43
package io.polyfrost.oneconfig.utils;

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

public class TickDelay {
    Integer delay;
    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--;
        }
    }

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

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

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