-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExamplePlugin.java
More file actions
52 lines (43 loc) · 2.06 KB
/
Copy pathExamplePlugin.java
File metadata and controls
52 lines (43 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.example;
import com.google.inject.Inject;
import dev.faststats.ErrorTracker;
import dev.faststats.data.Metric;
import dev.faststats.sponge.SpongeContext;
import org.jspecify.annotations.Nullable;
import org.spongepowered.api.Server;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
import org.spongepowered.plugin.builtin.jvm.Plugin;
import java.util.concurrent.atomic.AtomicInteger;
@Plugin("example")
public class ExamplePlugin {
public static final ErrorTracker ERROR_TRACKER = ErrorTracker.contextAware();
private @Inject SpongeContext.Builder contextBuilder;
private final AtomicInteger gameCount = new AtomicInteger();
private @Nullable SpongeContext context = null;
@Listener
public void onServerStart(final StartedEngineEvent<Server> event) {
this.context = contextBuilder
.token("YOUR_TOKEN_HERE")
.errorTrackerService(ERROR_TRACKER)
// .metrics(Metrics.Factory::create) // Define a minimal metrics instance without any custom metrics
.metrics(factory -> factory
// Custom metrics require a corresponding data source in your project settings
.addMetric(Metric.number("game_count", gameCount::get))
.addMetric(Metric.string("server_version", () -> "1.0.0"))
// #onFlush is invoked after successful metrics submission
// This is useful for cleaning up cached data
.onFlush(() -> gameCount.set(0)) // reset game count on flush
.create())
.create();
context.ready(); // start metrics and errors submission
}
@Listener
public void onServerStop(final StoppingEngineEvent<Server> event) {
if (context != null) context.shutdown(); // safely shut down configured services
}
public void startGame() {
gameCount.incrementAndGet();
}
}