Add method to get server TPS

This commit is contained in:
Mark Vainomaa 2016-09-25 15:08:16 +03:00
parent 3c06b62c91
commit 3532c91731
2 changed files with 49 additions and 0 deletions

View File

@ -10,8 +10,13 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>bukkitutils</artifactId>
<version>1.1-SNAPSHOT</version>
<repositories>
<repository>
<id>destroystokyo-repo</id>
<url>https://repo.destroystokyo.com/repository/maven-public//</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
@ -24,6 +29,12 @@
<artifactId>reflect</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.10.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>

View File

@ -0,0 +1,38 @@
package eu.mikroskeem.utils.bukkit;
import eu.mikroskeem.utils.reflect.Reflect;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ServerUtils {
/**
* Get server's TPS
*
* Will use PaperSpigot's API if available, reflection otherwise
* @return double array of TPS (not rounded!), values are -1 if reflection failed
*/
public static double[] getTPS(){
if(Bukkit.getVersion().contains("Paper")) {
return Bukkit.getTPS();
} else {
String bukkitPackageName = Bukkit.getServer().getClass().getPackage().getName();
String nmsVer = bukkitPackageName.substring(bukkitPackageName.lastIndexOf(".") + 1);
Class<?> MinecraftServerClass = Reflect.getClass(
String.format("net.minecraft.server.%s.MinecraftServer", nmsVer));
if(MinecraftServerClass != null){
Method getServer = Reflect.getMethod(MinecraftServerClass, "getServer");
Field recentTPS = Reflect.getField(MinecraftServerClass, "recentTps");
if(getServer != null && recentTPS != null){
Object server = Reflect.invokeMethod(getServer, null);
try {
return (double[]) Reflect.readField(recentTPS, server);
} catch (NullPointerException e){}
}
}
}
return new double[]{-1, -1, -1};
}
}