Fix Scala compiler freaking out on ListConfigData

- Moved the custom list's class to the top level
This commit is contained in:
Norbi Peti 2023-06-30 03:18:07 +02:00
parent ddd24a73f6
commit 05fe1c261c
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 159 additions and 150 deletions

View file

@ -169,6 +169,9 @@ object TBMCCoreAPI {
} }
} }
/**
* Returns true if the server is a test/dev/staging server
*/
@JvmStatic @JvmStatic
fun IsTestServer(): Boolean { fun IsTestServer(): Boolean {
return MainPlugin.instance.test.get() return MainPlugin.instance.test.get()

View file

@ -16,7 +16,7 @@ import java.util.function.Function
* **Note:** The instance can become outdated if the config is reloaded. * **Note:** The instance can become outdated if the config is reloaded.
* @param config The config object to use for the whole file * @param config The config object to use for the whole file
* @param path The path to the config value * @param path The path to the config value
* @param primitiveDef The default value, as stored in the config. Non-nullable as it needs to be saved sto the config * @param primitiveDef The default value, as stored in the config. Non-nullable as it needs to be saved to the config
* @param getter Function to convert primtive types to [T]. The parameter is of a primitive type as returned by [Configuration.get] * @param getter Function to convert primtive types to [T]. The parameter is of a primitive type as returned by [Configuration.get]
* @param setter Function to convert [T] to a primitive type. The result should be a primitive type or string that can be retrieved correctly later * @param setter Function to convert [T] to a primitive type. The result should be a primitive type or string that can be retrieved correctly later
* @param readOnly If true, changing the value will have no effect * @param readOnly If true, changing the value will have no effect

View file

@ -1,161 +1,24 @@
package buttondevteam.lib.architecture package buttondevteam.lib.architecture
import buttondevteam.lib.architecture.config.ConfigList
import buttondevteam.lib.architecture.config.IConfigData import buttondevteam.lib.architecture.config.IConfigData
import buttondevteam.lib.architecture.config.IListConfigData import buttondevteam.lib.architecture.config.IListConfigData
import java.util.function.Function import java.util.function.Function
import java.util.function.Predicate
import java.util.function.UnaryOperator
class ListConfigData<T> internal constructor( class ListConfigData<T> internal constructor(
config: IHaveConfig, config: IHaveConfig,
path: String, path: String,
primitiveDef: ArrayList<*>, primitiveDef: ArrayList<*>,
private val elementGetter: Function<Any?, T>, internal val elementGetter: Function<Any?, T>,
private val elementSetter: Function<T, Any?>, internal val elementSetter: Function<T, Any?>,
readOnly: Boolean readOnly: Boolean
) : IConfigData<ListConfigData<T>.List>, IListConfigData<T> { ) : IConfigData<ConfigList<T>>, IListConfigData<T> {
val listConfig: ConfigData<List> = val listConfig: ConfigData<ConfigList<T>> =
ConfigData(config, path, primitiveDef, { List((it as ArrayList<*>).toMutableList()) }, { it }, readOnly) ConfigData(config, path, primitiveDef, { ConfigList((it as ArrayList<*>).toMutableList(), this) }, { it }, readOnly)
override val path get() = listConfig.path override val path get() = listConfig.path
override fun get() = listConfig.get() override fun get() = listConfig.get()
override fun set(value: List) = listConfig.set(value) override fun set(value: ConfigList<T>) = listConfig.set(value)
override fun reload() = listConfig.reload() override fun reload() = listConfig.reload()
inner class List(backingList: MutableList<Any?>) : MutableList<T> {
private val primitiveList = backingList
override val size: Int get() = primitiveList.size
private fun update() {
val config = listConfig.config
ConfigData.signalChange(config) //Update the config model and start save task if needed
}
override fun set(index: Int, element: T): T {
val ret = primitiveList.set(index, elementSetter.apply(element))
update()
return elementGetter.apply(ret)
}
override fun add(element: T): Boolean {
val ret = primitiveList.add(elementSetter.apply(element))
update()
return ret
}
override fun add(index: Int, element: T) {
primitiveList.add(index, elementSetter.apply(element))
update()
}
override fun removeAt(index: Int): T {
val ret = primitiveList.removeAt(index)
update()
return elementGetter.apply(ret)
}
override fun subList(fromIndex: Int, toIndex: Int): MutableList<T> =
List(primitiveList.subList(fromIndex, toIndex))
override fun remove(element: T): Boolean {
val ret = primitiveList.remove(elementSetter.apply(element))
update()
return ret
}
override fun addAll(elements: Collection<T>): Boolean {
val ret = primitiveList.addAll(elements.map { elementSetter.apply(it) })
update()
return ret
}
override fun addAll(index: Int, elements: Collection<T>): Boolean {
val ret = primitiveList.addAll(index, elements.map { elementSetter.apply(it) })
update()
return ret
}
override fun removeAll(elements: Collection<T>): Boolean {
val ret = primitiveList.removeAll(elements.map { elementSetter.apply(it) })
update()
return ret
}
override fun retainAll(elements: Collection<T>): Boolean {
val ret = primitiveList.retainAll(elements.map { elementSetter.apply(it) })
update()
return ret
}
override fun removeIf(filter: Predicate<in T>): Boolean {
val ret = primitiveList.removeIf { filter.test(elementGetter.apply(it)) }
update()
return ret
}
override fun replaceAll(operator: UnaryOperator<T>) {
primitiveList.replaceAll { elementSetter.apply(operator.apply(elementGetter.apply(it))) }
update()
}
override fun sort(c: Comparator<in T>) {
primitiveList.sortWith { o1, o2 -> c.compare(elementGetter.apply(o1), elementGetter.apply(o2)) }
update()
}
override fun clear() {
primitiveList.clear()
update()
}
override fun get(index: Int): T = elementGetter.apply(primitiveList[index])
override fun isEmpty(): Boolean = primitiveList.isEmpty()
override fun lastIndexOf(element: T): Int = primitiveList.lastIndexOf(elementSetter.apply(element))
override fun indexOf(element: T): Int = primitiveList.indexOf(elementSetter.apply(element))
override fun containsAll(elements: Collection<T>): Boolean =
primitiveList.containsAll(elements.map { elementSetter.apply(it) })
override fun contains(element: T): Boolean = primitiveList.contains(elementSetter.apply(element))
override fun iterator(): MutableIterator<T> {
return object : MutableIterator<T> {
private val iterator = primitiveList.iterator()
override fun hasNext(): Boolean = iterator.hasNext()
override fun next(): T = elementGetter.apply(iterator.next())
override fun remove() {
iterator.remove()
update()
}
}
}
override fun listIterator(): MutableListIterator<T> {
return listIterator(0)
}
override fun listIterator(index: Int): MutableListIterator<T> {
return object : MutableListIterator<T> {
private val iterator = primitiveList.listIterator(index)
override fun hasNext(): Boolean = iterator.hasNext()
override fun next(): T = elementGetter.apply(iterator.next())
override fun remove() {
iterator.remove()
update()
}
override fun hasPrevious(): Boolean = iterator.hasPrevious()
override fun nextIndex(): Int = iterator.nextIndex()
override fun previous(): T = elementGetter.apply(iterator.previous())
override fun previousIndex(): Int = iterator.previousIndex()
override fun add(element: T) {
iterator.add(elementSetter.apply(element))
update()
}
override fun set(element: T) {
iterator.set(elementSetter.apply(element))
update()
}
}
}
}
} }

View file

@ -1,6 +1,5 @@
package buttondevteam.lib.architecture.config package buttondevteam.lib.architecture.config
import buttondevteam.lib.architecture.ListConfigData
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
open class ConfigDataDelegate<T>(private val data: IConfigData<T>) : IConfigData<T> by data { open class ConfigDataDelegate<T>(private val data: IConfigData<T>) : IConfigData<T> by data {
@ -8,7 +7,7 @@ open class ConfigDataDelegate<T>(private val data: IConfigData<T>) : IConfigData
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = data.set(value) operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = data.set(value)
} }
class ListConfigDataDelegate<T>(data: IConfigData<ListConfigData<T>.List>) : ConfigDataDelegate<ListConfigData<T>.List>(data), IListConfigData<T> class ListConfigDataDelegate<T>(data: IListConfigData<T>) : ConfigDataDelegate<ConfigList<T>>(data), IListConfigData<T>
fun <T> IConfigData<T>.delegate() = ConfigDataDelegate(this) fun <T> IConfigData<T>.delegate() = ConfigDataDelegate(this)
fun <T> IListConfigData<T>.delegate() = ListConfigDataDelegate(this) fun <T> IListConfigData<T>.delegate() = ListConfigDataDelegate(this)

View file

@ -0,0 +1,146 @@
package buttondevteam.lib.architecture.config
import buttondevteam.lib.architecture.ConfigData
import buttondevteam.lib.architecture.ListConfigData
import java.util.function.Predicate
import java.util.function.UnaryOperator
class ConfigList<T>(
backingList: MutableList<Any?>,
private val parentConfig: ListConfigData<T>
) : MutableList<T> {
private val primitiveList = backingList
override val size: Int get() = primitiveList.size
private fun update() {
val config = parentConfig.listConfig.config
ConfigData.signalChange(config) //Update the config model and start save task if needed
}
override fun set(index: Int, element: T): T {
val ret = primitiveList.set(index, parentConfig.elementSetter.apply(element))
update()
return parentConfig.elementGetter.apply(ret)
}
override fun add(element: T): Boolean {
val ret = primitiveList.add(parentConfig.elementSetter.apply(element))
update()
return ret
}
override fun add(index: Int, element: T) {
primitiveList.add(index, parentConfig.elementSetter.apply(element))
update()
}
override fun removeAt(index: Int): T {
val ret = primitiveList.removeAt(index)
update()
return parentConfig.elementGetter.apply(ret)
}
override fun subList(fromIndex: Int, toIndex: Int): MutableList<T> =
ConfigList(primitiveList.subList(fromIndex, toIndex), parentConfig)
override fun remove(element: T): Boolean {
val ret = primitiveList.remove(parentConfig.elementSetter.apply(element))
update()
return ret
}
override fun addAll(elements: Collection<T>): Boolean {
val ret = primitiveList.addAll(elements.map { parentConfig.elementSetter.apply(it) })
update()
return ret
}
override fun addAll(index: Int, elements: Collection<T>): Boolean {
val ret = primitiveList.addAll(index, elements.map { parentConfig.elementSetter.apply(it) })
update()
return ret
}
override fun removeAll(elements: Collection<T>): Boolean {
val ret = primitiveList.removeAll(elements.map { parentConfig.elementSetter.apply(it) })
update()
return ret
}
override fun retainAll(elements: Collection<T>): Boolean {
val ret = primitiveList.retainAll(elements.map { parentConfig.elementSetter.apply(it) })
update()
return ret
}
override fun removeIf(filter: Predicate<in T>): Boolean {
val ret = primitiveList.removeIf { filter.test(parentConfig.elementGetter.apply(it)) }
update()
return ret
}
override fun replaceAll(operator: UnaryOperator<T>) {
primitiveList.replaceAll { parentConfig.elementSetter.apply(operator.apply(parentConfig.elementGetter.apply(it))) }
update()
}
override fun sort(c: Comparator<in T>) {
primitiveList.sortWith { o1, o2 -> c.compare(parentConfig.elementGetter.apply(o1), parentConfig.elementGetter.apply(o2)) }
update()
}
override fun clear() {
primitiveList.clear()
update()
}
override fun get(index: Int): T = parentConfig.elementGetter.apply(primitiveList[index])
override fun isEmpty(): Boolean = primitiveList.isEmpty()
override fun lastIndexOf(element: T): Int = primitiveList.lastIndexOf(parentConfig.elementSetter.apply(element))
override fun indexOf(element: T): Int = primitiveList.indexOf(parentConfig.elementSetter.apply(element))
override fun containsAll(elements: Collection<T>): Boolean =
primitiveList.containsAll(elements.map { parentConfig.elementSetter.apply(it) })
override fun contains(element: T): Boolean = primitiveList.contains(parentConfig.elementSetter.apply(element))
override fun iterator(): MutableIterator<T> {
return object : MutableIterator<T> {
private val iterator = primitiveList.iterator()
override fun hasNext(): Boolean = iterator.hasNext()
override fun next(): T = parentConfig.elementGetter.apply(iterator.next())
override fun remove() {
iterator.remove()
update()
}
}
}
override fun listIterator(): MutableListIterator<T> {
return listIterator(0)
}
override fun listIterator(index: Int): MutableListIterator<T> {
return object : MutableListIterator<T> {
private val iterator = primitiveList.listIterator(index)
override fun hasNext(): Boolean = iterator.hasNext()
override fun next(): T = parentConfig.elementGetter.apply(iterator.next())
override fun remove() {
iterator.remove()
update()
}
override fun hasPrevious(): Boolean = iterator.hasPrevious()
override fun nextIndex(): Int = iterator.nextIndex()
override fun previous(): T = parentConfig.elementGetter.apply(iterator.previous())
override fun previousIndex(): Int = iterator.previousIndex()
override fun add(element: T) {
iterator.add(parentConfig.elementSetter.apply(element))
update()
}
override fun set(element: T) {
iterator.set(parentConfig.elementSetter.apply(element))
update()
}
}
}
}

View file

@ -1,7 +1,5 @@
package buttondevteam.lib.architecture.config package buttondevteam.lib.architecture.config
import buttondevteam.lib.architecture.ListConfigData
interface IConfigData<T> { interface IConfigData<T> {
/** /**
* Gets the value from the config using the getter specified for the config. If the config is not set, the default value is returned. * Gets the value from the config using the getter specified for the config. If the config is not set, the default value is returned.
@ -24,4 +22,4 @@ interface IConfigData<T> {
val path: String val path: String
} }
interface IListConfigData<T> : IConfigData<ListConfigData<T>.List> interface IListConfigData<T> : IConfigData<ConfigList<T>>