aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/util/TickDelay.java
blob: a07a45213c89040f30e1573afb606d65a1cac78e (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
package de.cowtipper.cowlection.util;

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

public class TickDelay {
    private final Runnable task;
    private int waitingTicks;

    public TickDelay(Runnable task, int ticks) {
        this.task = task;
        this.waitingTicks = ticks;

        MinecraftForge.EVENT_BUS.register(this);
    }

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent e) {
        if (e.phase == TickEvent.Phase.START) {
            if (waitingTicks < 1) {
                // we're done waiting! Do stuff and exit.
                task.run();
                MinecraftForge.EVENT_BUS.unregister(this);
            }
            waitingTicks--;
        }
    }
}