Skip to content

Commit

Permalink
store instance and turn into getter
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 6, 2024
1 parent 09c7082 commit 2d998de
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions shared/src/main/java/me/xginko/aef/utils/SpigotReflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@

public final class SpigotReflection {

private static SpigotReflection instance;
private static Class<?> MinecraftServer_class;
private static MethodHandle MinecraftServer_getServer_method;
private static Field MinecraftServer_recentTps_field, MinecraftServer_recentTickTimes_field;
private static Class<?> MinecraftServer_class;

public static SpigotReflection load() {
MinecraftServer_class = needNMSClassOrElse("MinecraftServer", "net.minecraft.server.MinecraftServer");
MinecraftServer_getServer_method = needStaticMethod(MinecraftServer_class, "getServer", MinecraftServer_class);
MinecraftServer_recentTps_field = needField(MinecraftServer_class, "recentTps"); // Spigot added field
MinecraftServer_recentTickTimes_field = tickTimesField();
return new SpigotReflection();
public static SpigotReflection getInstance() {
if (instance == null) {
MinecraftServer_class = needNMSClassOrElse("MinecraftServer", "net.minecraft.server.MinecraftServer");
MinecraftServer_getServer_method = needStaticMethod(MinecraftServer_class, "getServer", MinecraftServer_class);
MinecraftServer_recentTps_field = needField(MinecraftServer_class, "recentTps"); // Spigot added field
MinecraftServer_recentTickTimes_field = tickTimesField();
instance = new SpigotReflection();
}
return instance;
}

private static @NonNull Field tickTimesField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class FallbackTickReporter implements TickReporter {

public FallbackTickReporter(Duration cacheDuration) {
this.cache = Caffeine.newBuilder().expireAfterWrite(cacheDuration).build();
this.spigotReflection = SpigotReflection.load();
this.spigotReflection = SpigotReflection.getInstance();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class LegacyPaperTickReporter implements TickReporter {
public LegacyPaperTickReporter(JavaPlugin plugin, Duration cacheDuration) {
this.server = plugin.getServer();
this.cache = Caffeine.newBuilder().expireAfterWrite(cacheDuration).build();
this.spigotReflection = SpigotReflection.load();
this.spigotReflection = SpigotReflection.getInstance();
}

public static boolean isSupported() {
Expand Down

0 comments on commit 2d998de

Please sign in to comment.