Skip to content

Commit

Permalink
refactor: undo stack
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Aug 31, 2023
1 parent 2cea96e commit 0fe3d76
Show file tree
Hide file tree
Showing 23 changed files with 152 additions and 167 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ group = project.maven_group

repositories {
mavenCentral()
maven {
url 'https://masa.dy.fi/maven'
}
maven {
url "https://cursemaven.com"
}
Expand All @@ -36,7 +39,7 @@ dependencies {
// MaLiLib, required on client
modImplementation "curse.maven:malilib-303119:4623483"
// carpet, required on client & server
modImplementation "curse.maven:carpet-349239:4573334"
modImplementation "carpet:fabric-carpet:1.20-1.4.112+v230608"
// Game test
testImplementation "net.fabricmc:fabric-loader-junit:${project.loader_version}"
// Conditional Mixin
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/github/zly2006/reden/access/PlayerData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.zly2006.reden.access

import com.github.zly2006.reden.carpet.RedenCarpetSettings
import com.github.zly2006.reden.malilib.UNDO_CHEATING_ONLY
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper
import com.github.zly2006.reden.utils.isClient
import com.github.zly2006.reden.utils.isSinglePlayerAndCheating
import net.minecraft.command.EntitySelector
Expand Down Expand Up @@ -32,10 +31,6 @@ class PlayerData(
var isRecording: Boolean = false
var pearlListening: Boolean = false

fun stopRecording(world: World) {
UpdateMonitorHelper.playerStopRecording(player)
}

data class Entry(
val blockState: NbtCompound,
val blockEntity: NbtCompound?,
Expand Down

This file was deleted.

This file was deleted.

6 changes: 5 additions & 1 deletion src/main/java/com/github/zly2006/reden/access/WorldData.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.github.zly2006.reden.access

import com.github.zly2006.reden.mixinhelper.BreakpointHelper
import net.minecraft.server.world.ServerWorld
import net.minecraft.world.World

class WorldData(serverWorld: ServerWorld) {
class WorldData(
val serverWorld: ServerWorld
) {
var status: Long = 0
val breakpointHelper = BreakpointHelper(serverWorld)
interface WorldDataAccess {
fun getRedenWorldData(): WorldData
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ public class RedenCarpetSettings {
strict = false
)
public static int allowedUndoSizeInBytes = 52428800;

@Rule(
categories = {CATEGORY_REDEN, RuleCategory.CREATIVE},
options = {"0", "100"},
strict = false
)
public static int tickBackMaxTicks = 100;

@Rule(
categories = {CATEGORY_REDEN, RuleCategory.CREATIVE}
)
public static boolean undoScheduledTicks = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private fun <T : IConfigBase?> ConfigBase<T>.debug() = this.apply(DEBUG_TAB::add
@JvmField val UNDO_REPORT_UN_TRACKED_TNT = RedenConfigBoolean("undoReportUnTrackedTnt", false).debug()
@JvmField val OPEN_GITHUB_AUTH_SCREEN = RedenConfigHotkey("openGithubAuthScreen", "R,G").debug().hotkey()
@JvmField val GITHUB_TOKEN = RedenConfigString("githubToken", "").debug()
@JvmField val ALLOW_SOCIAL_FOLLOW = RedenConfigBoolean("allowSocialFollow", true).debug()

fun ConfigHotkey.runCommand(commands: ConfigStringList) {
this.keybind.setCallback { _, _ ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.zly2006.reden.mixin.tickBack;

import carpet.commands.TickCommand;
import com.github.zly2006.reden.carpet.RedenCarpetSettings;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(value = TickCommand.class, remap = false)
public class MixinTickCommand {
@Redirect(
method = "register",
at = @At(
value = "INVOKE",
target = "Lcom/mojang/brigadier/arguments/IntegerArgumentType;integer(II)Lcom/mojang/brigadier/arguments/IntegerArgumentType;"
)
)
private static IntegerArgumentType onIntegerArg(int min, int max) {
if (min == 1 && max == 72000) {
return IntegerArgumentType.integer(-RedenCarpetSettings.tickBackMaxTicks, max);
} else {
return IntegerArgumentType.integer(min, max);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.zly2006.reden.mixin.undo;

import com.github.zly2006.reden.access.PlayerData;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import com.github.zly2006.reden.utils.DebugKt;
import com.mojang.brigadier.ParseResults;
Expand Down Expand Up @@ -42,10 +41,7 @@ private void onExecute(ParseResults<ServerCommandSource> parseResults, String co
private void afterExecute(ParseResults<ServerCommandSource> parseResults, String command, CallbackInfoReturnable<Integer> cir) {
if (parseResults.getContext().getSource().getEntity() instanceof ServerPlayerEntity player) {
DebugKt.debugLogger.invoke("Stop monitoring of CHAIN - Command");
PlayerData playerView = PlayerData.Companion.data(player);
if (playerView.isRecording()) {
playerView.stopRecording(player.getWorld());
}
UpdateMonitorHelper.playerStartRecord(player);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.reden.mixin.undo;

import com.github.zly2006.reden.access.PlayerData;
import com.github.zly2006.reden.access.UndoRecordContainerImpl;
import com.github.zly2006.reden.access.UndoableAccess;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import com.github.zly2006.reden.utils.DebugKt;
Expand All @@ -19,10 +18,7 @@
@SuppressWarnings("AddedMixinMembersNamePattern")
@Mixin(Explosion.class)
public class MixinExplosion implements UndoableAccess {
@Unique
UndoRecordContainerImpl recordContainer = new UndoRecordContainerImpl();
@Unique
long undoId;
@Unique long undoId;

@Inject(
method = "<init>(Lnet/minecraft/world/World;Lnet/minecraft/entity/Entity;Lnet/minecraft/entity/damage/DamageSource;Lnet/minecraft/world/explosion/ExplosionBehavior;DDDFZLnet/minecraft/world/explosion/Explosion$DestructionType;)V",
Expand All @@ -40,35 +36,31 @@ private void onInit(World world, Entity entity, DamageSource damageSource, Explo
private void beforeAffectWorld(boolean particles, CallbackInfo ci) {
DebugKt.debugLogger.invoke("Explosion affect world start, undoID=" + undoId);
if (undoId != 0) {
recordContainer.setId(undoId);
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
UpdateMonitorHelper.pushRecord(undoId);
}
}

@Inject(method = "affectWorld", at = @At("TAIL"))
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);
UpdateMonitorHelper.popRecord();
}
}

@Inject(method = "collectBlocksAndDamageEntities", at = @At("HEAD"))
private void beforeDamageEntities(CallbackInfo ci) {
DebugKt.debugLogger.invoke("Explosion damage entities start, undoID=" + undoId);
if (undoId != 0) {
recordContainer.setId(undoId);
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
UpdateMonitorHelper.pushRecord(undoId);
}
}

@Inject(method = "collectBlocksAndDamageEntities", at = @At("RETURN"))
private void afterDamageEntities(CallbackInfo ci) {
DebugKt.debugLogger.invoke("Explosion damage entities end, undoID=" + undoId);
if (undoId != 0) {
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
recordContainer.setRecording(null);
UpdateMonitorHelper.popRecord();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.zly2006.reden.mixin.undo;

import com.github.zly2006.reden.access.UndoRecordContainerImpl;
import com.github.zly2006.reden.access.UndoableAccess;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import com.github.zly2006.reden.utils.DebugKt;
Expand All @@ -13,12 +12,9 @@
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Unique;

@Mixin(PistonExtensionBlock.class)
public class MixinMovingPiston {
@Unique UndoRecordContainerImpl recordContainer = new UndoRecordContainerImpl();

/**
* @author zly2006
* @reason track undo, block entity tick is not the same time as block event tick
Expand All @@ -32,16 +28,14 @@ public <T extends PistonBlockEntity> BlockEntityTicker<T> getTicker(World world,
if (shouldTrack) {
if (be instanceof UndoableAccess access) {
DebugKt.debugLogger.invoke("Before piston block entity tick: " + pos.toShortString() + ", id" + access.getUndoId());
recordContainer.setId(access.getUndoId());
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
UpdateMonitorHelper.pushRecord(access.getUndoId());
}
}
PistonBlockEntity.tick(world1, pos, state1, be);
if (shouldTrack) {
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);
UpdateMonitorHelper.popRecord();
}
}
} : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.reden.mixin.undo;

import com.github.zly2006.reden.access.ChainedUpdaterView;
import com.github.zly2006.reden.access.PlayerData;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import com.github.zly2006.reden.utils.DebugKt;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -40,8 +39,7 @@ private void onDestroy(BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
@Inject(method = "tryBreakBlock", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/server/world/ServerWorld;removeBlock(Lnet/minecraft/util/math/BlockPos;Z)Z"))
private void afterDestroy(BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
DebugKt.debugLogger.invoke("Stop monitoring of CHAIN - Break block");
PlayerData playerView = PlayerData.Companion.data(player);
playerView.stopRecording(world);
UpdateMonitorHelper.playerStartRecord(player);
}

@Inject(method = "interactBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;onUse(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;Lnet/minecraft/util/hit/BlockHitResult;)Lnet/minecraft/util/ActionResult;"))
Expand All @@ -53,8 +51,7 @@ private void onUseBlock(ServerPlayerEntity player, World world, ItemStack stack,
@Inject(method = "interactBlock", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/block/BlockState;onUse(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;Lnet/minecraft/util/hit/BlockHitResult;)Lnet/minecraft/util/ActionResult;"))
private void afterUseBlock(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
DebugKt.debugLogger.invoke("Stop monitoring of CHAIN - Interact block");
PlayerData playerView = PlayerData.Companion.data(player);
playerView.stopRecording(world);
UpdateMonitorHelper.playerStartRecord(player);
}

@Inject(method = "interactBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;"))
Expand All @@ -66,8 +63,7 @@ private void onUseItemOnBlock(ServerPlayerEntity player, World world, ItemStack
@Inject(method = "interactBlock", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;"))
private void afterUseItemOnBlock(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
DebugKt.debugLogger.invoke("Stop monitoring of CHAIN - Interact block with item");
PlayerData playerView = PlayerData.Companion.data(player);
playerView.stopRecording(world);
UpdateMonitorHelper.playerStartRecord(player);
}

@Inject(method = "interactItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;use(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/TypedActionResult;"))
Expand All @@ -79,7 +75,6 @@ private void onUseItem(ServerPlayerEntity player, World world, ItemStack stack,
@Inject(method = "interactItem", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Lnet/minecraft/item/ItemStack;use(Lnet/minecraft/world/World;Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/Hand;)Lnet/minecraft/util/TypedActionResult;"))
private void afterUseItem(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
DebugKt.debugLogger.invoke("Stop monitoring of CHAIN - Interact item");
PlayerData playerView = PlayerData.Companion.data(player);
playerView.stopRecording(world);
UpdateMonitorHelper.playerStartRecord(player);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.github.zly2006.reden.mixin.undo;

import com.github.zly2006.reden.access.PlayerData;
import com.github.zly2006.reden.access.UndoRecordContainerImpl;
import com.github.zly2006.reden.access.UndoableAccess;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import com.github.zly2006.reden.utils.DebugKt;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.tick.OrderedTick;
import net.minecraft.world.tick.WorldTickScheduler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -19,9 +17,6 @@

@Mixin(WorldTickScheduler.class)
public class MixinSchedule {
@Unique
UndoRecordContainerImpl recordContainer = new UndoRecordContainerImpl();

@SuppressWarnings("rawtypes")
@Inject(
method = "tick(Ljava/util/function/BiConsumer;)V",
Expand All @@ -34,8 +29,7 @@ public class MixinSchedule {
private <T> void onRunSchedule(BiConsumer<BlockPos, T> ticker, CallbackInfo ci, OrderedTick orderedTick) {
long undoId = ((UndoableAccess) orderedTick).getUndoId();
DebugKt.debugLogger.invoke("Running scheduled tick at " + orderedTick.pos() + ", record=" + undoId);
recordContainer.setId(undoId);
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
UpdateMonitorHelper.pushRecord(undoId);
}
@Inject(
method = "tick(Ljava/util/function/BiConsumer;)V",
Expand All @@ -47,8 +41,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);
UpdateMonitorHelper.popRecord();
}
@Inject(
method = "scheduleTick",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.zly2006.reden.mixin.undo;

import com.github.zly2006.reden.access.PlayerData;
import com.github.zly2006.reden.access.UndoRecordContainerImpl;
import com.github.zly2006.reden.access.UndoableAccess;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import com.github.zly2006.reden.utils.DebugKt;
Expand All @@ -12,15 +11,13 @@
import net.minecraft.server.world.ServerWorld;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ServerWorld.class)
public abstract class MixinServerWorld {
@Unique UndoRecordContainerImpl recordContainer = new UndoRecordContainerImpl();
@Shadow public abstract void removePlayer(ServerPlayerEntity player, Entity.RemovalReason reason);

@Redirect(
Expand Down Expand Up @@ -51,8 +48,7 @@ private boolean onAddBlockEvent(ObjectLinkedOpenHashSet<BlockEvent> instance, Ob
private void beforeProcessBlockEvent(BlockEvent event, CallbackInfoReturnable<Boolean> cir) {
long undoId = ((UndoableAccess) event).getUndoId();
DebugKt.debugLogger.invoke("block event start at " + event.pos().toShortString() + event.block().toString() + ", data=" + event.data() + ", type=" + event.type() + ", record "+ undoId);
recordContainer.setId(undoId);
UpdateMonitorHelper.INSTANCE.swap(recordContainer);
UpdateMonitorHelper.pushRecord(undoId);
}
@Inject(
method = "processBlockEvent",
Expand All @@ -64,7 +60,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);
UpdateMonitorHelper.popRecord();
}
}
Loading

0 comments on commit 0fe3d76

Please sign in to comment.