|
| 1 | +# Plugin Scheduler System |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Plugin Scheduler is a sophisticated system that allows for the automatic scheduling and management of plugins based on various conditions. It provides a flexible framework for defining when plugins should start and stop, using a powerful condition-based approach. |
| 6 | + |
| 7 | +## Key Components |
| 8 | + |
| 9 | +The Plugin Scheduler system consists of several key components: |
| 10 | + |
| 11 | +1. **[SchedulerPlugin](scheduler-plugin.md)**: The main plugin that manages the scheduling of other plugins. |
| 12 | +2. **[PluginScheduleEntry](plugin-schedule-entry.md)**: Represents a scheduled plugin with start and stop conditions. |
| 13 | +3. **[ConditionManager](conditions/README.md)**: Manages logical conditions for plugin scheduling in a hierarchical structure. |
| 14 | +4. **[Condition](conditions/README.md)**: The base interface for all conditions that determine when plugins should run. |
| 15 | +5. **[ConditionProvider](condition-provider.md)**: Interface that plugins must implement to be schedulable by the Scheduler. |
| 16 | + |
| 17 | +## Making Your Plugin Schedulable |
| 18 | + |
| 19 | +To make your plugin schedulable by the Plugin Scheduler, follow these steps: |
| 20 | + |
| 21 | +1. **Add the `canBeScheduled` attribute in your `@PluginDescriptor`**: |
| 22 | + ```java |
| 23 | + @PluginDescriptor( |
| 24 | + name = "My Plugin", |
| 25 | + description = "Description of what it does", |
| 26 | + tags = {"microbot", "tag1", "tag2"}, |
| 27 | + enabledByDefault = false, |
| 28 | + canBeScheduled = true // This marks the plugin as schedulable |
| 29 | + ) |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Implement the `ConditionProvider` interface**: |
| 33 | + ```java |
| 34 | + public class MyPlugin extends Plugin implements ConditionProvider { |
| 35 | + // Plugin implementation... |
| 36 | + } |
| 37 | + ``` |
| 38 | + |
| 39 | +3. **Define stop conditions**: |
| 40 | + ```java |
| 41 | + @Override |
| 42 | + public LogicalCondition getStopCondition() { |
| 43 | + // Create conditions that determine when your plugin should stop |
| 44 | + OrCondition orCondition = new OrCondition(); |
| 45 | + |
| 46 | + // Add time-based condition to stop after 30 minutes |
| 47 | + orCondition.addCondition(IntervalCondition.createRandomized( |
| 48 | + Duration.ofMinutes(25), |
| 49 | + Duration.ofMinutes(30) |
| 50 | + )); |
| 51 | + |
| 52 | + // Add inventory-based condition to stop when inventory is full |
| 53 | + // Add other conditions as needed |
| 54 | + |
| 55 | + return orCondition; |
| 56 | + } |
| 57 | + ``` |
| 58 | + |
| 59 | +4. **Define optional start conditions**: |
| 60 | + ```java |
| 61 | + @Override |
| 62 | + public LogicalCondition getStartCondition() { |
| 63 | + // Create conditions that determine when your plugin can start |
| 64 | + // Return null if the plugin can start anytime |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | +5. **Implement the soft stop event handler**: |
| 69 | + ```java |
| 70 | + @Override |
| 71 | + @Subscribe |
| 72 | + public void onPluginScheduleEntrySoftStopEvent(PluginScheduleEntrySoftStopEvent event) { |
| 73 | + if (event.getPlugin() == this) { |
| 74 | + // Save state if needed |
| 75 | + // Clean up resources |
| 76 | + } |
| 77 | + } |
| 78 | + ``` |
| 79 | + |
| 80 | +For complete details about implementing the ConditionProvider interface, see the [ConditionProvider documentation](condition-provider.md). |
| 81 | + |
| 82 | +## Condition System |
| 83 | + |
| 84 | +The heart of the Plugin Scheduler is its condition system, which allows for complex logic to determine when plugins should start and stop. Conditions can be combined using logical operators (AND/OR) to create sophisticated scheduling rules. |
| 85 | + |
| 86 | +### Condition Types |
| 87 | + |
| 88 | +The system supports various types of conditions, each serving a specific purpose: |
| 89 | + |
| 90 | +1. **[Time Conditions](conditions/time-conditions.md)**: Schedule plugins based on time-related factors such as intervals, specific times, or day of week. |
| 91 | +2. **[Skill Conditions](conditions/skill-conditions.md)**: Trigger plugins based on skill levels or experience. |
| 92 | +3. **[Resource Conditions](conditions/resource-conditions.md)**: Manage plugins based on inventory items, gathered resources, or loot. |
| 93 | +4. **[Location Conditions](conditions/location-conditions.md)**: Control plugins based on player position or area. |
| 94 | +5. **[NPC Conditions](conditions/npc-conditions.md)**: Trigger plugins based on NPC-related events. |
| 95 | +6. **[Logical Conditions](conditions/logical-conditions.md)**: Combine other conditions using logical operators (AND, OR, NOT). |
| 96 | + |
| 97 | +### Lock Condition |
| 98 | + |
| 99 | +The `LockCondition` is a special condition that can prevent a plugin from being stopped during critical operations: |
| 100 | + |
| 101 | +```java |
| 102 | +// In your plugin's getStopCondition method: |
| 103 | +LockCondition lockCondition = new LockCondition("Critical banking operation in progress"); |
| 104 | +AndCondition andCondition = new AndCondition(); |
| 105 | +andCondition.addCondition(orCondition); // Other stop conditions |
| 106 | +andCondition.addCondition(lockCondition); // Add the lock condition |
| 107 | +return andCondition; |
| 108 | +``` |
| 109 | + |
| 110 | +You can then control the lock in your plugin code: |
| 111 | + |
| 112 | +```java |
| 113 | +// Lock to prevent stopping during critical operations |
| 114 | +lockCondition.lock(); |
| 115 | + |
| 116 | +// Critical operation here... |
| 117 | + |
| 118 | +// Unlock when safe to stop |
| 119 | +lockCondition.unlock(); |
| 120 | +``` |
| 121 | + |
| 122 | +The lock condition ensures that your plugin won't be interrupted during critical operations, such as banking, trading, or complex interactions that should not be interrupted. |
| 123 | + |
| 124 | +## Start and Stop Conditions |
| 125 | + |
| 126 | +Each scheduled plugin can have both start and stop conditions: |
| 127 | + |
| 128 | +- **Start Conditions**: Determine when a plugin should be activated. |
| 129 | +- **Stop Conditions**: Determine when a plugin should be deactivated. |
| 130 | + |
| 131 | +These conditions operate independently, allowing for flexible plugin lifecycle management. The PluginScheduleEntry class manages these conditions through separate ConditionManager instances. |
| 132 | + |
| 133 | +## Plugin Scheduling Events |
| 134 | + |
| 135 | +The scheduler uses events to communicate with plugins about their lifecycle: |
| 136 | + |
| 137 | +- **[Plugin Schedule Entry Soft Stop Event](plugin-schedule-entry-soft-stop-event.md)**: Sent by the scheduler to request plugins to stop gracefully |
| 138 | +- **[Plugin Schedule Entry Finished Event](plugin-schedule-entry-finished-event.md)**: Sent by plugins to notify the scheduler they've completed their task |
| 139 | + |
| 140 | +## Usage Examples |
| 141 | + |
| 142 | +### Basic Scheduling |
| 143 | + |
| 144 | +```java |
| 145 | +// Schedule a plugin to run every 30 minutes |
| 146 | +PluginScheduleEntry entry = new PluginScheduleEntry( |
| 147 | + "MyPlugin", |
| 148 | + Duration.ofMinutes(30), |
| 149 | + true, // enabled |
| 150 | + true // allow random scheduling |
| 151 | +); |
| 152 | +``` |
| 153 | + |
| 154 | +### Advanced Condition-Based Scheduling |
| 155 | + |
| 156 | +```java |
| 157 | +// Create a schedule entry |
| 158 | +PluginScheduleEntry entry = new PluginScheduleEntry("MyPlugin", true); |
| 159 | + |
| 160 | +// Add a time window condition (run between 9 AM and 5 PM) |
| 161 | +entry.addStartCondition(new TimeWindowCondition( |
| 162 | + LocalTime.of(9, 0), |
| 163 | + LocalTime.of(17, 0) |
| 164 | +)); |
| 165 | + |
| 166 | +// Add a stop condition (stop when inventory is full) |
| 167 | +entry.addStopCondition(new InventoryItemCountCondition( |
| 168 | + ItemID.ANY, |
| 169 | + 28, |
| 170 | + |
| 171 | +)); |
| 172 | + |
| 173 | +// Register the scheduled entry |
| 174 | +schedulerPlugin.registerScheduledPlugin(entry); |
| 175 | +``` |
| 176 | + |
| 177 | +## Example Implementation |
| 178 | + |
| 179 | +For a complete example of a schedulable plugin, see the [SchedulableExamplePlugin](schedulable-example-plugin.md), which demonstrates all aspects of making a plugin work with the scheduler system. |
| 180 | + |
| 181 | +## Further Documentation |
| 182 | + |
| 183 | +For more detailed information about each component, refer to the specific documentation files: |
| 184 | + |
| 185 | +- [SchedulerPlugin](scheduler-plugin.md) |
| 186 | +- [PluginScheduleEntry](plugin-schedule-entry.md) |
| 187 | +- [ConditionProvider](condition-provider.md) |
| 188 | +- [Plugin Schedule Entry Soft Stop Event](plugin-schedule-entry-soft-stop-event.md) |
| 189 | +- [Plugin Schedule Entry Finished Event](plugin-schedule-entry-finished-event.md) |
| 190 | +- [SchedulableExamplePlugin](schedulable-example-plugin.md) |
| 191 | + |
| 192 | +For condition-specific documentation: |
| 193 | + |
| 194 | +- [Time Conditions](conditions/time-conditions.md) |
| 195 | +- [Skill Conditions](conditions/skill-conditions.md) |
| 196 | +- [Resource Conditions](conditions/resource-conditions.md) |
| 197 | +- [Location Conditions](conditions/location-conditions.md) |
| 198 | +- [NPC Conditions](conditions/npc-conditions.md) |
| 199 | +- [Logical Conditions](conditions/logical-conditions.md) |
0 commit comments