Class ConfigManager

java.lang.Object
redempt.redlib.configmanager.ConfigManager

public class ConfigManager extends Object
Loads config values into variables annotated with ConfigValue
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initiates a ConfigManager using a specific file for the config
    Initiates a ConfigManager using a specific path for the config
    ConfigManager(org.bukkit.plugin.Plugin plugin)
    Instantiates a ConfigManager with the default config name config.yml in the plugin's data folder
    ConfigManager(org.bukkit.plugin.Plugin plugin, String name)
    Instantiates a ConfigManager with a specific config name in the plugin's config folder
  • Method Summary

    Modifier and Type
    Method
    Description
    addConverter(Class<T> clazz, Function<String,T> load, Function<T,String> save)
    Adds a type converter, which will attempt to convert a String from config to another type that is not usually able to be stored in config
    static <T> ConfigCollection<T>
    collection(Class<T> clazz, Collection<T> toWrap, T... elements)
    Creates a ConfigCollection from a given type with initial elements.
    static <T> ConfigCollection<T>
    collection(Class<T> clazz, ConversionType type, Collection<T> toWrap, T... elements)
    Creates a ConfigCollection from a given type with initial elements.
    boolean
     
    org.bukkit.configuration.file.YamlConfiguration
     
    protected <T> redempt.redlib.configmanager.TypeConverter<T>
    getConverter(Class<T> clazz)
     
    static <T> redempt.redlib.configmanager.ConfigList<T>
    list(Class<T> clazz, ConversionType type, T... elements)
    Creates a ConfigList from a given type with initial elements.
    static <T> redempt.redlib.configmanager.ConfigList<T>
    list(Class<T> clazz, T... elements)
    Creates a ConfigList from a given type with initial elements.
    Loads all values from config into the annotated hook fields
    static <K, V> redempt.redlib.configmanager.ConfigMap<K,V>
    map(Class<K> keyClass, Class<V> valueClass)
    Creates a ConfigMap from a given type.
    static <K, V> redempt.redlib.configmanager.ConfigMap<K,V>
    map(Class<K> keyClass, Class<V> valueClass, ConversionType type)
    Creates a ConfigMap from a given type.
    static <T> redempt.redlib.configmanager.ConfigMap<String,T>
    map(Class<T> clazz)
    Creates a ConfigMap from a given type.
    static <T> redempt.redlib.configmanager.ConfigMap<String,T>
    map(Class<T> clazz, ConversionType type)
    Creates a ConfigMap from a given type.
    register(Object... data)
    Register all the hooks for annotated fields in the the given objects.
    Saves all values from the annotated hook fields to config
    Save default values - initial values in the hook fields - where they do not already exist in the config
    static <T> redempt.redlib.configmanager.ConfigSet<T>
    set(Class<T> clazz, ConversionType type, T... elements)
    Creates a ConfigSet from a given type with initial elements.
    static <T> redempt.redlib.configmanager.ConfigSet<T>
    set(Class<T> clazz, T... elements)
    Creates a ConfigSet from a given type with initial elements.
    static List<String>
    stringList(String... strings)
    Instantiates a List of Strings inline

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConfigManager

      public ConfigManager(org.bukkit.plugin.Plugin plugin)
      Instantiates a ConfigManager with the default config name config.yml in the plugin's data folder
      Parameters:
      plugin - The plugin
    • ConfigManager

      public ConfigManager(org.bukkit.plugin.Plugin plugin, String name)
      Instantiates a ConfigManager with a specific config name in the plugin's config folder
      Parameters:
      plugin - The plugin
      name - The name of the config file to generate
    • ConfigManager

      public ConfigManager(File file)
      Initiates a ConfigManager using a specific file for the config
      Parameters:
      file - The file
    • ConfigManager

      public ConfigManager(Path path)
      Initiates a ConfigManager using a specific path for the config
      Parameters:
      path - The path
  • Method Details

    • stringList

      public static List<String> stringList(String... strings)
      Instantiates a List of Strings inline
      Parameters:
      strings - The Strings to include in the List
      Returns:
      The List of Strings
    • map

      public static <T> redempt.redlib.configmanager.ConfigMap<String,T> map(Class<T> clazz)
      Creates a ConfigMap from a given type. A ConfigMap extends LinkedHashMap. The class is not accessible, so store it in a HashMap variable. This method must be used to set the initial value for a variable which loads in a section from config.
      Type Parameters:
      T - The value type
      Parameters:
      clazz - The class of the value type
      Returns:
      An empty map of the given type, which will be populated when load() is called
    • map

      public static <T> redempt.redlib.configmanager.ConfigMap<String,T> map(Class<T> clazz, ConversionType type)
      Creates a ConfigMap from a given type. A ConfigMap extends LinkedHashMap. The class is not accessible, so store it in a HashMap variable. This method must be used to set the initial value for a variable which loads in a section from config.
      Type Parameters:
      T - The value type
      Parameters:
      clazz - The class of the value type
      type - The method which will be used to convert the stored type
      Returns:
      An empty map of the given type, which will be populated when load() is called
    • map

      public static <K, V> redempt.redlib.configmanager.ConfigMap<K,V> map(Class<K> keyClass, Class<V> valueClass)
      Creates a ConfigMap from a given type. A ConfigMap extends LinkedHashMap. The class is not accessible, so store it in a HashMap variable. This method must be used to set the initial value for a variable which loads in a section from config. The key class may only be a type which has converter from string to another type. For obvious reasons, it cannot be a config-mappable object.
      Type Parameters:
      K - The key type
      V - The value type
      Parameters:
      keyClass - The class of the key type
      valueClass - The class of the value type
      Returns:
      An empty map of the given type, which will be populated when load() is called
    • map

      public static <K, V> redempt.redlib.configmanager.ConfigMap<K,V> map(Class<K> keyClass, Class<V> valueClass, ConversionType type)
      Creates a ConfigMap from a given type. A ConfigMap extends LinkedHashMap. The class is not accessible, so store it in a HashMap variable. This method must be used to set the initial value for a variable which loads in a section from config. The key class may only be a type which has converter from string to another type. For obvious reasons, it cannot be a config-mappable object.
      Type Parameters:
      K - The key type
      V - The value type
      Parameters:
      keyClass - The class of the key type
      valueClass - The class of the value type
      type - The method which will be used to convert the value type
      Returns:
      An empty map of the given type, which will be populated when load() is called
    • list

      public static <T> redempt.redlib.configmanager.ConfigList<T> list(Class<T> clazz, T... elements)
      Creates a ConfigList from a given type with initial elements. A ConfigList extends ArrayList. The class is not accessible, so store it in an ArrayList variable. This method must be used to set the initial value for a variable which loads a list from config using type converters or mapped objects.
      Type Parameters:
      T - The type
      Parameters:
      clazz - The class of the type of the list
      elements - The elements to initialize the list with
      Returns:
      A list of the given type which has been populated with the given elements
    • list

      public static <T> redempt.redlib.configmanager.ConfigList<T> list(Class<T> clazz, ConversionType type, T... elements)
      Creates a ConfigList from a given type with initial elements. A ConfigList extends ArrayList. The class is not accessible, so store it in an ArrayList variable. This method must be used to set the initial value for a variable which loads a list from config using type converters or mapped objects.
      Type Parameters:
      T - The type
      Parameters:
      clazz - The class of the type of the list
      elements - The elements to initialize the list with
      type - The method which will be used to convert the stored type
      Returns:
      A list of the given type which has been populated with the given elements
    • set

      public static <T> redempt.redlib.configmanager.ConfigSet<T> set(Class<T> clazz, T... elements)
      Creates a ConfigSet from a given type with initial elements. A ConfigSet extends LinkedHashSet. The class is not accessible, so store it in a HashSet variable. This method must be used to set the initial value for a variable which loads a set from config using type converters or mapped objects.
      Type Parameters:
      T - The type
      Parameters:
      clazz - The class of the type of the list
      elements - The elements to initialize the list with
      Returns:
      A set of the given type which has been populated with the given elements
    • set

      public static <T> redempt.redlib.configmanager.ConfigSet<T> set(Class<T> clazz, ConversionType type, T... elements)
      Creates a ConfigSet from a given type with initial elements. A ConfigSet extends LinkedHashSet. The class is not accessible, so store it in a HashSet variable. This method must be used to set the initial value for a variable which loads a set from config using type converters or mapped objects.
      Type Parameters:
      T - The type
      Parameters:
      clazz - The class of the type of the list
      elements - The elements to initialize the list with
      type - The method which will be used to convert the stored type
      Returns:
      A set of the given type which has been populated with the given elements
    • collection

      public static <T> ConfigCollection<T> collection(Class<T> clazz, ConversionType type, Collection<T> toWrap, T... elements)
      Creates a ConfigCollection from a given type with initial elements. A ConfigCollection wraps the given collection. This method must be used to set the initial value for a variable which loads a set from config using type converters or mapped objects.
      Type Parameters:
      T - The type
      Parameters:
      clazz - The class of the type of the list
      elements - The elements to initialize the list with
      type - The method which will be used to convert the stored type
      toWrap - The Collection to wrap
      Returns:
      A wrapped collection of the given type which has been populated with the given elements
    • collection

      public static <T> ConfigCollection<T> collection(Class<T> clazz, Collection<T> toWrap, T... elements)
      Creates a ConfigCollection from a given type with initial elements. A ConfigCollection wraps the given collection. This method must be used to set the initial value for a variable which loads a set from config using type converters or mapped objects.
      Type Parameters:
      T - The type
      Parameters:
      clazz - The class of the type of the list
      elements - The elements to initialize the list with
      toWrap - The Collection to wrap
      Returns:
      A wrapped collection of the given type which has been populated with the given elements
    • addConverter

      public <T> ConfigManager addConverter(Class<T> clazz, Function<String,T> load, Function<T,String> save)
      Adds a type converter, which will attempt to convert a String from config to another type that is not usually able to be stored in config
      Type Parameters:
      T - The type
      Parameters:
      clazz - The class of the type
      load - A function to convert from a string to the type
      save - A function to convert from the type to a string
      Returns:
      This ConfigManager
    • getConverter

      protected <T> redempt.redlib.configmanager.TypeConverter<T> getConverter(Class<T> clazz)
    • register

      public ConfigManager register(Object... data)
      Register all the hooks for annotated fields in the the given objects. Pass classes instead if static fields are used.
      Parameters:
      data - The object to be registered
      Returns:
      This ConfigManager
    • configExists

      public boolean configExists()
      Returns:
      Whether the config file exists
    • saveDefaults

      public ConfigManager saveDefaults()
      Save default values - initial values in the hook fields - where they do not already exist in the config
      Returns:
      This ConfigManager
      Throws:
      IllegalStateException - if this ConfigManager has not been registered yet
    • load

      public ConfigManager load()
      Loads all values from config into the annotated hook fields
      Returns:
      This ConfigManager
      Throws:
      IllegalStateException - if this ConfigManager has not been registered yet
    • save

      public ConfigManager save()
      Saves all values from the annotated hook fields to config
      Returns:
      This ConfigManager
      Throws:
      IllegalStateException - if this ConfigManager has not been registered yet
    • getConfig

      public org.bukkit.configuration.file.YamlConfiguration getConfig()
      Returns:
      The configuration this ConfigManager is loading from and saving to