Class ConfigWrapper

java.lang.Object
com.github.nedelis.jc4j.ConfigWrapper

public class ConfigWrapper extends Object
Wrapper for config files and default config files
  • Method Details

    • of

      @NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull String pathToConfigDir, @NotNull @NotNull String configFileName, @NotNull @NotNull File defaultConfig)
      Creates new ConfigWrapper from provided path to config(-s) directory, config file name without extension and default config file
      Parameters:
      configFileName - config file name without extension
      defaultConfig - default config file (you may want to put it in your mod's resources folder)
      pathToConfigDir - path to directory, that contains config(-s)
      Returns:
      new ConfigWrapper with loaded config values
    • of

      @NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull File configFile, @NotNull @NotNull File defaultConfigFile)
      Creates new ConfigWrapper from provided config file and provided default config file
      Parameters:
      configFile - config file to be wrapped
      defaultConfigFile - default config file to be wrapped
      Returns:
      new ConfigWrapper with loaded config values
    • of

      @NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull File configFile, @NotNull @NotNull Map<?,?> defaultConfigMap)
      Creates new ConfigWrapper from provided config file and default config map (default config values)
      Parameters:
      configFile - config file to be wrapped
      defaultConfigMap - default config map (default config values)
      Returns:
      new ConfigWrapper with loaded config values
    • of

      @NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull String pathToConfigDir, @NotNull @NotNull String configFileName, @NotNull @NotNull Map<?,?> defaultConfigMap)
      Creates new ConfigWrapper from provided path to config(-s) directory, config file name without extension and default config map (default config values)
      Parameters:
      pathToConfigDir - path to config(-s) directory
      configFileName - config file name without extension
      defaultConfigMap - default config map (default config values)
      Returns:
      new ConfigWrapper with loaded config values
    • getRaw

      @Deprecated @Nullable public @Nullable Object getRaw(@NotNull @NotNull String key)
      Deprecated.
      Quires a value from config, returns null if the key does not exist
      Parameters:
      key - key to get the value
      Returns:
      value of corresponding key
    • getRawOrDefault

      @Nullable public @Nullable Object getRawOrDefault(@NotNull @NotNull String key, Object def)
      Tries to get the value of corresponding key and if there is no such key in config, returns provided default value
      Parameters:
      key - key to get the value
      def - default value
      Returns:
      value of corresponding key or default value
      See Also:
    • getRawOrDefault

      @Nullable public @Nullable Object getRawOrDefault(@NotNull @NotNull String key)
      Returns raw value corresponding to the provided key if config contains such key, otherwise returns raw value from default config corresponding to the same key
      Parameters:
      key - key to get value
      Returns:
      raw value from config or raw value from default config corresponding to the same key
    • getRawFromDefault

      @Deprecated @Nullable public @Nullable Object getRawFromDefault(@NotNull @NotNull String key)
      Deprecated.
      Returns raw value from default config If default config doesn't contain provided key, method will return null
      Parameters:
      key - key to get value from default config
      Returns:
      raw value from default config or null
      See Also:
    • get

      public JSONValue get(@NotNull @NotNull String key)
      Returns wrapped value from the config corresponding to the provided key
      Parameters:
      key - key to get value
      Returns:
      wrapped value from the config corresponding to the provided key
    • getOrDefault

      public JSONValue getOrDefault(@NotNull @NotNull String key, Object def)
      Returns wrapped value from the config corresponding to the provided key if the config contains such key, otherwise returns provided default value
      Parameters:
      key - key to get value
      def - default value to return if key does not exist
      Returns:
      wrapped value from the config corresponding to the provided key or default value
      See Also:
    • getOrDefault

      public JSONValue getOrDefault(@NotNull @NotNull String key)
      Returns wrapped value from the config corresponding to the provided key if the config contains such key, otherwise returns value from default config corresponding to the same key
      Parameters:
      key - key to get value
      Returns:
      wrapped value from the config corresponding to the provided key or default value from default config corresponding to the same key
    • getFromDefault

      public JSONValue getFromDefault(@NotNull @NotNull String key)
      Returns wrapped value from default config corresponding to the provided key
      Parameters:
      key - key to get value
      Returns:
      wrapped value from default config
    • getAsJavaValue

      public <T> T getAsJavaValue(@NotNull @NotNull String key, T def)
      Shortcut for "get(String).toJavaValue(Object)"
      Type Parameters:
      T - type of required value
      Parameters:
      key - key to get value from config
      def - default value that will be returned if conversion would fail
      Returns:
      converted value corresponding to the provided key
      See Also:
    • getAsJavaValue

      public <T> T getAsJavaValue(@NotNull @NotNull String key, @NotNull @NotNull IJSONValueType<T> type)
      Shortcut for "get(String).toJavaValue(IJSONValueType)". If there is provided key in default config, it'll use convert function with default value, if not it'll use convert function with null as default value
      Type Parameters:
      T - type of required value
      Parameters:
      key - key to get value from config (and from default config if it has such key)
      type - converter
      Returns:
      value corresponding to the provided key or default value from default config or null
      See Also:
    • putInConfig

      public void putInConfig(@NotNull @NotNull String key, JSONValue value)
      Puts provided value to the config under the given key. After using this method, you should call saveConfig() function to save all changes to the config file
      Parameters:
      key - key which will represent provided value
      value - value to be stored
    • putAllInConfig

      public void putAllInConfig(@NotNull @NotNull Map<String,JSONValue> toPut)
      Puts all provided values to the config. After using this method, you should call saveConfig() to save all changes to the config file
      Parameters:
      toPut - values to put
    • saveConfig

      public boolean saveConfig()
      This method saves the config to the config file
      Returns:
      true if method can save changes to the config file, false otherwise
    • putInConfigAndSave

      public boolean putInConfigAndSave(@NotNull @NotNull String key, JSONValue value)
      This method puts provided value to the config under the given key and immediately saves changes to the config file
      Parameters:
      key - key which will represent provided value
      value - value to be stored
      Returns:
      true if method can save changes to the config file, false otherwise
      See Also:
    • putAllInConfigAndSave

      public boolean putAllInConfigAndSave(@NotNull @NotNull Map<String,JSONValue> toPut)
      Puts all provided values to the config and immediately saves changes to the config file
      Parameters:
      toPut - values to put
      Returns:
      true if method can save changes to the config file, false otherwise
      See Also:
    • copy

      @NotNull public @NotNull ConfigWrapper copy()
      Creates a copy of whole json config
      Returns:
      a copy of json config
    • config

      @NotNull public @NotNull Map<String,JSONValue> config()
      Creates a copy of the config and returns it
      Returns:
      a copy of the config
    • defaultConfig

      @NotNull public @NotNull Map<String,JSONValue> defaultConfig()
      Creates a copy of the defaultConfig and returns it
      Returns:
      a copy of the default config
    • isBroken

      public boolean isBroken()
      Check if the config is broken
      Returns:
      isBroken
    • delete

      public boolean delete()
      Deletes the config file from the config files directory
      Returns:
      value of File.delete() function