Use Google commons

This commit is contained in:
Mark Vainomaa 2016-12-22 03:55:59 +02:00
parent 0a0c2dbe1b
commit 9fd007bfb1

View File

@ -12,6 +12,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
public class ServerUtils { public class ServerUtils {
/** /**
* Get server's TPS * Get server's TPS
@ -20,21 +22,17 @@ public class ServerUtils {
* @return double array of TPS (not rounded!), values are -1 if reflection failed * @return double array of TPS (not rounded!), values are -1 if reflection failed
*/ */
public static double[] getTPS(){ public static double[] getTPS(){
if(Bukkit.getVersion().contains("Paper")) { if(Reflect.getMethod(Bukkit.class, "getTps") != null) {
return Bukkit.getTPS(); return Bukkit.getTPS();
} else { } else {
Class<?> MinecraftServerClass = Reflect.getClass( try {
String.format("net.minecraft.server.%s.MinecraftServer", getNmsVersion())); Class<?> MinecraftServerClass = checkNotNull(Reflect.getClass(
if(MinecraftServerClass != null){ String.format("net.minecraft.server.%s.MinecraftServer", getNmsVersion())));
Method getServer = Reflect.getMethod(MinecraftServerClass, "getServer"); Method getServer = checkNotNull(Reflect.getMethod(MinecraftServerClass, "getServer"));
Field recentTPS = Reflect.getField(MinecraftServerClass, "recentTps"); Field recentTPS = checkNotNull(Reflect.getField(MinecraftServerClass, "recentTps"));
if(getServer != null && recentTPS != null){ Object server = checkNotNull(Reflect.invokeMethod(getServer, null));
Object server = Reflect.invokeMethod(getServer, null); return (double[]) checkNotNull(Reflect.readField(recentTPS, server));
try { } catch (NullPointerException ignored){}
return (double[]) Reflect.readField(recentTPS, server);
} catch (NullPointerException e){}
}
}
} }
return new double[]{-1, -1, -1}; return new double[]{-1, -1, -1};
} }