Class ConfigHelper

java.lang.Object
tk.airshipcraft.commonlib.configuration.ConfigHelper

public class ConfigHelper extends Object
Manages the configuration for plugins that extend CommonLib. This class handles loading, saving, and repairing configuration settings. It uses reflection to automatically initialize configuration values based on annotated fields in a given class. Additionally, it maintains a default configuration file to ensure robustness in configuration management.
Since:
2024-1-4
Version:
2.0.0
Author:
notzune
  • Constructor Details

    • ConfigHelper

      public ConfigHelper(CommonLib plugin, Class<?> configClass)
      Constructs a ConfigHelper for the given plugin and configuration class. This will initialize the default configuration by either creating it or loading it if it already exists.
      Parameters:
      plugin - The plugin instance. This is used to access plugin-specific configuration and data.
      configClass - The class containing configuration fields. Fields in this class should be annotated with ConfigOption.
  • Method Details

    • saveConfig

      public void saveConfig()
      Saves the current configuration to the config.yml file. This method iterates through all fields in the provided configClass and saves the corresponding values to the config.yml file.
    • loadConfig

      public void loadConfig()
      Loads the configuration from the config.yml file. This method iterates through all fields in the provided configClass and loads the corresponding values from the config.yml file.
    • repairConfig

      public void repairConfig()
      Repairs the configuration file on plugin restart. This method ensures that the config file is correctly formatted and contains all necessary values. Existing values are not overwritten unless they are malformed or missing.
    • getString

      public String getString(String key)
      Gets the value of a configuration option as a String. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a String.
    • getInt

      public int getInt(String key)
      Gets the value of a configuration option as an int. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as an int.
    • getBoolean

      public boolean getBoolean(String key)
      Gets the value of a configuration option as a short. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a short.
    • getLong

      public long getLong(String key)
      Gets the value of a configuration option as a byte. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a byte.
    • getDouble

      public double getDouble(String key)
      Gets the value of a configuration option as a double. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a double.
    • getFloatList

      public List<Float> getFloatList(String key)
      Gets the value of a configuration option as a List of Floats. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Floats.
    • getDoubleList

      public List<Double> getDoubleList(String key)
      Gets the value of a configuration option as a List of Doubles. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Doubles.
    • getStringList

      public List<String> getStringList(String key)
      Gets the value of a configuration option as a List of Strings. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Strings.
    • getIntegerList

      public List<Integer> getIntegerList(String key)
      Gets the value of a configuration option as a List of Integers. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Integers.
    • getLongList

      public List<Long> getLongList(String key)
      Gets the value of a configuration option as a List of Longs. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Longs.
    • getByteList

      public List<Byte> getByteList(String key)
      Gets the value of a configuration option as a List of Bytes. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Bytes.
    • getCharacterList

      public List<Character> getCharacterList(String key)
      Gets the value of a configuration option as a List of Characters. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Characters.
    • getBooleanList

      public List<Boolean> getBooleanList(String key)
      Gets the value of a configuration option as a List of Booleans. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Booleans.
    • getShortList

      public List<Short> getShortList(String key)
      Gets the value of a configuration option as a List of Shorts. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Shorts.
    • getMapList

      public List<Map<?,?>> getMapList(String key)
      Gets the value of a configuration option as a List of Maps. If the option is not present in the config.yml file, the default value is returned. Each map in the list represents a section of the configuration file.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a List of Maps.
    • getItemStack

      public org.bukkit.inventory.ItemStack getItemStack(String key)
      Gets the value of an ItemStack configuration option. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as an ItemStack.
    • getColor

      public org.bukkit.Color getColor(String key)
      Gets the value of a Color configuration option. If the option is not present in the config.yml file, the default value is returned.
      Parameters:
      key - The key of the configuration option.
      Returns:
      The value of the configuration option as a Color.