From 737e4166b92119b2ef4cafd69de18b54b12bcd08 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Tue, 4 Jan 2022 16:06:14 +0100 Subject: [PATCH] outsource delegate stuff into kutil, bump dependencies --- pom.xml | 4 +- .../delegate/delegate/BackingDelegate.kt | 2 +- .../delegate/entry/ListDelegateProfile.kt | 2 +- .../delegate/entry/MapDelegateProfile.kt | 2 +- .../delegate/entry/SetDelegateProfile.kt | 2 +- .../watcher/SimpleProfileDelegateWatcher.kt | 2 +- .../entry/ListProfileDelegateWatcher.kt | 2 +- .../entry/MapProfileDelegateWatcher.kt | 2 +- .../entry/SetProfileDelegateWatcher.kt | 2 +- .../minosoft/data/inventory/ItemStack.kt | 2 +- .../de/bixilon/minosoft/data/world/World.kt | 10 +- .../main/play/server/ServerListController.kt | 16 ++- .../server/type/ServerTypeCardController.kt | 4 +- .../server/type/types/CustomServerType.kt | 4 +- .../play/server/type/types/LANServerType.kt | 5 +- .../main/profiles/ProfilesListController.kt | 9 +- .../type/ProfilesTypeCardController.kt | 4 +- .../gui/rendering/camera/FogManager.kt | 5 - .../protocol/protocol/PlayInByteBuffer.kt | 2 +- .../minosoft/util/delegate/DelegateManager.kt | 118 ------------------ .../minosoft/util/delegate/JavaFXDelegate.kt | 34 +++++ .../util/delegate/RenderingDelegate.kt | 52 ++++++++ .../util/delegate/delegate/Delegate.kt | 31 ----- .../delegate/delegate/entry/EntryDelegate.kt | 30 ----- .../delegate/delegate/entry/ListDelegate.kt | 39 ------ .../delegate/delegate/entry/MapDelegate.kt | 45 ------- .../delegate/delegate/entry/SetDelegate.kt | 39 ------ .../util/delegate/watcher/DelegateListener.kt | 11 -- .../delegate/watcher/SimpleDelegateWatcher.kt | 56 --------- .../watcher/entry/ListDelegateWatcher.kt | 32 ----- .../watcher/entry/MapDelegateWatcher.kt | 31 ----- .../watcher/entry/SetDelegateWatcher.kt | 31 ----- .../bixilon/minosoft/util/nbt/tag/NBTUtil.kt | 20 +-- 33 files changed, 125 insertions(+), 525 deletions(-) delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/DelegateManager.kt create mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/JavaFXDelegate.kt create mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/RenderingDelegate.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/delegate/Delegate.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/EntryDelegate.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/ListDelegate.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/MapDelegate.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/SetDelegate.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/watcher/DelegateListener.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/watcher/SimpleDelegateWatcher.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/ListDelegateWatcher.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/MapDelegateWatcher.kt delete mode 100644 src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/SetDelegateWatcher.kt diff --git a/pom.xml b/pom.xml index 32c40afc2..a96cef90e 100644 --- a/pom.xml +++ b/pom.xml @@ -447,7 +447,7 @@ com.github.luben zstd-jni - 1.5.0-5 + 1.5.1-1 com.github.afester.javafx @@ -457,7 +457,7 @@ de.bixilon kutil - 1.3 + 1.4.1 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/BackingDelegate.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/BackingDelegate.kt index d101977ab..d3c386584 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/BackingDelegate.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/BackingDelegate.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/ListDelegateProfile.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/ListDelegateProfile.kt index 281814fef..7cbbaa82d 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/ListDelegateProfile.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/ListDelegateProfile.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/MapDelegateProfile.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/MapDelegateProfile.kt index 34001c190..9d2be94da 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/MapDelegateProfile.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/MapDelegateProfile.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/SetDelegateProfile.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/SetDelegateProfile.kt index 141ba29de..62f108a0d 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/SetDelegateProfile.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/delegate/entry/SetDelegateProfile.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/SimpleProfileDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/SimpleProfileDelegateWatcher.kt index ab18415a0..39db6315c 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/SimpleProfileDelegateWatcher.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/SimpleProfileDelegateWatcher.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/ListProfileDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/ListProfileDelegateWatcher.kt index 18d9326e8..a23ffa9b3 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/ListProfileDelegateWatcher.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/ListProfileDelegateWatcher.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/MapProfileDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/MapProfileDelegateWatcher.kt index d30bf0968..8170bcb18 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/MapProfileDelegateWatcher.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/MapProfileDelegateWatcher.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/SetProfileDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/SetProfileDelegateWatcher.kt index d109ed691..15aabd3c2 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/SetProfileDelegateWatcher.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/watcher/entry/SetProfileDelegateWatcher.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/data/inventory/ItemStack.kt b/src/main/java/de/bixilon/minosoft/data/inventory/ItemStack.kt index e6e78eef2..75c7c39a2 100644 --- a/src/main/java/de/bixilon/minosoft/data/inventory/ItemStack.kt +++ b/src/main/java/de/bixilon/minosoft/data/inventory/ItemStack.kt @@ -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( diff --git a/src/main/java/de/bixilon/minosoft/data/world/World.kt b/src/main/java/de/bixilon/minosoft/data/world/World.kt index 70169c8e9..c871f6f43 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/World.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/World.kt @@ -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 = 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() diff --git a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/ServerListController.kt b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/ServerListController.kt index f5be43b84..dc25bf2f2 100644 --- a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/ServerListController.kt +++ b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/ServerListController.kt @@ -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(), 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) } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/ServerTypeCardController.kt b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/ServerTypeCardController.kt index cdcee156d..6e8bad4a0 100644 --- a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/ServerTypeCardController.kt +++ b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/ServerTypeCardController.kt @@ -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() { 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) { diff --git a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/CustomServerType.kt b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/CustomServerType.kt index 0d56b6313..9fb737465 100644 --- a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/CustomServerType.kt +++ b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/CustomServerType.kt @@ -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 by listDelegate(ErosProfileManager.selected.server.entries) + override var servers: MutableList by watchedList(ErosProfileManager.selected.server.entries) private set override val translationKey: ResourceLocation = "minosoft:server_type.custom".toResourceLocation() diff --git a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/LANServerType.kt b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/LANServerType.kt index 4d72411a6..639db8fc5 100644 --- a/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/LANServerType.kt +++ b/src/main/java/de/bixilon/minosoft/gui/eros/main/play/server/type/types/LANServerType.kt @@ -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 by listDelegate() + override val servers: MutableList by watchedList(synchronizedListOf()) override val translationKey: ResourceLocation = "minosoft:server_type.lan".toResourceLocation() override fun refresh(cards: List) { diff --git a/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/ProfilesListController.kt b/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/ProfilesListController.kt index 22a1e46bd..a8e1da3e6 100644 --- a/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/ProfilesListController.kt +++ b/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/ProfilesListController.kt @@ -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() { } 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() } } diff --git a/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/type/ProfilesTypeCardController.kt b/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/type/ProfilesTypeCardController.kt index 78fbf2fa0..fd8b1d8ef 100644 --- a/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/type/ProfilesTypeCardController.kt +++ b/src/main/java/de/bixilon/minosoft/gui/eros/main/profiles/type/ProfilesTypeCardController.kt @@ -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>() { 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<*>) { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/FogManager.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/FogManager.kt index e9fc33ad4..2c3e1f04e 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/camera/FogManager.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/camera/FogManager.kt @@ -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) { diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/PlayInByteBuffer.kt b/src/main/java/de/bixilon/minosoft/protocol/protocol/PlayInByteBuffer.kt index 4d9ddd8e3..ec8b14a3b 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/PlayInByteBuffer.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/PlayInByteBuffer.kt @@ -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 diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/DelegateManager.kt b/src/main/java/de/bixilon/minosoft/util/delegate/DelegateManager.kt deleted file mode 100644 index 53fdab111..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/DelegateManager.kt +++ /dev/null @@ -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][][reference][listener] - private val listeners: SynchronizedMap, MutableSet, DelegateListener>>>> = 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 delegate(value: V, verify: ((V) -> Unit)? = null): Delegate { - return Delegate(value, verify) - } - - fun mapDelegate(default: MutableMap = mutableMapOf(), verify: ((MapChangeListener.Change) -> Unit)? = null): MapDelegate { - return MapDelegate(FXCollections.synchronizedObservableMap(FXCollections.observableMap(default)), verify) - } - - fun listDelegate(default: MutableList = mutableListOf(), verify: ((ListChangeListener.Change) -> Unit)? = null): ListDelegate { - return ListDelegate(FXCollections.synchronizedObservableList(FXCollections.observableList(default)), verify = verify) - } - - fun setDelegate(default: MutableSet = mutableSetOf(), verify: ((SetChangeListener.Change) -> Unit)? = null): SetDelegate { - 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> = mutableSetOf() - for ((receiverReference, rest) in listeners) { - val referenced = receiverReference.get() - if (referenced == null) { - toRemove += receiverReference - continue - } - if (receiverReference.objectEquals(thisRef)) { - val referenceToRemove: MutableSet, DelegateListener>> = 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 register(reference: Any, listener: DelegateListener) { - val receiver = listener.property.receiver - val receiverListeners = this.listeners.getOrPut(listener.field) { synchronizedMapOf() } - var set: MutableSet, DelegateListener>>? = 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 - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/JavaFXDelegate.kt b/src/main/java/de/bixilon/minosoft/util/delegate/JavaFXDelegate.kt new file mode 100644 index 000000000..5befe2578 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/delegate/JavaFXDelegate.kt @@ -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 KProperty0.observeFX(owner: Any, observer: (V) -> Unit) { + this.observe(owner) { JavaFXUtil.runLater { observer(it) } } + } + + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + fun KProperty0>.observeSetFX(owner: Any, observer: (SetChange) -> Unit) { + this.observeSet(owner) { JavaFXUtil.runLater { observer(it) } } + } + + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + fun KProperty0>.observeListFX(owner: Any, observer: (ListChange) -> Unit) { + this.observeList(owner) { JavaFXUtil.runLater { observer(it) } } + } + + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + fun KProperty0>.observeMapFX(owner: Any, observer: (MapChange) -> Unit) { + this.observeMap(owner) { JavaFXUtil.runLater { observer(it) } } + } +} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/RenderingDelegate.kt b/src/main/java/de/bixilon/minosoft/util/delegate/RenderingDelegate.kt new file mode 100644 index 000000000..224f055ba --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/delegate/RenderingDelegate.kt @@ -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 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 KProperty0.observeRendering(owner: Any, observer: (V) -> Unit) { + val context = requireContext() + this.observe(owner) { runInContext(context, it, observer) } + } + + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + fun KProperty0>.observeSetRendering(owner: Any, observer: (SetChange) -> Unit) { + val context = requireContext() + this.observeSet(owner) { runInContext(context, it, observer) } + } + + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + fun KProperty0>.observeListRendering(owner: Any, observer: (ListChange) -> Unit) { + val context = requireContext() + this.observeList(owner) { runInContext(context, it, observer) } + } + + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") + fun KProperty0>.observeMapRendering(owner: Any, observer: (MapChange) -> Unit) { + val context = requireContext() + this.observeMap(owner) { runInContext(context, it, observer) } + } +} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/Delegate.kt b/src/main/java/de/bixilon/minosoft/util/delegate/delegate/Delegate.kt deleted file mode 100644 index d0b56be9e..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/Delegate.kt +++ /dev/null @@ -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( - private var value: V, - private var check: ((V) -> Unit)?, -) : ReadWriteProperty { - - 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) - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/EntryDelegate.kt b/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/EntryDelegate.kt deleted file mode 100644 index c8d15b376..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/EntryDelegate.kt +++ /dev/null @@ -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 : ReadWriteProperty, DelegateSetter { - protected lateinit var property: KProperty - 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) - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/ListDelegate.kt b/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/ListDelegate.kt deleted file mode 100644 index 34bdb7dc7..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/ListDelegate.kt +++ /dev/null @@ -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( - private var value: ObservableList, - private val verify: ((ListChangeListener.Change) -> Unit)?, -) : EntryDelegate>() { - - 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 = value - - override fun set(value: MutableList) { - this.value = FXCollections.synchronizedObservableList(FXCollections.observableList(value)) - initListener() - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/MapDelegate.kt b/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/MapDelegate.kt deleted file mode 100644 index 6ec39e5b6..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/MapDelegate.kt +++ /dev/null @@ -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( - private var value: ObservableMap, - private val verify: ((MapChangeListener.Change) -> Unit)?, -) : EntryDelegate>() { - - 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 { - checkLateinitValues(property) - return value - } - - override fun get(): MutableMap = value - - override fun set(value: MutableMap) { - this.value = FXCollections.synchronizedObservableMap(FXCollections.observableMap(value)) - initListener() - return - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/SetDelegate.kt b/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/SetDelegate.kt deleted file mode 100644 index aa521da77..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/delegate/entry/SetDelegate.kt +++ /dev/null @@ -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( - private var value: ObservableSet, - private val verify: ((SetChangeListener.Change) -> Unit)?, -) : EntryDelegate>() { - - 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 = value - - override fun set(value: MutableSet) { - this.value = FXCollections.synchronizedObservableSet(FXCollections.observableSet(value)) - initListener() - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/DelegateListener.kt b/src/main/java/de/bixilon/minosoft/util/delegate/watcher/DelegateListener.kt deleted file mode 100644 index 4bc2aec10..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/DelegateListener.kt +++ /dev/null @@ -1,11 +0,0 @@ -package de.bixilon.minosoft.util.delegate.watcher - -import kotlin.reflect.KProperty - -interface DelegateListener { - val property: KProperty - val field: String - - - fun invoke(previous: Any?, value: Any?) -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/SimpleDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/util/delegate/watcher/SimpleDelegateWatcher.kt deleted file mode 100644 index 4965800d0..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/SimpleDelegateWatcher.kt +++ /dev/null @@ -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( - override val property: KProperty, - instant: Boolean, - private val callback: (T) -> Unit, -) : DelegateListener { - 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 KProperty.watch(reference: Any, instant: Boolean = false, callback: ((T) -> Unit)) { - DelegateManager.register(reference, SimpleDelegateWatcher(this, instant, callback)) - } - - @JvmOverloads - fun KProperty.watchFX(reference: Any, instant: Boolean = false, callback: ((T) -> Unit)) { - DelegateManager.register(reference, SimpleDelegateWatcher(this, instant) { JavaFXUtil.runLater { callback(it) } }) - } - - @JvmOverloads - fun KProperty.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) } - } - }) - } - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/ListDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/ListDelegateWatcher.kt deleted file mode 100644 index 729473e55..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/ListDelegateWatcher.kt +++ /dev/null @@ -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( - override val property: KProperty>, - private val callback: (ListChangeListener.Change) -> Unit, -) : DelegateListener> { - override val field: String = property.identifier - - - override fun invoke(previous: Any?, value: Any?) { - callback(value.unsafeCast()) - } - - companion object { - - fun KProperty>.watchList(reference: Any, callback: ((ListChangeListener.Change) -> Unit)) { - DelegateManager.register(reference, ListDelegateWatcher(this, callback)) - } - - fun KProperty>.watchListFX(reference: Any, callback: ((ListChangeListener.Change) -> Unit)) { - DelegateManager.register(reference, ListDelegateWatcher(this) { JavaFXUtil.runLater { callback(it) } }) - } - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/MapDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/MapDelegateWatcher.kt deleted file mode 100644 index 5fd9647df..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/MapDelegateWatcher.kt +++ /dev/null @@ -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( - override val property: KProperty>, - private val callback: (MapChangeListener.Change) -> Unit, -) : DelegateListener> { - override val field: String = property.identifier - - override fun invoke(previous: Any?, value: Any?) { - callback(value.unsafeCast()) - } - - companion object { - - fun KProperty>.watchMap(reference: Any, callback: ((MapChangeListener.Change) -> Unit)) { - DelegateManager.register(reference, MapDelegateWatcher(this, callback)) - } - - fun KProperty>.watchMapFX(reference: Any, callback: ((MapChangeListener.Change) -> Unit)) { - DelegateManager.register(reference, MapDelegateWatcher(this) { JavaFXUtil.runLater { callback(it) } }) - } - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/SetDelegateWatcher.kt b/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/SetDelegateWatcher.kt deleted file mode 100644 index ad8214546..000000000 --- a/src/main/java/de/bixilon/minosoft/util/delegate/watcher/entry/SetDelegateWatcher.kt +++ /dev/null @@ -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( - override val property: KProperty>, - private val callback: (SetChangeListener.Change) -> Unit, -) : DelegateListener> { - override val field: String = property.identifier - - override fun invoke(previous: Any?, value: Any?) { - callback(value.unsafeCast()) - } - - companion object { - - fun KProperty>.watchSet(reference: Any, callback: ((SetChangeListener.Change) -> Unit)) { - DelegateManager.register(reference, SetDelegateWatcher(this, callback)) - } - - fun KProperty>.watchSetFX(reference: Any, callback: ((SetChangeListener.Change) -> Unit)) { - DelegateManager.register(reference, SetDelegateWatcher(this) { JavaFXUtil.runLater { callback(it) } }) - } - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt b/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt index 8f7c5a278..62aa9c406 100644 --- a/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt +++ b/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt @@ -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 Any?.listCast(): MutableList? { - try { - return this as MutableList - } catch (ignored: ClassCastException) { - } - return null + return this.nullCast() } - operator fun Map.get(vararg keys: String): Any? { for (key in keys) { this[key]?.let { return it }