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.
  • 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.
    • 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.
    • 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.
    • 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 access.