Class Config

java.lang.Object
dev.cashire.cashconfig.Config

public final class Config extends Object
Configuration Object. Use this to load and save configuration data from a file.
  • Constructor Details

    • Config

      public Config(@NotNull @NotNull String fileName)
      Create new Configuration Instance. Loads/Saves file with given name from FabricLoader.getConfigDir()
      Parameters:
      fileName - filename of config file.
    • Config

      public Config(@NotNull @NotNull BaseConfigItem<?> item, @NotNull @NotNull String fileName)
      Create new Configuration Instance. Loads/Saves file with given name from FabricLoader.getConfigDir()
      Parameters:
      item - item to default in if no values.
      fileName - filename of config file.
    • Config

      public Config(@NotNull @NotNull List<BaseConfigItem<?>> items, @NotNull @NotNull String fileName)
      Create new Configuration Instance. Loads/Saves file with given name from FabricLoader.getConfigDir()
      Parameters:
      items - items to default in if no values.
      fileName - filename of config file.
    • Config

      public Config(@NotNull @NotNull File file)
      Create new Configuration Instance.
      Parameters:
      file - file to read/save to.
    • Config

      public Config(BaseConfigItem<?> item, @NotNull @NotNull File file)
      Create new Configuration Instance.
      Parameters:
      item - item to default in if no values
      file - file to read/save to.
    • Config

      public Config(List<BaseConfigItem<?>> items, @NotNull @NotNull File file)
      Create new Configuration Instance.
      Parameters:
      items - List of items to default in if no values
      file - file to read/save to.
  • Method Details

    • getItems

      public List<BaseConfigItem<?>> getItems()
      Get list of Configuration items in this configuration. If readFile() hasn't been called, this will contain the defaults given.
      Returns:
      list of BaseConfigItem's containing configuration data.
      See Also:
      for retrieving an individual item
    • saveFile

      public void saveFile()
      Saves current configuration to a file.
    • hasFile

      public boolean hasFile()
      Checks to see if the file exists.
      Returns:
      true if file exists, false otherwise
    • readFile

      public void readFile()
      Reads configuration from file. Values can be retrieved via getItem(String, Type) or getItems()
    • getItem

      public BaseConfigItem<?> getItem(@NotNull @NotNull String path, @NotNull @NotNull BaseConfigItem.Type type) throws IllegalArgumentException
      Retrieves an Item from the configuration structure.
      Parameters:
      path - path to item in format: group.item
      type - Type of the value expected to find at the end of the path.
      Returns:
      item found, null otherwise
      Throws:
      IllegalArgumentException - thrown if type doesn't match item.
    • getItem

      public BaseConfigItem<?> getItem(@NotNull @NotNull String path)
      Retrieves an Item from the configuration structure.
      Parameters:
      path - path to item in format: group.item
      Returns:
      item if found, null otherwise
    • removeItem

      public void removeItem(@NotNull @NotNull String path)
      Removes an item from the Config File.
      Parameters:
      path - path to item in format: group.item
    • hasItem

      public boolean hasItem(@NotNull @NotNull String path)
      Checks if Config has item at given path.
      Parameters:
      path - path to item in format: group.item
      Returns:
      true if item found, false otherwise
    • setItem

      public void setItem(@NotNull @NotNull String path, @NotNull @NotNull BaseConfigItem<?> item)
      Sets the given item in the existing item at the end of the path. If existing item is an array, item is added.
      Parameters:
      path - path where to add item in format: group.item
      item - item to add, must have key if adding to group
    • setItem

      public void setItem(@NotNull @NotNull BaseConfigItem<?> item)
      Sets the given item at the root of the config.
      Parameters:
      item - item to set, must have a key
    • addItem

      public boolean addItem(@NotNull @NotNull BaseConfigItem<?> item)
      Adds item to root of the config.
      Parameters:
      item - item to add, must have key
      Returns:
      true if added, false otherwise
    • addItem

      public boolean addItem(@NotNull @NotNull String path, @NotNull @NotNull BaseConfigItem<?> item)
      Adds item to existing item found at end of path.
      Parameters:
      path - path to item to add to. format: group.item
      item - Item to add, must have key if path item is a group
      Returns:
      true if added, false otherwise
    • getType

      public BaseConfigItem.Type getType(@NotNull @NotNull String path)
      Get the type of the item at the given path.
      Parameters:
      path - path to item in the format: group.item
      Returns:
      Item Type if found, null otherwise.