Skip to content

Commit fddb279

Browse files
committed
Added injection support for platform context
1 parent 6d0c4f8 commit fddb279

7 files changed

Lines changed: 129 additions & 58 deletions

File tree

sponge/example-plugin/src/main/java/com/example/ExamplePlugin.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,30 @@
55
import dev.faststats.Metrics;
66
import dev.faststats.data.Metric;
77
import dev.faststats.sponge.SpongeContext;
8-
import org.apache.logging.log4j.Logger;
98
import org.jspecify.annotations.Nullable;
109
import org.spongepowered.api.Server;
11-
import org.spongepowered.api.config.ConfigDir;
1210
import org.spongepowered.api.event.Listener;
1311
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
1412
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
15-
import org.spongepowered.plugin.PluginContainer;
1613
import org.spongepowered.plugin.builtin.jvm.Plugin;
1714

18-
import java.nio.file.Path;
19-
2015
@Plugin("example")
2116
public class ExamplePlugin {
22-
private @Inject PluginContainer pluginContainer;
23-
private @Inject Logger logger;
24-
private @ConfigDir(sharedRoot = true)
25-
@Inject Path dataDirectory;
17+
private @Inject SpongeContext.Builder contextBuilder;
2618

2719
private @Nullable Metrics metrics = null;
2820

2921
@Listener
3022
public void onServerStart(final StartedEngineEvent<Server> event) {
31-
final var context = new SpongeContext(pluginContainer, logger, dataDirectory, "YOUR_TOKEN_HERE");
23+
final var context = contextBuilder.build("YOUR_TOKEN_HERE");
3224
this.metrics = context.metrics()
3325
// Custom metrics require a corresponding data source in your project settings
3426
.addMetric(Metric.number("example_metric", () -> 42))
3527

3628
// Error tracking must be enabled in the project settings
3729
.errorTracker(ErrorTracker.contextAware())
3830

39-
.create(pluginContainer);
31+
.create();
4032
}
4133

4234
@Listener

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.faststats.sponge;
22

3+
import com.google.inject.Inject;
34
import dev.faststats.SimpleContext;
45
import dev.faststats.Token;
56
import org.apache.logging.log4j.Logger;
@@ -21,7 +22,7 @@ public SpongeContext(
2122
final PluginContainer plugin,
2223
final Logger logger,
2324
@ConfigDir(sharedRoot = true) final Path dataDirectory,
24-
@Token final String token // fixme: cannot have a token here
25+
@Token final String token
2526
) {
2627
super(SpongeConfig.read(plugin, dataDirectory.resolve("faststats").resolve("config.properties")), token);
2728
this.plugin = plugin;
@@ -32,4 +33,47 @@ public SpongeContext(
3233
public SpongeMetrics.Factory metrics() {
3334
return new SpongeMetrics.Factory(this);
3435
}
36+
37+
/**
38+
* Injectable Sponge context builder.
39+
*
40+
* @since 0.23.0
41+
*/
42+
public static final class Builder {
43+
private final PluginContainer plugin;
44+
private final Logger logger;
45+
private final Path dataDirectory;
46+
47+
/**
48+
* Creates a new Sponge context builder.
49+
*
50+
* @param plugin the plugin container
51+
* @param logger the plugin logger
52+
* @param dataDirectory the shared Sponge config directory
53+
* @apiNote This instance can be injected into your plugin.
54+
* @since 0.23.0
55+
*/
56+
@Inject
57+
public Builder(
58+
final PluginContainer plugin,
59+
final Logger logger,
60+
@ConfigDir(sharedRoot = true) final Path dataDirectory
61+
) {
62+
this.plugin = plugin;
63+
this.logger = logger;
64+
this.dataDirectory = dataDirectory;
65+
}
66+
67+
/**
68+
* Builds the finalized Sponge context.
69+
*
70+
* @param token the FastStats project token
71+
* @return the Sponge context
72+
* @throws IllegalArgumentException if the token is invalid
73+
* @since 0.23.0
74+
*/
75+
public SpongeContext build(@Token final String token) throws IllegalArgumentException {
76+
return new SpongeContext(plugin, logger, dataDirectory, token);
77+
}
78+
}
3579
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected void appendDefaultData(final JsonObject metrics) {
4141
metrics.addProperty("server_type", Sponge.platform().container(Platform.Component.IMPLEMENTATION).metadata().id());
4242
}
4343

44-
static class Factory extends SimpleMetrics.Factory<SpongeMetrics.Factory> {
44+
static class Factory extends SimpleMetrics.Factory {
4545
public Factory(final FastStatsContext context) {
4646
super(context);
4747
}

velocity/example-plugin/src/main/java/com/example/ExamplePlugin.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@
55
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
66
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
77
import com.velocitypowered.api.plugin.Plugin;
8-
import com.velocitypowered.api.plugin.annotation.DataDirectory;
9-
import com.velocitypowered.api.proxy.ProxyServer;
108
import dev.faststats.ErrorTracker;
119
import dev.faststats.Metrics;
1210
import dev.faststats.data.Metric;
1311
import dev.faststats.velocity.VelocityContext;
1412
import org.jspecify.annotations.Nullable;
15-
import org.slf4j.Logger;
16-
17-
import java.nio.file.Path;
1813

1914
@Plugin(id = "example", name = "Example Plugin", version = "1.0.0",
2015
url = "https://example.com", authors = {"Your Name"})
@@ -23,8 +18,8 @@ public class ExamplePlugin {
2318
private @Nullable Metrics metrics = null;
2419

2520
@Inject
26-
public ExamplePlugin(final ProxyServer server, final Logger logger, @DataDirectory final Path dataDirectory) {
27-
this.context = new VelocityContext(server, logger, dataDirectory, "YOUR_TOKEN_HERE");
21+
public ExamplePlugin(final VelocityContext.Builder contextBuilder) {
22+
this.context = contextBuilder.build("YOUR_TOKEN_HERE");
2823
}
2924

3025
@Subscribe
@@ -36,7 +31,7 @@ public void onProxyInitialize(final ProxyInitializeEvent event) {
3631
// Error tracking must be enabled in the project settings
3732
.errorTracker(ErrorTracker.contextAware())
3833

39-
.create(this);
34+
.create();
4035
}
4136

4237
@Subscribe
Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package dev.faststats.velocity;
22

3+
import com.google.inject.Inject;
4+
import com.velocitypowered.api.plugin.PluginContainer;
35
import com.velocitypowered.api.plugin.annotation.DataDirectory;
46
import com.velocitypowered.api.proxy.ProxyServer;
57
import dev.faststats.Config;
6-
import dev.faststats.FastStatsContext;
8+
import dev.faststats.SimpleContext;
79
import dev.faststats.Token;
810
import dev.faststats.config.SimpleConfig;
911
import org.slf4j.Logger;
@@ -15,35 +17,85 @@
1517
*
1618
* @since 0.23.0
1719
*/
18-
public final class VelocityContext extends FastStatsContext {
20+
public final class VelocityContext extends SimpleContext {
21+
private final PluginContainer plugin;
1922
private final ProxyServer server;
2023
private final Logger logger;
2124
private final Path dataDirectory;
2225

2326
public VelocityContext(
2427
final Config config,
28+
final PluginContainer plugin,
2529
final ProxyServer server,
2630
final Logger logger,
2731
final Path dataDirectory,
2832
@Token final String token
2933
) {
3034
super(config, token);
35+
this.plugin = plugin;
3136
this.server = server;
3237
this.logger = logger;
3338
this.dataDirectory = dataDirectory;
3439
}
3540

3641
public VelocityContext(
42+
final PluginContainer plugin,
3743
final ProxyServer server,
3844
final Logger logger,
3945
@DataDirectory final Path dataDirectory,
4046
@Token final String token
4147
) {
42-
this(SimpleConfig.read(dataDirectory.resolveSibling("faststats").resolve("config.properties")), server, logger, dataDirectory, token);
48+
this(SimpleConfig.read(dataDirectory.resolveSibling("faststats").resolve("config.properties")), plugin, server, logger, dataDirectory, token);
4349
}
4450

4551
@Override
4652
public VelocityMetrics.Factory metrics() {
47-
return new VelocityMetrics.Factory(this, server, logger, dataDirectory);
53+
return new VelocityMetrics.Factory(this, plugin, server, logger, dataDirectory);
54+
}
55+
56+
/**
57+
* Injectable Velocity context builder.
58+
*
59+
* @since 0.23.0
60+
*/
61+
public static final class Builder {
62+
private final PluginContainer plugin;
63+
private final ProxyServer server;
64+
private final Logger logger;
65+
private final Path dataDirectory;
66+
67+
/**
68+
* Creates a new Velocity context builder.
69+
*
70+
* @param server the velocity server
71+
* @param logger the plugin logger
72+
* @param dataDirectory the plugin data directory
73+
* @apiNote This instance can be injected into your plugin.
74+
* @since 0.23.0
75+
*/
76+
@Inject
77+
public Builder(
78+
final PluginContainer plugin,
79+
final ProxyServer server,
80+
final Logger logger,
81+
@DataDirectory final Path dataDirectory
82+
) {
83+
this.plugin = plugin;
84+
this.server = server;
85+
this.logger = logger;
86+
this.dataDirectory = dataDirectory;
87+
}
88+
89+
/**
90+
* Builds the finalized Velocity context.
91+
*
92+
* @param token the FastStats project token
93+
* @return the Velocity context
94+
* @throws IllegalArgumentException if the token is invalid
95+
* @since 0.23.0
96+
*/
97+
public VelocityContext build(@Token final String token) throws IllegalArgumentException {
98+
return new VelocityContext(plugin, server, logger, dataDirectory, token);
99+
}
48100
}
49101
}
Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.faststats.velocity;
22

3-
import com.google.inject.Inject;
3+
import com.velocitypowered.api.plugin.PluginContainer;
44
import com.velocitypowered.api.plugin.annotation.DataDirectory;
55
import com.velocitypowered.api.proxy.ProxyServer;
66
import dev.faststats.Metrics;
@@ -15,22 +15,14 @@
1515
*/
1616
public sealed interface VelocityMetrics extends Metrics permits VelocityMetricsImpl {
1717
final class Factory extends VelocityMetricsImpl.Factory {
18-
public Factory(final VelocityContext context, final ProxyServer server, final Logger logger, @DataDirectory final Path dataDirectory) {
19-
super(context, server, logger, dataDirectory);
20-
}
21-
22-
/**
23-
* Creates a new metrics factory for Velocity.
24-
*
25-
* @param server the velocity server
26-
* @param logger the logger
27-
* @param dataDirectory the data directory
28-
* @apiNote This instance is automatically injected into your plugin.
29-
* @since 0.1.0
30-
*/
31-
@Inject
32-
private Factory(final ProxyServer server, final Logger logger, @DataDirectory final Path dataDirectory) {
33-
super(server, logger, dataDirectory);
18+
public Factory(
19+
final VelocityContext context,
20+
final PluginContainer plugin,
21+
final ProxyServer server,
22+
final Logger logger,
23+
@DataDirectory final Path dataDirectory
24+
) {
25+
super(context, plugin, server, logger, dataDirectory);
3426
}
3527
}
3628
}

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,21 @@ protected void appendDefaultData(final JsonObject metrics) {
5151
metrics.addProperty("server_type", server.getVersion().getName());
5252
}
5353

54-
static class Factory extends SimpleMetrics.Factory<VelocityMetrics.Factory> {
54+
static class Factory extends SimpleMetrics.Factory {
5555
protected final Logger logger;
5656
protected final Path dataDirectory;
5757
protected final ProxyServer server;
58+
protected final PluginContainer plugin;
5859

59-
public Factory(final ProxyServer server, final Logger logger, @DataDirectory final Path dataDirectory) {
60-
this(null, server, logger, dataDirectory);
61-
}
62-
63-
public Factory(final FastStatsContext context, final ProxyServer server, final Logger logger, @DataDirectory final Path dataDirectory) {
60+
public Factory(
61+
final FastStatsContext context,
62+
final PluginContainer plugin,
63+
final ProxyServer server,
64+
final Logger logger,
65+
@DataDirectory final Path dataDirectory
66+
) {
6467
super(context);
68+
this.plugin = plugin;
6569
this.logger = logger;
6670
this.dataDirectory = dataDirectory;
6771
this.server = server;
@@ -72,21 +76,13 @@ public Factory(final FastStatsContext context, final ProxyServer server, final L
7276
* <p>
7377
* Metrics submission will start automatically.
7478
*
75-
* @param plugin the plugin instance
7679
* @return the metrics instance
7780
* @throws IllegalStateException if the token is not specified
78-
* @throws IllegalArgumentException if the given object is not a valid plugin
79-
* @see #token(String)
8081
* @since 0.23.0
8182
*/
8283
@Override
83-
public Metrics create(final Object plugin) throws IllegalStateException, IllegalArgumentException {
84-
final var faststats = dataDirectory.resolveSibling("faststats");
85-
final var container = server.getPluginManager().ensurePluginContainer(plugin);
86-
final var config = hasContext()
87-
? getConfigOrThrow()
88-
: SimpleConfig.read(faststats.resolve("config.properties"));
89-
return new VelocityMetricsImpl(this, logger, server, config, container);
84+
public Metrics create() throws IllegalStateException {
85+
return new VelocityMetricsImpl(this, logger, server, context.getConfig(), plugin);
9086
}
9187
}
9288
}

0 commit comments

Comments
 (0)