outsource delegate stuff into kutil, bump dependencies

This commit is contained in:
Bixilon 2022-01-04 16:06:14 +01:00
parent a4d5b9502d
commit 737e4166b9
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
33 changed files with 125 additions and 525 deletions

View File

@ -447,7 +447,7 @@
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>1.5.0-5</version>
<version>1.5.1-1</version>
</dependency>
<dependency>
<groupId>com.github.afester.javafx</groupId>
@ -457,7 +457,7 @@
<dependency>
<groupId>de.bixilon</groupId>
<artifactId>kutil</artifactId>
<version>1.3</version>
<version>1.4.1</version>
</dependency>
</dependencies>
</project>

View File

@ -1,10 +1,10 @@
package de.bixilon.minosoft.config.profile.delegate.delegate
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.profile.ProfileManager
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.config.profile.profiles.Profile
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.delegate.delegate.DelegateSetter
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels

View File

@ -1,9 +1,9 @@
package de.bixilon.minosoft.config.profile.delegate.delegate.entry
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.profile.ProfileManager
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType

View File

@ -1,9 +1,9 @@
package de.bixilon.minosoft.config.profile.delegate.delegate.entry
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.profile.ProfileManager
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType

View File

@ -1,9 +1,9 @@
package de.bixilon.minosoft.config.profile.delegate.delegate.entry
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.config.profile.ProfileManager
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType

View File

@ -1,11 +1,11 @@
package de.bixilon.minosoft.config.profile.delegate.watcher
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.config.profile.profiles.Profile
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.gui.rendering.Rendering
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0

View File

@ -1,11 +1,11 @@
package de.bixilon.minosoft.config.profile.delegate.watcher.entry
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.config.profile.delegate.watcher.ProfileDelegateWatcher
import de.bixilon.minosoft.config.profile.profiles.Profile
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import javafx.collections.ListChangeListener
import kotlin.reflect.KProperty

View File

@ -1,11 +1,11 @@
package de.bixilon.minosoft.config.profile.delegate.watcher.entry
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.config.profile.delegate.watcher.ProfileDelegateWatcher
import de.bixilon.minosoft.config.profile.profiles.Profile
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import javafx.collections.MapChangeListener
import kotlin.reflect.KProperty

View File

@ -1,11 +1,11 @@
package de.bixilon.minosoft.config.profile.delegate.watcher.entry
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.watcher.WatchUtil.identifier
import de.bixilon.minosoft.config.profile.delegate.ProfilesDelegateManager
import de.bixilon.minosoft.config.profile.delegate.watcher.ProfileDelegateWatcher
import de.bixilon.minosoft.config.profile.profiles.Profile
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import javafx.collections.SetChangeListener
import kotlin.reflect.KProperty

View File

