Skip to content

Commit

Permalink
Support PaperSpigot
Browse files Browse the repository at this point in the history
  • Loading branch information
vemacs committed Jul 24, 2015
1 parent 26cc0a6 commit ecbfd0b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.vemacs</groupId>
<artifactId>MagicTPS</artifactId>
<version>6.9-R2</version>
<version>6.9-R3</version>
<name>MagicTPS</name>
<repositories>
<repository>
Expand Down Expand Up @@ -32,8 +32,8 @@
</build>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<groupId>org.github.paperspigot</groupId>
<artifactId>paperspigot-api</artifactId>
<version>1.8.7-R0.1-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/me/vemacs/magictps/MagicTPS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}
}
52 changes: 40 additions & 12 deletions src/main/java/me/vemacs/magictps/TPSUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit ecbfd0b

Please sign in to comment.