Class Register

java.lang.Object
com.github.pinont.singularitylib.plugin.register.Register

public class Register extends Object
Class for automatically registering annotated components.

Primary path: reads the compile-time @AutoRegister index written by the singularitylib-processor annotation processor from every META-INF/singularitylib/auto-register-index.properties resource visible on the runtime classpath, instantiates each listed class via its no-arg constructor and sorts it into listeners, commands and custom items.

Fallback path: if no index resource exists (legacy consumers compiled without the processor), scanAndCollect(String) performs the old slow Reflections classpath scan. Both paths share the same classification logic.

  • Field Details

    • INDEX_RESOURCE

      public static final String INDEX_RESOURCE
      Classpath resource path of the compile-time auto-register index. Kept in sync with AutoRegisterProcessor#INDEX_RESOURCE in the singularitylib-processor module.
      See Also:
  • Constructor Details

    • Register

      public Register()
      Default constructor for Register.
  • Method Details

    • resetDedupe

      public static void resetDedupe()
      Resets the cross-process dedupe set. Used by tests between runs; not intended for production use.
    • loadFromIndex

      public boolean loadFromIndex()
      Loads the compile-time @AutoRegister index from every resource named "META-INF/singularitylib/auto-register-index.properties" on the runtime classpath and collects the listed classes for registration.

      The classpath is resolved from Thread.currentThread().getContextClassLoader(), which on a Paper server is the PluginClassLoader of the plugin doing the lookup. Because the library plugin declares join-classpath: true in its paper-plugin.yml, every consumer plugin can also see the library's own jar (and thus its index).

      Returns:
      true if at least one index was found and processed, false if no index resource exists anywhere on the classpath
    • loadFromIndex

      public boolean loadFromIndex(ClassLoader classLoader)
      Loads the compile-time @AutoRegister index using an explicit classloader.
      Parameters:
      classLoader - the classloader to resolve index resources with
      Returns:
      true if at least one index was found and processed, false if no index resource exists
    • register

      public void register(Class<?>... classes)
      Explicitly registers component CLASSES (no index, no classpath scanning). Each class is instantiated via its no-arg constructor and classified into listeners / commands / custom items — the fast, explicit path.
      Parameters:
      classes - the component classes to register (must have a no-arg constructor)
    • register

      public void register(Object... instances)
      Explicitly registers component INSTANCES (already constructed by the caller) — the fastest path, with no reflection at all.
      Parameters:
      instances - the component instances to register
    • scanAndCollect

      @Deprecated public void scanAndCollect(String packageName)
      Deprecated.
      Scans the specified package for annotated classes and collects them for registration.

      Deprecated: the compile-time index (loadFromIndex()) replaces the Reflections classpath scan. Consumers compiled with the singularitylib-processor annotation processor do not need to call this method at all — loadFromIndex() covers them. This fallback only exists for legacy consumers whose jars were built without an index.

      Parameters:
      packageName - the package name to scan
    • registerAll

      public void registerAll(org.bukkit.plugin.Plugin plugin)
      Registers all collected components with the specified plugin.
      Parameters:
      plugin - the plugin to register components with
    • getListeners

      public Set<org.bukkit.event.Listener> getListeners()
      Gets the collected listeners.
      Returns:
      set of listeners
    • getCommands

      public List<SimpleCommand> getCommands()
      Gets the collected commands.
      Returns:
      list of commands
    • getCustomItems

      public List<CustomItem> getCustomItems()
      Gets the collected custom items.
      Returns:
      list of custom items