fix: smart instrument block entity serialization

This commit is contained in:
xtex 2023-07-30 10:33:10 +08:00
parent b574d8a3d4
commit fd7766b38c
Signed by: xtex
GPG Key ID: B918086ED8045B91

View File

@ -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,9 +164,13 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) :
override fun load(tag: CompoundTag) {
super.load(tag)
if (TAG_MUSIC in tag) {
if (level == null) {
playerData = tag.getCompound(TAG_MUSIC)
} else {
player = MusicPlayer(tag.getCompound(TAG_MUSIC), level!!, blockPos)
}
}
}
override fun saveAdditional(tag: CompoundTag) {
super.saveAdditional(tag)
@ -199,10 +205,14 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) :
}
fun tick() {
if (playerData != null) {
player = MusicPlayer(playerData!!, level!!, blockPos)
}
if (player != null) {
if (checkProjections()) {
player?.tick()
player!!.tick()
if (!level!!.isClientSide) {
if (player?.isEnd == true) {
if (player!!.isEnd == true) {
player = null
setChanged()
sendBlockUpdated()
@ -217,5 +227,6 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) :
sendBlockUpdated()
}
}
}
}