Skip to content

Commit

Permalink
feat(debug-command)
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Oct 17, 2023
1 parent 33fba65 commit f239761
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
12 changes: 11 additions & 1 deletion src/main/java/com/github/zly2006/reden/Reden.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import carpet.CarpetExtension;
import carpet.CarpetServer;
import com.github.zly2006.reden.access.PlayerData;
import com.github.zly2006.reden.carpet.RedenCarpetSettings;
import com.github.zly2006.reden.network.ChannelsKt;
import com.github.zly2006.reden.rvc.RvcCommandKt;
Expand Down Expand Up @@ -56,9 +57,18 @@ public void onInitialize() {
ChannelsKt.register();
CarpetServer.manageExtension(this);
CommandRegistrationCallback.EVENT.register((dispatcher, access, environment) -> {
boolean isDev = true;
// Debug command
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
if (FabricLoader.getInstance().isDevelopmentEnvironment() || isDev) {
dispatcher.register(CommandManager.literal("reden-debug")
.then(CommandManager.literal("top-undo").executes(context -> {
PlayerData.Companion.data(context.getSource().getPlayer()).topUndo();
return 1;
}))
.then(CommandManager.literal("top-redo").executes(context -> {
PlayerData.Companion.data(context.getSource().getPlayer()).topRedo();
return 1;
}))
.then(CommandManager.literal("delay-test")
.executes(context -> {
try {
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/com/github/zly2006/reden/access/PlayerData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import com.github.zly2006.reden.carpet.RedenCarpetSettings
import com.github.zly2006.reden.malilib.UNDO_CHEATING_ONLY
import com.github.zly2006.reden.utils.isClient
import com.github.zly2006.reden.utils.isSinglePlayerAndCheating
import net.minecraft.block.BlockState
import net.minecraft.command.EntitySelector
import net.minecraft.entity.EntityType
import net.minecraft.entity.TntEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtHelper
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.Text
import net.minecraft.util.math.BlockPos
Expand All @@ -20,6 +20,14 @@ import java.util.*
class PlayerData(
val player: ServerPlayerEntity,
) {
fun topRedo() {
player.sendMessage(Text.of(redo.lastOrNull()?.toString()))
}

fun topUndo() {
player.sendMessage(Text.of(undo.lastOrNull()?.toString()))
}

val canRecord: Boolean
get() = if (!isClient) {
RedenCarpetSettings.allowedUndoSizeInBytes != 0
Expand All @@ -32,12 +40,10 @@ class PlayerData(
var pearlListening: Boolean = false

data class Entry(
val blockState: NbtCompound,
val state: BlockState,
val blockEntity: NbtCompound?,
) {

fun getMemorySize() =
blockState.sizeInBytes + (blockEntity?.sizeInBytes ?: 0)
fun getMemorySize() = blockEntity?.sizeInBytes ?: 0
}
internal interface PlayerDataAccess {
fun getRedenPlayerData(): PlayerData
Expand All @@ -55,12 +61,20 @@ class PlayerData(
val entities: MutableMap<UUID, EntityEntry> = mutableMapOf(),
val data: MutableMap<Long, Entry> = mutableMapOf()
) {
override fun toString(): String {
return """
+ UndoRedoRecord id=$id
+ size = ${getMemorySize()}
+ entities:
${entities.map { "${it.key} = ${it.value}" }.joinToString("\n")}
+ blocks:
${data.map { "${BlockPos.fromLong(it.key).toShortString()} = ${it.value.state}" }.joinToString("\n")}
""".trimIndent()
}
fun fromWorld(world: World, pos: BlockPos): Entry {
val be = world.getBlockEntity(pos)
return Entry(
NbtHelper.fromBlockState(world.getBlockState(pos)),
world.getBlockEntity(pos)?.createNbtWithId()
).apply {
val state = world.getBlockState(pos)
return Entry(state, be?.createNbtWithId()).apply {
if (world.getBlockState(pos).getCollisionShape(world, pos).boundingBoxes.size != 0) {
val list = world.getEntitiesByType(
EntitySelector.PASSTHROUGH_FILTER,
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/github/zly2006/reden/debugger/EndStage.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.github.zly2006.reden.debugger

import com.github.zly2006.reden.utils.server

class EndStage(
parent: ServerRootStage
) : TickStage(name = "end", parent = parent) {
private val _parent: ServerRootStage
) : TickStage(name = "end", parent = _parent) {
override fun tick() {
server.runTasksTillTickEnd()
_parent.server.runTasksTillTickEnd()
}

fun waitAll() {
server.runTasks { false }
_parent.server.runTasks { false }
}
}
9 changes: 3 additions & 6 deletions src/main/java/com/github/zly2006/reden/network/Rollback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import net.minecraft.block.Block
import net.minecraft.block.entity.BlockEntity
import net.minecraft.entity.SpawnReason
import net.minecraft.entity.mob.MobEntity
import net.minecraft.nbt.NbtHelper
import net.minecraft.network.PacketByteBuf
import net.minecraft.registry.Registries
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.math.BlockPos
import net.minecraft.world.tick.ChunkTickScheduler
Expand All @@ -35,10 +33,9 @@ class Rollback(
private fun operate(world: ServerWorld, record: PlayerData.UndoRedoRecord, redoRecord: PlayerData.RedoRecord?) {
record.data.forEach { (posLong, entry) ->
val pos = BlockPos.fromLong(posLong)
val state = NbtHelper.toBlockState(Registries.BLOCK.readOnlyWrapper, entry.blockState)
debugLogger("undo ${BlockPos.fromLong(posLong)}, $state")
debugLogger("undo ${BlockPos.fromLong(posLong)}, ${entry.state}")
// set block
world.setBlockNoPP(pos, state, Block.NOTIFY_LISTENERS)
world.setBlockNoPP(pos, entry.state, Block.NOTIFY_LISTENERS)
// clear schedules
world.syncedBlockEventQueue.removeIf { it.pos == pos }
val blockTickScheduler = world.getChunk(pos).blockTickScheduler as ChunkTickScheduler
Expand All @@ -47,7 +44,7 @@ class Rollback(
fluidTickScheduler.removeTicksIf { it.pos == pos }
// apply block entity
entry.blockEntity?.let { beNbt ->
world.addBlockEntity(BlockEntity.createFromNbt(pos, state, beNbt))
world.addBlockEntity(BlockEntity.createFromNbt(pos, entry.state, beNbt))
}
}
record.entities.forEach {
Expand Down

0 comments on commit f239761

Please sign in to comment.