Skip to content

Commit

Permalink
Simplify abstraction of config manager
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmq committed Jan 18, 2025
1 parent 5c386ac commit be285f4
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package dev.magicmq.pyspigot.bukkit.manager.config;

import dev.magicmq.pyspigot.exception.InvalidConfigurationException;
import dev.magicmq.pyspigot.manager.config.ConfigManager;
import dev.magicmq.pyspigot.manager.config.ScriptConfig;

import java.io.IOException;
import java.nio.file.Path;

/**
* The Bukkit-specific implementation of the config manager.
*/
public class BukkitConfigManager extends ConfigManager<BukkitScriptConfig> {
public class BukkitConfigManager extends ConfigManager {

private static BukkitConfigManager instance;

Expand All @@ -34,14 +34,7 @@ private BukkitConfigManager() {
}

@Override
public BukkitScriptConfig loadConfig(String filePath) throws IOException, InvalidConfigurationException {
return loadConfig(filePath, null);
}

@Override
public BukkitScriptConfig loadConfig(String filePath, String defaults) throws IOException, InvalidConfigurationException {
Path configFile = createConfigIfNotExists(filePath);

protected ScriptConfig loadConfigImpl(Path configFile, String defaults) throws IOException {
BukkitScriptConfig config = new BukkitScriptConfig(configFile.toFile(), defaults);
config.load();
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.magicmq.pyspigot.bukkit.manager.config;

import dev.magicmq.pyspigot.manager.config.ScriptConfig;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

Expand All @@ -28,7 +29,7 @@
* A class representing a script configuration file, for the Bukkit implementation.
* @see org.bukkit.configuration.file.YamlConfiguration
*/
public class BukkitScriptConfig extends YamlConfiguration {
public class BukkitScriptConfig extends YamlConfiguration implements ScriptConfig {

private final File configFile;
private final String defaults;
Expand All @@ -43,43 +44,18 @@ public BukkitScriptConfig(File configFile, String defaults) {
this.defaults = defaults;
}

/**
* Get the file associated with this configuration.
* @return The file associated with this configuration
*/
@Override
public File getConfigFile() {
return configFile;
}

/**
* Get the absolute path of the file associated with this configuration.
* @return The path of the file
*/
@Override
public Path getConfigPath() {
return Paths.get(configFile.getAbsolutePath());
}

/**
* Sets the specified path to the given value only if the path is not already set in the config file. Any specified default values are ignored when checking if the path is set.
* @see org.bukkit.configuration.ConfigurationSection#set(String, Object)
* @param path Path of the object to set
* @param value Value to set the path to
* @return True if the path was set to the value (in other words the path was not previously set), false if the path was not set to the value (in other words the path was already previously set)
*/
public boolean setIfNotExists(String path, Object value) {
if (!super.isSet(path)) {
super.set(path, value);
return true;
}
return false;
}

/**
* Loads the config from the configuration file. Will also set defaults for the configuration, if they were specified.
* @throws IOException If there was an exception when loading the file
* @throws dev.magicmq.pyspigot.exception.InvalidConfigurationException If there was an error when parsing the loaded file (invalid configuration)
*/
public void load() throws IOException, dev.magicmq.pyspigot.exception.InvalidConfigurationException {
@Override
public void load() throws IOException {
try {
super.load(configFile);
if (defaults != null) {
Expand All @@ -88,26 +64,33 @@ public void load() throws IOException, dev.magicmq.pyspigot.exception.InvalidCon
this.setDefaults(defaultConfig);
}
} catch (InvalidConfigurationException e) {
throw new dev.magicmq.pyspigot.exception.InvalidConfigurationException(e.getMessage(), e.getCause());
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* Reload the configuration. Will read all changes made to the configuration file since the configuration was last loaded/reloaded.
* @throws IOException If there was an exception when loading the file
* @throws dev.magicmq.pyspigot.exception.InvalidConfigurationException If there was an error when parsing the loaded file (invalid configuration)
*/
public void reload() throws IOException, dev.magicmq.pyspigot.exception.InvalidConfigurationException {
@Override
public void reload() throws IOException {
load();
}

/**
* Save the configuration to its associated file. For continuity purposes, the configuration is also reloaded from the file after saving.
* @throws IOException If there is an IOException when saving the file
* @throws dev.magicmq.pyspigot.exception.InvalidConfigurationException If there was an error when parsing the file when reloading (invalid configuration)
*/
public void save() throws IOException, dev.magicmq.pyspigot.exception.InvalidConfigurationException {
@Override
public void save() throws IOException {
this.save(configFile);
reload();
}

/**
* Sets the specified path to the given value only if the path is not already set in the config file. Any specified default values are ignored when checking if the path is set.
* @see org.bukkit.configuration.ConfigurationSection#set(String, Object)
* @param path Path of the object to set
* @param value Value to set the path to
* @return True if the path was set to the value (in other words the path was not previously set), false if the path was not set to the value (in other words the path was already previously set)
*/
public boolean setIfNotExists(String path, Object value) {
if (!super.isSet(path)) {
super.set(path, value);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@


import dev.magicmq.pyspigot.manager.config.ConfigManager;
import dev.magicmq.pyspigot.manager.config.ScriptConfig;

import java.io.IOException;
import java.nio.file.Path;

/**
* The BungeeCord-specific implementation of the config manager.
*/
public class BungeeConfigManager extends ConfigManager<BungeeScriptConfig> {
public class BungeeConfigManager extends ConfigManager {

private static BungeeConfigManager instance;

Expand All @@ -34,14 +35,7 @@ private BungeeConfigManager() {
}

@Override
public BungeeScriptConfig loadConfig(String filePath) throws IOException {
return loadConfig(filePath, null);
}

@Override
public BungeeScriptConfig loadConfig(String filePath, String defaults) throws IOException {
Path configFile = createConfigIfNotExists(filePath);

protected ScriptConfig loadConfigImpl(Path configFile, String defaults) throws IOException {
BungeeScriptConfig config = new BungeeScriptConfig(configFile.toFile(), defaults);
config.load();
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.magicmq.pyspigot.bungee.manager.config;

import dev.magicmq.pyspigot.manager.config.ScriptConfig;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
Expand All @@ -31,7 +32,7 @@
* A class representing a script configuration file, for the BungeeCord implementation.
* @see net.md_5.bungee.config.Configuration
*/
public class BungeeScriptConfig {
public class BungeeScriptConfig implements ScriptConfig {

private final File configFile;
private final String defaults;
Expand All @@ -48,22 +49,37 @@ public BungeeScriptConfig(File configFile, String defaults) {
this.defaults = defaults;
}

/**
* Get the file associated with this configuration.
* @return The file associated with this configuration
*/
@Override
public File getConfigFile() {
return configFile;
}

/**
* Get the absolute path of the file associated with this configuration.
* @return The path of the file
*/
@Override
public Path getConfigPath() {
return Paths.get(configFile.getAbsolutePath());
}

@Override
public void load() throws IOException {
if (defaults != null) {
Configuration defaultConfig = ConfigurationProvider.getProvider(YamlConfiguration.class).load(defaults);
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile, defaultConfig);
} else {
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
}
}

@Override
public void reload() throws IOException {
load();
}

@Override
public void save() throws IOException {
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, configFile);
reload();
}

/**
* Sets the specified path to the given value only if the path is not already set in the config file. Any specified default values are ignored when checking if the path is set.
* @see net.md_5.bungee.config.Configuration#set(String, Object)
Expand All @@ -80,33 +96,11 @@ public boolean setIfNotExists(String path, Object value) {
}

/**
* Loads the config from the configuration file. Will also set defaults for the configuration, if they were specified.
* @throws IOException If there was an exception when loading the file
* Gets the underlying BungeeCord {@link net.md_5.bungee.config.Configuration} object.
* @return The underlying config object
*/
public void load() throws IOException {
if (defaults != null) {
Configuration defaultConfig = ConfigurationProvider.getProvider(YamlConfiguration.class).load(defaults);
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile, defaultConfig);
} else {
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
}
}

/**
* Reload the configuration. Will read all changes made to the configuration file since the configuration was last loaded/reloaded.
* @throws IOException If there was an exception when loading the file
*/
public void reload() throws IOException {
load();
}

/**
* Save the configuration to its associated file. For continuity purposes, the configuration is also reloaded from the file after saving.
* @throws IOException If there is an IOException when saving the file
*/
public void save() throws IOException {
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, configFile);
reload();
public Configuration getUnderlyingConfig() {
return config;
}

/*------------------------------- Passthrough methods for consistency with Bukkit implementation -------------------------------*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package dev.magicmq.pyspigot.manager.config;

import dev.magicmq.pyspigot.PyCore;
import dev.magicmq.pyspigot.exception.InvalidConfigurationException;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -28,9 +27,9 @@
/**
* Manager for scripts to interface with configuration files. Primarily used by scripts to load, write to, and save .yml files.
*/
public abstract class ConfigManager<T> {
public abstract class ConfigManager {

private static ConfigManager<?> instance;
private static ConfigManager instance;

private final Path configFolder;

Expand All @@ -47,16 +46,19 @@ protected ConfigManager() {
}
}

protected abstract ScriptConfig loadConfigImpl(Path configFile, String defaults) throws IOException;

/**
* Load a configuration file with the given path/name, relative to the {@code configs} folder. If the configuration file exists, it will load the existing file. If the configuration file does not exist, a new file will be created with the given path/name.
* <p>
* <b>Note:</b> This should be called from scripts only!
* @param filePath The path of the configuration file to load, can be either the file name alone or a path (containing subfolders)
* @return A ScriptConfig representing the configuration file that was loaded
* @throws IOException If there was an IOException when attempting to load the configuration
* @throws InvalidConfigurationException If there was an error when parsing the loaded file (invalid configuration)
*/
public abstract T loadConfig(String filePath) throws IOException, InvalidConfigurationException;
public ScriptConfig loadConfig(String filePath) throws IOException {
return loadConfig(filePath, null);
}

/**
* Load a configuration file with the given path/name, relative to the {@code configs} folder. If the configuration file exists, it will load the existing file. If the configuration file does not exist, a new file will be created with the given path/name.
Expand All @@ -66,9 +68,11 @@ protected ConfigManager() {
* @param defaults A YAML-formatted string containing the desired default values for the configuration
* @return A ScriptConfig representing the configuration file that was loaded
* @throws IOException If there was an IOException when attempting to load the configuration
* @throws InvalidConfigurationException If there was an error when parsing the loaded file (invalid configuration)
*/
public abstract T loadConfig(String filePath, String defaults) throws IOException, InvalidConfigurationException;
public ScriptConfig loadConfig(String filePath, String defaults) throws IOException {
Path configFile = createConfigIfNotExists(filePath);
return loadConfigImpl(configFile, defaults);
}

/**
* Check if a configuration file exists with the given path/name, relative to the {@code configs} folder.
Expand Down Expand Up @@ -124,7 +128,7 @@ public Path getConfigFolder() {
* Get the singleton instance of this ConfigManager.
* @return The instance
*/
public static ConfigManager<?> get() {
public static ConfigManager get() {
return instance;
}

Expand Down
Loading

0 comments on commit be285f4

Please sign in to comment.