Skip to content

Commit

Permalink
fix: cleanup dirty data on server start
Browse files Browse the repository at this point in the history
Signed-off-by: Liyan Zhao <[email protected]>
  • Loading branch information
zly2006 committed Apr 20, 2024
1 parent ba028fa commit 9aa9259
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/github/zly2006/reden/Reden.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.github.zly2006.reden.carpet.RedenCarpetSettings
import com.github.zly2006.reden.indexing.blockId
import com.github.zly2006.reden.indexing.entityId
import com.github.zly2006.reden.indexing.propertyId
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper
import com.github.zly2006.reden.network.registerChannels
import com.github.zly2006.reden.rvc.registerRvc
import com.github.zly2006.reden.transformers.ThisIsReden
Expand Down Expand Up @@ -82,7 +83,10 @@ class Reden : ModInitializer, CarpetExtension {

@OptIn(DelicateCoroutinesApi::class)
override fun onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register { server = it }
ServerLifecycleEvents.SERVER_STARTING.register {
UpdateMonitorHelper.cleanup()
server = it
}
registerChannels()
CarpetServer.manageExtension(this)
CommandRegistrationCallback.EVENT.register { dispatcher, access, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper.undoRecordsMap
import com.github.zly2006.reden.utils.debugLogger
import com.github.zly2006.reden.utils.isClient
import com.github.zly2006.reden.utils.server
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents
import net.minecraft.block.BlockState
import net.minecraft.entity.Entity
Expand Down Expand Up @@ -88,6 +87,14 @@ object UpdateMonitorHelper {
val undoRecordsMap: MutableMap<Long, PlayerData.UndoRecord> = HashMap()
internal val undoRecords = mutableListOf<UndoRecordEntry>()

/**
* Used for crash recovery.
*/
fun cleanup() {
undoRecordsMap.clear()
undoRecords.clear()
}

private fun filterLogById(undoId: Long) =
undoId != 0L || if (isClient) !DEBUG_LOGGER_IGNORE_UNDO_ID_0.booleanValue else true

Expand All @@ -114,12 +121,6 @@ object UpdateMonitorHelper {
}
return undoRecords.removeLast()
}
data class Changed(
val record: PlayerData.UndoRecord,
val pos: BlockPos
)
var lastTickChanged: MutableSet<Changed> = hashSetOf(); private set
var thisTickChanged: MutableSet<Changed> = hashSetOf(); private set
val recording: PlayerData.UndoRecord? get() = undoRecords.lastOrNull()?.record

/**
Expand Down Expand Up @@ -297,9 +298,5 @@ object UpdateMonitorHelper {

init {
ServerPlayConnectionEvents.DISCONNECT.register { handler, _ -> playerQuit(handler.player) }
ServerTickEvents.START_SERVER_TICK.register {
lastTickChanged = thisTickChanged
thisTickChanged = hashSetOf()
}
}
}

0 comments on commit 9aa9259

Please sign in to comment.