From 6d4ad4120a869bf2937b356cca3dde29e2570b1e Mon Sep 17 00:00:00 2001 From: Spexx Date: Sun, 5 Apr 2026 11:32:45 +0200 Subject: [PATCH] feat: add path-based configuration access methods --- .../manager/ConfigManager.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/main/java/dev/spexx/configurationAPI/manager/ConfigManager.java b/src/main/java/dev/spexx/configurationAPI/manager/ConfigManager.java index 8123821..5f1dc6e 100644 --- a/src/main/java/dev/spexx/configurationAPI/manager/ConfigManager.java +++ b/src/main/java/dev/spexx/configurationAPI/manager/ConfigManager.java @@ -110,6 +110,45 @@ public ConfigManager(@NotNull JavaPlugin plugin) { return load(file); } + /** + * Returns the latest configuration snapshot for a file located + * relative to the plugin's data folder. + * + *

This is a convenience method that resolves the provided path + * against {@link JavaPlugin#getDataFolder()}.

+ * + *

Example usage:

+ *
+     * YamlConfig config = manager.getByPath("configs/example.yml");
+     * 
+ * + * @param path relative file path using forward slashes, must not be {@code null} + * @return latest {@link YamlConfig} snapshot + * + * @throws IllegalStateException if the configuration is not loaded + */ + public @NotNull YamlConfig getByPath(@NotNull String path) { + File file = resolvePath(path); + return get(file); + } + + /** + * Returns an existing configuration or loads it if not already tracked, + * using a path relative to the plugin's data folder. + * + *

Example usage:

+ *
+     * YamlConfig config = manager.getOrLoadByPath("configs/example.yml");
+     * 
+ * + * @param path relative file path using forward slashes, must not be {@code null} + * @return latest {@link YamlConfig} snapshot + */ + public @NotNull YamlConfig getOrLoadByPath(@NotNull String path) { + File file = resolvePath(path); + return getOrLoad(file); + } + /** * Loads a configuration file from plugin resources if necessary. * @@ -194,6 +233,24 @@ private void ensureFileExists(@NotNull File file) { } } + /** + * Resolves a relative path against the plugin's data folder. + * + *

Backslashes are normalized to forward slashes to ensure + * cross-platform compatibility.

+ * + * @param path relative path, must not be {@code null} + * @return resolved file + */ + private @NotNull File resolvePath(@NotNull String path) { + Objects.requireNonNull(path, "path"); + + // Normalize separators (Windows → Unix style) + String normalized = path.replace("\\", "/"); + + return new File(plugin.getDataFolder(), normalized); + } + /** * Normalizes a path to ensure consistent identity. *