Lifecycle Phases
The plugin lifecycle consists of three main phases, plus optional bootstrapping and loading phases:Loader Phase (Optional)
The loader phase occurs before the plugin class is instantiated and runs in a separate classloader.PluginLoader Interface
Implementio.papermc.paper.plugin.loader.PluginLoader to configure your plugin’s classpath:
paper-plugin.yml:
Bootstrap Phase (Optional)
The bootstrap phase runs before the server is fully loaded, allowing early initialization.PluginBootstrap Interface
Implementio.papermc.paper.plugin.bootstrap.PluginBootstrap for early setup:
paper-plugin.yml:
The bootstrap phase is experimental. Only call API methods explicitly documented to work during bootstrap. Most Bukkit API calls will throw exceptions or return null.
Custom Plugin Instantiation
You can override how your plugin is created:Load Phase
The load phase occurs when your plugin is loaded but before it’s enabled.onLoad() Method
Override theonLoad() method in your JavaPlugin class:
Load Order
Control when your plugin loads with theload field in paper-plugin.yml:
STARTUP- Loads during server startup, before worldsPOSTWORLD- Loads after worlds are loaded
Enable Phase
The enable phase is when your plugin becomes active and starts functioning.onEnable() Method
This is the main entry point for your plugin:Lifecycle Event Registration
DuringonEnable(), you can register lifecycle event handlers:
Running Phase
Once enabled, your plugin is fully active and responds to events, commands, and scheduled tasks.Checking if Enabled
You can check if your plugin is currently enabled:Internal State
TheJavaPlugin class tracks its state:
JavaPlugin.java:272-288:
Disable Phase
The disable phase occurs when the plugin is being shut down.onDisable() Method
Clean up resources when your plugin is disabled:Always implement
onDisable() to ensure proper cleanup. This prevents resource leaks and data loss.Complete Lifecycle Example
Key Lifecycle Methods Summary
Best Practices
- Use the right phase: Don’t do heavy initialization in
onLoad()that belongs inonEnable() - Always implement onDisable(): Clean up resources to prevent memory leaks
- Don’t block the main thread: Keep lifecycle methods fast
- Handle errors gracefully: Catch exceptions in lifecycle methods to prevent plugin load failures
- Log lifecycle events: Help with debugging by logging what’s happening