diff --git a/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java b/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java index f3c8beec..7023427a 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java +++ b/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java @@ -1,11 +1,14 @@ package com.willfp.eco.core.config.interfaces; import com.willfp.eco.core.config.ConfigType; +import com.willfp.eco.core.config.TransientConfig; import com.willfp.eco.util.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * All configs implement this interface. @@ -67,10 +70,12 @@ public interface Config extends Cloneable { * Get subsection from config. * * @param path The key to check. - * @return The subsection. Throws NPE if not found. + * @return The subsection. Returns an empty section if not found. */ @NotNull - Config getSubsection(@NotNull String path); + default Config getSubsection(@NotNull String path) { + return Objects.requireNonNullElse(getSubsectionOrNull(path), new TransientConfig()); + } /** * Get subsection from config. @@ -87,7 +92,21 @@ public interface Config extends Cloneable { * @param path The key to fetch the value from. * @return The found value, or 0 if not found. */ - int getInt(@NotNull String path); + default int getInt(@NotNull String path) { + return Objects.requireNonNullElse(getIntOrNull(path), 0); + } + + /** + * Get an integer from config with a specified default (not found) value. + * + * @param path The key to fetch the value from. + * @param def The value to default to if not found. + * @return The found value, or the default. + */ + default int getInt(@NotNull String path, + int def) { + return Objects.requireNonNullElse(getIntOrNull(path), def); + } /** * Get an integer from config. @@ -98,16 +117,6 @@ public interface Config extends Cloneable { @Nullable Integer getIntOrNull(@NotNull String path); - /** - * Get an integer from config with a specified default (not found) value. - * - * @param path The key to fetch the value from. - * @param def The value to default to if not found. - * @return The found value, or the default. - */ - int getInt(@NotNull String path, - int def); - /** * Get a list of integers from config. * @@ -115,7 +124,9 @@ public interface Config extends Cloneable { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull - List getInts(@NotNull String path); + default List getInts(@NotNull String path) { + return Objects.requireNonNullElse(getIntsOrNull(path), new ArrayList<>()); + } /** * Get a list of integers from config. @@ -132,7 +143,9 @@ public interface Config extends Cloneable { * @param path The key to fetch the value from. * @return The found value, or false if not found. */ - boolean getBool(@NotNull String path); + default boolean getBool(@NotNull String path) { + return Objects.requireNonNullElse(getBoolOrNull(path), false); + } /** * Get a boolean from config. @@ -150,7 +163,9 @@ public interface Config extends Cloneable { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull - List getBools(@NotNull String path); + default List getBools(@NotNull String path) { + return Objects.requireNonNullElse(getBoolsOrNull(path), new ArrayList<>()); + } /** * Get a list of booleans from config. @@ -236,9 +251,11 @@ public interface Config extends Cloneable { * @return The found value, or an empty string if not found. */ @NotNull - String getString(@NotNull String path, - boolean format, - @NotNull StringUtils.FormatOption option); + default String getString(@NotNull String path, + boolean format, + @NotNull StringUtils.FormatOption option) { + return Objects.requireNonNullElse(getStringOrNull(path, format, option), ""); + } /** * Get a formatted string from config. @@ -400,9 +417,11 @@ public interface Config extends Cloneable { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull - List getStrings(@NotNull String path, - boolean format, - @NotNull StringUtils.FormatOption option); + default List getStrings(@NotNull String path, + boolean format, + @NotNull StringUtils.FormatOption option) { + return Objects.requireNonNullElse(getStringsOrNull(path, format, option), new ArrayList<>()); + } /** * Get a list of strings from config. @@ -496,7 +515,9 @@ public interface Config extends Cloneable { * @param path The key to fetch the value from. * @return The found value, or 0 if not found. */ - double getDouble(@NotNull String path); + default double getDouble(@NotNull String path) { + return Objects.requireNonNullElse(getDoubleOrNull(path), 0.0); + } /** * Get a decimal from config. @@ -514,7 +535,9 @@ public interface Config extends Cloneable { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull - List getDoubles(@NotNull String path); + default List getDoubles(@NotNull String path) { + return Objects.requireNonNullElse(getDoublesOrNull(path), new ArrayList<>()); + } /** * Get a list of decimals from config. @@ -532,7 +555,9 @@ public interface Config extends Cloneable { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull - List getSubsections(@NotNull String path); + default List getSubsections(@NotNull String path) { + return Objects.requireNonNullElse(getSubsectionsOrNull(path), new ArrayList<>()); + } /** * Get a list of subsections from config. diff --git a/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/JSONConfig.java b/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/JSONConfig.java index 8a898c65..b348c7c1 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/JSONConfig.java +++ b/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/JSONConfig.java @@ -3,7 +3,9 @@ package com.willfp.eco.core.config.interfaces; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * JSON config. @@ -22,7 +24,9 @@ public interface JSONConfig extends Config { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull - List getSubsections(@NotNull String path); + default List getSubsections(@NotNull String path) { + return Objects.requireNonNullElse(getSubsectionsOrNull(path), new ArrayList<>()); + } /** * Get a list of subsections from config. diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt index 39d26d59..f7b571f5 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt @@ -7,7 +7,6 @@ import com.google.gson.GsonBuilder import com.willfp.eco.core.config.ConfigType import com.willfp.eco.core.config.interfaces.JSONConfig import com.willfp.eco.util.StringUtils -import org.bukkit.configuration.file.YamlConfiguration import java.util.Objects import java.util.concurrent.ConcurrentHashMap @@ -131,24 +130,18 @@ open class EcoJSONConfigWrapper : JSONConfig { } override fun getSubsection(path: String): JSONConfig { - val subsection = getSubsectionOrNull(path) - return subsection ?: EcoJSONConfigSection(emptyMap()) + return getSubsectionOrNull(path) ?: EcoJSONConfigSection(mutableMapOf()) } override fun getSubsectionOrNull(path: String): JSONConfig? { return if (values.containsKey(path)) { - val subsection = values[path] as Map? - EcoJSONConfigSection(subsection!!) + val subsection = values[path] as Map + EcoJSONConfigSection(subsection) } else { null } } - override fun getSubsections(path: String): List { - val subsections = getSubsectionsOrNull(path) - return subsections ?: mutableListOf() - } - override fun getSubsectionsOrNull(path: String): List? { val maps = getOfKnownType(path, Any::class.java) as List>? ?: return null @@ -159,74 +152,20 @@ open class EcoJSONConfigWrapper : JSONConfig { return configs.toMutableList() } - override fun getInt(path: String): Int { - return (getOfKnownType(path, Double::class.java) ?: 0.0).toInt() - } - override fun getIntOrNull(path: String): Int? { - return if (has(path)) { - getInt(path) - } else { - null - } - } - - override fun getInt( - path: String, - def: Int - ): Int { - return Objects.requireNonNullElse(getOfKnownType(path, Int::class.java), def) - } - - override fun getInts(path: String): MutableList { - return (Objects.requireNonNullElse( - getOfKnownType(path, Any::class.java), - emptyList() - ) as List).toMutableList() + return getOfKnownType(path, Double::class.java)?.toInt() } override fun getIntsOrNull(path: String): MutableList? { - return if (has(path)) { - getInts(path) - } else { - null - } - } - - override fun getBool(path: String): Boolean { - return Objects.requireNonNullElse(getOfKnownType(path, Boolean::class.java), false) + return (getOfKnownType(path, Any::class.java) as Collection?)?.toMutableList() } override fun getBoolOrNull(path: String): Boolean? { - return if (has(path)) { - getBool(path) - } else { - null - } - } - - override fun getBools(path: String): MutableList { - return (Objects.requireNonNullElse( - getOfKnownType(path, Any::class.java), - emptyList() - ) as List).toMutableList() + return getOfKnownType(path, Boolean::class.java) } override fun getBoolsOrNull(path: String): MutableList? { - return if (has(path)) { - getBools(path) - } else { - null - } - } - - override fun getString( - path: String, - format: Boolean, - option: StringUtils.FormatOption - ): String { - val string = getOfKnownType(path, String::class.java) ?: "" - return if (format) StringUtils.format(string, option) else string + return (getOfKnownType(path, Any::class.java) as Collection?)?.toMutableList() } override fun getStringOrNull( @@ -235,62 +174,36 @@ open class EcoJSONConfigWrapper : JSONConfig { option: StringUtils.FormatOption ): String? { return if (has(path)) { - getString(path, format, option) + val string = getOfKnownType(path, String::class.java) ?: "" + return if (format) StringUtils.format(string, option) else string } else { null } } - override fun getStrings( - path: String, - format: Boolean, - option: StringUtils.FormatOption - ): MutableList { - val strings = - (Objects.requireNonNullElse( - getOfKnownType(path, Any::class.java), - emptyList() - ) as List).toMutableList() - return if (format) StringUtils.formatList(strings, option) else strings - } - override fun getStringsOrNull( path: String, format: Boolean, option: StringUtils.FormatOption ): MutableList? { return if (has(path)) { - getStrings(path, format, option) + val strings = + (Objects.requireNonNullElse( + getOfKnownType(path, Any::class.java), + emptyList() + ) as List).toMutableList() + return if (format) StringUtils.formatList(strings, option) else strings } else { null } } - override fun getDouble(path: String): Double { - return Objects.requireNonNullElse(getOfKnownType(path, Double::class.java), 0.0) - } - override fun getDoubleOrNull(path: String): Double? { - return if (has(path)) { - getDouble(path) - } else { - null - } - } - - override fun getDoubles(path: String): MutableList { - return (Objects.requireNonNullElse( - getOfKnownType(path, Any::class.java), - emptyList() - ) as List).toMutableList() + return getOfKnownType(path, Double::class.java) } override fun getDoublesOrNull(path: String): MutableList? { - return if (has(path)) { - getDoubles(path) - } else { - null - } + return (getOfKnownType(path, Any::class.java) as Collection?)?.toMutableList() } override fun getType(): ConfigType { diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt index 6a04058b..efd610f7 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt @@ -49,11 +49,6 @@ open class EcoYamlConfigWrapper : Config { handle[path] = obj } - override fun getSubsection(path: String): Config { - val subsection = getSubsectionOrNull(path) - return subsection ?: EcoYamlConfigSection(YamlConfiguration()) - } - override fun getSubsectionOrNull(path: String): Config? { return if (cache.containsKey(path)) { cache[path] as Config? @@ -68,109 +63,55 @@ open class EcoYamlConfigWrapper : Config { } } - override fun getInt(path: String): Int { - return if (cache.containsKey(path)) { - (cache[path] as Number).toInt() - } else { - cache[path] = handle.getDouble(path, 0.0).toInt() - getInt(path) - } - } - override fun getIntOrNull(path: String): Int? { - return if (has(path)) { - getInt(path) - } else { - null - } - } - - override fun getInt( - path: String, - def: Int - ): Int { return if (cache.containsKey(path)) { (cache[path] as Number).toInt() } else { - cache[path] = handle.getDouble(path, def.toDouble()).toInt() - getInt(path) - } - } - - override fun getInts(path: String): MutableList { - return if (cache.containsKey(path)) { - (cache[path] as MutableList).toMutableList() - } else { - cache[path] = if (has(path)) ArrayList(handle.getIntegerList(path)) else mutableListOf() - getInts(path) + if (has(path)) { + cache[path] = handle.getDouble(path).toInt() + } else { + return null + } + getIntOrNull(path) } } override fun getIntsOrNull(path: String): MutableList? { - return if (has(path)) { - getInts(path) - } else { - null - } - } - - override fun getBool(path: String): Boolean { return if (cache.containsKey(path)) { - cache[path] as Boolean + (cache[path] as Collection).toMutableList() } else { - cache[path] = handle.getBoolean(path) - getBool(path) + if (has(path)) { + cache[path] = handle.getIntegerList(path).toMutableList() + } else { + return null + } + getIntsOrNull(path) } } override fun getBoolOrNull(path: String): Boolean? { - return if (has(path)) { - getBool(path) - } else { - null - } - } - - override fun getBools(path: String): MutableList { return if (cache.containsKey(path)) { - (cache[path] as MutableList).toMutableList() + cache[path] as Boolean } else { - cache[path] = - if (has(path)) ArrayList(handle.getBooleanList(path)) else mutableListOf() - getBools(path) + if (has(path)) { + cache[path] = handle.getBoolean(path) + } else { + return null + } + getBoolOrNull(path) } } override fun getBoolsOrNull(path: String): MutableList? { - return if (has(path)) { - getBools(path) + return if (cache.containsKey(path)) { + (cache[path] as Collection).toMutableList() } else { - null - } - } - - override fun getString( - path: String, - format: Boolean, - option: StringUtils.FormatOption - ): String { - if (format && option == StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) { - return if (cache.containsKey("$path\$FMT")) { - cache["$path\$FMT"] as String + if (has(path)) { + cache[path] = handle.getBooleanList(path).toMutableList() } else { - val string: String = handle.getString(path, "")!! - cache["$path\$FMT"] = StringUtils.format(string, option) - getString(path, format, option) + return null } - } else { - val string = if (cache.containsKey(path)) { - cache[path] as String - } else { - cache[path] = handle.getString(path, "")!! - getString(path, format, option) - } - - return if (format) StringUtils.format(string) else string + getBoolsOrNull(path) } } @@ -180,108 +121,105 @@ open class EcoYamlConfigWrapper : Config { option: StringUtils.FormatOption ): String? { return if (has(path)) { - getString(path, format, option) + if (format && option == StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) { + return if (cache.containsKey("$path\$FMT")) { + cache["$path\$FMT"] as String + } else { + val string: String = handle.getString(path, "")!! + cache["$path\$FMT"] = StringUtils.format(string, option) + getString(path, format, option) + } + } else { + val string = if (cache.containsKey(path)) { + cache[path] as String + } else { + cache[path] = handle.getString(path, "")!! + getString(path, format, option) + } + + return if (format) StringUtils.format(string) else string + } } else { null } } - override fun getStrings( - path: String, - format: Boolean, - option: StringUtils.FormatOption - ): MutableList { - if (format && option == StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) { - return if (cache.containsKey("$path\$FMT")) { - (cache["$path\$FMT"] as MutableList).toMutableList() - } else { - val list = if (has(path)) handle.getStringList(path) else mutableListOf() - cache["$path\$FMT"] = StringUtils.formatList(list, option) - getStrings(path, true, option) - } - } else { - val strings = if (cache.containsKey(path)) { - (cache[path] as MutableList).toMutableList() - } else { - cache[path] = - if (has(path)) ArrayList(handle.getStringList(path)) else mutableListOf() - getStrings(path, false, option) - } - - return if (format) { - StringUtils.formatList(strings, StringUtils.FormatOption.WITH_PLACEHOLDERS) - } else { - strings - } - } - } - override fun getStringsOrNull( path: String, format: Boolean, option: StringUtils.FormatOption ): MutableList? { return if (has(path)) { - getStrings(path, format, option) + if (format && option == StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) { + return if (cache.containsKey("$path\$FMT")) { + (cache["$path\$FMT"] as MutableList).toMutableList() + } else { + val list = if (has(path)) handle.getStringList(path) else mutableListOf() + cache["$path\$FMT"] = StringUtils.formatList(list, option) + getStrings(path, true, option) + } + } else { + val strings = if (cache.containsKey(path)) { + (cache[path] as MutableList).toMutableList() + } else { + cache[path] = + if (has(path)) ArrayList(handle.getStringList(path)) else mutableListOf() + getStrings(path, false, option) + } + + return if (format) { + StringUtils.formatList(strings, StringUtils.FormatOption.WITH_PLACEHOLDERS) + } else { + strings + } + } } else { null } } - override fun getDouble(path: String): Double { - return if (cache.containsKey(path)) { - cache[path] as Double - } else { - cache[path] = handle.getDouble(path) - getDouble(path) - } - } - override fun getDoubleOrNull(path: String): Double? { - return if (has(path)) { - getDouble(path) - } else { - null - } - } - - override fun getDoubles(path: String): MutableList { return if (cache.containsKey(path)) { - (cache[path] as MutableList).toMutableList() + (cache[path] as Number).toDouble() } else { - cache[path] = if (has(path)) ArrayList(handle.getDoubleList(path)) else emptyList() - getDoubles(path) + if (has(path)) { + cache[path] = handle.getDouble(path) + } else { + return null + } + getDoubleOrNull(path) } } override fun getDoublesOrNull(path: String): MutableList? { - return if (has(path)) { - getDoubles(path) - } else { - null - } - } - - override fun getSubsections(path: String): MutableList { return if (cache.containsKey(path)) { - (cache[path] as Collection).toMutableList() + (cache[path] as Collection).toMutableList() } else { - val mapList = ArrayList(handle.getMapList(path)) as List> - val configList = mutableListOf() - for (map in mapList) { - val temp = YamlConfiguration.loadConfiguration(StringReader("")) - temp.createSection("a", map) - configList.add(EcoYamlConfigSection(temp.getConfigurationSection("a")!!)) + if (has(path)) { + cache[path] = handle.getDoubleList(path).toMutableList() + } else { + return null } - - cache[path] = if (has(path)) configList else emptyList() - getSubsections(path) + getDoublesOrNull(path) } } override fun getSubsectionsOrNull(path: String): MutableList? { return if (has(path)) { - getSubsections(path) + return if (cache.containsKey(path)) { + (cache[path] as Collection).toMutableList() + } else { + val mapList = ArrayList(handle.getMapList(path)) as List> + val configList = mutableListOf() + for (map in mapList) { + val temp = YamlConfiguration.loadConfiguration(StringReader("")) + temp.createSection("a", map) + configList.add(EcoYamlConfigSection(temp.getConfigurationSection("a")!!)) + } + + cache[path] = if (has(path)) configList else emptyList() + getSubsections(path) + } } else { null }