Plugin Lifecycle Overview
Plugins in Pumpkin go through a well-defined lifecycle managed by thePluginManager. Understanding this lifecycle is essential for proper resource management and plugin behavior.
Lifecycle Hooks
ThePlugin trait defines two main lifecycle hooks:
on_load
Called when the plugin is loaded and initialized.- After the plugin is discovered in the
plugins/directory - When the loader successfully loads the plugin binary
- Before the plugin is marked as active
- Initialize plugin state
- Register event handlers
- Register commands
- Load configuration files
- Set up database connections
- Register services
Ok(())- Plugin initialized successfullyErr(String)- Initialization failed, plugin will be unloaded
on_unload
Called when the plugin is being unloaded or when initialization fails.- When the server shuts down
- When the plugin is manually unloaded
- After
on_loadfails (cleanup after failed initialization) - Before the plugin is removed from the plugin manager
- Unregister commands
- Close database connections
- Save configuration
- Clean up temporary files
- Release resources
Plugin States
Plugins progress through several states:Checking Plugin State
Lifecycle Example
Here’s a complete example showing proper lifecycle management:Async Loading
Plugins are loaded asynchronously to prevent blocking the server:Waiting for All Plugins
You can wait for all plugins to finish loading:Best Practices
Do’s
- Always clean up resources in
on_unload - Return descriptive error messages from
on_load - Use async operations efficiently
- Validate configuration during
on_load - Handle errors gracefully
Don’ts
- Don’t perform blocking operations in lifecycle hooks
- Don’t assume other plugins are loaded during
on_load - Don’t ignore errors from async operations
- Don’t leak resources if initialization fails
Platform Considerations
Windows
On Windows, plugins cannot be fully unloaded from memory due to OS limitations. They can only be deactivated. Theon_unload hook still runs, but the binary remains loaded.
Linux/macOS
Plugins can be completely unloaded and their memory freed.Next Steps
- Event System - Learn about event handling
- Explore advanced plugin patterns
- Review plugin API reference
