Module watamebot

Class Plugin

java.lang.Object
net.foxgenesis.watame.plugin.Plugin

public abstract class Plugin extends Object
A service providing functionality to WatameBot.

Providers should provide a no-argument constructor or a public static Plugin provider() method in accordance to Provider.get()

Author:
Ashley
  • Field Details

    • logger

      @NotNull protected final @NotNull org.slf4j.Logger logger
      Plugin logger
    • configurationPath

      @NotNull public final @NotNull Path configurationPath
      Path to the plugin's configuration folder
    • name

      @NotNull public final @NotNull String name
      Name identifier of the plugin.
    • friendlyName

      @NotNull public final @NotNull String friendlyName
      Friendly identifier of the plugin.
    • description

      @NotNull public final @NotNull String description
      Description of the plugin.
    • version

      @NotNull public final @NotNull ModuleDescriptor.Version version
      Version of the plugin.
    • providesCommands

      public final boolean providesCommands
      Does this plugin provide commands.
    • needsDatabase

      public final boolean needsDatabase
      Does this plugin require access to the database.
  • Constructor Details

    • Plugin

      public Plugin() throws SeverePluginException
      No-argument constructor called by the ServiceLoader to load and initialize required plugin data.

      Types of plugin data would include:

      • Configuration settings
      • Constants
      Anything beyond the previous should be loaded in the preInit() method.
      Throws:
      SeverePluginException - if the plugin is not in a named module or there was a problem while loading the plugin.properties file
  • Method Details

    • configurationKeySet

      protected Set<String> configurationKeySet()
      Get a list of all loaded configuration IDs.
      Returns:
      Returns a Set containing the IDs of all configurations loaded
    • forEachConfiguration

      protected void forEachConfiguration(BiConsumer<String,org.apache.commons.configuration2.Configuration> consumer)
      Loop through all loaded Configuration files.
      Parameters:
      consumer - - PluginConfiguration.identifier() and the constructed Configuration
    • hasConfiguration

      protected boolean hasConfiguration(String identifier)
      Check if a configuration file with the specified identifier exists.
      Parameters:
      identifier - - the PluginConfiguration.identifier()
      Returns:
      Returns true if the specified identifier points to a valid configuration
    • getConfiguration

      @Nullable protected @Nullable org.apache.commons.configuration2.Configuration getConfiguration(String identifier)
      Get the configuration file that is linked to an identifier or null if not found.
      Parameters:
      identifier - - the PluginConfiguration.identifier()
      Returns:
      Returns the PropertiesConfiguration linked to the identifier
    • registerProperty

      protected final PropertyInfo registerProperty(@NotNull @NotNull String name, boolean modifiable, @NotNull @NotNull PropertyType type)
      Register a plugin property inside the database.
      Parameters:
      name - - name of the property
      modifiable - - if this property can be modified by a user
      type - - property storage type
      Returns:
      Returns the PropertyInfo of the registered property
    • upsertProperty

      protected final PluginProperty upsertProperty(@NotNull @NotNull String name, boolean modifiable, @NotNull @NotNull PropertyType type)
      Register a plugin property if it doesn't exist and resolve it into a PluginProperty.
      Parameters:
      name - - property name
      modifiable - - if this property can be modified by a user
      type - - property storage type
      Returns:
      Returns the resolved PluginProperty
    • getProperty

      protected final PluginProperty getProperty(PropertyInfo info)
      Resolve a PropertyInfo into a usable PluginProperty.
      Parameters:
      info - - property information
      Returns:
      Returns the resolved PluginProperty
    • getProperty

      protected final PluginProperty getProperty(String name)
      Resolve the PropertyInfo by the specified name into a usable PluginProperty.
      Parameters:
      name - - property name
      Returns:
      Returns the resolved PluginProperty
    • getPropertyProvider

      protected final PluginPropertyProvider getPropertyProvider()
      Get the provider for registering and getting PluginProperties.
      Returns:
      Returns the PluginPropertyProvider used by Plugins to register plugin properties
    • registerDatabase

      protected final void registerDatabase(AbstractDatabase database) throws IOException
      Register an AbstractDatabase that this Plugin requires.
      Parameters:
      database - - database to register
      Throws:
      IOException - Thrown if there was an error while reading the database setup files
    • preInit

      protected abstract void preInit() throws SeverePluginException
      Startup method called when resources, needed for functionality initialization, are to be loaded. Resources that do not require connection to Discord or the database should be loaded here.

      The database and Discord information might not be loaded at the time of this method! Use init(IEventStore) for functionality that requires valid connections.

      Typical resources to load here include:

      • Custom database registration
      • System Data
      • Files
      • Images
      Throws:
      SeverePluginException - Thrown if the plugin has encountered a severe exception. If the exception is fatal, the plugin will be unloaded
      See Also:
    • init

      protected abstract void init(IEventStore builder) throws SeverePluginException
      Startup method called when methods providing functionality are to be loaded. Methods that require connection the database should be called here.

      Discord information might not be loaded at the time of this method! Use onReady(WatameBot) for functionality that requires valid connections.

      Typical methods to call here include:

      • Main chunk of program initialization
      • PluginProperty registration/retrieval
      • Event listener registration
      • Custom database operations
      Parameters:
      builder - - discord event listener register
      Throws:
      SeverePluginException - Thrown if the plugin has encountered a severe exception. If the exception is fatal, the plugin will be unloaded
      See Also:
    • postInit

      protected abstract void postInit(WatameBot bot) throws SeverePluginException
      Startup method called when JDA is building a connection to discord and all CommandData is being collected from CommandProvider.getCommands().

      Discord information might not be loaded at the time of this method! Use onReady(WatameBot) for functionality that requires valid connections.

      Typical methods to call here include:

      • Initialization cleanup
      Parameters:
      bot - - reference of WatameBot
      Throws:
      SeverePluginException - Thrown if the plugin has encountered a severe exception. If the exception is fatal, the plugin will be unloaded
    • onReady

      protected abstract void onReady(WatameBot bot) throws SeverePluginException
      Called by the PluginHandler when JDA and all Plugins have finished startup and have finished loading.
      Parameters:
      bot - - reference of WatameBot
      Throws:
      SeverePluginException - Thrown if the plugin has encountered a severe exception. If the exception is fatal, the plugin will be unloaded
      See Also:
    • close

      protected abstract void close() throws Exception
      Called when the plugin is to be unloaded.

      The shutdown sequence runs as followed:

      • Remove event listeners
      • close()
      • Close databases
      Throws:
      Exception - Thrown if an underlying exception is thrown during close
    • getDisplayInfo

      public String getDisplayInfo()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(@Nullable @Nullable Object obj)
      Overrides:
      equals in class Object