Skip to content

Commit ea42205

Browse files
committed
Move context initialization out of constructor
1 parent 894a3c9 commit ea42205

10 files changed

Lines changed: 16 additions & 3 deletions

File tree

bukkit/src/main/java/dev/faststats/bukkit/BukkitContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public final class BukkitContext extends SimpleContext {
1919
private BukkitContext(final Factory factory, final Plugin plugin, @Token final String token) {
2020
super(factory, SimpleConfig.read(getConfigPath(plugin)), "bukkit", token);
2121
this.plugin = plugin;
22+
initializeServices(factory);
2223
}
2324

2425
@Override

bungeecord/src/main/java/dev/faststats/bungee/BungeeContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public final class BungeeContext extends SimpleContext {
1919
private BungeeContext(final Factory factory, final Plugin plugin, @Token final String token) {
2020
super(factory, SimpleConfig.read(plugin.getProxy().getPluginsFolder().toPath().resolve("faststats").resolve("config.properties")), "bungeecord", token);
2121
this.plugin = plugin;
22+
initializeServices(factory);
2223
}
2324

2425
@Override

core/src/main/java/dev/faststats/SimpleContext.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.faststats.internal.Logger;
44
import dev.faststats.internal.LoggerFactory;
55
import org.jetbrains.annotations.Contract;
6+
import org.jetbrains.annotations.MustBeInvokedByOverriders;
67
import org.jspecify.annotations.Nullable;
78

89
import java.io.IOException;
@@ -19,9 +20,9 @@ public non-sealed abstract class SimpleContext implements FastStatsContext {
1920
private final @Token String token;
2021
private final SdkInfo sdkInfo;
2122

22-
private final @Nullable Metrics metrics;
23-
private final @Nullable FeatureFlagService featureFlagService;
24-
private final @Nullable ErrorTrackerService errorTrackerService;
23+
private @Nullable Metrics metrics;
24+
private @Nullable FeatureFlagService featureFlagService;
25+
private @Nullable ErrorTrackerService errorTrackerService;
2526

2627
/**
2728
* Creates a new context that stores the shared configuration and token for all FastStats services.
@@ -42,7 +43,10 @@ protected SimpleContext(final Factory<?, ?> factory, final Config config, final
4243
this.sdkInfo = constructSdkInfo(name);
4344
this.config = config;
4445
this.token = token;
46+
}
4547

48+
@MustBeInvokedByOverriders
49+
protected final void initializeServices(final Factory<?, ?> factory) throws IllegalStateException {
4650
this.metrics = factory.metrics != null ? factory.metrics.apply(metricsFactory()) : null;
4751
this.errorTrackerService = factory.errorTracker != null ? new SimpleErrorTrackerService(this, factory.errorTracker) : null;
4852
this.featureFlagService = factory.featureFlagService != null ? factory.featureFlagService.apply(new SimpleFeatureFlagService.Factory(config, token)) : null;

core/src/test/java/dev/faststats/MockContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public final class MockContext extends SimpleContext {
66
private MockContext(final Factory factory) throws IllegalArgumentException {
77
super(factory, new MockConfig(UUID.randomUUID()), "test", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
8+
initializeServices(factory);
89
}
910

1011
@Override

fabric/src/main/java/dev/faststats/fabric/FabricContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private FabricContext(final Factory factory, final String modId, @Token final St
2222
this.mod = FabricLoader.getInstance().getModContainer(modId).orElseThrow(() -> {
2323
return new IllegalArgumentException("Mod not found: " + modId);
2424
});
25+
initializeServices(factory);
2526
}
2627

2728
@Override

hytale/src/main/java/dev/faststats/hytale/HytaleContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public final class HytaleContext extends SimpleContext {
1919
private HytaleContext(final Factory factory, final JavaPlugin plugin, @Token final String token) {
2020
super(factory, SimpleConfig.read(plugin.getDataDirectory().toAbsolutePath().getParent().resolve("faststats").resolve("config.properties")), "hytale", token);
2121
this.pluginName = plugin.getName();
22+
initializeServices(factory);
2223
}
2324

2425
@Override

minestom/src/main/java/dev/faststats/minestom/MinestomContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
public final class MinestomContext extends SimpleContext {
1919
MinestomContext(final Factory factory, @Token final String token) {
2020
super(factory, SimpleConfig.read(Path.of("faststats", "config.properties")), "minestom", token);
21+
initializeServices(factory);
2122
}
2223

2324
@Override

nukkit/src/main/java/dev/faststats/nukkit/NukkitContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public final class NukkitContext extends SimpleContext {
2121
private NukkitContext(final Factory factory, final PluginBase plugin, @Token final String token) {
2222
super(factory, SimpleConfig.read(Path.of(plugin.getServer().getPluginPath(), "faststats", "config.properties")), "nukkit", token);
2323
this.plugin = plugin;
24+
initializeServices(factory);
2425
}
2526

2627
@Override

sponge/src/main/java/dev/faststats/sponge/SpongeContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private SpongeContext(
2727
) {
2828
super(factory, SpongeConfig.read(plugin, dataDirectory.resolve("faststats").resolve("config.properties")), "sponge", token);
2929
this.plugin = plugin;
30+
initializeServices(factory);
3031
}
3132

3233
@Override

velocity/src/main/java/dev/faststats/velocity/VelocityContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private VelocityContext(
3232
super(factory, SimpleConfig.read(dataDirectory.resolveSibling("faststats").resolve("config.properties")), "velocity", token);
3333
this.plugin = plugin;
3434
this.server = server;
35+
initializeServices(factory);
3536
}
3637

3738
@Override

0 commit comments

Comments
 (0)