Skip to content

Commit

Permalink
fix: undo
Browse files Browse the repository at this point in the history
recordContainer置null,并用更根本的方式修改了同时修改的异常
  • Loading branch information
zly2006 committed Aug 28, 2023
1 parent b7dc107 commit f752f4c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.zly2006.reden.access

import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper
import org.jetbrains.annotations.TestOnly

class UndoRecordContainerImpl : UndoRecordContainer {
override var recording: PlayerData.UndoRecord? = null
var id: Long
get() = recording?.id ?: 0
set(value) { recording = UpdateMonitorHelper.undoRecordsMap[value] }
@TestOnly @JvmField internal var swapped = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private void afterAffectWorld(boolean particles, CallbackInfo ci) {
DebugKt.debugLogger.invoke("Explosion affect world end, undoID=" + undoId);
if (undoId != 0) {
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
recordContainer.setRecording(null);
}
}

Expand All @@ -67,6 +68,7 @@ private void afterDamageEntities(CallbackInfo ci) {
DebugKt.debugLogger.invoke("Explosion damage entities end, undoID=" + undoId);
if (undoId != 0) {
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
recordContainer.setRecording(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public <T extends PistonBlockEntity> BlockEntityTicker<T> getTicker(World world,
if (be instanceof UndoableAccess access) {
DebugKt.debugLogger.invoke("After piston block entity tick: " + pos.toShortString() + ", id" + access.getUndoId());
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
recordContainer.setRecording(null);
}
}
} : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private <T> void onRunSchedule(BiConsumer<BlockPos, T> ticker, CallbackInfo ci,
private void afterRunSchedule(CallbackInfo ci) {
DebugKt.debugLogger.invoke("scheduled tick finished, removing it from record");
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
recordContainer.setRecording(null);
}
@Inject(
method = "scheduleTick",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ private void beforeProcessBlockEvent(BlockEvent event, CallbackInfoReturnable<Bo
private void afterProcessBlockEvent(BlockEvent event, CallbackInfoReturnable<Boolean> cir) {
DebugKt.debugLogger.invoke("block event end");
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
recordContainer.setRecording(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,9 @@ private void beforeExplode(CallbackInfo ci) {
private void afterExplode(CallbackInfo ci) {
DebugKt.debugLogger.invoke("TNT explode end, undoId=" + undoId);
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
recordContainer.setRecording(null);
}

/*
@Inject(method = "explode", at = @At("HEAD"), cancellable = true)
private void onExplode(CallbackInfo ci) {
Explosion explosion = new Explosion(
this.getWorld(),
this,
null,
null,
this.getX(),
this.getBodyY(0.0625),
this.getZ(),
4.0f,
false,
this.getWorld().getDestructionType(GameRules.TNT_EXPLOSION_DROP_DECAY)
);
if (undoId != 0) {
DebugKt.debugLogger.invoke("TNT exploded, adding it into record "+ undoId);
((UndoableAccess) explosion).setUndoId(undoId);
}
explosion.collectBlocksAndDamageEntities();
explosion.affectWorld(true);
ci.cancel();
}
*/

@Override
public long getUndoId() {
return undoId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.zly2006.reden.mixinhelper
import com.github.zly2006.reden.access.PlayerData
import com.github.zly2006.reden.access.PlayerData.Companion.data
import com.github.zly2006.reden.access.UndoRecordContainer
import com.github.zly2006.reden.access.UndoRecordContainerImpl
import com.github.zly2006.reden.carpet.RedenCarpetSettings
import com.github.zly2006.reden.utils.debugLogger
import com.github.zly2006.reden.utils.server
Expand Down Expand Up @@ -67,6 +68,17 @@ object UpdateMonitorHelper: UndoRecordContainer {
}
}

var depth = 0; private set
override fun swap(another: UndoRecordContainer) {
if (another is UndoRecordContainerImpl) {
if (another.swapped) depth--
else depth++
another.swapped = !another.swapped
}
debugLogger("swap, depth=$depth")
super.swap(another)
}

@JvmStatic
fun onChainFinish(world: World) {
//debugLogger("UpdateMonitorHelper.finish")
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/github/zly2006/reden/network/Rollback.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.zly2006.reden.network

import com.github.zly2006.reden.Reden
import com.github.zly2006.reden.access.PlayerData
import com.github.zly2006.reden.access.PlayerData.Companion.data
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper
Expand All @@ -19,7 +20,6 @@ import net.minecraft.registry.Registries
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.Text
import net.minecraft.util.math.BlockPos
import okhttp3.internal.toImmutableMap

private val pType = PacketType.create(ROLLBACK) {
Rollback(it.readVarInt())
Expand All @@ -46,8 +46,7 @@ class Rollback(
world.getBlockEntity(BlockPos.fromLong(pos))?.readNbt(be)
}
}
// 这里不用forEach是因为要避免ConcurrentModificationException,堆屎山ing
record.entities.toImmutableMap().forEach {
record.entities.forEach {
if (it.value != null) {
val entry = it.value!!
val entity = world.getEntity(it.key)
Expand Down Expand Up @@ -83,6 +82,11 @@ class Rollback(
return@registerGlobalReceiver
}
UpdateMonitorHelper.playerStopRecording(player)
if (UpdateMonitorHelper.recording != null) {
Reden.LOGGER.warn("Undo when a record is still active, id=" + UpdateMonitorHelper.recording?.id)
// 不取消跟踪会导致undo的更改也被记录,边读边写异常
UpdateMonitorHelper.recording = null
}
when (packet.status) {
0 -> view.undo.lastValid()?.let { undoRecord ->
view.undo.removeLast()
Expand Down

0 comments on commit f752f4c

Please sign in to comment.