Add method to get server TPS
This commit is contained in:
parent
3c06b62c91
commit
3532c91731
@ -10,8 +10,13 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>bukkitutils</artifactId>
|
<artifactId>bukkitutils</artifactId>
|
||||||
|
<version>1.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>destroystokyo-repo</id>
|
||||||
|
<url>https://repo.destroystokyo.com/repository/maven-public//</url>
|
||||||
|
</repository>
|
||||||
<repository>
|
<repository>
|
||||||
<id>spigot-repo</id>
|
<id>spigot-repo</id>
|
||||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||||
@ -24,6 +29,12 @@
|
|||||||
<artifactId>reflect</artifactId>
|
<artifactId>reflect</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.destroystokyo.paper</groupId>
|
||||||
|
<artifactId>paper-api</artifactId>
|
||||||
|
<version>1.10.2-R0.1-SNAPSHOT</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bukkit</groupId>
|
<groupId>org.bukkit</groupId>
|
||||||
<artifactId>bukkit</artifactId>
|
<artifactId>bukkit</artifactId>
|
||||||
|
@ -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};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user