From 05fe1c261cf59287b1d43a7781b1d10c605fb3f5 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 30 Jun 2023 03:18:07 +0200 Subject: [PATCH] Fix Scala compiler freaking out on ListConfigData - Moved the custom list's class to the top level --- .../java/buttondevteam/lib/TBMCCoreAPI.kt | 3 + .../lib/architecture/ConfigData.kt | 2 +- .../lib/architecture/ListConfigData.kt | 151 +----------------- .../architecture/config/ConfigDataDelegate.kt | 3 +- .../lib/architecture/config/ConfigList.kt | 146 +++++++++++++++++ .../lib/architecture/config/IConfigData.kt | 4 +- 6 files changed, 159 insertions(+), 150 deletions(-) create mode 100644 Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigList.kt diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.kt b/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.kt index b45d5ff..169c47b 100755 --- a/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.kt +++ b/Chroma-Core/src/main/java/buttondevteam/lib/TBMCCoreAPI.kt @@ -169,6 +169,9 @@ object TBMCCoreAPI { } } + /** + * Returns true if the server is a test/dev/staging server + */ @JvmStatic fun IsTestServer(): Boolean { return MainPlugin.instance.test.get() diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.kt b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.kt index 486af82..8f8034c 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.kt +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ConfigData.kt @@ -16,7 +16,7 @@ import java.util.function.Function * **Note:** The instance can become outdated if the config is reloaded. * @param config The config object to use for the whole file * @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 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 diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ListConfigData.kt b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ListConfigData.kt index 42e3453..91d4d21 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ListConfigData.kt +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/ListConfigData.kt @@ -1,161 +1,24 @@ package buttondevteam.lib.architecture +import buttondevteam.lib.architecture.config.ConfigList import buttondevteam.lib.architecture.config.IConfigData import buttondevteam.lib.architecture.config.IListConfigData import java.util.function.Function -import java.util.function.Predicate -import java.util.function.UnaryOperator class ListConfigData internal constructor( config: IHaveConfig, path: String, primitiveDef: ArrayList<*>, - private val elementGetter: Function, - private val elementSetter: Function, + internal val elementGetter: Function, + internal val elementSetter: Function, readOnly: Boolean -) : IConfigData.List>, IListConfigData { - val listConfig: ConfigData = - ConfigData(config, path, primitiveDef, { List((it as ArrayList<*>).toMutableList()) }, { it }, readOnly) +) : IConfigData>, IListConfigData { + val listConfig: ConfigData> = + ConfigData(config, path, primitiveDef, { ConfigList((it as ArrayList<*>).toMutableList(), this) }, { it }, readOnly) override val path get() = listConfig.path override fun get() = listConfig.get() - override fun set(value: List) = listConfig.set(value) + override fun set(value: ConfigList) = listConfig.set(value) override fun reload() = listConfig.reload() - inner class List(backingList: MutableList) : MutableList { - 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 = - 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): Boolean { - val ret = primitiveList.addAll(elements.map { elementSetter.apply(it) }) - update() - return ret - } - - override fun addAll(index: Int, elements: Collection): Boolean { - val ret = primitiveList.addAll(index, elements.map { elementSetter.apply(it) }) - update() - return ret - } - - override fun removeAll(elements: Collection): Boolean { - val ret = primitiveList.removeAll(elements.map { elementSetter.apply(it) }) - update() - return ret - } - - override fun retainAll(elements: Collection): Boolean { - val ret = primitiveList.retainAll(elements.map { elementSetter.apply(it) }) - update() - return ret - } - - override fun removeIf(filter: Predicate): Boolean { - val ret = primitiveList.removeIf { filter.test(elementGetter.apply(it)) } - update() - return ret - } - - override fun replaceAll(operator: UnaryOperator) { - primitiveList.replaceAll { elementSetter.apply(operator.apply(elementGetter.apply(it))) } - update() - } - - override fun sort(c: Comparator) { - 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): Boolean = - primitiveList.containsAll(elements.map { elementSetter.apply(it) }) - - override fun contains(element: T): Boolean = primitiveList.contains(elementSetter.apply(element)) - override fun iterator(): MutableIterator { - return object : MutableIterator { - 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 { - return listIterator(0) - } - - override fun listIterator(index: Int): MutableListIterator { - return object : MutableListIterator { - 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() - } - } - } - } } diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigDataDelegate.kt b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigDataDelegate.kt index 79fc36c..87095d2 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigDataDelegate.kt +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigDataDelegate.kt @@ -1,6 +1,5 @@ package buttondevteam.lib.architecture.config -import buttondevteam.lib.architecture.ListConfigData import kotlin.reflect.KProperty open class ConfigDataDelegate(private val data: IConfigData) : IConfigData by data { @@ -8,7 +7,7 @@ open class ConfigDataDelegate(private val data: IConfigData) : IConfigData operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) = data.set(value) } -class ListConfigDataDelegate(data: IConfigData.List>) : ConfigDataDelegate.List>(data), IListConfigData +class ListConfigDataDelegate(data: IListConfigData) : ConfigDataDelegate>(data), IListConfigData fun IConfigData.delegate() = ConfigDataDelegate(this) fun IListConfigData.delegate() = ListConfigDataDelegate(this) diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigList.kt b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigList.kt new file mode 100644 index 0000000..8cdabda --- /dev/null +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/ConfigList.kt @@ -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( + backingList: MutableList, + private val parentConfig: ListConfigData +) : MutableList { + 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 = + 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): Boolean { + val ret = primitiveList.addAll(elements.map { parentConfig.elementSetter.apply(it) }) + update() + return ret + } + + override fun addAll(index: Int, elements: Collection): Boolean { + val ret = primitiveList.addAll(index, elements.map { parentConfig.elementSetter.apply(it) }) + update() + return ret + } + + override fun removeAll(elements: Collection): Boolean { + val ret = primitiveList.removeAll(elements.map { parentConfig.elementSetter.apply(it) }) + update() + return ret + } + + override fun retainAll(elements: Collection): Boolean { + val ret = primitiveList.retainAll(elements.map { parentConfig.elementSetter.apply(it) }) + update() + return ret + } + + override fun removeIf(filter: Predicate): Boolean { + val ret = primitiveList.removeIf { filter.test(parentConfig.elementGetter.apply(it)) } + update() + return ret + } + + override fun replaceAll(operator: UnaryOperator) { + primitiveList.replaceAll { parentConfig.elementSetter.apply(operator.apply(parentConfig.elementGetter.apply(it))) } + update() + } + + override fun sort(c: Comparator) { + 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): 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 { + return object : MutableIterator { + 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 { + return listIterator(0) + } + + override fun listIterator(index: Int): MutableListIterator { + return object : MutableListIterator { + 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() + } + } + } +} \ No newline at end of file diff --git a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/IConfigData.kt b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/IConfigData.kt index 9032138..3d57d04 100644 --- a/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/IConfigData.kt +++ b/Chroma-Core/src/main/java/buttondevteam/lib/architecture/config/IConfigData.kt @@ -1,7 +1,5 @@ package buttondevteam.lib.architecture.config -import buttondevteam.lib.architecture.ListConfigData - interface IConfigData { /** * 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 { val path: String } -interface IListConfigData : IConfigData.List> +interface IListConfigData : IConfigData>