Interface BreakHandler


public interface BreakHandler
Manages break scheduling and execution for plugins.

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 Details

    • CONFIG_GROUP

      static final String 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

      void registerPlugin(Plugin p, boolean configure)
      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 configure is true, the plugin will have configurable break settings in the UI.

      Parameters:
      p - the plugin to register
      configure - whether the plugin's break settings should be configurable
    • registerPlugin

      void registerPlugin(Plugin p)
      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

      void unregisterPlugin(Plugin p)
      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

      void startPlugin(Plugin p)
      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

      void stopPlugin(Plugin p)
      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

      boolean isBreakActive(Plugin p)
      Checks if a break is currently active for the specified plugin.
      Parameters:
      p - the plugin to check
      Returns:
      true if a break is currently active for the plugin, false otherwise
    • isBreakActive

      boolean isBreakActive()
      Checks if any break is currently active across all plugins.
      Returns:
      true if any plugin has an active break, false otherwise
    • shouldBreak

      boolean shouldBreak(Plugin p)
      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:
      true if the plugin should start a break, false otherwise
    • startBreak

      void startBreak(Plugin p)
      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

      io.reactivex.rxjava3.core.Observable<Set<Plugin>> 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

      io.reactivex.rxjava3.core.Observable<Map<Plugin,Boolean>> 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

      io.reactivex.rxjava3.core.Observable<Map<Plugin,Instant>> 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

      io.reactivex.rxjava3.core.Observable<Map<Plugin,Map<String,String>>> 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

      Map<Plugin,Boolean> 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

      Set<Plugin> getActivePlugins()
      Returns the set of plugins that are currently active (started).
      Returns:
      the set of active plugins
    • getStartTimes

      Map<Plugin,Instant> 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

      Map<Plugin,Integer> 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

      boolean isBreakPlanned(Plugin plugin)
      Checks if a break is planned for the specified plugin.
      Parameters:
      plugin - the plugin to check
      Returns:
      true if a break is planned for the plugin, false otherwise
    • getPlannedBreak

      Instant getPlannedBreak(Plugin plugin)
      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 null if no break is planned
    • getActiveBreak

      Instant getActiveBreak(Plugin plugin)
      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 null if no break is active
    • planBreak

      void planBreak(Plugin plugin, Instant instant)
      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 for
      instant - the time when the break should start
    • removePlannedBreak

      void removePlannedBreak(Plugin plugin)
      Removes any planned break for the specified plugin.
      Parameters:
      plugin - the plugin to remove the planned break for
    • getPlannedBreaksObservable

      io.reactivex.rxjava3.core.Observable<Map<Plugin,Instant>> 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

      void startBreak(Plugin plugin, Instant instant)
      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 for
      instant - the time when the break should end
    • stopBreak

      void stopBreak(Plugin plugin)
      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

      void setExtraData(Plugin plugin, String key, String value)
      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 for
      key - the key for the extra data entry
      value - the value for the extra data entry
    • setExtraData

      void setExtraData(Plugin plugin, Map<String,String> data)
      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 for
      data - the map of key-value pairs to set
    • removeExtraData

      void removeExtraData(Plugin plugin, String key)
      Removes a single extra data entry for the specified plugin.
      Parameters:
      plugin - the plugin to remove extra data from
      key - the key of the entry to remove
    • resetExtraData

      void resetExtraData(Plugin plugin)
      Removes all extra data for the specified plugin.
      Parameters:
      plugin - the plugin to reset extra data for
    • logoutNow

      void logoutNow(Plugin plugin)
      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

      Map<Plugin,Instant> 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

      static String sanitizedName(Plugin plugin)
      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