Skip to content

Commit ca5f5be

Browse files
committed
Cache MSPT accessor
Affects issues: - Fixed #4404
1 parent 0896fdc commit ca5f5be

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

  • Plan/bukkit/src/main/java/com/djrapitops/plan/gathering/timed/mspt

Plan/bukkit/src/main/java/com/djrapitops/plan/gathering/timed/mspt/SpigotMspt.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.djrapitops.plan.utilities.java.Reflection;
2020

2121
import java.util.Optional;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2223

2324
/**
2425
* Accessor for the average MSPT field in MinecraftServer class which is a long[] array with 100 values.
@@ -27,18 +28,40 @@
2728
*/
2829
public class SpigotMspt {
2930

31+
private static final AtomicBoolean PREPARED_SERVER = new AtomicBoolean(false);
32+
private static Reflection.FieldAccessor<long[]> msptField;
33+
private static Object server;
34+
35+
private SpigotMspt() {
36+
/* Static method class */
37+
}
38+
3039
public static Optional<long[]> getMspt() {
3140
return Optional.ofNullable(getValue());
3241
}
3342

3443
private static long[] getValue() {
3544
try {
36-
Class<?> minecraftServerClass = Reflection.getMinecraftClass("MinecraftServer");
37-
Reflection.FieldAccessor<long[]> field = Reflection.findField(minecraftServerClass, long[].class);
38-
return field.get(Reflection.getMinecraftServer().orElse(null));
45+
prepField();
46+
prepServer();
47+
return msptField.get(server);
3948
} catch (Exception | NoClassDefFoundError | NoSuchFieldError e) {
4049
return null;
4150
}
4251
}
4352

53+
private static void prepServer() {
54+
if (!PREPARED_SERVER.get()) {
55+
server = Reflection.getMinecraftServer().orElse(null);
56+
PREPARED_SERVER.set(true);
57+
}
58+
}
59+
60+
private static void prepField() {
61+
if (msptField == null) {
62+
Class<?> minecraftServerClass = Reflection.getMinecraftClass("MinecraftServer");
63+
msptField = Reflection.findField(minecraftServerClass, long[].class);
64+
}
65+
}
66+
4467
}

0 commit comments

Comments
 (0)