Overview
The Maintenance API provides an event system that allows you to react to changes in maintenance status and configuration reloads. Events are handled through theEventManager interface.
Registering Event Listeners
Event listeners extend theEventListener<T> abstract class and are registered through the EventManager:
import eu.kennytv.maintenance.api.Maintenance;
import eu.kennytv.maintenance.api.MaintenanceProvider;
import eu.kennytv.maintenance.api.event.MaintenanceChangedEvent;
import eu.kennytv.maintenance.api.event.manager.EventListener;
import eu.kennytv.maintenance.api.event.manager.EventManager;
import org.bukkit.plugin.java.JavaPlugin;
public class MyPlugin extends JavaPlugin {
@Override
public void onEnable() {
Maintenance maintenance = MaintenanceProvider.get();
if (maintenance == null) {
getLogger().warning("Maintenance plugin not available!");
return;
}
EventManager eventManager = maintenance.getEventManager();
// Register a listener for MaintenanceChangedEvent
eventManager.registerListener(
new EventListener<MaintenanceChangedEvent>() {
@Override
public void onEvent(MaintenanceChangedEvent event) {
if (event.isMaintenance()) {
getLogger().info("Maintenance mode has been enabled!");
} else {
getLogger().info("Maintenance mode has been disabled!");
}
}
},
MaintenanceChangedEvent.class
);
}
}
Handling MaintenanceChangedEvent
TheMaintenanceChangedEvent is fired whenever maintenance mode is enabled or disabled:
import eu.kennytv.maintenance.api.Maintenance;
import eu.kennytv.maintenance.api.MaintenanceProvider;
import eu.kennytv.maintenance.api.event.MaintenanceChangedEvent;
import eu.kennytv.maintenance.api.event.manager.EventListener;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public class MaintenanceAlerts extends JavaPlugin {
@Override
public void onEnable() {
Maintenance maintenance = MaintenanceProvider.get();
if (maintenance == null) {
return;
}
// Create a custom listener class
EventListener<MaintenanceChangedEvent> listener = new EventListener<MaintenanceChangedEvent>() {
@Override
public void onEvent(MaintenanceChangedEvent event) {
handleMaintenanceChange(event);
}
};
// Register the listener
maintenance.getEventManager().registerListener(listener, MaintenanceChangedEvent.class);
}
private void handleMaintenanceChange(MaintenanceChangedEvent event) {
if (event.isMaintenance()) {
// Maintenance was enabled
getLogger().info("Server entered maintenance mode");
// Broadcast to operators
Bukkit.getOnlinePlayers().stream()
.filter(player -> player.hasPermission("maintenance.notify"))
.forEach(player -> player.sendMessage("§c[Alert] Server is now in maintenance mode"));
// Disable certain features
disablePublicFeatures();
} else {
// Maintenance was disabled
getLogger().info("Server exited maintenance mode");
// Broadcast to operators
Bukkit.getOnlinePlayers().stream()
.filter(player -> player.hasPermission("maintenance.notify"))
.forEach(player -> player.sendMessage("§a[Alert] Server is now open to the public"));
// Re-enable features
enablePublicFeatures();
}
}
private void disablePublicFeatures() {
// Disable public warps, shops, etc.
getLogger().info("Disabling public features during maintenance...");
}
private void enablePublicFeatures() {
// Re-enable public warps, shops, etc.
getLogger().info("Re-enabling public features...");
}
}
The
MaintenanceChangedEvent is a record containing a single boolean field isMaintenance() that indicates whether maintenance is now enabled or disabled.Handling MaintenanceReloadedEvent
TheMaintenanceReloadedEvent is fired when the Maintenance plugin’s configuration is reloaded:
import eu.kennytv.maintenance.api.Maintenance;
import eu.kennytv.maintenance.api.MaintenanceProvider;
import eu.kennytv.maintenance.api.event.MaintenanceReloadedEvent;
import eu.kennytv.maintenance.api.event.manager.EventListener;
import org.bukkit.plugin.java.JavaPlugin;
public class ConfigSync extends JavaPlugin {
@Override
public void onEnable() {
Maintenance maintenance = MaintenanceProvider.get();
if (maintenance == null) {
return;
}
// Listen for config reloads
maintenance.getEventManager().registerListener(
new EventListener<MaintenanceReloadedEvent>() {
@Override
public void onEvent(MaintenanceReloadedEvent event) {
handleConfigReload();
}
},
MaintenanceReloadedEvent.class
);
}
private void handleConfigReload() {
Maintenance maintenance = MaintenanceProvider.get();
getLogger().info("Maintenance configuration was reloaded");
// Check the new maintenance status
if (maintenance.isMaintenance()) {
getLogger().info("Maintenance is now enabled after reload");
} else {
getLogger().info("Maintenance is now disabled after reload");
}
// Sync your plugin's configuration based on new Maintenance settings
syncConfiguration();
}
private void syncConfiguration() {
// Update your plugin's behavior based on new settings
getLogger().info("Synchronizing configuration with Maintenance plugin...");
}
}
The
MaintenanceReloadedEvent doesn’t contain any data. Use Maintenance.isMaintenance() to check the current status after a reload.Multiple Event Listeners
You can register multiple listeners for different events:import eu.kennytv.maintenance.api.Maintenance;
import eu.kennytv.maintenance.api.MaintenanceProvider;
import eu.kennytv.maintenance.api.event.MaintenanceChangedEvent;
import eu.kennytv.maintenance.api.event.MaintenanceReloadedEvent;
import eu.kennytv.maintenance.api.event.manager.EventListener;
import eu.kennytv.maintenance.api.event.manager.EventManager;
import org.bukkit.plugin.java.JavaPlugin;
public class MaintenanceListener extends JavaPlugin {
@Override
public void onEnable() {
Maintenance maintenance = MaintenanceProvider.get();
if (maintenance == null) {
getLogger().severe("Maintenance plugin not found!");
return;
}
EventManager eventManager = maintenance.getEventManager();
// Register listener for status changes
eventManager.registerListener(
new EventListener<MaintenanceChangedEvent>() {
@Override
public void onEvent(MaintenanceChangedEvent event) {
onMaintenanceChanged(event);
}
},
MaintenanceChangedEvent.class
);
// Register listener for config reloads
eventManager.registerListener(
new EventListener<MaintenanceReloadedEvent>() {
@Override
public void onEvent(MaintenanceReloadedEvent event) {
onMaintenanceReloaded(event);
}
},
MaintenanceReloadedEvent.class
);
getLogger().info("Registered all maintenance event listeners");
}
private void onMaintenanceChanged(MaintenanceChangedEvent event) {
getLogger().info("Maintenance changed: " + event.isMaintenance());
}
private void onMaintenanceReloaded(MaintenanceReloadedEvent event) {
getLogger().info("Maintenance configuration reloaded");
}
}
Complete Integration Example
Here’s a complete example showing a plugin that integrates with maintenance events:import eu.kennytv.maintenance.api.Maintenance;
import eu.kennytv.maintenance.api.MaintenanceProvider;
import eu.kennytv.maintenance.api.Settings;
import eu.kennytv.maintenance.api.event.MaintenanceChangedEvent;
import eu.kennytv.maintenance.api.event.MaintenanceReloadedEvent;
import eu.kennytv.maintenance.api.event.manager.EventListener;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Level;
public class MaintenanceIntegration extends JavaPlugin {
private Maintenance maintenance;
private boolean publicFeaturesEnabled = true;
@Override
public void onEnable() {
// Get the Maintenance API
maintenance = MaintenanceProvider.get();
if (maintenance == null) {
getLogger().severe("Maintenance plugin not found! Some features will be disabled.");
return;
}
// Register event listeners
registerMaintenanceListeners();
// Initialize based on current state
if (maintenance.isMaintenance()) {
disablePublicFeatures();
}
getLogger().info("MaintenanceIntegration enabled successfully!");
}
private void registerMaintenanceListeners() {
var eventManager = maintenance.getEventManager();
// Listen for maintenance status changes
eventManager.registerListener(
new EventListener<MaintenanceChangedEvent>() {
@Override
public void onEvent(MaintenanceChangedEvent event) {
handleMaintenanceChange(event.isMaintenance());
}
},
MaintenanceChangedEvent.class
);
// Listen for configuration reloads
eventManager.registerListener(
new EventListener<MaintenanceReloadedEvent>() {
@Override
public void onEvent(MaintenanceReloadedEvent event) {
handleConfigReload();
}
},
MaintenanceReloadedEvent.class
);
}
private void handleMaintenanceChange(boolean enabled) {
if (enabled) {
getLogger().info("Maintenance mode enabled - adjusting plugin behavior");
disablePublicFeatures();
notifyStaff("§6[Maintenance] §eServer is now in maintenance mode");
} else {
getLogger().info("Maintenance mode disabled - restoring normal operation");
enablePublicFeatures();
notifyStaff("§6[Maintenance] §aServer is now open to the public");
}
}
private void handleConfigReload() {
getLogger().info("Maintenance configuration reloaded");
// Check current state and adjust accordingly
boolean shouldDisableFeatures = maintenance.isMaintenance();
if (shouldDisableFeatures && publicFeaturesEnabled) {
disablePublicFeatures();
} else if (!shouldDisableFeatures && !publicFeaturesEnabled) {
enablePublicFeatures();
}
// Log current whitelist size
Settings settings = maintenance.getSettings();
getLogger().info("Current whitelist size: " + settings.getWhitelistedPlayers().size());
}
private void disablePublicFeatures() {
if (!publicFeaturesEnabled) {
return;
}
publicFeaturesEnabled = false;
getLogger().info("Disabling public features (shops, warps, etc.)");
// Your code to disable features during maintenance
// For example:
// - Disable economy shops
// - Disable public warps
// - Disable minigame joins
}
private void enablePublicFeatures() {
if (publicFeaturesEnabled) {
return;
}
publicFeaturesEnabled = true;
getLogger().info("Enabling public features");
// Your code to re-enable features
}
private void notifyStaff(String message) {
Bukkit.getOnlinePlayers().stream()
.filter(player -> player.hasPermission("maintenance.notify"))
.forEach(player -> player.sendMessage(message));
}
@Override
public void onDisable() {
getLogger().info("MaintenanceIntegration disabled");
}
}
Best Practices
Check for null
Check for null
Always check if
MaintenanceProvider.get() returns null before using the API. The Maintenance plugin may not be loaded yet.Register listeners in onEnable
Register listeners in onEnable
Register your event listeners in your plugin’s
onEnable() method to ensure they’re active as soon as your plugin loads.Handle initial state
Handle initial state
When your plugin enables, check the current maintenance state with
maintenance.isMaintenance() and adjust your plugin’s behavior accordingly.Use separate listener classes
Use separate listener classes
For complex event handling, consider creating separate listener classes instead of anonymous inner classes for better code organization.
Next Steps
Basic Usage
Learn basic API operations and usage
Proxy Integration
Manage per-server maintenance on proxy networks