SteamWar/SpigotCore
Archiviert
13
0
Dieser Commit ist enthalten in:
jojo 2020-09-27 15:59:25 +02:00
Ursprung 74c3a5ab41
Commit 987f4f5477
2 geänderte Dateien mit 98 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -32,6 +32,7 @@ import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin{ public class Core extends JavaPlugin{
private static Core instance; private static Core instance;
private static final int version; private static final int version;
private static TPSWatcher tpsWatcher;
static{ static{
String packageName = Bukkit.getServer().getClass().getPackage().getName(); String packageName = Bukkit.getServer().getClass().getPackage().getName();
@ -52,6 +53,7 @@ public class Core extends JavaPlugin{
@Override @Override
public void onLoad() { public void onLoad() {
setInstance(this); setInstance(this);
tpsWatcher = new TPSWatcher();
} }
@Override @Override
@ -75,10 +77,14 @@ public class Core extends JavaPlugin{
public static Core getInstance() { public static Core getInstance() {
return instance; return instance;
} }
public static int getVersion(){ public static int getVersion(){
return version; return version;
} }
public static TPSWatcher getTpsWatcher() {
return tpsWatcher;
}
private static void setInstance(Core instance) { private static void setInstance(Core instance) {
Core.instance = instance; Core.instance = instance;

Datei anzeigen

@ -0,0 +1,92 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.core;
import org.bukkit.Bukkit;
import java.util.LinkedList;
public class TPSWatcher {
private static class TPSMeter {
private LinkedList<Double> meter = new LinkedList<>();
private int count;
public TPSMeter(int count) {
this.count = count;
}
public void add(double tps) {
meter.addLast(tps);
if (meter.size() > count) meter.removeFirst();
}
public double get() {
double total = 0;
for (double d : meter) total += d;
return total / meter.size();
}
}
private double now = 20.0;
private TPSMeter one_minute = new TPSMeter(1200);
private TPSMeter five_minutes = new TPSMeter(6000);
private TPSMeter ten_minutes = new TPSMeter(12000);
private long lastTime = System.currentTimeMillis();
private static final double tickTimeDefault = 1000 / 20.0;
private static final double tickDefault = 20.0;
TPSWatcher() {
Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> {
long tickTime = System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
if (tickTime != 0) {
now = (tickTimeDefault / tickTime) * tickDefault;
} else {
now = 0;
}
one_minute.add(now);
five_minutes.add(now);
ten_minutes.add(now);
}, 1, 1);
}
public double getTPS() {
return now;
}
public double get1TPS() {
return one_minute.get();
}
public double get5TPS() {
return five_minutes.get();
}
public double get10TPS() {
return ten_minutes.get();
}
}