|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import dev.faststats.bungee.BungeeMetrics; |
| 4 | +import dev.faststats.core.ErrorTracker; |
| 5 | +import dev.faststats.core.Metrics; |
| 6 | +import dev.faststats.core.chart.Chart; |
| 7 | +import net.md_5.bungee.api.plugin.Plugin; |
| 8 | + |
| 9 | +import java.net.URI; |
| 10 | + |
| 11 | +public class ExamplePlugin extends Plugin { |
| 12 | + // context-aware error tracker, automatically tracks errors in the same class loader |
| 13 | + public static final ErrorTracker ERROR_TRACKER = ErrorTracker.contextAware(); |
| 14 | + |
| 15 | + // context-unaware error tracker, does not automatically track errors |
| 16 | + public static final ErrorTracker CONTEXT_UNAWARE_ERROR_TRACKER = ErrorTracker.contextUnaware(); |
| 17 | + |
| 18 | + private final Metrics metrics = BungeeMetrics.factory() |
| 19 | + .url(URI.create("https://metrics.example.com/v1/collect")) // For self-hosted metrics servers only |
| 20 | + |
| 21 | + // Custom example charts |
| 22 | + // For this to work you have to create a corresponding data source in your project settings first |
| 23 | + .addChart(Chart.number("example_chart", () -> 42)) |
| 24 | + .addChart(Chart.string("example_string", () -> "Hello, World!")) |
| 25 | + .addChart(Chart.bool("example_boolean", () -> true)) |
| 26 | + .addChart(Chart.stringArray("example_string_array", () -> new String[]{"Option 1", "Option 2"})) |
| 27 | + .addChart(Chart.numberArray("example_number_array", () -> new Number[]{1, 2, 3})) |
| 28 | + .addChart(Chart.booleanArray("example_boolean_array", () -> new Boolean[]{true, false})) |
| 29 | + |
| 30 | + // Attach an error tracker |
| 31 | + // This must be enabled in the project settings |
| 32 | + .errorTracker(ERROR_TRACKER) |
| 33 | + |
| 34 | + .debug(true) // Enable debug mode for development and testing |
| 35 | + |
| 36 | + .token("YOUR_TOKEN_HERE") // required -> token can be found in the settings of your project |
| 37 | + .create(this); |
| 38 | + |
| 39 | + @Override |
| 40 | + public void onDisable() { |
| 41 | + metrics.shutdown(); |
| 42 | + } |
| 43 | + |
| 44 | + public void doSomethingWrong() { |
| 45 | + try { |
| 46 | + // Do something that might throw an error |
| 47 | + throw new RuntimeException("Something went wrong!"); |
| 48 | + } catch (Exception e) { |
| 49 | + CONTEXT_UNAWARE_ERROR_TRACKER.trackError(e); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments