Skip to content

Commit 247fc23

Browse files
authored
Merge pull request #217 from radiant-ai/master
Use paper tick times method when available
2 parents dc08036 + a364980 commit 247fc23

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/main/java/de/sldk/mc/metrics/TickDurationCollector.java

+47-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public abstract class TickDurationCollector extends Metric {
1717
*/
1818
private static long[] tickDurationReference = null;
1919

20+
/*
21+
* If this server is Paper and has a shorthand method
22+
* this will be set to true to use the method instead
23+
*/
24+
private static boolean usePaperMethod = false;
25+
2026
public TickDurationCollector(Plugin plugin, Gauge gauge, String name) {
2127
super(plugin, gauge);
2228

@@ -27,7 +33,17 @@ public TickDurationCollector(Plugin plugin, Gauge gauge, String name) {
2733
* This searches for any long[] array in the MinecraftServer class. It should
2834
* work across many versions of Spigot/Paper and various obfuscation mappings
2935
*/
30-
if (tickDurationReference == null) {
36+
if (tickDurationReference == null && !usePaperMethod) {
37+
38+
/* Check if this server is Paper and has a handy method */
39+
if (getPaperTickTimes() != null) {
40+
usePaperMethod = true;
41+
plugin.getLogger().log(Level.FINE, "Managed to get Paper tick times method.");
42+
return;
43+
}
44+
45+
plugin.getLogger().log(Level.FINE, "Could not get Paper tick times method.");
46+
3147
long[] longestArray = null;
3248

3349
try {
@@ -62,12 +78,42 @@ public TickDurationCollector(Plugin plugin, Gauge gauge, String name) {
6278
}
6379
}
6480

81+
/**
82+
* Attempts to get tick times from Paper
83+
* returns null if fails
84+
*/
85+
private static long[] getPaperTickTimes() {
86+
try {
87+
/* Get the actual minecraft server class */
88+
Server server = Bukkit.getServer();
89+
90+
/* Attempt to get Paper tick times method */
91+
Method paperGetTickTimesMethod = server.getClass().getMethod("getTickTimes");
92+
93+
Object tickTimes = paperGetTickTimesMethod.invoke(server);
94+
95+
/* Check the method actual return type */
96+
if (!(tickTimes instanceof long[])) {
97+
return null;
98+
}
99+
100+
return (long[]) tickTimes;
101+
}
102+
catch (Exception e) {
103+
return null;
104+
}
105+
}
106+
65107
/**
66108
* Returns either the internal minecraft long array for tick times in ns,
67109
* or a long array containing just one element of value -1 if reflection
68110
* was unable to locate the minecraft tick times buffer
69111
*/
70112
protected static long[] getTickDurations() {
113+
if (usePaperMethod) {
114+
return getPaperTickTimes();
115+
}
116+
71117
return tickDurationReference;
72118
}
73119
}

0 commit comments

Comments
 (0)