@ -17,6 +17,7 @@ import de.bixilon.kutil.collections.CollectionUtil.synchronizedListOf
import de.bixilon.kutil.collections.CollectionUtil.synchronizedMapOf
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedList
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedMap
import de.bixilon.kutil.json.JsonUtil.toMutableJsonObject
import de.bixilon.kutil.primitive.IntUtil.toInt
import de.bixilon.minosoft.data.Rarities
import de.bixilon.minosoft.data.inventory.ItemNBTValues.DISPLAY_COLOR_TAG
@ -44,7 +45,6 @@ import de.bixilon.minosoft.util.BitByte.isBit
import de.bixilon.minosoft.util.KUtil.synchronizedDeepCopy
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.getAndRemove
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.listCast
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.toMutableJsonObject
import java.util.*
class ItemStack(

View File

@ -16,6 +16,7 @@ import de.bixilon.kutil.collections.CollectionUtil.lockMapOf
import de.bixilon.kutil.collections.CollectionUtil.toSynchronizedMap
import de.bixilon.kutil.collections.map.LockMap
import de.bixilon.kutil.concurrent.lock.ReadWriteLock
import de.bixilon.kutil.watcher.DataWatcher.Companion.watched
import de.bixilon.minosoft.data.Difficulties
import de.bixilon.minosoft.data.entities.block.BlockEntity
import de.bixilon.minosoft.data.registries.AABB
@ -45,7 +46,6 @@ import de.bixilon.minosoft.util.chunk.ChunkUtil.canBuildBiomeCache
import de.bixilon.minosoft.util.chunk.ChunkUtil.getChunkNeighbourPositions
import de.bixilon.minosoft.util.chunk.ChunkUtil.isInViewDistance
import de.bixilon.minosoft.util.chunk.ChunkUtil.received
import de.bixilon.minosoft.util.delegate.DelegateManager.delegate
import glm_.vec2.Vec2i
import glm_.vec3.Vec3
import glm_.vec3.Vec3i
@ -61,10 +61,10 @@ class World(
var cacheBiomeAccessor: NoiseBiomeAccessor? = null
val chunks: LockMap<Vec2i, Chunk> = lockMapOf()
val entities = WorldEntities()
var hardcore by delegate(false)
var dimension: DimensionProperties? = null
var difficulty: Difficulties? = null
var difficultyLocked = false
var hardcore by watched(false)
var dimension: DimensionProperties? by watched(null)
var difficulty: Difficulties? by watched(null)
var difficultyLocked by watched(false)
var hashedSeed = 0L
val time = WorldTime(this)
val weather = WorldWeather()

View File

@ -50,7 +50,7 @@ import de.bixilon.minosoft.protocol.network.connection.status.StatusConnection
import de.bixilon.minosoft.protocol.network.connection.status.StatusConnectionStates
import de.bixilon.minosoft.util.DNSUtil
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.delegate.watcher.entry.ListDelegateWatcher.Companion.watchListFX
import de.bixilon.minosoft.util.delegate.JavaFXDelegate.observeListFX
import javafx.fxml.FXML
import javafx.geometry.HPos
import javafx.geometry.Insets
@ -173,14 +173,12 @@ class ServerListController : EmbeddedJavaFXController<Pane>(), Refreshable {
}
fun initWatch() {
serverType!!::servers.watchListFX(this) {
while (it.next()) {
for (removed in it.removed) {
serverListViewFX.items -= ServerCard.CARDS.remove(removed)
}
for (added in it.addedSubList) {
updateServer(added)
}
serverType!!::servers.observeListFX(this) {
for (remove in it.removes) {
serverListViewFX.items -= ServerCard.CARDS.remove(remove)
}
for (add in it.adds) {
updateServer(add)
}
}
}

View File

@ -19,7 +19,7 @@ import de.bixilon.minosoft.gui.eros.card.CardFactory
import de.bixilon.minosoft.gui.eros.main.play.server.type.types.ServerType
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil.text
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.delegate.watcher.entry.ListDelegateWatcher.Companion.watchListFX
import de.bixilon.minosoft.util.delegate.JavaFXDelegate.observeListFX
import javafx.fxml.FXML
import javafx.scene.text.TextFlow
import org.kordamp.ikonli.javafx.FontIcon
@ -44,7 +44,7 @@ class ServerTypeCardController : AbstractCardController<ServerType>() {
headerFX.text = Minosoft.LANGUAGE_MANAGER.translate(item)
recalculate(item)
item::servers.watchListFX(this) { recalculate(item) }
item::servers.observeListFX(this) { recalculate(item) }
}
private fun recalculate(item: ServerType) {

View File

@ -1,5 +1,6 @@
package de.bixilon.minosoft.gui.eros.main.play.server.type.types
import de.bixilon.kutil.watcher.list.ListDataWatcher.Companion.watchedList
import de.bixilon.minosoft.config.profile.profiles.eros.ErosProfileManager
import de.bixilon.minosoft.config.profile.profiles.eros.ErosProfileSelectEvent
import de.bixilon.minosoft.config.profile.profiles.eros.server.entries.Server
@ -10,7 +11,6 @@ import de.bixilon.minosoft.modding.event.master.GlobalEventMaster
import de.bixilon.minosoft.modding.loading.Priorities
import de.bixilon.minosoft.protocol.network.connection.status.StatusConnectionStates
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.delegate.DelegateManager.listDelegate
import org.kordamp.ikonli.Ikon
import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
@ -18,7 +18,7 @@ object CustomServerType : ServerType {
override val icon: Ikon = FontAwesomeSolid.SERVER
override val hidden: Boolean = false
override var readOnly: Boolean = false
override var servers: MutableList<Server> by listDelegate(ErosProfileManager.selected.server.entries)
override var servers: MutableList<Server> by watchedList(ErosProfileManager.selected.server.entries)
private set
override val translationKey: ResourceLocation = "minosoft:server_type.custom".toResourceLocation()

View File

@ -1,11 +1,12 @@
package de.bixilon.minosoft.gui.eros.main.play.server.type.types
import de.bixilon.kutil.collections.CollectionUtil.synchronizedListOf
import de.bixilon.kutil.watcher.list.ListDataWatcher.Companion.watchedList
import de.bixilon.minosoft.config.profile.profiles.eros.server.entries.Server
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.gui.eros.main.play.server.card.ServerCard
import de.bixilon.minosoft.protocol.protocol.LANServerListener
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.delegate.DelegateManager.listDelegate
import org.kordamp.ikonli.Ikon
import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
@ -14,7 +15,7 @@ object LANServerType : ServerType {
override val hidden: Boolean
get() = !LANServerListener.listening
override var readOnly: Boolean = true
override val servers: MutableList<Server> by listDelegate()
override val servers: MutableList<Server> by watchedList(synchronizedListOf())
override val translationKey: ResourceLocation = "minosoft:server_type.lan".toResourceLocation()
override fun refresh(cards: List<ServerCard>) {

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.gui.eros.main.profiles
import de.bixilon.kutil.primitive.BooleanUtil.decide
import de.bixilon.kutil.watcher.map.MapChange.Companion.values
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.config.profile.ProfileManager
import de.bixilon.minosoft.config.profile.profiles.Profile
@ -28,7 +29,7 @@ import de.bixilon.minosoft.gui.eros.dialog.profiles.ProfileCreateDialog
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil.ctext
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.delegate.watcher.entry.MapDelegateWatcher.Companion.watchMapFX
import de.bixilon.minosoft.util.delegate.JavaFXDelegate.observeMapFX
import javafx.fxml.FXML
import javafx.geometry.HPos
import javafx.geometry.Insets
@ -74,9 +75,9 @@ class ProfilesListController : EmbeddedJavaFXController<Pane>() {
}
fun initWatch() {
profileManager!!::profiles.watchMapFX(this) {
profilesListViewFX.items -= it.valueRemoved
profilesListViewFX.items += it.valueAdded
profileManager!!::profiles.observeMapFX(this) {
profilesListViewFX.items -= it.removes.values()
profilesListViewFX.items += it.adds.values()
profilesListViewFX.refresh()
}
}

View File

@ -19,7 +19,7 @@ import de.bixilon.minosoft.gui.eros.card.AbstractCardController
import de.bixilon.minosoft.gui.eros.card.CardFactory
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil.text
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.delegate.watcher.SimpleDelegateWatcher.Companion.watchFX
import de.bixilon.minosoft.util.delegate.JavaFXDelegate.observeFX
import javafx.fxml.FXML
import javafx.scene.text.TextFlow
import org.kordamp.ikonli.javafx.FontIcon
@ -44,7 +44,7 @@ class ProfilesTypeCardController : AbstractCardController<ProfileManager<*>>() {
headerFX.text = Minosoft.LANGUAGE_MANAGER.translate(item.namespace)
recalculate(item)
item::profiles.watchFX(this) { recalculate(item) } // ToDo: Not a watchable map yet
item::profiles.observeFX(this) { recalculate(item) } // ToDo: Not a watchable map yet
}
private fun recalculate(item: ProfileManager<*>) {

View File

@ -7,7 +7,6 @@ import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.delegate.watcher.SimpleDelegateWatcher.Companion.watchRendering
class FogManager(
private val renderWindow: RenderWindow,
@ -42,10 +41,6 @@ class FogManager(
private var updateShaders = true
fun init() {
renderWindow.connection.world.view::viewDistance.watchRendering(this, true) { calculateFog() }
}
fun draw() {
calculateFog()
if (!updateShaders) {

View File

@ -12,6 +12,7 @@
*/
package de.bixilon.minosoft.protocol.protocol
import de.bixilon.kutil.json.JsonUtil.toMutableJsonObject
import de.bixilon.minosoft.data.entities.meta.EntityMetaData
import de.bixilon.minosoft.data.inventory.ItemStack
import de.bixilon.minosoft.data.player.properties.PlayerProperties
@ -35,7 +36,6 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_19W36A
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_1_13_2_PRE1
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_1_9_1_PRE1
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_20W28A
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.toMutableJsonObject
import glm_.vec3.Vec3i

View File

@ -1,118 +0,0 @@
package de.bixilon.minosoft.util.delegate
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.collections.CollectionUtil.synchronizedMapOf
import de.bixilon.kutil.collections.map.SynchronizedMap
import de.bixilon.minosoft.util.delegate.delegate.Delegate
import de.bixilon.minosoft.util.delegate.delegate.entry.ListDelegate
import de.bixilon.minosoft.util.delegate.delegate.entry.MapDelegate
import de.bixilon.minosoft.util.delegate.delegate.entry.SetDelegate
import de.bixilon.minosoft.util.delegate.watcher.DelegateListener
import javafx.collections.FXCollections
import javafx.collections.ListChangeListener
import javafx.collections.MapChangeListener
import javafx.collections.SetChangeListener
import java.lang.ref.WeakReference
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
import kotlin.reflect.KProperty1
@Deprecated("Will be integrated in KUtil")
object DelegateManager {
// [field][<receiver>][reference][listener]
private val listeners: SynchronizedMap<String, SynchronizedMap<WeakReference<Any>, MutableSet<Pair<WeakReference<Any>, DelegateListener<Any>>>>> = synchronizedMapOf()
private val CLASS = Class.forName("kotlin.jvm.internal.CallableReference")
private val RECEIVER_FIELD = CLASS.getDeclaredField("receiver")
private val PROPERTY_1_CLASS = Class.forName("kotlin.reflect.jvm.internal.KPropertyImpl")
private val CONTAINER_FIELD = PROPERTY_1_CLASS.getDeclaredField("container")
private val KCLASS_CLASS = Class.forName("kotlin.reflect.jvm.internal.KClassImpl")
private val JCLASS_FIELD = KCLASS_CLASS.getDeclaredField("jClass")
init {
RECEIVER_FIELD.isAccessible = true
CONTAINER_FIELD.isAccessible = true
JCLASS_FIELD.isAccessible = true
}
val KProperty<*>.receiver: Any
get() = RECEIVER_FIELD.get(this)
val KProperty<*>.container: KClass<*>
get() = CONTAINER_FIELD.get(this).unsafeCast()
val KClass<*>.jClass: Class<*>
get() = JCLASS_FIELD.get(this).unsafeCast()
val KProperty<*>.identifier: String
get() = when (this) {
is KProperty0<*> -> this.receiver::class.java.name + ":" + this.name
is KProperty1<*, *> -> this.container.jClass.name + ":" + this.name
else -> TODO("Can not identify $this")
}
fun <V> delegate(value: V, verify: ((V) -> Unit)? = null): Delegate<V> {
return Delegate(value, verify)
}
fun <K, V> mapDelegate(default: MutableMap<K, V> = mutableMapOf(), verify: ((MapChangeListener.Change<out K, out V>) -> Unit)? = null): MapDelegate<K, V> {
return MapDelegate(FXCollections.synchronizedObservableMap(FXCollections.observableMap(default)), verify)
}
fun <V> listDelegate(default: MutableList<V> = mutableListOf(), verify: ((ListChangeListener.Change<out V>) -> Unit)? = null): ListDelegate<V> {
return ListDelegate(FXCollections.synchronizedObservableList(FXCollections.observableList(default)), verify = verify)
}
fun <V> setDelegate(default: MutableSet<V> = mutableSetOf(), verify: ((SetChangeListener.Change<out V>) -> Unit)? = null): SetDelegate<V> {
return SetDelegate(FXCollections.synchronizedObservableSet(FXCollections.observableSet(default)), verify = verify)
}
fun onChange(thisRef: Any, field: String, previous: Any?, value: Any?) {
val listeners = this.listeners[field] ?: return
val toRemove: MutableSet<WeakReference<Any>> = mutableSetOf()
for ((receiverReference, rest) in listeners) {
val referenced = receiverReference.get()
if (referenced == null) {
toRemove += receiverReference
continue
}
if (receiverReference.objectEquals(thisRef)) {
val referenceToRemove: MutableSet<Pair<WeakReference<Any>, DelegateListener<Any>>> = mutableSetOf()
for (pair in rest) {
val (referenceReference, listener) = pair
val reference = referenceReference.get()
if (reference == null) {
referenceToRemove += pair
continue
}
listener.invoke(previous, value)
}
rest -= referenceToRemove
}
}
listeners -= toRemove
}
fun <T> register(reference: Any, listener: DelegateListener<T>) {
val receiver = listener.property.receiver
val receiverListeners = this.listeners.getOrPut(listener.field) { synchronizedMapOf() }
var set: MutableSet<Pair<WeakReference<Any>, DelegateListener<Any>>>? = null
for ((receiverReference, rest) in receiverListeners) {
if (receiverReference.objectEquals(receiver)) {
set = rest
break
}
}
if (set == null) {
set = mutableSetOf()
receiverListeners[WeakReference(receiver)] = set
}
set += Pair(WeakReference(reference), listener.unsafeCast())
}
fun WeakReference<*>.objectEquals(other: Any): Boolean {
return this.get() == other
}
}

View File

@ -0,0 +1,34 @@
package de.bixilon.minosoft.util.delegate
import de.bixilon.kutil.watcher.DataWatcher.Companion.observe
import de.bixilon.kutil.watcher.list.ListChange
import de.bixilon.kutil.watcher.list.ListDataWatcher.Companion.observeList
import de.bixilon.kutil.watcher.map.MapChange
import de.bixilon.kutil.watcher.map.MapDataWatcher.Companion.observeMap
import de.bixilon.kutil.watcher.set.SetChange
import de.bixilon.kutil.watcher.set.SetDataWatcher.Companion.observeSet
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import kotlin.reflect.KProperty0
object JavaFXDelegate {
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <V> KProperty0<V>.observeFX(owner: Any, observer: (V) -> Unit) {
this.observe(owner) { JavaFXUtil.runLater { observer(it) } }
}
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <V> KProperty0<Set<V>>.observeSetFX(owner: Any, observer: (SetChange<V>) -> Unit) {
this.observeSet(owner) { JavaFXUtil.runLater { observer(it) } }
}
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <V> KProperty0<List<V>>.observeListFX(owner: Any, observer: (ListChange<V>) -> Unit) {
this.observeList(owner) { JavaFXUtil.runLater { observer(it) } }
}
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <K, V> KProperty0<Map<K, V>>.observeMapFX(owner: Any, observer: (MapChange<K, V>) -> Unit) {
this.observeMap(owner) { JavaFXUtil.runLater { observer(it) } }
}
}

View File

@ -0,0 +1,52 @@
package de.bixilon.minosoft.util.delegate
import de.bixilon.kutil.watcher.DataWatcher.Companion.observe
import de.bixilon.kutil.watcher.list.ListChange
import de.bixilon.kutil.watcher.list.ListDataWatcher.Companion.observeList
import de.bixilon.kutil.watcher.map.MapChange
import de.bixilon.kutil.watcher.map.MapDataWatcher.Companion.observeMap
import de.bixilon.kutil.watcher.set.SetChange
import de.bixilon.kutil.watcher.set.SetDataWatcher.Companion.observeSet
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.Rendering
import kotlin.reflect.KProperty0
object RenderingDelegate {
private fun requireContext(): RenderWindow {
return Rendering.currentContext ?: throw IllegalStateException("Can only be registered in a render context!")
}
private fun <V> runInContext(context: RenderWindow, value: V, runnable: (V) -> Unit) {
val changeContext = Rendering.currentContext
if (changeContext === context) {
runnable(value)
} else {
context.queue += { runnable(value) }
}
}
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <V> KProperty0<V>.observeRendering(owner: Any, observer: (V) -> Unit) {
val context = requireContext()
this.observe(owner) { runInContext(context, it, observer) }
}
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <V> KProperty0<Set<V>>.observeSetRendering(owner: Any, observer: (SetChange<V>) -> Unit) {
val context = requireContext()
this.observeSet(owner) { runInContext(context, it, observer) }
}
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <V> KProperty0<List<V>>.observeListRendering(owner: Any, observer: (ListChange<V>) -> Unit) {
val context = requireContext()
this.observeList(owner) { runInContext(context, it, observer) }
}
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
fun <K, V> KProperty0<Map<K, V>>.observeMapRendering(owner: Any, observer: (MapChange<K, V>) -> Unit) {
val context = requireContext()
this.observeMap(owner) { runInContext(context, it, observer) }
}
}

View File

@ -1,31 +0,0 @@
package de.bixilon.minosoft.util.delegate.delegate
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class Delegate<V>(
private var value: V,
private var check: ((V) -> Unit)?,
) : ReadWriteProperty<Any, V> {
override fun getValue(thisRef: Any, property: KProperty<*>): V {
return value
}
override fun setValue(thisRef: Any, property: KProperty<*>, value: V) {
if (this.value == value) {
return
}
check?.invoke(value)
Log.log(LogMessageType.OTHER, LogLevels.VERBOSE) { "Changed ${this.value} in $thisRef to $value" }
val previous = this.value
this.value = value
DelegateManager.onChange(thisRef, property.identifier, previous, value)
}
}

View File

@ -1,30 +0,0 @@
package de.bixilon.minosoft.util.delegate.delegate.entry
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.minosoft.util.delegate.delegate.DelegateSetter
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
abstract class EntryDelegate<V> : ReadWriteProperty<Any, V>, DelegateSetter<V> {
protected lateinit var property: KProperty<V>
protected lateinit var thisRef: Any
protected fun checkLateinitValues(property: KProperty<*>) {
if (!this::property.isInitialized) {
this.property = property.unsafeCast()
}
}
override fun getValue(thisRef: Any, property: KProperty<*>): V {
checkLateinitValues(property)
this.thisRef = thisRef
return get()
}
override fun setValue(thisRef: Any, property: KProperty<*>, value: V) {
checkLateinitValues(property)
this.thisRef = thisRef
set(value)
}
}

View File

@ -1,39 +0,0 @@
package de.bixilon.minosoft.util.delegate.delegate.entry
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
import javafx.collections.FXCollections
import javafx.collections.ListChangeListener
import javafx.collections.ObservableList
open class ListDelegate<V>(
private var value: ObservableList<V>,
private val verify: ((ListChangeListener.Change<out V>) -> Unit)?,
) : EntryDelegate<MutableList<V>>() {
init {
initListener()
}
private fun initListener() {
value.addListener(ListChangeListener {
verify?.invoke(it)
if (StaticConfiguration.LOG_DELEGATE) {
Log.log(LogMessageType.OTHER, LogLevels.VERBOSE) { "Changed list entry $it" }
}
DelegateManager.onChange(thisRef, property.identifier, null, it)
})
}
override fun get(): MutableList<V> = value
override fun set(value: MutableList<V>) {
this.value = FXCollections.synchronizedObservableList(FXCollections.observableList(value))
initListener()
}
}

View File

@ -1,45 +0,0 @@
package de.bixilon.minosoft.util.delegate.delegate.entry
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
import javafx.collections.FXCollections
import javafx.collections.MapChangeListener
import javafx.collections.ObservableMap
import kotlin.reflect.KProperty
open class MapDelegate<K, V>(
private var value: ObservableMap<K, V>,
private val verify: ((MapChangeListener.Change<out K, out V>) -> Unit)?,
) : EntryDelegate<MutableMap<K, V>>() {
init {
initListener()
}
private fun initListener() {
value.addListener(MapChangeListener {
verify?.invoke(it)
if (StaticConfiguration.LOG_DELEGATE) {
Log.log(LogMessageType.OTHER, LogLevels.VERBOSE) { "Changed map entry $it" }
}
DelegateManager.onChange(thisRef, property.identifier, null, it)
})
}
override fun getValue(thisRef: Any, property: KProperty<*>): MutableMap<K, V> {
checkLateinitValues(property)
return value
}
override fun get(): MutableMap<K, V> = value
override fun set(value: MutableMap<K, V>) {
this.value = FXCollections.synchronizedObservableMap(FXCollections.observableMap(value))
initListener()
return
}
}

View File

@ -1,39 +0,0 @@
package de.bixilon.minosoft.util.delegate.delegate.entry
import de.bixilon.minosoft.config.StaticConfiguration
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
import javafx.collections.FXCollections
import javafx.collections.ObservableSet
import javafx.collections.SetChangeListener
open class SetDelegate<V>(
private var value: ObservableSet<V>,
private val verify: ((SetChangeListener.Change<out V>) -> Unit)?,
) : EntryDelegate<MutableSet<V>>() {
init {
initListener()
}
private fun initListener() {
value.addListener(SetChangeListener {
verify?.invoke(it)
if (StaticConfiguration.LOG_DELEGATE) {
Log.log(LogMessageType.OTHER, LogLevels.VERBOSE) { "Changed set entry $it" }
}
DelegateManager.onChange(thisRef, property.identifier, null, it)
})
}
override fun get(): MutableSet<V> = value
override fun set(value: MutableSet<V>) {
this.value = FXCollections.synchronizedObservableSet(FXCollections.observableSet(value))
initListener()
}
}

View File

@ -1,11 +0,0 @@
package de.bixilon.minosoft.util.delegate.watcher
import kotlin.reflect.KProperty
interface DelegateListener<T> {
val property: KProperty<T>
val field: String
fun invoke(previous: Any?, value: Any?)
}

View File

@ -1,56 +0,0 @@
package de.bixilon.minosoft.util.delegate.watcher
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.gui.rendering.Rendering
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
import kotlin.reflect.KProperty1
class SimpleDelegateWatcher<T>(
override val property: KProperty<T>,
instant: Boolean,
private val callback: (T) -> Unit,
) : DelegateListener<T> {
override val field: String = property.identifier
init {
check(property !is KProperty1<*, *>) { "Can only listen on instanced, not classes!" }
check(property is KProperty0<*>) { "Can only listen on delegates!" }
if (instant) {
invoke(property.get(), property.get())
}
}
override fun invoke(previous: Any?, value: Any?) {
callback(value.unsafeCast())
}
companion object {
@JvmOverloads
fun <T> KProperty<T>.watch(reference: Any, instant: Boolean = false, callback: ((T) -> Unit)) {
DelegateManager.register(reference, SimpleDelegateWatcher(this, instant, callback))
}
@JvmOverloads
fun <T> KProperty<T>.watchFX(reference: Any, instant: Boolean = false, callback: ((T) -> Unit)) {
DelegateManager.register(reference, SimpleDelegateWatcher(this, instant) { JavaFXUtil.runLater { callback(it) } })
}
@JvmOverloads
fun <T> KProperty<T>.watchRendering(reference: Any, instant: Boolean = false, callback: ((T) -> Unit)) {
val context = Rendering.currentContext ?: throw IllegalStateException("Can only be registered in a render context!")
DelegateManager.register(reference, SimpleDelegateWatcher(this, instant) {
val changeContext = Rendering.currentContext
if (changeContext === context) {
callback(it)
} else {
context.queue += { callback(it) }
}
})
}
}
}

View File

@ -1,32 +0,0 @@
package de.bixilon.minosoft.util.delegate.watcher.entry
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.delegate.watcher.DelegateListener
import javafx.collections.ListChangeListener
import kotlin.reflect.KProperty
class ListDelegateWatcher<V>(
override val property: KProperty<MutableList<V>>,
private val callback: (ListChangeListener.Change<V>) -> Unit,
) : DelegateListener<MutableList<V>> {
override val field: String = property.identifier
override fun invoke(previous: Any?, value: Any?) {
callback(value.unsafeCast())
}
companion object {
fun <V> KProperty<MutableList<V>>.watchList(reference: Any, callback: ((ListChangeListener.Change<V>) -> Unit)) {
DelegateManager.register(reference, ListDelegateWatcher(this, callback))
}
fun <V> KProperty<MutableList<V>>.watchListFX(reference: Any, callback: ((ListChangeListener.Change<V>) -> Unit)) {
DelegateManager.register(reference, ListDelegateWatcher(this) { JavaFXUtil.runLater { callback(it) } })
}
}
}

View File

@ -1,31 +0,0 @@
package de.bixilon.minosoft.util.delegate.watcher.entry
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.delegate.watcher.DelegateListener
import javafx.collections.MapChangeListener
import kotlin.reflect.KProperty
class MapDelegateWatcher<K, V>(
override val property: KProperty<MutableMap<K, V>>,
private val callback: (MapChangeListener.Change<K, V>) -> Unit,
) : DelegateListener<MutableMap<K, V>> {
override val field: String = property.identifier
override fun invoke(previous: Any?, value: Any?) {
callback(value.unsafeCast())
}
companion object {
fun <K, V> KProperty<MutableMap<K, V>>.watchMap(reference: Any, callback: ((MapChangeListener.Change<K, V>) -> Unit)) {
DelegateManager.register(reference, MapDelegateWatcher(this, callback))
}
fun <K, V> KProperty<MutableMap<K, V>>.watchMapFX(reference: Any, callback: ((MapChangeListener.Change<K, V>) -> Unit)) {
DelegateManager.register(reference, MapDelegateWatcher(this) { JavaFXUtil.runLater { callback(it) } })
}
}
}

View File

@ -1,31 +0,0 @@
package de.bixilon.minosoft.util.delegate.watcher.entry
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.minosoft.gui.eros.util.JavaFXUtil
import de.bixilon.minosoft.util.delegate.DelegateManager
import de.bixilon.minosoft.util.delegate.DelegateManager.identifier
import de.bixilon.minosoft.util.delegate.watcher.DelegateListener
import javafx.collections.SetChangeListener
import kotlin.reflect.KProperty
class SetDelegateWatcher<V>(
override val property: KProperty<MutableSet<V>>,
private val callback: (SetChangeListener.Change<V>) -> Unit,
) : DelegateListener<MutableSet<V>> {
override val field: String = property.identifier
override fun invoke(previous: Any?, value: Any?) {
callback(value.unsafeCast())
}
companion object {
fun <V> KProperty<MutableSet<V>>.watchSet(reference: Any, callback: ((SetChangeListener.Change<V>) -> Unit)) {
DelegateManager.register(reference, SetDelegateWatcher(this, callback))
}
fun <V> KProperty<MutableSet<V>>.watchSetFX(reference: Any, callback: ((SetChangeListener.Change<V>) -> Unit)) {
DelegateManager.register(reference, SetDelegateWatcher(this) { JavaFXUtil.runLater { callback(it) } })
}
}
}

View File

@ -14,8 +14,6 @@
package de.bixilon.minosoft.util.nbt.tag
import de.bixilon.kutil.cast.CastUtil.nullCast
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.json.MutableJsonObject
object NBTUtil {
@ -36,26 +34,10 @@ object NBTUtil {
return null
}
@Deprecated("Will be in Kutil 1.4")
fun Any?.asMutableJsonObject(): MutableJsonObject {
return this.unsafeCast()
}
@Deprecated("Will be in Kutil 1.4")
fun Any?.toMutableJsonObject(): MutableJsonObject? {
return this?.nullCast()
}
fun <T> Any?.listCast(): MutableList<T>? {
try {
return this as MutableList<T>
} catch (ignored: ClassCastException) {
}
return null
return this.nullCast()
}
operator fun Map<String, Any>.get(vararg keys: String): Any? {
for (key in keys) {
this[key]?.let { return it }