Package com.github.nedelis.jc4j
Class ConfigWrapper
java.lang.Object
com.github.nedelis.jc4j.ConfigWrapper
Wrapper for config files and default config files
-
Method Summary
Modifier and TypeMethodDescriptionconfig()Creates a copy of theconfigand returns it@NotNull ConfigWrappercopy()Creates a copy of whole json configCreates a copy of thedefaultConfigand returns itbooleandelete()Deletes the config file from the config files directoryReturns wrapped value from the config corresponding to the provided key<T> TgetAsJavaValue(@NotNull String key, @NotNull IJSONValueType<T> type) Shortcut for "get(String).toJavaValue(IJSONValueType)".<T> TgetAsJavaValue(@NotNull String key, T def) Shortcut for "get(String).toJavaValue(Object)"getFromDefault(@NotNull String key) Returns wrapped value from default config corresponding to the provided keygetOrDefault(@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 keygetOrDefault(@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@Nullable ObjectDeprecated.@Nullable ObjectgetRawFromDefault(@NotNull String key) Deprecated.@Nullable ObjectgetRawOrDefault(@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@Nullable ObjectgetRawOrDefault(@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 valuebooleanisBroken()Check if the config is brokenstatic @NotNull ConfigWrapperCreates newConfigWrapperfrom provided config file and provided default config filestatic @NotNull ConfigWrapperCreates newConfigWrapperfrom provided config file and default config map (default config values)static @NotNull ConfigWrapperCreates newConfigWrapperfrom provided path to config(-s) directory, config file name without extension and default config filestatic @NotNull ConfigWrapperof(@NotNull String pathToConfigDir, @NotNull String configFileName, @NotNull Map<?, ?> defaultConfigMap) Creates newConfigWrapperfrom provided path to config(-s) directory, config file name without extension and default config map (default config values)voidputAllInConfig(@NotNull Map<String, JSONValue> toPut) Puts all provided values to the config.booleanputAllInConfigAndSave(@NotNull Map<String, JSONValue> toPut) Puts all provided values to the config and immediately saves changes to the config filevoidputInConfig(@NotNull String key, JSONValue value) Puts provided value to the config under the given key.booleanputInConfigAndSave(@NotNull String key, JSONValue value) This method puts provided value to the config under the given key and immediately saves changes to the config filebooleanThis method saves theconfigto the config file
-
Method Details
-
of
@NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull String pathToConfigDir, @NotNull @NotNull String configFileName, @NotNull @NotNull File defaultConfig) Creates newConfigWrapperfrom provided path to config(-s) directory, config file name without extension and default config file- Parameters:
configFileName- config file name without extensiondefaultConfig- 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
ConfigWrapperwith loaded config values
-
of
@NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull File configFile, @NotNull @NotNull File defaultConfigFile) Creates newConfigWrapperfrom provided config file and provided default config file- Parameters:
configFile- config file to be wrappeddefaultConfigFile- default config file to be wrapped- Returns:
- new
ConfigWrapperwith loaded config values
-
of
@NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull File configFile, @NotNull @NotNull Map<?, ?> defaultConfigMap) Creates newConfigWrapperfrom provided config file and default config map (default config values)- Parameters:
configFile- config file to be wrappeddefaultConfigMap- default config map (default config values)- Returns:
- new
ConfigWrapperwith loaded config values
-
of
@NotNull public static @NotNull ConfigWrapper of(@NotNull @NotNull String pathToConfigDir, @NotNull @NotNull String configFileName, @NotNull @NotNull Map<?, ?> defaultConfigMap) Creates newConfigWrapperfrom provided path to config(-s) directory, config file name without extension and default config map (default config values)- Parameters:
pathToConfigDir- path to config(-s) directoryconfigFileName- config file name without extensiondefaultConfigMap- default config map (default config values)- Returns:
- new
ConfigWrapperwith loaded config values
-
getRaw
Deprecated.UsegetRawOrDefault(String, Object)insteadQuires 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
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 valuedef- default value- Returns:
- value of corresponding key or default value
- See Also:
-
getRawOrDefault
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.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
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
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 valuedef- 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
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
Returns wrapped value from default config corresponding to the provided key- Parameters:
key- key to get value- Returns:
- wrapped value from default config
-
getAsJavaValue
Shortcut for "get(String).toJavaValue(Object)"- Type Parameters:
T- type of required value- Parameters:
key- key to get value from configdef- default value that will be returned if conversion would fail- Returns:
- converted value corresponding to the provided key
- See Also:
-
getAsJavaValue
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
Puts provided value to the config under the given key. After using this method, you should callsaveConfig()function to save all changes to the config file- Parameters:
key- key which will represent provided valuevalue- value to be stored
-
putAllInConfig
Puts all provided values to the config. After using this method, you should callsaveConfig()to save all changes to the config file- Parameters:
toPut- values to put
-
saveConfig
public boolean saveConfig()This method saves theconfigto the config file- Returns:
- true if method can save changes to the config file, false otherwise
-
putInConfigAndSave
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 valuevalue- value to be stored- Returns:
- true if method can save changes to the config file, false otherwise
- See Also:
-
putAllInConfigAndSave
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
Creates a copy of whole json config- Returns:
- a copy of json config
-
config
Creates a copy of theconfigand returns it- Returns:
- a copy of the config
-
defaultConfig
Creates a copy of thedefaultConfigand 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
-
getRawOrDefault(String, Object)instead