Advertisement
bobmarley12345

bukkit config manager

Nov 3rd, 2021
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.97 KB | None | 0 0
  1. package reghzy.api.config;
  2.  
  3. import org.bukkit.configuration.InvalidConfigurationException;
  4. import org.bukkit.plugin.Plugin;
  5. import reghzy.api.utils.KVObjectCache;
  6.  
  7. import javax.annotation.Nonnull;
  8. import javax.annotation.Nullable;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.List;
  15.  
  16. public class ConfigManager {
  17.     private final Plugin plugin;
  18.     private final HashMap<String, Config> configMap;
  19.     private final HashMap<String, ConfigLoadHandler> loadHandlerMap;
  20.     private final HashMap<String, ConfigPreSaveHandler> saveHandlerMap;
  21.  
  22.     private final KVObjectCache<String, Config> lastAccessedCache = new KVObjectCache<String, Config>() {
  23.         @Override
  24.         public Config getValue(String key) {
  25.             Config config = configMap.get(key);
  26.             if (config == null) {
  27.                 throw new NoSuchConfigException(key);
  28.             }
  29.  
  30.             return config;
  31.         }
  32.     };
  33.  
  34.     public ConfigManager(Plugin plugin) {
  35.         this.configMap = new HashMap<String, Config>();
  36.         this.plugin = plugin;
  37.         this.loadHandlerMap = new HashMap<String,ConfigLoadHandler>();
  38.         this.saveHandlerMap = new HashMap<String,ConfigPreSaveHandler>();
  39.     }
  40.  
  41.     /**
  42.      * Gets a registered config
  43.      * @param name The config name
  44.      * @throws NoSuchConfigException If the config is not registered
  45.      */
  46.     @Nonnull
  47.     public Config getConfig(String name) {
  48.         return this.lastAccessedCache.get(name);
  49.     }
  50.  
  51.     /**
  52.      * Returns true if the config with the given name is registered. Otherwise, false
  53.      * @param name The config name
  54.      */
  55.     public boolean doesConfigExist(String name) {
  56.         return this.configMap.containsKey(name);
  57.     }
  58.  
  59.     /**
  60.      * Creates a config instance located in the plugin folder named by the given file name (.yml is automatically added).
  61.      * Does not create the file, nor does it register it
  62.      * @param name The config name
  63.      */
  64.     @Nonnull
  65.     public Config createConfig(String name) {
  66.         return new Config(this, getNormalFileWithYAMLExtension(name));
  67.     }
  68.  
  69.     /**
  70.      * Creates a config instance located in the plugin folder, in the given sub folder, named by the given file name (.yml is automatically added).
  71.      * Does not create the file, nor does it register it
  72.      * @param name The config name
  73.      */
  74.     @Nonnull
  75.     public Config createConfig(String folder, String name) {
  76.         return new Config(this, getFileWithYAMLExtension(new File(this.plugin.getDataFolder(), folder), name));
  77.     }
  78.  
  79.     /**
  80.      * Creates a config instance located at the given file.
  81.      * Does not create the file, nor does it register it
  82.      * @param file The config name
  83.      */
  84.     @Nonnull
  85.     public Config createConfig(File file) {
  86.         return new Config(this, file);
  87.     }
  88.  
  89.     /**
  90.      * Registers a config
  91.      * @param name           Config name
  92.      * @param loadHandler    Load handler
  93.      * @param preSaveHandler Pre-save handler
  94.      */
  95.     public void registerConfig(String name, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
  96.         registerConfig(name, getNormalFileWithYAMLExtension(name), loadHandler, preSaveHandler);
  97.     }
  98.  
  99.     /**
  100.      * 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)
  101.      * @param name           Config name
  102.      * @param loadHandler    Load handler
  103.      * @param preSaveHandler Pre-save handler
  104.      */
  105.     public boolean registerResourceConfig(String name, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
  106.         return registerResourceConfig(name, getNormalFileWithYAMLExtension(name), loadHandler, preSaveHandler);
  107.     }
  108.  
  109.     /**
  110.      * Registers a config
  111.      * @param name           Config name
  112.      * @param file           The file where the config is located
  113.      * @param loadHandler    Load handler
  114.      * @param preSaveHandler Pre-save handler
  115.      * @throws RuntimeException If the file failed to be created
  116.      */
  117.     public void registerConfig(String name, File file, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
  118.         if (this.configMap.containsKey(name)) {
  119.             throw new ConfigAlreadyRegisteredException(name);
  120.         }
  121.  
  122.         Config config = new Config(this, file);
  123.         if (!config.exists()) {
  124.             try {
  125.                 if (!config.createFile()) {
  126.                     throw new RuntimeException("Failed to create config file " + name);
  127.                 }
  128.             }
  129.             catch (IOException e) {
  130.                 throw new RuntimeException("IOException while creating config file " + name, e);
  131.             }
  132.         }
  133.  
  134.         this.configMap.put(name, config);
  135.         this.loadHandlerMap.put(name, loadHandler);
  136.         this.saveHandlerMap.put(name, preSaveHandler);
  137.     }
  138.  
  139.     /**
  140.      * 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)
  141.      * @param name           Config name
  142.      * @param file           The file where the config is located
  143.      * @param loadHandler    Load handler
  144.      * @param preSaveHandler Pre-save handler
  145.      * @throws RuntimeException If the file failed to be created, or the resource failed to copy
  146.      * @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.
  147.      *         True if the file didn't exist and the resource existed
  148.      */
  149.     public boolean registerResourceConfig(String name, File file, ConfigLoadHandler loadHandler, ConfigPreSaveHandler preSaveHandler) {
  150.         if (this.configMap.containsKey(name)) {
  151.             throw new ConfigAlreadyRegisteredException(name);
  152.         }
  153.  
  154.         Config config = new Config(this, file);
  155.         if (!config.exists()) {
  156.             try {
  157.                 if (config.createFile()) {
  158.                     try {
  159.                         return config.copyDefaultResource();
  160.                     }
  161.                     catch (IOException e) {
  162.                         throw new RuntimeException("IOException while copying plugin resource to config file: " + name, e);
  163.                     }
  164.                 }
  165.                 else {
  166.                     throw new RuntimeException("Failed to create config file " + name);
  167.                 }
  168.             }
  169.             catch (IOException e) {
  170.                 throw new RuntimeException("IOException while creating config file " + name, e);
  171.             }
  172.         }
  173.  
  174.         this.configMap.put(name, config);
  175.         this.loadHandlerMap.put(name, loadHandler);
  176.         this.saveHandlerMap.put(name, preSaveHandler);
  177.         return false;
  178.     }
  179.  
  180.     /**
  181.      * Gets a config load handler for the given config name
  182.      * @return The load handler, or null if one doesn't exist for the given config name
  183.      */
  184.     @Nullable
  185.     public ConfigLoadHandler getLoadHandler(@Nonnull String name) {
  186.         return this.loadHandlerMap.get(name);
  187.     }
  188.  
  189.     /**
  190.      * Gets a config load handler for the given config
  191.      * @return The load handler, or null if one doesn't exist for the given config
  192.      */
  193.     @Nullable
  194.     public ConfigLoadHandler getLoadHandler(@Nonnull Config config) {
  195.         return this.loadHandlerMap.get(config.getConfigName());
  196.     }
  197.  
  198.     /**
  199.      * Gets a config pre-save handler for the given config name
  200.      * @return The pre-save handler, or null if one doesn't exist for the given config name
  201.      */
  202.     @Nullable
  203.     public ConfigPreSaveHandler getSaveHandler(@Nonnull String name) {
  204.         return this.saveHandlerMap.get(name);
  205.     }
  206.  
  207.     /**
  208.      * Gets a config pre-save handler for the given config
  209.      * @return The pre-save handler, or null if one doesn't exist for the given config
  210.      */
  211.     @Nullable
  212.     public ConfigPreSaveHandler getSaveHandler(@Nonnull Config config) {
  213.         return this.saveHandlerMap.get(config.getConfigName());
  214.     }
  215.  
  216.     /**
  217.      * Returns whether there's a save handler for the given config
  218.      * @param config The config
  219.      */
  220.     public boolean isSavable(@Nonnull Config config) {
  221.         return this.saveHandlerMap.containsKey(config.getConfigName());
  222.     }
  223.  
  224.     /**
  225.      * Returns whether there's a save handler for the given config name
  226.      * @param name The config name
  227.      */
  228.     public boolean isSavable(@Nonnull String name) {
  229.         return this.saveHandlerMap.containsKey(name);
  230.     }
  231.  
  232.     /**
  233.      * Calls the config's load handler
  234.      * @param config The config to call the load handler of
  235.      * @return True if the load handler was called successfully, otherwise false if it doesn't exist
  236.      */
  237.     public boolean callLoadHandler(Config config) {
  238.         ConfigLoadHandler handler = getLoadHandler(config);
  239.         if (handler == null) {
  240.             return false;
  241.         }
  242.  
  243.         try {
  244.             handler.onLoaded(config);
  245.         }
  246.         catch (Throwable e) {
  247.             throw new RuntimeException("Failed to invoke load handler for config " + config.getConfigName(), e);
  248.         }
  249.  
  250.         return true;
  251.     }
  252.  
  253.     /**
  254.      * Calls the config's pre-save handler
  255.      * @param config The config to call the load handler of
  256.      * @return True if the load handler was called successfully, otherwise false if it doesn't exist
  257.      */
  258.     public boolean callPreSaveHandler(Config config) {
  259.         ConfigPreSaveHandler handler = getSaveHandler(config);
  260.         if (handler == null) {
  261.             return false;
  262.         }
  263.  
  264.         try {
  265.             handler.onSaving(config);
  266.         }
  267.         catch (Throwable e) {
  268.             throw new RuntimeException("Failed to invoke pre-save handler for config " + config.getConfigName(), e);
  269.         }
  270.  
  271.         return true;
  272.     }
  273.  
  274.     public boolean callLoadHandler(String name) {
  275.         return callLoadHandler(getConfig(name));
  276.     }
  277.  
  278.     public boolean callPreSaveHandler(String name) {
  279.         return callPreSaveHandler(getConfig(name));
  280.     }
  281.  
  282.     /**
  283.      * Tries to reload a config's raw data
  284.      * @param name The name of the config to reload
  285.      * @return True if the config load handler was called. False if the load handler didn't exist
  286.      * @throws RuntimeException If the config failed to reload, or the load handler threw an exception. The inner-exception contains the exception thrown
  287.      */
  288.     public void loadConfig(String name) {
  289.         loadConfig(getConfig(name));
  290.     }
  291.  
  292.     /**
  293.      * Tries to save a config's raw data to the file
  294.      * @param name The name of the config to save
  295.      * @return True if the config pre-save handler was called. False if the pre-save handler didn't exist
  296.      * @throws RuntimeException If the config failed to reload, or the pre-save handler threw an exception. The inner-exception contains the exception thrown
  297.      */
  298.     public void saveConfig(String name) {
  299.         saveConfig(getConfig(name));
  300.     }
  301.  
  302.     /**
  303.      * Tries to reload a config's raw data
  304.      * @param config The config to reload
  305.      * @throws RuntimeException If the config failed to reload, or the load handler threw an exception. The inner-exception contains the exception thrown
  306.      */
  307.     public void loadConfig(Config config) {
  308.         try {
  309.             config.load();
  310.         }
  311.         catch (FileNotFoundException e) {
  312.             throw new RuntimeException("File did not exist while reloading config " + config.getConfigName(), e);
  313.         }
  314.         catch (IOException e) {
  315.             throw new RuntimeException("IOException while reloading config " + config.getConfigName(), e);
  316.         }
  317.         catch (InvalidConfigurationException e) {
  318.             throw new RuntimeException("Invalid YAML data while reloading config " + config.getConfigName(), e);
  319.         }
  320.  
  321.         ConfigLoadHandler handler = getLoadHandler(config);
  322.         if (handler != null) {
  323.             try {
  324.                 handler.onLoaded(config);
  325.             }
  326.             catch (Throwable e) {
  327.                 throw new RuntimeException("Failed to invoke load handler for config " + config.getConfigName(), e);
  328.             }
  329.         }
  330.     }
  331.  
  332.     /**
  333.      * Tries to save a config's raw data to the file
  334.      * @param config The config to save
  335.      * @throws RuntimeException If the config failed to reload, or the pre-save handler threw an exception. The inner-exception contains the exception thrown
  336.      */
  337.     public void saveConfig(Config config) {
  338.         ConfigPreSaveHandler handler = getSaveHandler(config);
  339.         if (handler == null) {
  340.             throw new RuntimeException("The config " + config.getConfigName() + " cannot be saved");
  341.         }
  342.  
  343.         try {
  344.             handler.onSaving(config);
  345.         }
  346.         catch (Throwable e) {
  347.             throw new RuntimeException("Failed to invoke pre-save handler for config " + config.getConfigName(), e);
  348.         }
  349.  
  350.         try {
  351.             config.save();
  352.         }
  353.         catch (FileNotFoundException e) {
  354.             throw new RuntimeException("File did not exist while saving config " + config.getConfigName(), e);
  355.         }
  356.         catch (IOException e) {
  357.             throw new RuntimeException("IOException while saving config " + config.getConfigName(), e);
  358.         }
  359.     }
  360.  
  361.     @Nonnull
  362.     public List<String> getConfigNames() {
  363.         return new ArrayList<String>(this.configMap.keySet());
  364.     }
  365.  
  366.     @Nonnull
  367.     public List<Config> getConfigs() {
  368.         return new ArrayList<Config>(this.configMap.values());
  369.     }
  370.  
  371.     @Nonnull
  372.     public Plugin getPlugin() {
  373.         return plugin;
  374.     }
  375.  
  376.     @Nonnull
  377.     public File getNormalFileWithYAMLExtension(String name) {
  378.         return getFileWithYAMLExtension(this.plugin.getDataFolder(), name);
  379.     }
  380.  
  381.     @Nonnull
  382.     public File getFileWithYAMLExtension(File parent, String name) {
  383.         if (!name.endsWith(".yml")) {
  384.             name += ".yml";
  385.         }
  386.  
  387.         return new File(parent, name);
  388.     }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement