Start converting more config stuff and add TODOs

This commit is contained in:
Norbi Peti 2023-02-23 01:38:36 +01:00
parent 482df40992
commit ca00422ce8
2 changed files with 16 additions and 46 deletions

View file

@ -2,7 +2,6 @@ package buttondevteam.lib.architecture
import buttondevteam.core.MainPlugin
import buttondevteam.lib.ChromaUtils
import buttondevteam.lib.architecture.IHaveConfig.getConfig
import lombok.*
import org.bukkit.Bukkit
import org.bukkit.configuration.Configuration
@ -13,35 +12,19 @@ import java.util.function.Function
/**
* Use the getter/setter constructor if [T] isn't a primitive type or String.<br></br>
* Use [Component.getConfig] or [ButtonPlugin.getIConfig] then [IHaveConfig.getData] to get an instance.
* @param config May be null for testing
* @param getter The parameter is of a primitive type as returned by [Configuration.get]
* @param setter The result should be a primitive type or string that can be retrieved correctly later
*/
open class ConfigData<T> internal constructor(
config: IHaveConfig?,
path: String?,
def: T,
private val config: IHaveConfig?,
val path: String,
def: T?,
primitiveDef: Any?,
getter: Function<Any?, T>?,
setter: Function<T, Any?>?
) {
/**
* May be null for testing
*/
private val config: IHaveConfig?
@Getter
@Setter(AccessLevel.PACKAGE)
private val path: String?
protected val def: T?
private val primitiveDef: Any?
/**
* The parameter is of a primitive type as returned by [YamlConfiguration.get]
*/
private val getter: Function<Any?, T>?
/**
* The result should be a primitive type or string that can be retrieved correctly later
*/
private val getter: Function<Any?, T>?,
private val setter: Function<T, Any?>?
) {
private val def: Any?
/**
* The config value should not change outside this instance
@ -49,20 +32,9 @@ open class ConfigData<T> internal constructor(
private var value: T? = null
init {
var def: T? = def
var primitiveDef = primitiveDef
if (def == null) {
requireNotNull(primitiveDef) { "Either def or primitiveDef must be set." }
requireNotNull(getter) { "A getter and setter must be present when using primitiveDef." }
def = getter.apply(primitiveDef)
} else if (primitiveDef == null) primitiveDef = if (setter == null) def else setter.apply(def)
this.def = primitiveDef ?: def?.let { setter?.apply(it) }
?: throw IllegalArgumentException("Either def or primitiveDef must be set. A getter and setter must be present when using primitiveDef.")
require(getter == null == (setter == null)) { "Both setters and getters must be present (or none if def is primitive)." }
this.config = config
this.path = path
this.def = def
this.primitiveDef = primitiveDef
this.getter = getter
this.setter = setter
get() //Generate config automatically
}
@ -76,10 +48,10 @@ open class ConfigData<T> internal constructor(
fun get(): T? {
if (value != null) return value //Speed things up
val config = config!!.getConfig<Any>()
val config = config?.config
var `val`: Any?
if (config == null || !config.isSet(path)) { //Call set() if config == null
`val` = primitiveDef
`val` = primitiveDef // TODO: primitiveDef --> def, def --> getter(primitiveDef)
if ((def == null || this is ReadOnlyConfigData<*>) && config != null) //In Discord's case def may be null
setInternal(primitiveDef) //If read-only then we still need to save the default value so it can be set
else set(def) //Save default value - def is always set

View file

@ -3,7 +3,6 @@ package buttondevteam.lib.architecture
import buttondevteam.core.MainPlugin
import buttondevteam.lib.TBMCCoreAPI
import buttondevteam.lib.architecture.ConfigData.ConfigDataBuilder
import lombok.Getter
import org.bukkit.Bukkit
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.plugin.java.JavaPlugin
@ -26,8 +25,7 @@ class IHaveConfig(var saveAction: Runnable?) { // TODO: Make non-nullable after
/**
* Returns the Bukkit ConfigurationSection. Use [.signalChange] after changing it.
*/
@Getter
private var config: ConfigurationSection? = null
var config: ConfigurationSection? = null // TODO: Make non-nullable after removing reset() method
/**
* Gets a config object for the given path. The def or primitiveDef must be set. If a getter is present, a setter must be present as well.
@ -190,9 +188,9 @@ class IHaveConfig(var saveAction: Runnable?) { // TODO: Make non-nullable after
/**
* Clears all caches and loads everything from yaml.
*/
fun reset(config: ConfigurationSection?) {
fun reset(config: ConfigurationSection?) { // TODO: Simply replace the object
this.config = config
datamap.forEach { (path: String?, data: ConfigData<*>) -> data.reset() }
datamap.forEach { (_, data) -> data.reset() }
}
companion object {