From 6f11d1f698357b8cf8bccf7e443db9d4a8944543 Mon Sep 17 00:00:00 2001 From: Liyan Zhao Date: Fri, 15 Dec 2023 22:26:30 +0800 Subject: [PATCH] fix: undo modify time #63 --- .../java/com/github/zly2006/reden/network/Undo.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/zly2006/reden/network/Undo.kt b/src/main/java/com/github/zly2006/reden/network/Undo.kt index 3ec2b55..c66c5f2 100644 --- a/src/main/java/com/github/zly2006/reden/network/Undo.kt +++ b/src/main/java/com/github/zly2006/reden/network/Undo.kt @@ -37,17 +37,22 @@ class Undo( val pType = PacketType.create(id) { Undo(it.readVarInt()) } - private fun operate(world: ServerWorld, record: PlayerData.UndoRedoRecord, redoRecord: PlayerData.RedoRecord?) { + private fun operate(world: ServerWorld, record: PlayerData.UndoRedoRecord, redoRecord: PlayerData.RedoRecord?, isUndo: Boolean = true) { + debugLogger("undoing record ${record.id}, isUndo=$isUndo") record.data.forEach { (posLong, entry) -> val pos = BlockPos.fromLong(posLong) debugLogger("undo ${pos}, ${entry.state}") // set block val sec = world.getChunk(pos).run { getSection(getSectionIndex(pos.y)) } as ChunkSectionInterface - if (sec.`getModifyTime$reden`(pos) < entry.time) { + if (sec.`getModifyTime$reden`(pos) < entry.time && isUndo) { debugLogger("undo $pos skipped (${sec.`getModifyTime$reden`(pos)} < ${entry.time})") return@forEach } world.modified(pos, entry.time) + // two situations: + // if isUndo, the block is modified by the undo record, so we need to set the modify time to the undo record's time + // if isRedo, the block is modified by the player operations, so we need to set the modify time to the current time + // luckily, the redo record's time is the current time world.setBlockNoPP(pos, entry.state, Block.NOTIFY_LISTENERS) // clear schedules if (RedenCarpetSettings.Options.undoApplyingClearScheduledTicks) { @@ -156,7 +161,7 @@ class Undo( 1 -> view.redo.lastValid()?.let { view.redo.removeLast() server.execute { - operate(player.serverWorld, it, null) + operate(player.serverWorld, it, null, isUndo = false) view.undo.add(it.undoRecord) sendStatus(1) }