1
- package de .sldk .mc .metrics ;
2
-
3
- import java .lang .reflect .Field ;
4
- import java .lang .reflect .Method ;
5
- import java .util .logging .Level ;
1
+ package de .sldk .mc .metrics .tick_duration ;
6
2
7
3
import org .bukkit .Bukkit ;
8
4
import org .bukkit .Server ;
9
- import org .bukkit .plugin .Plugin ;
10
5
11
- import io .prometheus .client .Gauge ;
6
+ import java .lang .reflect .Field ;
7
+ import java .lang .reflect .Method ;
8
+ import java .util .logging .Level ;
9
+ import java .util .logging .Logger ;
12
10
13
- public abstract class TickDurationCollector extends Metric {
11
+ public class DefaultTickDurationStrategy implements TickDurationStrategy {
14
12
/*
15
13
* If reflection is successful, this will hold a reference directly to the
16
14
* MinecraftServer internal tick duration tracker
17
15
*/
18
16
private static long [] tickDurationReference = null ;
19
17
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
-
26
- public TickDurationCollector (Plugin plugin , Gauge gauge , String name ) {
27
- super (plugin , gauge );
28
-
18
+ public DefaultTickDurationStrategy (Logger logger ) {
29
19
/*
30
20
* If there is not yet a handle to the internal tick duration buffer, try
31
21
* to acquire one using reflection.
32
22
*
33
23
* This searches for any long[] array in the MinecraftServer class. It should
34
24
* work across many versions of Spigot/Paper and various obfuscation mappings
35
25
*/
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
- }
26
+ if (tickDurationReference == null ) {
44
27
45
- plugin . getLogger () .log (Level .FINE , "Could not get Paper tick times method." );
28
+ logger .log (Level .FINE , "Could not get Paper tick times method." );
46
29
47
30
long [] longestArray = null ;
48
31
@@ -63,7 +46,7 @@ public TickDurationCollector(Plugin plugin, Gauge gauge, String name) {
63
46
}
64
47
}
65
48
} catch (Exception e ) {
66
- plugin . getLogger () .log (Level .FINE , "Caught exception looking for tick times array: " , e );
49
+ logger .log (Level .FINE , "Caught exception looking for tick times array: " , e );
67
50
}
68
51
69
52
if (longestArray != null ) {
@@ -73,47 +56,18 @@ public TickDurationCollector(Plugin plugin, Gauge gauge, String name) {
73
56
tickDurationReference = new long [1 ];
74
57
tickDurationReference [0 ] = -1 ;
75
58
76
- plugin . getLogger () .log (Level .WARNING , "Failed to find tick times buffer via reflection. Tick duration metrics will not be available." );
59
+ logger .log (Level .WARNING , "Failed to find tick times buffer via reflection. Tick duration metrics will not be available." );
77
60
}
78
61
}
79
62
}
80
63
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
-
107
64
/**
108
65
* Returns either the internal minecraft long array for tick times in ns,
109
66
* or a long array containing just one element of value -1 if reflection
110
67
* was unable to locate the minecraft tick times buffer
111
68
*/
112
- protected static long [] getTickDurations () {
113
- if (usePaperMethod ) {
114
- return getPaperTickTimes ();
115
- }
116
-
117
- return tickDurationReference ;
69
+ public long [] getTickDurations () {
70
+ // Return a copy of the array to prevent modification
71
+ return tickDurationReference .clone ();
118
72
}
119
73
}
0 commit comments