-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExamplePlugin.java
More file actions
68 lines (55 loc) · 2.9 KB
/
ExamplePlugin.java
File metadata and controls
68 lines (55 loc) · 2.9 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.example;
import com.google.inject.Inject;
import dev.faststats.core.ErrorTracker;
import dev.faststats.core.Metrics;
import dev.faststats.core.chart.Chart;
import dev.faststats.sponge.SpongeMetrics;
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.PluginContainer;
import org.spongepowered.plugin.builtin.jvm.Plugin;
@Plugin("example")
public class ExamplePlugin {
// context-aware error tracker, automatically tracks errors in the same class loader
public static final ErrorTracker ERROR_TRACKER = ErrorTracker.contextAware();
// context-unaware error tracker, does not automatically track errors
public static final ErrorTracker CONTEXT_UNAWARE_ERROR_TRACKER = ErrorTracker.contextUnaware();
private @Inject PluginContainer pluginContainer;
private @Inject SpongeMetrics.Factory factory;
private @Nullable Metrics metrics = null;
@Listener
public void onServerStart(final StartedEngineEvent<Server> event) {
this.metrics = factory
// .url(URI.create("https://metrics.example.com/v1/collect")) // For self-hosted metrics servers only
// Custom example charts
// For this to work you have to create a corresponding data source in your project settings first
.addChart(Chart.number("example_chart", () -> 42))
.addChart(Chart.string("example_string", () -> "Hello, World!"))
.addChart(Chart.bool("example_boolean", () -> true))
.addChart(Chart.stringArray("example_string_array", () -> new String[]{"Option 1", "Option 2"}))
.addChart(Chart.numberArray("example_number_array", () -> new Number[]{1, 2, 3}))
.addChart(Chart.booleanArray("example_boolean_array", () -> new Boolean[]{true, false}))
// Attach an error tracker
// This must be enabled in the project settings
.errorTracker(ERROR_TRACKER)
//.debug(true) // Enable debug mode for development and testing
.token("bafe240e8d1a4b919e5083928539799d") // required -> token can be found in the settings of your project
.create(pluginContainer);
ERROR_TRACKER.trackError(new RuntimeException("Something went wrong!", new Exception("Cause")));
}
@Listener
public void onServerStop(final StoppingEngineEvent<Server> event) {
if (metrics != null) metrics.shutdown();
}
public void doSomethingWrong() {
try {
// Do something that might throw an error
throw new RuntimeException("Something went wrong!");
} catch (Exception e) {
CONTEXT_UNAWARE_ERROR_TRACKER.trackError(e);
}
}
}