feat: save rhythm seed

This commit is contained in:
xtex 2023-07-30 11:20:52 +08:00
parent 1bd5965ba1
commit 3ce9081400
Signed by: xtex
GPG Key ID: B918086ED8045B91
2 changed files with 19 additions and 10 deletions

View File

@ -17,23 +17,31 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties
import quaedam.projector.Projector import quaedam.projector.Projector
import kotlin.random.Random 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 { 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" const val TAG_STARTED_AT = "StartedAt"
} }
constructor(tag: CompoundTag, level: Level, pos: BlockPos) : this( constructor(tag: CompoundTag, level: Level, pos: BlockPos) : this(
tag.getLong(TAG_SEED), tag.getLong(TAG_NOTE_SEED),
tag.getLong(TAG_RHYTHM_SEED),
level, level,
pos, pos,
tag.getLong(TAG_STARTED_AT) tag.getLong(TAG_STARTED_AT)
) )
var notes = Composer( var notes = Composer(
noteRandom = Random(seed), noteRandom = Random(noteSeed),
rhythmRandom = Random(startedAt / 20), rhythmRandom = Random(rhythmSeed),
instrument = level.getBlockState(pos).getValue(BlockStateProperties.NOTEBLOCK_INSTRUMENT) instrument = level.getBlockState(pos).getValue(BlockStateProperties.NOTEBLOCK_INSTRUMENT)
).composeMusic().toMutableList() ).composeMusic().toMutableList()
val totalTime = notes.sumOf { it.time }.toLong() 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 { fun toTag() = CompoundTag().apply {
putLong(TAG_SEED, seed) putLong(TAG_NOTE_SEED, noteSeed)
putLong(TAG_RHYTHM_SEED, rhythmSeed)
putLong(TAG_STARTED_AT, startedAt) putLong(TAG_STARTED_AT, startedAt)
} }

View File

@ -164,10 +164,10 @@ class SmartInstrumentBlockEntity(pos: BlockPos, state: BlockState) :
override fun load(tag: CompoundTag) { override fun load(tag: CompoundTag) {
super.load(tag) super.load(tag)
if (TAG_MUSIC in tag) { if (TAG_MUSIC in tag) {
if (level == null) { try {
playerData = tag.getCompound(TAG_MUSIC)
} else {
player = MusicPlayer(tag.getCompound(TAG_MUSIC), level!!, blockPos) 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) { fun startMusic(force: Boolean = false, synced: Boolean = false) {
if ((player == null || force) && !level!!.isClientSide && checkProjections()) { 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() setChanged()
sendBlockUpdated() sendBlockUpdated()
if (!synced) { if (!synced) {