From 3ce90814003a564391b5d1442a2111d33d72cb2e Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 30 Jul 2023 11:20:52 +0800 Subject: [PATCH] feat: save rhythm seed --- .../quaedam/projection/music/MusicPlayer.kt | 21 +++++++++++++------ .../projection/music/SmartInstrument.kt | 8 +++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt b/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt index c6ed339..03d7690 100644 --- a/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt +++ b/common/src/main/kotlin/quaedam/projection/music/MusicPlayer.kt @@ -17,23 +17,31 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties import quaedam.projector.Projector import kotlin.random.Random -class MusicPlayer(val seed: Long, val level: Level, val pos: BlockPos, val startedAt: Long = level.gameTime) { +class MusicPlayer( + val noteSeed: Long, + val rhythmSeed: Long, + val level: Level, + val pos: BlockPos, + val startedAt: Long = level.gameTime +) { companion object { - const val TAG_SEED = "Seed" + const val TAG_NOTE_SEED = "NoteSeed" + const val TAG_RHYTHM_SEED = "RhythmSeed" const val TAG_STARTED_AT = "StartedAt" } constructor(tag: CompoundTag, level: Level, pos: BlockPos) : this( - tag.getLong(TAG_SEED), + tag.getLong(TAG_NOTE_SEED), + tag.getLong(TAG_RHYTHM_SEED), level, pos, tag.getLong(TAG_STARTED_AT) ) var notes = Composer( - noteRandom = Random(seed), - rhythmRandom = Random(startedAt / 20), + noteRandom = Random(noteSeed), + rhythmRandom = Random(rhythmSeed), instrument = level.getBlockState(pos).getValue(BlockStateProperties.NOTEBLOCK_INSTRUMENT) ).composeMusic().toMutableList() val totalTime = notes.sumOf { it.time }.toLong() @@ -112,7 +120,8 @@ class MusicPlayer(val seed: Long, val level: Level, val pos: BlockPos, val start } fun toTag() = CompoundTag().apply { - putLong(TAG_SEED, seed) + putLong(TAG_NOTE_SEED, noteSeed) + putLong(TAG_RHYTHM_SEED, rhythmSeed) putLong(TAG_STARTED_AT, startedAt) } diff --git a/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt b/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt index 3e38034..78077cc 100644 --- a/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt +++ b/common/src/main/kotlin/quaedam/projection/music/SmartInstrument.kt @@ -164,10 +164,10 @@ 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 { + try { player = MusicPlayer(tag.getCompound(TAG_MUSIC), level!!, blockPos) + } catch (e: Throwable) { + playerData = tag.getCompound(TAG_MUSIC) } } } @@ -185,7 +185,7 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) : fun startMusic(force: Boolean = false, synced: Boolean = false) { if ((player == null || force) && !level!!.isClientSide && checkProjections()) { - player = MusicPlayer(level!!.random.nextLong(), level!!, blockPos) + player = MusicPlayer(level!!.random.nextLong(), level!!.gameTime / 20, level!!, blockPos) setChanged() sendBlockUpdated() if (!synced) {