Interface BreakHandler
The BreakHandler provides a centralized system for managing automated breaks during bot operation. It allows plugins to register for break management, schedule breaks at specific times, and receive notifications about break states through RxJava Observables.
Break configuration is stored using the configuration group defined by
CONFIG_GROUP. Each plugin's break settings are stored with a sanitized
version of the plugin name as the key prefix.
Example usage:
// Register a plugin for break handling
breakHandler.registerPlugin(myPlugin);
// Start tracking the plugin
breakHandler.startPlugin(myPlugin);
// Check if it's time to take a break
if (breakHandler.shouldBreak(myPlugin)) {
breakHandler.startBreak(myPlugin);
}
// Subscribe to break events
breakHandler.getActiveBreaksObservable()
.subscribe(breaks -> {
// Handle active breaks
});
// Clean up when plugin stops
breakHandler.stopPlugin(myPlugin);
breakHandler.unregisterPlugin(myPlugin);
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe configuration group name used for storing break handler settings. -
Method Summary
Modifier and TypeMethodDescriptiongetActiveBreak(Plugin plugin) Returns the end time of the active break for the specified plugin.Returns the map of currently active breaks.Returns an Observable that emits the map of active breaks.Returns an Observable that emits the set of currently active plugins.Returns the set of plugins that are currently active (started).Returns the map of break counts per plugin.io.reactivex.rxjava3.subjects.PublishSubject<ConfigChanged> Returns the PublishSubject for configuration change events.Returns an Observable that emits newly started breaks as they occur.Returns an Observable that emits extra data associated with plugins.io.reactivex.rxjava3.core.Observable<Plugin> Returns an Observable that emits plugins when they request an immediate logout.getPlannedBreak(Plugin plugin) Returns the planned break time for the specified plugin.Returns an Observable that emits the map of planned breaks.Returns an Observable that emits the map of all registered plugins.Returns the map of all registered plugins.Returns the map of plugin start times.intReturns the total number of breaks taken across all plugins.booleanChecks if any break is currently active across all plugins.booleanChecks if a break is currently active for the specified plugin.booleanisBreakPlanned(Plugin plugin) Checks if a break is planned for the specified plugin.voidTriggers an immediate logout action for the specified plugin.voidPlans a break for the specified plugin at the given time.voidRegisters a plugin with the break handler with default configurable settings.voidregisterPlugin(Plugin p, boolean configure) Registers a plugin with the break handler with configurable settings.voidremoveExtraData(Plugin plugin, String key) Removes a single extra data entry for the specified plugin.voidremovePlannedBreak(Plugin plugin) Removes any planned break for the specified plugin.voidresetExtraData(Plugin plugin) Removes all extra data for the specified plugin.static StringsanitizedName(Plugin plugin) Returns a sanitized version of the plugin name for use as a configuration key.voidsetExtraData(Plugin plugin, String key, String value) Sets a single extra data entry for the specified plugin.voidsetExtraData(Plugin plugin, Map<String, String> data) Sets multiple extra data entries for the specified plugin.booleanDetermines whether the specified plugin should take a break now.voidstartBreak(Plugin p) Starts a break for the specified plugin using configured duration settings.voidstartBreak(Plugin plugin, Instant instant) Starts a break for the specified plugin that ends at the given time.voidStarts tracking a plugin for break management.voidStops the active break for the specified plugin.voidstopPlugin(Plugin p) Stops tracking a plugin for break management.voidUnregisters a plugin from the break handler.
-
Field Details
-
CONFIG_GROUP
The configuration group name used for storing break handler settings. All break-related configuration keys are prefixed with this group.- See Also:
-
-
Method Details
-
registerPlugin
Registers a plugin with the break handler with configurable settings.When a plugin is registered, it becomes available in the break handler's plugin list. If
configureis true, the plugin will have configurable break settings in the UI.- Parameters:
p- the plugin to registerconfigure- whether the plugin's break settings should be configurable
-
registerPlugin
Registers a plugin with the break handler with default configurable settings.This is equivalent to calling
registerPlugin(p, true).- Parameters:
p- the plugin to register
-
unregisterPlugin
Unregisters a plugin from the break handler.After unregistering, the plugin will no longer appear in the break handler's plugin list and all associated break data will be removed.
- Parameters:
p- the plugin to unregister
-
startPlugin
Starts tracking a plugin for break management.This method should be called when a plugin begins its operation. It initializes the start time and break count for the plugin.
- Parameters:
p- the plugin to start tracking
-
stopPlugin
Stops tracking a plugin for break management.This method should be called when a plugin ends its operation. It removes the plugin from the active set and clears any planned or active breaks.
- Parameters:
p- the plugin to stop tracking
-
isBreakActive
Checks if a break is currently active for the specified plugin.- Parameters:
p- the plugin to check- Returns:
trueif a break is currently active for the plugin,falseotherwise
-
isBreakActive
boolean isBreakActive()Checks if any break is currently active across all plugins.- Returns:
trueif any plugin has an active break,falseotherwise
-
shouldBreak
Determines whether the specified plugin should take a break now.This method checks if a break has been planned for the plugin and if the current time has passed the planned break time.
- Parameters:
p- the plugin to check- Returns:
trueif the plugin should start a break,falseotherwise
-
startBreak
Starts a break for the specified plugin using configured duration settings.The break duration is randomly selected between the configured minimum and maximum break times for the plugin. Any planned break for the plugin is removed when a break starts.
- Parameters:
p- the plugin to start a break for
-
getCurrentActiveBreaksObservable
io.reactivex.rxjava3.core.Observable<org.apache.commons.lang3.tuple.Pair<Plugin,Instant>> getCurrentActiveBreaksObservable()Returns an Observable that emits newly started breaks as they occur.Each emission is a pair containing the plugin and the instant when the break will end.
- Returns:
- an Observable emitting pairs of plugin and break end time
-
getActiveObservable
Returns an Observable that emits the set of currently active plugins.The set is emitted whenever a plugin starts or stops.
- Returns:
- an Observable emitting the set of active plugins
-
getlogoutActionObservable
io.reactivex.rxjava3.core.Observable<Plugin> getlogoutActionObservable()Returns an Observable that emits plugins when they request an immediate logout.- Returns:
- an Observable emitting plugins requesting logout
-
getConfigChanged
io.reactivex.rxjava3.subjects.PublishSubject<ConfigChanged> getConfigChanged()Returns the PublishSubject for configuration change events.This subject emits events when break handler configuration changes.
- Returns:
- the PublishSubject for configuration changes
-
getPluginObservable
Returns an Observable that emits the map of all registered plugins.The map key is the plugin, and the value indicates whether the plugin has configurable break settings.
- Returns:
- an Observable emitting the plugin registration map
-
getActiveBreaksObservable
Returns an Observable that emits the map of active breaks.The map key is the plugin, and the value is the instant when the break will end.
- Returns:
- an Observable emitting the active breaks map
-
getExtraDataObservable
Returns an Observable that emits extra data associated with plugins.Extra data is a map of key-value pairs that plugins can use to store additional information related to break handling.
- Returns:
- an Observable emitting the extra data map
-
getPlugins
Returns the map of all registered plugins.The map key is the plugin, and the value indicates whether the plugin has configurable break settings.
- Returns:
- the map of registered plugins and their configurability
-
getActivePlugins
Returns the set of plugins that are currently active (started).- Returns:
- the set of active plugins
-
getStartTimes
Returns the map of plugin start times.The map key is the plugin, and the value is the instant when the plugin was started.
- Returns:
- the map of plugin start times
-
getAmountOfBreaks
Returns the map of break counts per plugin.The map key is the plugin, and the value is the number of breaks the plugin has taken since it was started.
- Returns:
- the map of break counts
-
isBreakPlanned
Checks if a break is planned for the specified plugin.- Parameters:
plugin- the plugin to check- Returns:
trueif a break is planned for the plugin,falseotherwise
-
getPlannedBreak
Returns the planned break time for the specified plugin.- Parameters:
plugin- the plugin to get the planned break for- Returns:
- the instant when the break is planned to start,
or
nullif no break is planned
-
getActiveBreak
Returns the end time of the active break for the specified plugin.- Parameters:
plugin- the plugin to get the active break for- Returns:
- the instant when the active break will end,
or
nullif no break is active
-
planBreak
Plans a break for the specified plugin at the given time.When the current time passes the planned break time,
shouldBreak(Plugin)will return true.- Parameters:
plugin- the plugin to plan a break forinstant- the time when the break should start
-
removePlannedBreak
Removes any planned break for the specified plugin.- Parameters:
plugin- the plugin to remove the planned break for
-
getPlannedBreaksObservable
Returns an Observable that emits the map of planned breaks.The map key is the plugin, and the value is the instant when the break is planned to start.
- Returns:
- an Observable emitting the planned breaks map
-
startBreak
Starts a break for the specified plugin that ends at the given time.Any planned break for the plugin is removed when a break starts.
- Parameters:
plugin- the plugin to start a break forinstant- the time when the break should end
-
stopBreak
Stops the active break for the specified plugin.The plugin will be removed from the active breaks map.
- Parameters:
plugin- the plugin to stop the break for
-
setExtraData
Sets a single extra data entry for the specified plugin.Extra data can be used to store plugin-specific information related to break handling.
- Parameters:
plugin- the plugin to set extra data forkey- the key for the extra data entryvalue- the value for the extra data entry
-
setExtraData
Sets multiple extra data entries for the specified plugin.Existing entries with the same keys will be overwritten.
- Parameters:
plugin- the plugin to set extra data fordata- the map of key-value pairs to set
-
removeExtraData
Removes a single extra data entry for the specified plugin.- Parameters:
plugin- the plugin to remove extra data fromkey- the key of the entry to remove
-
resetExtraData
Removes all extra data for the specified plugin.- Parameters:
plugin- the plugin to reset extra data for
-
logoutNow
Triggers an immediate logout action for the specified plugin.Subscribers to
getlogoutActionObservable()will be notified.- Parameters:
plugin- the plugin requesting logout
-
getTotalAmountOfBreaks
int getTotalAmountOfBreaks()Returns the total number of breaks taken across all plugins.- Returns:
- the sum of all break counts for all plugins
-
getActiveBreaks
Returns the map of currently active breaks.The map key is the plugin, and the value is the instant when the break will end.
- Returns:
- the map of active breaks
-
sanitizedName
Returns a sanitized version of the plugin name for use as a configuration key.The name is converted to lowercase and spaces are removed.
- Parameters:
plugin- the plugin to get the sanitized name for- Returns:
- the sanitized plugin name
-