-
Notifications
You must be signed in to change notification settings - Fork 2
5. Developer API
Vincenzo Reina edited this page May 5, 2025
·
9 revisions
ServerPulse provides a comprehensive API that allows other plugins to interact with and extend its functionality. This guide covers how to integrate with ServerPulse and use its features in your plugins.
Add ServerPulse to your plugin.yml
:
depend: [ServerPulse] # Hard dependency
# OR
softdepend: [ServerPulse] # Soft dependency
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<dependency>
<groupId>com.github.renvins</groupId>
<artifactId>serverpulse</artifactId>
<version>vx.x.x</version>
<scope>provided</scope>
</dependency>
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
dependencies {
compileOnly("com.github.renvins:serverpulse:vx.x.x")
}
import it.renvins.serverpulse.api.ServerPulseAPI;
import it.renvins.serverpulse.api.ServerPulseProvider;
public class YourPlugin extends JavaPlugin {
@Override
public void onEnable() {
// Check if ServerPulse is available
if (getServer().getPluginManager().getPlugin("ServerPulse") != null) {
try {
ServerPulseAPI api = ServerPulseProvider.get();
// Use the API here
} catch (IllegalStateException e) {
getLogger().warning("ServerPulse API is not available!");
}
}
}
}
The IDatabaseService
interface provides access to InfluxDB operations:
IDatabaseService dbService = api.getDatabaseService();
// Check connection status
boolean isConnected = dbService.isConnected();
// Get InfluxDB client for custom queries
InfluxDBClient client = dbService.getClient();
WriteApi writeApi = dbService.getWriteApi();
// Send custom data point
Point point = Point.measurement("custom_stats")
.addTag("plugin", "your_plugin")
.addField("value", 42)
.time(Instant.now(), WritePrecision.NS);
writeApi.writePoint(point);
The IMetricsService
interface handles metric collection:
IMetricsService metricsService = api.getMetricsService();
// Trigger metrics collection manually
metricsService.collectAndSendMetrics();
The IDiskRetriever
interface provides disk space information:
IDiskRetriever diskRetriever = api.getDiskRetriever();
// Get disk space metrics
long totalSpace = diskRetriever.getTotalSpace();
long usableSpace = diskRetriever.getUsableSpace();
The IPingRetriever
interface provides player ping statistics:
IPingRetriever pingRetriever = api.getPingRetriever();
// Get ping statistics
int minPing = pingRetriever.getMinPing();
int maxPing = pingRetriever.getMaxPing();
int avgPing = pingRetriever.getAveragePing();
Here's a complete example of a plugin that tracks custom metrics:
public class CustomMetricsPlugin extends JavaPlugin {
private ServerPulseAPI api;
@Override
public void onEnable() {
if (!setupServerPulse()) {
getLogger().severe("Failed to setup ServerPulse integration!");
getServer().getPluginManager().disablePlugin(this);
return;
}
startMetricsTask();
}
private boolean setupServerPulse() {
Plugin serverPulse = getServer().getPluginManager().getPlugin("ServerPulse");
if (serverPulse == null) {
return false;
}
try {
api = ServerPulseProvider.get();
return true;
} catch (IllegalStateException e) {
return false;
}
}
private void startMetricsTask() {
// Run every 5 minutes (6000 ticks)
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
if (!api.getDatabaseService().isConnected()) {
return;
}
// Create and send custom metrics
Point point = Point.measurement("custom_plugin_stats")
.addTag("server", "your_server")
.addTag("plugin", "CustomMetrics")
.addField("active_features", countActiveFeatures())
.addField("custom_count", getCustomCount())
.time(Instant.now(), WritePrecision.NS);
api.getDatabaseService().getWriteApi().writePoint(point);
}, 0L, 6000L);
}
private int countActiveFeatures() {
// Your implementation
return 42;
}
private int getCustomCount() {
// Your implementation
return 123;
}
}