Plugin Lifecycle
Understand when plugins start, stop, and how to properly manage resources throughout the plugin lifecycle.
Every RuneLite plugin has a lifecycle - it starts when enabled, runs while active, and stops when disabled.
Lifecycle Methods
startUp()
Called when the plugin is enabled. Use this to:
- Add overlays
- Initialize data structures
- Set up timers
- Register key listeners, menu entries, etc.
- Note: @Subscribe methods are automatically registered (no manual registration needed)
- For any delayed/async game-state changes, use ClientThread.invokeLater() instead of direct calls from timers/threads
shutDown()
Called when the plugin is disabled. Use this to:
- Remove overlays
- Clean up resources
- Save data
- Unregister key listeners, menu entries, etc.
- Note: @Subscribe methods are automatically unregistered (no manual cleanup needed)
Best Practices
- Always clean up in shutDown() what you set up in startUp()
- Don't perform heavy operations in startUp()
- Check if resources exist before cleaning up
- Save user data in shutDown()
Common Patterns
@Override
protected void startUp() {
// No eventBus.register(this) needed — @Subscribe is auto-registered
overlayManager.add(overlay);
// Initialize
}
@Override
protected void shutDown() {
// No eventBus.unregister(this) needed
overlayManager.remove(overlay);
// Cleanup
}
Code Examples
Example 1
1// Complete lifecycle example
2@PluginDescriptor(name = "Example", description = "Example plugin")
3public class ExamplePlugin extends Plugin {
4 @Inject
5 private OverlayManager overlayManager;
6
7 private final MyOverlay overlay = new MyOverlay();
8
9 @Override
10 protected void startUp() {
11 // No eventBus.register(this) needed — @Subscribe is auto-registered
12
13 // Add overlay
14 overlayManager.add(overlay);
15
16 // Initialize other resources...
17 }
18
19 @Override
20 protected void shutDown() {
21 // Remove overlay
22 overlayManager.remove(overlay);
23
24 // No eventBus.unregister(this) needed
25
26 // Clean up other resources...
27 }
28
29 @Subscribe
30 public void onGameTick(GameTick event) {
31 // This is automatically registered - no manual registration needed!
32 }
33}