Class ModuleContext

java.lang.Object
dev.cwhead.GravesX.module.ModuleContext

public final class ModuleContext extends Object
Provides services and utilities to a single module: data folder, logging, config handling, resource I/O, event/task/service registration, and cleanup.

When running on Folia, scheduling helpers delegate to the GravesX scheduler, which is expected to provide a Folia-aware implementation (e.g. region threads).

  • Constructor Summary

    Constructors
    Constructor
    Description
    ModuleContext(Graves plugin, String moduleName, ClassLoader moduleClassLoader, LibraryImporter importer)
    Creates a context for a module and prepares its storage and config.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a hook to be invoked during cleanup.
    Ensures a subfolder exists under the module data folder and returns it.
    void
    executeRegion(org.bukkit.Location location, Runnable r)
    Executes a task on the region thread associated with the given location using the GravesX scheduler.
    Gets the module's class loader.
    org.bukkit.configuration.file.FileConfiguration
    Gets the YAML configuration, loading it if not already loaded.
    Gets the module-specific data folder.
    Exposes the per-module controller for enable/disable/isEnabled/Folia access.
    Gets the logger to use for this module.
    Gets this module's name.
    Gets the owning Graves plugin.
    Opens an embedded resource from the module JAR using the module class loader.
    Opens an embedded text resource as UTF-8.
    void
    importLibrary(String coordinates)
    Imports external libraries for this module using the configured importer.
    org.bukkit.configuration.file.YamlConfiguration
    loadYaml(String relativePath)
    Loads a YAML file from the module data folder.
    <T extends AutoCloseable>
    T
    registerCloseable(T closeable)
    Registers a closeable resource to be closed during cleanup.
    <T extends org.bukkit.event.Listener>
    T
    registerListener(T listener)
    Registers an event listener and tracks it for automatic cleanup.
    <T> void
    registerService(Class<T> service, T provider)
    Registers a Bukkit service and tracks it for automatic unregister.
    <T> void
    registerService(Class<T> service, T provider, org.bukkit.plugin.ServicePriority prio)
    Registers a Bukkit service and tracks it for automatic unregister.
    void
    Reloads the configuration from disk and applies default values from resources.
    resolveFile(String relativePath)
    Resolves a file under this module's data folder.
    void
    Schedules a synchronous task using the GravesX scheduler.
    void
    Schedules an asynchronous task using the GravesX scheduler.
    void
    runTaskLater(Runnable r, long delay)
    Schedules a delayed synchronous task using the GravesX scheduler.
    void
    runTaskTimer(Runnable r, long delay, long period)
    Schedules a repeating synchronous task using the GravesX scheduler.
    void
    runTaskTimerAsync(Runnable r, long delay, long period)
    Schedules a repeating asynchronous task using the GravesX scheduler.
    void
    Saves the current configuration to disk.
    void
    Copies all default YAML resources (except module.yml) from the module JAR into this module's data folder, preserving subfolders.
    void
    saveResource(String path, boolean replace)
    Saves an embedded resource from the module jar into the module data folder.
    void
    saveYaml(String relativePath, org.bukkit.configuration.file.YamlConfiguration yaml)
    Saves a YAML configuration to the module data folder.
    boolean
    Whether this module declares Folia support in module.yml via supportsFolia: true.
    void
    unregisterListener(org.bukkit.event.Listener listener)
    Unregisters a previously registered listener and removes it from tracking.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ModuleContext

      public ModuleContext(Graves plugin, String moduleName, ClassLoader moduleClassLoader, LibraryImporter importer)
      Creates a context for a module and prepares its storage and config.
      Parameters:
      plugin - Owning Graves plugin.
      moduleName - Module name used for paths and messages.
      moduleClassLoader - Class loader that serves module resources.
      importer - Library importer used by importLibrary(String).
  • Method Details

    • getPlugin

      public Graves getPlugin()
      Gets the owning Graves plugin.
      Returns:
      Plugin instance.
    • getModuleName

      public String getModuleName()
      Gets this module's name.
      Returns:
      Module name.
    • getDataFolder

      public File getDataFolder()
      Gets the module-specific data folder.
      Returns:
      Data folder path.
    • getLogger

      public Logger getLogger()
      Gets the logger to use for this module.
      Returns:
      Logger instance.
    • getClassLoader

      public ClassLoader getClassLoader()
      Gets the module's class loader.
      Returns:
      Class loader serving module resources.
    • getResource

      public InputStream getResource(String path)
      Opens an embedded resource from the module JAR using the module class loader.
      Parameters:
      path - resource path inside the module JAR
      Returns:
      input stream for the resource, or null if not found.
    • getTextResource

      public Reader getTextResource(String path)
      Opens an embedded text resource as UTF-8.
      Parameters:
      path - resource path inside the module JAR
      Returns:
      reader for the resource, or null if not found.
    • resolveFile

      public File resolveFile(String relativePath)
      Resolves a file under this module's data folder.
      Parameters:
      relativePath - path relative to getDataFolder()
      Returns:
      resolved file
    • ensureSubfolder

      public File ensureSubfolder(String name)
      Ensures a subfolder exists under the module data folder and returns it.
      Parameters:
      name - subfolder name (e.g. "languages", "cache")
      Returns:
      the subfolder
    • loadYaml

      public org.bukkit.configuration.file.YamlConfiguration loadYaml(String relativePath)
      Loads a YAML file from the module data folder.
      Parameters:
      relativePath - path relative to data folder (e.g. "languages/en_us.yml")
    • saveYaml

      public void saveYaml(String relativePath, org.bukkit.configuration.file.YamlConfiguration yaml)
      Saves a YAML configuration to the module data folder.
      Parameters:
      relativePath - path relative to data folder
      yaml - configuration to save
    • supportsFolia

      public boolean supportsFolia()
      Whether this module declares Folia support in module.yml via supportsFolia: true.

      This is a convenience that forwards to the underlying GravesXModuleDescriptor via the module controller. Returns false if the controller or descriptor is not yet attached.

      Returns:
      true if the module descriptor reports Folia support; false otherwise
    • saveDefaultConfig

      public void saveDefaultConfig()
      Copies all default YAML resources (except module.yml) from the module JAR into this module's data folder, preserving subfolders. Existing files are not overwritten. Ensures a config.yml exists (copy or stub), then reloads the config.
    • getConfig

      public org.bukkit.configuration.file.FileConfiguration getConfig()
      Gets the YAML configuration, loading it if not already loaded.
      Returns:
      Configuration handle.
    • saveConfig

      public void saveConfig()
      Saves the current configuration to disk.
    • reloadConfig

      public void reloadConfig()
      Reloads the configuration from disk and applies default values from resources.
    • saveResource

      public void saveResource(String path, boolean replace)
      Saves an embedded resource from the module jar into the module data folder.
      Parameters:
      path - Resource path inside the jar.
      replace - If true, overwrites an existing file.
    • registerListener

      public <T extends org.bukkit.event.Listener> T registerListener(T listener)
      Registers an event listener and tracks it for automatic cleanup.
      Type Parameters:
      T - Listener type.
      Parameters:
      listener - Listener to register.
      Returns:
      The same listener for chaining.
    • unregisterListener

      public void unregisterListener(org.bukkit.event.Listener listener)
      Unregisters a previously registered listener and removes it from tracking.
      Parameters:
      listener - Listener to unregister.
    • runTask

      public void runTask(Runnable r)
      Schedules a synchronous task using the GravesX scheduler.
      Parameters:
      r - Task to run.
    • runTaskLater

      public void runTaskLater(Runnable r, long delay)
      Schedules a delayed synchronous task using the GravesX scheduler.
      Parameters:
      r - Task to run.
      delay - Delay in ticks before first run.
    • runTaskTimer

      public void runTaskTimer(Runnable r, long delay, long period)
      Schedules a repeating synchronous task using the GravesX scheduler.
      Parameters:
      r - Task to run.
      delay - Delay in ticks before first run.
      period - Period in ticks between runs.
    • runTaskAsync

      public void runTaskAsync(Runnable r)
      Schedules an asynchronous task using the GravesX scheduler.
      Parameters:
      r - Task to run.
    • runTaskTimerAsync

      public void runTaskTimerAsync(Runnable r, long delay, long period)
      Schedules a repeating asynchronous task using the GravesX scheduler.
      Parameters:
      r - Task to run.
      delay - Delay in ticks before first run.
      period - Period in ticks between runs.
    • executeRegion

      public void executeRegion(org.bukkit.Location location, Runnable r)
      Executes a task on the region thread associated with the given location using the GravesX scheduler.

      On Folia, this should run the task on the appropriate region thread. On non-Folia servers, the scheduler may fall back to the main thread or another compatible implementation.

      Parameters:
      location - Location whose region thread should be used.
      r - Task to run.
    • registerService

      public <T> void registerService(Class<T> service, T provider)
      Registers a Bukkit service and tracks it for automatic unregister.
      Parameters:
      service - Service interface class.
      provider - Service implementation instance.
    • registerService

      public <T> void registerService(Class<T> service, T provider, org.bukkit.plugin.ServicePriority prio)
      Registers a Bukkit service and tracks it for automatic unregister.
      Parameters:
      service - Service interface class.
      provider - Service implementation instance.
      prio - Registration priority.
    • registerCloseable

      public <T extends AutoCloseable> T registerCloseable(T closeable)
      Registers a closeable resource to be closed during cleanup.
      Type Parameters:
      T - Resource type.
      Parameters:
      closeable - Resource to track.
      Returns:
      The same resource for chaining.
    • addShutdownHook

      public void addShutdownHook(Runnable hook)
      Adds a hook to be invoked during cleanup.
      Parameters:
      hook - Runnable to execute on shutdown.
    • importLibrary

      public void importLibrary(String coordinates)
      Imports external libraries for this module using the configured importer.
      Parameters:
      coordinates - One or more coordinates (implementation-defined).
    • getGravesXModules

      public GravesXModuleController getGravesXModules()
      Exposes the per-module controller for enable/disable/isEnabled/Folia access.
      Returns:
      controller instance, or null if not yet attached