From fd7766b38c91db689b4870beec3d06d7271a49ca Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 30 Jul 2023 10:33:10 +0800 Subject: [PATCH] fix: smart instrument block entity serialization --- .../projection/music/SmartInstrument.kt | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt b/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt index 9109280..d4aa638 100644 --- a/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt +++ b/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt @@ -153,6 +153,8 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) : const val TAG_MUSIC = "Music" } + // delay MusicPlayer initialization until level is available + var playerData: CompoundTag? = null var player: MusicPlayer? = null override fun getUpdateTag(): CompoundTag = saveWithoutMetadata() @@ -162,7 +164,11 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) : override fun load(tag: CompoundTag) { super.load(tag) if (TAG_MUSIC in tag) { - player = MusicPlayer(tag.getCompound(TAG_MUSIC), level!!, blockPos) + if (level == null) { + playerData = tag.getCompound(TAG_MUSIC) + } else { + player = MusicPlayer(tag.getCompound(TAG_MUSIC), level!!, blockPos) + } } } @@ -199,22 +205,27 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) : } fun tick() { - if (checkProjections()) { - player?.tick() - if (!level!!.isClientSide) { - if (player?.isEnd == true) { - player = null - setChanged() - sendBlockUpdated() - if (CausalityAnchor.checkEffect(level!!, blockPos) || level!!.random.nextInt(7) != 0) { - startMusic() + if (playerData != null) { + player = MusicPlayer(playerData!!, level!!, blockPos) + } + if (player != null) { + if (checkProjections()) { + player!!.tick() + if (!level!!.isClientSide) { + if (player!!.isEnd == true) { + player = null + setChanged() + sendBlockUpdated() + if (CausalityAnchor.checkEffect(level!!, blockPos) || level!!.random.nextInt(7) != 0) { + startMusic() + } } } + } else { + player = null + setChanged() + sendBlockUpdated() } - } else { - player = null - setChanged() - sendBlockUpdated() } }