diff --git a/pom.xml b/pom.xml index 247473c..308159e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 me.vemacs MagicTPS - 6.9-R2 + 6.9-R3 MagicTPS @@ -32,8 +32,8 @@ - org.spigotmc - spigot-api + org.github.paperspigot + paperspigot-api 1.8.7-R0.1-SNAPSHOT jar compile diff --git a/src/main/java/me/vemacs/magictps/MagicTPS.java b/src/main/java/me/vemacs/magictps/MagicTPS.java index 2699510..7c300de 100644 --- a/src/main/java/me/vemacs/magictps/MagicTPS.java +++ b/src/main/java/me/vemacs/magictps/MagicTPS.java @@ -8,9 +8,11 @@ public class MagicTPS extends JavaPlugin implements Listener { private double tpsMultiplier; + private static MagicTPS instance; @Override public void onEnable() { + instance = this; saveDefaultConfig(); getServer().getPluginManager().registerEvents(this, this); tpsMultiplier = getConfig().getDouble("tps-multiplier"); @@ -38,4 +40,8 @@ private String format(double actualTps) { + ((magicTps > maxMagicTps) ? "*" : "") + Math.min(Math.round(magicTps * 100.0) / 100.0, maxMagicTps); } + + public static MagicTPS getInstance() { + return instance; + } } \ No newline at end of file diff --git a/src/main/java/me/vemacs/magictps/TPSUtil.java b/src/main/java/me/vemacs/magictps/TPSUtil.java index 8656eb6..45daa23 100644 --- a/src/main/java/me/vemacs/magictps/TPSUtil.java +++ b/src/main/java/me/vemacs/magictps/TPSUtil.java @@ -8,22 +8,50 @@ public class TPSUtil { private static Object minecraftServer; private static Field recentTps; + private static Boolean isPaperSpigot = null; public static double[] getRecentTps() { - try { - if (minecraftServer == null) { - Server server = Bukkit.getServer(); - Field consoleField = server.getClass().getDeclaredField("console"); - consoleField.setAccessible(true); - minecraftServer = consoleField.get(server); + if (isPaperSpigot == null) { + try { + double[] recentTps = getRecentTpsPaperSpigot(); + isPaperSpigot = true; + MagicTPS.getInstance().getLogger().info("Using PaperSpigot-based recentTps"); + return recentTps; + } catch (Throwable t) { + isPaperSpigot = false; + MagicTPS.getInstance().getLogger().info("Using reflection-based recentTps"); } - if (recentTps == null) { - recentTps = minecraftServer.getClass().getSuperclass().getDeclaredField("recentTps"); - recentTps.setAccessible(true); + } + if (isPaperSpigot) { + try { + return getRecentTpsPaperSpigot(); + } catch (Throwable throwable) { + return new double[]{20, 20, 20}; + } + } else { + try { + return getRecentTpsRefl(); + } catch (Throwable throwable) { + return new double[]{20, 20, 20}; } - return (double[]) recentTps.get(minecraftServer); - } catch (IllegalAccessException | NoSuchFieldException ignored) { } - return new double[] {20, 20, 20}; + } + + private static double[] getRecentTpsPaperSpigot() throws Throwable { + return Bukkit.getServer().spigot().getTPS(); + } + + private static double[] getRecentTpsRefl() throws Throwable { + if (minecraftServer == null) { + Server server = Bukkit.getServer(); + Field consoleField = server.getClass().getDeclaredField("console"); + consoleField.setAccessible(true); + minecraftServer = consoleField.get(server); + } + if (recentTps == null) { + recentTps = minecraftServer.getClass().getSuperclass().getDeclaredField("recentTps"); + recentTps.setAccessible(true); + } + return (double[]) recentTps.get(minecraftServer); } }