Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package reghzy.api.config;
- import org.bukkit.configuration.InvalidConfigurationException;
- import org.bukkit.plugin.Plugin;
- import reghzy.api.utils.KVObjectCache;
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- public class ConfigManager {
- private final Plugin plugin;
- private final HashMap<String, Config> configMap;
- private final HashMap<String, ConfigLoadHandler> loadHandlerMap;
- private final HashMap<String, ConfigPreSaveHandler> saveHandlerMap;
- private final KVObjectCache<String, Config> lastAccessedCache = new KVObjectCache<String, Config>() {
- @Override
- public Config getValue(String key) {
- Config config = configMap.get(key);
- if (config == null) {
- throw new NoSuchConfigException(key);
- }
- return config;
- }
- };
- public ConfigManager(Plugin plugin) {
- this.configMap = new HashMap<String, Config>();
- this.plugin = plugin;
- this.loadHandlerMap = new HashMap<String,ConfigLoadHandler>();
- this.saveHandlerMap = new HashMap<String,ConfigPreSaveHandler>();
- }
- /**
- * Gets a registered config
- * @param name The config name
- * @throws NoSuchConfigException If the config is not registered
- */
- @Nonnull
- public Config getConfig(String name) {
- return this.lastAccessedCache.get(name);
- }
- /**
- * Returns true if the config with the given name is registered. Otherwise, false
- * @param name The config name
- */
- public boolean doesConfigExist(String name) {
- return this.configMap.containsKey(name);
- }
- /**
- * Creates a config instance located in the plugin folder named by the given file name (.yml is automatically added).
- * Does not create the file, nor does it register it
- * @param name The config name
- */
- @Nonnull
- public Config createConfig(String name) {
- return new Config(this, getNormalFileWithYAMLExtension(name));
- }
- /**
- * Creates a config instance located in the plugin folder, in the given sub folder, named by the given file name (.yml is automatically added).
- * Does not create the file, nor does it register it
- * @param name The config name
- */
- @Nonnull
- public Config createConfig(String folder, String name) {
- return new Config(this, getFileWithYAMLExtension(new File(this.plugin.getDataFolder(), folder), name));
- }
- /**
- * Creates a config instance located at the given file.
- * Does not create the file, nor does it register it
- * @param file The config name
- */
- @Nonnull
- public Config createConfig(File file) {
- return new Config(this, file);
- }
- /**
- * Registers a config
- * @param name Config name
- * @param loadHandler Load handler
- * @param preSaveHandler Pre-save handler
- */
- public void registerConfig(String name, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
- registerConfig(name, getNormalFileWithYAMLExtension(name), loadHandler, preSaveHandler);
- }
- /**
- * Registers a config, and tries to copy the contents of a plugin resource to the config file (only if the config file doesn't exist)
- * @param name Config name
- * @param loadHandler Load handler
- * @param preSaveHandler Pre-save handler
- */
- public boolean registerResourceConfig(String name, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
- return registerResourceConfig(name, getNormalFileWithYAMLExtension(name), loadHandler, preSaveHandler);
- }
- /**
- * Registers a config
- * @param name Config name
- * @param file The file where the config is located
- * @param loadHandler Load handler
- * @param preSaveHandler Pre-save handler
- * @throws RuntimeException If the file failed to be created
- */
- public void registerConfig(String name, File file, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
- if (this.configMap.containsKey(name)) {
- throw new ConfigAlreadyRegisteredException(name);
- }
- Config config = new Config(this, file);
- if (!config.exists()) {
- try {
- if (!config.createFile()) {
- throw new RuntimeException("Failed to create config file " + name);
- }
- }
- catch (IOException e) {
- throw new RuntimeException("IOException while creating config file " + name, e);
- }
- }
- this.configMap.put(name, config);
- this.loadHandlerMap.put(name, loadHandler);
- this.saveHandlerMap.put(name, preSaveHandler);
- }
- /**
- * Registers a config, and tries to copy the contents of a plugin resource to the config file (only if the config file doesn't exist)
- * @param name Config name
- * @param file The file where the config is located
- * @param loadHandler Load handler
- * @param preSaveHandler Pre-save handler
- * @throws RuntimeException If the file failed to be created, or the resource failed to copy
- * @return Whether the resource was copied or not. False if the file already exists, or if it didn't exist but the resource didn't exist.
- * True if the file didn't exist and the resource existed
- */
- public boolean registerResourceConfig(String name, File file, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
- if (this.configMap.containsKey(name)) {
- throw new ConfigAlreadyRegisteredException(name);
- }
- Config config = new Config(this, file);
- if (!config.exists()) {
- try {
- if (config.createFile()) {
- try {
- return config.copyDefaultResource();
- }
- catch (IOException e) {
- throw new RuntimeException("IOException while copying plugin resource to config file: " + name, e);
- }
- }
- else {
- throw new RuntimeException("Failed to create config file " + name);
- }
- }
- catch (IOException e) {
- throw new RuntimeException("IOException while creating config file " + name, e);
- }
- }
- this.configMap.put(name, config);
- this.loadHandlerMap.put(name, loadHandler);
- this.saveHandlerMap.put(name, preSaveHandler);
- return false;
- }
- /**
- * Gets a config load handler for the given config name
- * @return The load handler, or null if one doesn't exist for the given config name
- */
- @Nullable
- public ConfigLoadHandler getLoadHandler(@Nonnull String name) {
- return this.loadHandlerMap.get(name);
- }
- /**
- * Gets a config load handler for the given config
- * @return The load handler, or null if one doesn't exist for the given config
- */
- @Nullable
- public ConfigLoadHandler getLoadHandler(@Nonnull Config config) {
- return this.loadHandlerMap.get(config.getConfigName());
- }
- /**
- * Gets a config pre-save handler for the given config name
- * @return The pre-save handler, or null if one doesn't exist for the given config name
- */
- @Nullable
- public ConfigPreSaveHandler getSaveHandler(@Nonnull String name) {
- return this.saveHandlerMap.get(name);
- }
- /**
- * Gets a config pre-save handler for the given config
- * @return The pre-save handler, or null if one doesn't exist for the given config
- */
- @Nullable
- public ConfigPreSaveHandler getSaveHandler(@Nonnull Config config) {
- return this.saveHandlerMap.get(config.getConfigName());
- }
- /**
- * Returns whether there's a save handler for the given config
- * @param config The config
- */
- public boolean isSavable(@Nonnull Config config) {
- return this.saveHandlerMap.containsKey(config.getConfigName());
- }
- /**
- * Returns whether there's a save handler for the given config name
- * @param name The config name
- */
- public boolean isSavable(@Nonnull String name) {
- return this.saveHandlerMap.containsKey(name);
- }
- /**
- * Calls the config's load handler
- * @param config The config to call the load handler of
- * @return True if the load handler was called successfully, otherwise false if it doesn't exist
- */
- public boolean callLoadHandler(Config config) {
- ConfigLoadHandler handler = getLoadHandler(config);
- if (handler == null) {
- return false;
- }
- try {
- handler.onLoaded(config);
- }
- catch (Throwable e) {
- throw new RuntimeException("Failed to invoke load handler for config " + config.getConfigName(), e);
- }
- return true;
- }
- /**
- * Calls the config's pre-save handler
- * @param config The config to call the load handler of
- * @return True if the load handler was called successfully, otherwise false if it doesn't exist
- */
- public boolean callPreSaveHandler(Config config) {
- ConfigPreSaveHandler handler = getSaveHandler(config);
- if (handler == null) {
- return false;
- }
- try {
- handler.onSaving(config);
- }
- catch (Throwable e) {
- throw new RuntimeException("Failed to invoke pre-save handler for config " + config.getConfigName(), e);
- }
- return true;
- }
- public boolean callLoadHandler(String name) {
- return callLoadHandler(getConfig(name));
- }
- public boolean callPreSaveHandler(String name) {
- return callPreSaveHandler(getConfig(name));
- }
- /**
- * Tries to reload a config's raw data
- * @param name The name of the config to reload
- * @return True if the config load handler was called. False if the load handler didn't exist
- * @throws RuntimeException If the config failed to reload, or the load handler threw an exception. The inner-exception contains the exception thrown
- */
- public void loadConfig(String name) {
- loadConfig(getConfig(name));
- }
- /**
- * Tries to save a config's raw data to the file
- * @param name The name of the config to save
- * @return True if the config pre-save handler was called. False if the pre-save handler didn't exist
- * @throws RuntimeException If the config failed to reload, or the pre-save handler threw an exception. The inner-exception contains the exception thrown
- */
- public void saveConfig(String name) {
- saveConfig(getConfig(name));
- }
- /**
- * Tries to reload a config's raw data
- * @param config The config to reload
- * @throws RuntimeException If the config failed to reload, or the load handler threw an exception. The inner-exception contains the exception thrown
- */
- public void loadConfig(Config config) {
- try {
- config.load();
- }
- catch (FileNotFoundException e) {
- throw new RuntimeException("File did not exist while reloading config " + config.getConfigName(), e);
- }
- catch (IOException e) {
- throw new RuntimeException("IOException while reloading config " + config.getConfigName(), e);
- }
- catch (InvalidConfigurationException e) {
- throw new RuntimeException("Invalid YAML data while reloading config " + config.getConfigName(), e);
- }
- ConfigLoadHandler handler = getLoadHandler(config);
- if (handler != null) {
- try {
- handler.onLoaded(config);
- }
- catch (Throwable e) {
- throw new RuntimeException("Failed to invoke load handler for config " + config.getConfigName(), e);
- }
- }
- }
- /**
- * Tries to save a config's raw data to the file
- * @param config The config to save
- * @throws RuntimeException If the config failed to reload, or the pre-save handler threw an exception. The inner-exception contains the exception thrown
- */
- public void saveConfig(Config config) {
- ConfigPreSaveHandler handler = getSaveHandler(config);
- if (handler == null) {
- throw new RuntimeException("The config " + config.getConfigName() + " cannot be saved");
- }
- try {
- handler.onSaving(config);
- }
- catch (Throwable e) {
- throw new RuntimeException("Failed to invoke pre-save handler for config " + config.getConfigName(), e);
- }
- try {
- config.save();
- }
- catch (FileNotFoundException e) {
- throw new RuntimeException("File did not exist while saving config " + config.getConfigName(), e);
- }
- catch (IOException e) {
- throw new RuntimeException("IOException while saving config " + config.getConfigName(), e);
- }
- }
- @Nonnull
- public List<String> getConfigNames() {
- return new ArrayList<String>(this.configMap.keySet());
- }
- @Nonnull
- public List<Config> getConfigs() {
- return new ArrayList<Config>(this.configMap.values());
- }
- @Nonnull
- public Plugin getPlugin() {
- return plugin;
- }
- @Nonnull
- public File getNormalFileWithYAMLExtension(String name) {
- return getFileWithYAMLExtension(this.plugin.getDataFolder(), name);
- }
- @Nonnull
- public File getFileWithYAMLExtension(File parent, String name) {
- if (!name.endsWith(".yml")) {
- name += ".yml";
- }
- return new File(parent, name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement