Class UltiToolsPlugin

java.lang.Object
com.ultikits.ultitools.abstracts.UltiToolsPlugin
All Implemented Interfaces:
Configurable, IPlugin

public abstract class UltiToolsPlugin extends Object implements IPlugin, Configurable
Abstract class representing a plugin module.
Version:
1.0.0
Author:
wisdomme
  • Field Details

    • LOGGER

      private static final Logger LOGGER
      Named logger for module reload outcomes.
    • RELOAD_REFUSED_OFF_THREAD_REASON

      static final String RELOAD_REFUSED_OFF_THREAD_REASON
      The partial reason in the report reloadWithReport() returns when the call was refused because it was made off the server thread (#538). Not an i18n key: nothing reaches the operator through it on the framework's own reload paths, which run on the server thread; it is for a module that calls reloadWithReport() itself.
      See Also:
    • textEngine

      private TextEngine textEngine
    • preparedMessages

      private TextEngine.Prepared preparedMessages
    • version

      private final String version
    • pluginName

      private final String pluginName
    • authors

      private final List<String> authors
    • loadAfter

      private final List<String> loadAfter
    • minUltiToolsVersion

      private final int minUltiToolsVersion
    • mainClass

      private final String mainClass
    • identifyString

      private final String identifyString
    • resourceFolderPath

      private String resourceFolderPath
    • context

      private SimpleContainer context
    • dataScope

      private DataScope dataScope
  • Constructor Details

    • UltiToolsPlugin

      protected UltiToolsPlugin()
      Constructor for UltiToolsPlugin. For module development only.
    • UltiToolsPlugin

      public UltiToolsPlugin(String pluginName, String version, List<String> authors, List<String> loadAfter, int minUltiToolsVersion, String mainClass, String resourceFolderPath)
      Constructor for UltiToolsPlugin. For plugin connector.
      Parameters:
      pluginName - the name of the plugin
      version - the version of the plugin
      authors - the authors of the plugin
      loadAfter - the plugins which should be loaded before this plugin
      minUltiToolsVersion - the minimum version of UltiTools required by this plugin
      mainClass - the main class of the plugin
      resourceFolderPath - the path to the resource folder
  • Method Details

    • publishMessageCatalogue

      @Internal public final void publishMessageCatalogue()
      Publishes an accepted candidate's prepared message catalogue before container assembly.
    • rollbackMessagePublication

      @Internal public final void rollbackMessagePublication()
      Restores the prior message catalogue when an accepted candidate subsequently fails to load.
    • loadPluginConfiguration

      private org.bukkit.configuration.file.YamlConfiguration loadPluginConfiguration()
      Loads the plugin configuration from plugin.yml
      Returns:
      YamlConfiguration object with default values if loading fails
    • setContext

      @Internal public void setContext(SimpleContainer context)
      Injects the IoC container created for this plugin.

      Called by PluginManager while a module is being loaded, before registerSelf() runs. It is not part of the module-facing API — a module that calls it replaces the container the framework already wired up, losing every bean that was injected into it.

      Deliberately still public: PluginManager lives in another package, so this cannot be narrowed to package-private, and deleting it outright would remove a public method, which the compatibility policy forbids in a PATCH release. The annotation is a signal to humans and IDEs; it enforces nothing at runtime.

      Parameters:
      context - the container created for this plugin
    • moduleTexts

      @Internal public final Optional<ModuleTexts> moduleTexts()
      Exposes the module's text lookup without making its prepared catalogue visible to other modules.
      Returns:
      an empty result when no module messages service was prepared
    • setDataScope

      @Internal public void setDataScope(DataScope scope)
      Injects the DataScope credential PluginManager minted for this module (D-17). Called by PluginManager right after minting, before wireAop runs -- the same lifecycle point setContext(SimpleContainer) documents.

      Deliberately still public, for the same reason setContext(SimpleContainer) is: PluginManager lives in another package and cannot be narrowed to package-private, and the compatibility policy forbids removing a public method in a PATCH release. The annotation is a signal to humans and IDEs; it enforces nothing at runtime.

      Parameters:
      scope - the scope minted for this plugin
      Since:
      6.3.0
    • getConfigManager

      public static ConfigManager getConfigManager()
      Returns:
      the config manager
    • getListenerManager

      public static ListenerManager getListenerManager()
      Returns:
      the listener manager
    • getCommandManager

      public static CommandManager getCommandManager()
      Returns:
      the command manager
    • getPluginManager

      public static PluginManager getPluginManager()
      Returns:
      the plugin manager
    • prepareConfigConverters

      private void prepareConfigConverters()
      Prepares converters and checks only enabled auto-config entities before resources are extracted.
    • initConfig

      private void initConfig() throws IOException
      Initializes the configuration entity.
      Throws:
      IOException
    • diffGetAllConfigsOverride

      private void diffGetAllConfigsOverride()
      Diffs a Configurable.getAllConfigs() override against what auto-registration already registered for this module (D-06 / SILENT-18 / #336), called once right after package-scan auto-registration finishes.

      An empty override (the interface default - the module never wrote Configurable.getAllConfigs()) has nothing to compare and nothing to log. A non-empty override whose every configFilePath was already registered by the package scan is pure redundancy, logged at Level.FINE only. A non-empty override naming a configFilePath the scan never registered is real capability loss - #336's warning-only ask cannot tell these two cases apart, so this refuses the module and names every missing entity instead of guessing.

      A directory-backed entity is the exception to the path comparison. Auto-registration expands a configFilePath that names a directory under getResourceFolderPath() (the same isDirectory() test ConfigManager#register applies) into one registration per file inside it, and registers nothing for an empty directory, so the unexpanded path is never a registered key. Such an override entity counts as found when its class is among the config classes package-scan auto-registration selected - the same ConverterRegistry.prepareSelectedConfigs(UltiToolsPlugin, String[], ClassLoader) selection over the same scan packages and class loader initConfig() hands to ConfigManager#registerAll. An entity class the scan did not select is still refused. This method only reads registrations, the selected classes and whether a path is a directory; it never writes or creates a configuration file.

      Throws:
      ConfigurationException - if the override names a configFilePath auto-registration never registered
    • namesConfigDirectory

      private boolean namesConfigDirectory(String configFilePath)
      Parameters:
      configFilePath - a configFilePath from a Configurable.getAllConfigs() override
      Returns:
      whether it names an existing directory under this module's resource folder, the test ConfigManager#register uses to expand a directory-backed entity
    • getInputStream

      private InputStream getInputStream() throws IOException
      Throws:
      IOException
    • resolveJarFileNameForError

      private String resolveJarFileNameForError()
      Best-effort resolution of this module's own JAR file name, for the name:-missing refusal message only. Never throws -- falls back to the class name if the code source is unavailable (e.g. when running from unpacked classes in a test).
      Returns:
      the JAR file name, or this class's name if it cannot be determined
    • getConfigFolder

      protected final String getConfigFolder()
    • getConfigFile

      protected final File getConfigFile(String path)
    • getConfig

      public <T extends AbstractConfigEntity> T getConfig(Class<T> configType)
      Description copied from interface: Configurable
      Get config by config type.
      Specified by:
      getConfig in interface Configurable
      Type Parameters:
      T - Config type
      Parameters:
      configType - Config type
      Returns:
      Config
    • getConfig

      public <T extends AbstractConfigEntity> T getConfig(String path, Class<T> configType)
      Description copied from interface: Configurable
      Get config by config path and config type.
      Specified by:
      getConfig in interface Configurable
      Type Parameters:
      T - Config type
      Parameters:
      path - Config path
      configType - Config type
      Returns:
      the entity registered for path, or null if none is registered (a registered entity of another type throws ClassCastException)
    • getConfigs

      public <T extends AbstractConfigEntity> List<T> getConfigs(Class<T> configType)
    • saveConfig

      public <T extends AbstractConfigEntity> void saveConfig(String path, Class<T> configType) throws IOException
      Saves this module's configuration registered at path through AbstractConfigEntity.save(): only the settings the module changed since the last load or save are written, and only where the file still holds the value the module started from; every other byte of the file stays as the operator left it, and a change that cannot be written stays in memory and is named in one warning. It cannot overwrite operator content: a value the operator edited, deleted or wrote unusably is never written over (maintainer decision 2026-10-04). Use it for a change the operator asked for through the module, or for shipped text re-rendered after a language switch.

      If no entity is registered for path in this module, this method throws IllegalStateException naming the module and path without creating, registering or writing a file, even if the file already exists on disk. Register the entity first, or call AbstractConfigEntity.save() on an entity you hold. Call this method on the server thread.

      Specified by:
      saveConfig in interface Configurable
      Type Parameters:
      T - the configuration entity type
      Parameters:
      path - the registered configuration file path, for example config/config.yml
      configType - the configuration entity class
      Throws:
      IOException - if publishing the verified file fails
      IllegalStateException - if no configuration entity is registered for path in this module, or if the server is running and this is called off the server thread
      ClassCastException - if the registered entity is not an instance of configType; the message names the module, path, expected type and actual type
    • saveResources

      private void saveResources()
      Extracts missing bundled resources and configuration into resourceFolderPath. Validates each destination before writing, leaves existing files intact, and records the hashes of extracted files in a single batch for later configuration ownership checks.
    • getDataOperator

      public final <T extends BaseDataEntity<String>> DataOperator<T> getDataOperator(Class<T> dataClazz)
      Gets data operator. Refuses outright if dataClazz is not registered to this module (D-14) -- checked against the same DataScope minted for this module at load, via the same refusal DataStore.getOperator(DataScope, Class) builds, so the exception type, error code, and message shape are identical regardless of which entry point a caller reaches.

      02-13 (CR-03): before this, dataScope.owns(...) was checked here inline and this method then delegated to the deprecated getOperator(UltiToolsPlugin, Class) overload directly -- so DataStore.getOperator(DataScope, Class), the method D-17/02-07 built specifically as the credential-typed supported path, had zero real callers anywhere in the framework. This now routes through it, so production actually uses the path it was built for. dataScope is non-null by the time any module calls this (set by PluginManager right after minting, before registerSelf() runs). A bare instance constructed outside the normal PluginManager load flow has no scope and is refused with ENTITY_NOT_OWNED; before 6.4.0 it fell back to the deprecated DataStore.getOperator(UltiToolsPlugin, Class) overload, which was removed in 6.4.0.

      Type Parameters:
      T - the type of the data entity
      Parameters:
      dataClazz - the class of the data entity
      Returns:
      the data operator
      Throws:
      DataAccessException - if dataClazz is not registered to this module, or this module was not loaded by the framework and so holds no data scope
    • isNewerVersionThan

      public boolean isNewerVersionThan(UltiToolsPlugin plugin)
      Parameters:
      plugin - the plugin to be checked
      Returns:
      whether the plugin is newer than the given plugin
    • onUnregister

      protected void onUnregister()
      Extension point for a module's own unload cleanup.

      Called by unregisterSelf() before the framework unregisters this plugin's commands and listeners (D-02), so the module's own beans are still alive while this runs. unregisterSelf() is final and always calls this hook and the framework's own unregistration afterward -- a module cannot skip either by overriding unregisterSelf() itself, because that is no longer possible (D-01). The default body does nothing; override this method, not unregisterSelf(), to add cleanup work.

      This hook may throw anything: an unchecked exception, an Error, or a checked exception it does not declare (Lombok @SneakyThrows, or a rethrow helper). Whatever it throws, unregisterSelf() still unregisters this module's own commands and listeners before the exception propagates -- a throwing hook is surfaced to the caller, not swallowed, but it cannot skip the framework's own cleanup the way an unguarded super call could. The exception reaches the caller unchanged: the same instance, of the same type, never wrapped. If a cleanup step also fails, that failure does not replace this hook's own exception: it is attached to it via Throwable.addSuppressed(Throwable) instead, so the module author's own failure is always the one that propagates and is never silently lost behind a secondary framework failure (Codex review on #457, issue #484).

    • unregisterSelf

      public final void unregisterSelf()
      Unloads this module's own registrations: runs onUnregister(), then unregisters this module's commands, then its listeners. final, so a module cannot skip either cleanup step.

      Every step runs whatever an earlier step threw, including a checked exception the hook does not declare. When a step failed, the first failure, in that order, is rethrown afterwards as the same instance and type, never wrapped; every later failure is attached to it via Throwable.addSuppressed(Throwable). A checked exception therefore reaches the caller although this method declares none.

      Specified by:
      unregisterSelf in interface IPlugin
    • runUnregisterStep

      private static Throwable runUnregisterStep(Throwable previousFailure, Runnable step)
      Runs one unregisterSelf() cleanup step, collecting any failure onto previousFailure instead of letting it replace or be replaced by another step's failure.

      onUnregister(), CommandManager#unregisterAll and ListenerManager#unregisterAll declare no checked exceptions, but module code can still throw one without declaring it (Lombok @SneakyThrows, or a rethrow helper). Catching only RuntimeException | Error let such an exception escape this barrier and skip every later step, leaving the module's commands and listeners registered after its unload. So this catches Exception | Error: every Throwable a step can really throw (this is a deliberate cleanup barrier, not a broad catch: unregisterSelf() always runs every remaining step and always rethrows exactly one collected failure unchanged, per its own javadoc).

      Parameters:
      previousFailure - the failure already collected from an earlier step, or null if every earlier step (if any) succeeded
      step - the cleanup step to run
      Returns:
      previousFailure, with step's own failure (if any) attached to it via Throwable.addSuppressed(Throwable); or, if previousFailure was null, step's own failure, or null if step completed normally
    • rethrowUnchanged

      private static <T extends Throwable> void rethrowUnchanged(Throwable failure) throws T
      Throws failure itself -- the same instance, of its own type, unwrapped -- although it may be a checked exception this method's callers do not declare. unregisterSelf() uses it to hand a hook's undeclared checked exception to its caller unchanged.
      Type Parameters:
      T - inferred as an unchecked type at the call site, so no throws clause is needed
      Parameters:
      failure - the collected failure to rethrow
      Throws:
      T - always: failure
    • onReload

      protected void onReload()
      Extension point for a module's own reload work.

      Called by reloadSelf() after the framework's own reload steps -- config reload, config-bound timing refresh (#531), language-catalogue refresh and @ConditionalOnConfig drift report (D-02) -- so a real-work override sees the already-reloaded configuration rather than the stale one. The framework-owned per-module reload line (D-03) is logged only after this hook returned; if it throws, a failure line naming the module is logged instead and the exception propagates to the caller (#509). reloadSelf() is final and always runs its own steps first; a module cannot skip them by overriding reloadSelf() itself, because that is no longer possible (D-01). The default body does nothing; override this method, not reloadSelf(), to add reload work.

    • onReload

      protected void onReload(ReloadReport report)
      Extension point for a module's own reload work, with a way to report a partial reload (#529).

      Called by reloadSelf() and reloadWithReport() at the point onReload() is documented to run, with a fresh ReloadReport. A module that reloads only part of what it should records each part that did not reload with ReloadReport.partial(String) and returns normally; the framework then reports the reload as partial, naming those parts, instead of as a success. The default body calls onReload(), so a module that overrides only that method behaves exactly as before 6.3.0. Override one of the two, not both: an override of this method replaces the call to onReload() unless it calls super.onReload(report).

      Parameters:
      report - the report for this reload, to record the parts that did not reload
      Since:
      6.3.0
    • reloadSelf

      public final void reloadSelf()
      Reload this plugin's configuration and language files.

      Also reports (but does not act on) any @ConditionalOnConfig drift: the condition is evaluated once, at component-scan time during startup, so a reload can only log that a watched key has changed direction since then -- it never registers, unregisters, or rebuilds anything (issue #392, D-01). Right after the configuration reload, and only if it succeeded, the module's config-bound @Scheduled tasks and @CmdCD cooldowns pick up their reloaded values (#531; see PluginManager#applyReloadedConfigBindings); a value that step refuses and keeps, or a part of it that fails, is recorded in the ReloadReport as a part that did not reload (#595). final and always runs its own steps, then calls onReload() -- a module can no longer skip any of this by overriding reloadSelf() itself, because that override point no longer exists (D-01). The hook it calls is onReload(ReloadReport), whose default body calls onReload().

      Only when every step and the hook returned does it log one framework-owned line naming this module (D-03): an INFO success line, or a WARNING naming the parts that did not reload when the hook recorded any in its ReloadReport (#529). If anything throws, it logs one SEVERE line naming the module and the cause instead, never the success line, and rethrows the failure unchanged, so a caller can report it -- /ul reload <name> replies failure, and a full /ul reload carries on with the next module and names this one in its summary (#509).

      The signature is unchanged since 6.2; reloadWithReport() runs the same reload and returns the report.

      Specified by:
      reloadSelf in interface IPlugin
    • reloadWithReport

      public final ReloadReport reloadWithReport()
      Reloads this module exactly as reloadSelf() does, and returns what its reload hook reported (#529).

      /ul reload and /ul reload <name> call this to tell the operator which parts of a module did not reload. A module's own reload command can call it for the same reason.

      Returns:
      the report of this reload, never null; empty when every part reloaded
      Throws:
      RuntimeException - whatever a reload step or the module's hook threw, after it was logged (a checked exception the hook threw without declaring it propagates unchanged in the same way)
      Error - whatever a reload step or the module's hook threw, after it was logged
      Since:
      6.3.0
    • performReload

      private ReloadReport performReload()
      The body of reloadSelf() and reloadWithReport(). Private, so both public entry points always run exactly this.
      Returns:
      the report the module's hook filled in; when the call was refused off the server thread (#538), a report whose only partial reason is that refusal
    • runReloadSteps

      private void runReloadSteps(ReloadReport report)
      The reload steps, in order, ending with the module's own hook.
      Parameters:
      report - the report handed to the module's hook
    • pendingLanguageSetting

      private String pendingLanguageSetting()
      The framework's language setting as it is in config.yml on disk now, when it differs from the value the framework runs with (#502).

      The framework reads its configuration once and re-reads it only on a full /ul reload. A missing, unreadable or unparseable file, or one without the key, is not a pending change: that is reported, if at all, by the full reload that reads it.

      Returns:
      the reason reported for a pending change, or null if none was detected
    • describeFailure

      private static String describeFailure(Throwable failure)
      A failure's message for a one-line report, or its class name when it carries none.
      Parameters:
      failure - the failure to describe
      Returns:
      a non-null description
    • getLogger

      public PluginLogger getLogger()
      Returns:
      plugin logger