Skip to content

Instantly share code, notes, and snippets.

@vemacs
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save vemacs/b760ab4002cfa5e8ddc3 to your computer and use it in GitHub Desktop.

Select an option

Save vemacs/b760ab4002cfa5e8ddc3 to your computer and use it in GitHub Desktop.
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
public class TPSUtil extends BukkitRunnable {
private long lastTick;
private Deque<Long> tickIntervals;
int resolution = 40;
public TPSTask(Plugin plugin) {
lastTick = System.currentTimeMillis();
tickIntervals = new ArrayDeque<>(Collections.nCopies(resolution, 50L));
this.runTaskTimer(plugin, 1, 1);
}
@Override
public void run() {
long curr = System.currentTimeMillis();
long delta = curr - lastTick;
lastTick = curr;
tickIntervals.removeFirst();
tickIntervals.addLast(delta);
}
public double getTPS() {
int base = 0;
for (long delta : tickIntervals) {
base += delta;
}
return 1000D / ((double) base / resolution);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment