aboutsummaryrefslogtreecommitdiff
path: root/bukkit/src/main/java/me/lucko/spark/bukkit/BukkitTickCounter.java
blob: 61a7690be4ae4a341d445fb293f9b8d4033e91ee (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
44
45
46
47
48
49
50
51
52
53
54
55
package me.lucko.spark.bukkit;

import me.lucko.spark.profiler.TickCounter;

import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.LongAdder;

public class BukkitTickCounter implements TickCounter, Runnable {
    private final Plugin plugin;
    private BukkitTask task;

    private final Set<Runnable> tasks = new HashSet<>();
    private final LongAdder tick = new LongAdder();

    public BukkitTickCounter(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public void run() {
        this.tick.increment();
        for (Runnable r : this.tasks){
            r.run();
        }
    }

    @Override
    public void start() {
        this.task = this.plugin.getServer().getScheduler().runTaskTimer(this.plugin, this, 1, 1);
    }

    @Override
    public void close() {
        this.task.cancel();
    }

    @Override
    public long getCurrentTick() {
        return this.tick.longValue();
    }

    @Override
    public void addTickTask(Runnable runnable) {
        this.tasks.add(runnable);
    }

    @Override
    public void removeTickTask(Runnable runnable) {
        this.tasks.remove(runnable);
    }
}