Package dev.cwhead.GravesX.module
Class ModuleContext
java.lang.Object
dev.cwhead.GravesX.module.ModuleContext
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
ConstructorsConstructorDescriptionModuleContext(Graves plugin, String moduleName, ClassLoader moduleClassLoader, LibraryImporter importer) Creates a context for a module and prepares its storage and config. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddShutdownHook(Runnable hook) Adds a hook to be invoked during cleanup.ensureSubfolder(String name) Ensures a subfolder exists under the module data folder and returns it.voidexecuteRegion(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.FileConfigurationGets 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.getResource(String path) Opens an embedded resource from the module JAR using the module class loader.getTextResource(String path) Opens an embedded text resource as UTF-8.voidimportLibrary(String coordinates) Imports external libraries for this module using the configured importer.org.bukkit.configuration.file.YamlConfigurationLoads a YAML file from the module data folder.<T extends AutoCloseable>
TregisterCloseable(T closeable) Registers a closeable resource to be closed during cleanup.<T extends org.bukkit.event.Listener>
TregisterListener(T listener) Registers an event listener and tracks it for automatic cleanup.<T> voidregisterService(Class<T> service, T provider) Registers a Bukkit service and tracks it for automatic unregister.<T> voidregisterService(Class<T> service, T provider, org.bukkit.plugin.ServicePriority prio) Registers a Bukkit service and tracks it for automatic unregister.voidReloads the configuration from disk and applies default values from resources.resolveFile(String relativePath) Resolves a file under this module's data folder.voidSchedules a synchronous task using the GravesX scheduler.voidSchedules an asynchronous task using the GravesX scheduler.voidrunTaskLater(Runnable r, long delay) Schedules a delayed synchronous task using the GravesX scheduler.voidrunTaskTimer(Runnable r, long delay, long period) Schedules a repeating synchronous task using the GravesX scheduler.voidrunTaskTimerAsync(Runnable r, long delay, long period) Schedules a repeating asynchronous task using the GravesX scheduler.voidSaves the current configuration to disk.voidCopies all default YAML resources (except module.yml) from the module JAR into this module's data folder, preserving subfolders.voidsaveResource(String path, boolean replace) Saves an embedded resource from the module jar into the module data folder.voidSaves a YAML configuration to the module data folder.booleanWhether this module declares Folia support inmodule.ymlviasupportsFolia: true.voidunregisterListener(org.bukkit.event.Listener listener) Unregisters a previously registered listener and removes it from tracking.
-
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 byimportLibrary(String).
-
-
Method Details
-
getPlugin
Gets the owning Graves plugin.- Returns:
- Plugin instance.
-
getModuleName
Gets this module's name.- Returns:
- Module name.
-
getDataFolder
Gets the module-specific data folder.- Returns:
- Data folder path.
-
getLogger
Gets the logger to use for this module.- Returns:
- Logger instance.
-
getClassLoader
Gets the module's class loader.- Returns:
- Class loader serving module resources.
-
getResource
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
nullif not found.
-
getTextResource
Opens an embedded text resource as UTF-8.- Parameters:
path- resource path inside the module JAR- Returns:
- reader for the resource, or
nullif not found.
-
resolveFile
Resolves a file under this module's data folder.- Parameters:
relativePath- path relative togetDataFolder()- Returns:
- resolved file
-
ensureSubfolder
Ensures a subfolder exists under the module data folder and returns it.- Parameters:
name- subfolder name (e.g. "languages", "cache")- Returns:
- the subfolder
-
loadYaml
Loads a YAML file from the module data folder.- Parameters:
relativePath- path relative to data folder (e.g. "languages/en_us.yml")
-
saveYaml
Saves a YAML configuration to the module data folder.- Parameters:
relativePath- path relative to data folderyaml- configuration to save
-
supportsFolia
public boolean supportsFolia()Whether this module declares Folia support inmodule.ymlviasupportsFolia: true.This is a convenience that forwards to the underlying
GravesXModuleDescriptorvia the module controller. Returnsfalseif the controller or descriptor is not yet attached.- Returns:
trueif the module descriptor reports Folia support;falseotherwise
-
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
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
Schedules a synchronous task using the GravesX scheduler.- Parameters:
r- Task to run.
-
runTaskLater
Schedules a delayed synchronous task using the GravesX scheduler.- Parameters:
r- Task to run.delay- Delay in ticks before first run.
-
runTaskTimer
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
Schedules an asynchronous task using the GravesX scheduler.- Parameters:
r- Task to run.
-
runTaskTimerAsync
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
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
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
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
Adds a hook to be invoked during cleanup.- Parameters:
hook- Runnable to execute on shutdown.
-
importLibrary
Imports external libraries for this module using the configured importer.- Parameters:
coordinates- One or more coordinates (implementation-defined).
-
getGravesXModules
Exposes the per-module controller for enable/disable/isEnabled/Folia access.- Returns:
- controller instance, or
nullif not yet attached
-