@@ -17,6 +17,12 @@ public abstract class TickDurationCollector extends Metric {
17
17
*/
18
18
private static long [] tickDurationReference = null ;
19
19
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
+
20
26
public TickDurationCollector (Plugin plugin , Gauge gauge , String name ) {
21
27
super (plugin , gauge );
22
28
@@ -27,7 +33,17 @@ public TickDurationCollector(Plugin plugin, Gauge gauge, String name) {
27
33
* This searches for any long[] array in the MinecraftServer class. It should
28
34
* work across many versions of Spigot/Paper and various obfuscation mappings
29
35
*/
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
+
31
47
long [] longestArray = null ;
32
48
33
49
try {
@@ -62,12 +78,42 @@ public TickDurationCollector(Plugin plugin, Gauge gauge, String name) {
62
78
}
63
79
}
64
80
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
+
65
107
/**
66
108
* Returns either the internal minecraft long array for tick times in ns,
67
109
* or a long array containing just one element of value -1 if reflection
68
110
* was unable to locate the minecraft tick times buffer
69
111
*/
70
112
protected static long [] getTickDurations () {
113
+ if (usePaperMethod ) {
114
+ return getPaperTickTimes ();
115
+ }
116
+
71
117
return tickDurationReference ;
72
118
}
73
119
}
0 commit comments