Skip to content

Commit

Permalink
feat: undo reason
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Sep 4, 2023
1 parent 1ca9e1e commit 491442a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/github/zly2006/reden/RedenClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void onInitializeClient() {
// Http IOs
ReportKt.initReport();
SponsorKt.updateSponsors();
}).start();
}, "Report Worker").start();
PearlTask.Companion.register();
InitializationHandler.getInstance().registerInitializationHandler(() -> {
ConfigManager.getInstance().registerConfigHandler("reden", new IConfigHandler() {
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/github/zly2006/reden/access/PlayerData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ 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.utils.debugLogger
import com.github.zly2006.reden.utils.isClient
import com.github.zly2006.reden.utils.isSinglePlayerAndCheating
import net.minecraft.block.Blocks
import net.minecraft.block.entity.BlockEntity
import net.minecraft.command.EntitySelector
import net.minecraft.entity.EntityType
Expand All @@ -14,6 +12,7 @@ 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
import net.minecraft.world.World
import java.util.*
Expand Down Expand Up @@ -65,10 +64,6 @@ class PlayerData(
be?.javaClass,
world.getBlockEntity(pos)?.createNbt()
).apply {
if (world.getBlockState(pos).isOf(Blocks.MOVING_PISTON) &&
blockEntity == null) {
debugLogger("OHHHHHHHHH")
}
if (world.getBlockState(pos).getCollisionShape(world, pos).boundingBoxes.size != 0) {
val list = world.getEntitiesByType(
EntitySelector.PASSTHROUGH_FILTER,
Expand All @@ -92,8 +87,21 @@ class PlayerData(
id: Long,
lastChangedTick: Int = 0,
entities: MutableMap<UUID, EntityEntry?> = mutableMapOf(),
data: MutableMap<Long, Entry> = mutableMapOf()
) : UndoRedoRecord(id, lastChangedTick, entities, data)
data: MutableMap<Long, Entry> = mutableMapOf(),
val cause: Cause = Cause.UNKNOWN
) : UndoRedoRecord(id, lastChangedTick, entities, data) {
enum class Cause(message: Text? = null) {
BREAK_BLOCK(Text.of("Break Block")),
USE_BLOCK(Text.of("Use Block")),
USE_ITEM(Text.of("Use Item")),
ATTACK_ENTITY(Text.of("Attack Entity")),
COMMAND(Text.of("Command")),
LITEMATICA_TASK(Text.of("Litematica Task")),
UNKNOWN(Text.of("Unknown"));

val message: Text = message ?: Text.translatable("reden.undo.cause.${name.lowercase()}")
}
}
class RedoRecord(
id: Long,
lastChangedTick: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.zly2006.reden.mixin.otherMods.litematica.undo;

import com.github.zly2006.reden.ModNames;
import com.github.zly2006.reden.access.PlayerData;
import com.github.zly2006.reden.malilib.MalilibSettingsKt;
import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import fi.dy.masa.litematica.scheduler.TaskScheduler;
Expand Down Expand Up @@ -34,7 +35,7 @@ private void beforeRunLitematicaTask(CallbackInfo ci) {
IntegratedServer server = mc.getServer();
if (mc.isIntegratedServerRunning() && server != null && MalilibSettingsKt.UNDO_SUPPORT_LITEMATICA_OPERATION.getBooleanValue()) {
//noinspection DataFlowIssue
UpdateMonitorHelper.playerStartRecording(server.getPlayerManager().getPlayer(server.localPlayerUuid));
UpdateMonitorHelper.playerStartRecording(server.getPlayerManager().getPlayer(server.localPlayerUuid), PlayerData.UndoRecord.Cause.LITEMATICA_TASK);
}
}
@Inject(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -24,8 +24,7 @@ public class MixinCommands {
)
private void onExecute(ParseResults<ServerCommandSource> parseResults, String command, CallbackInfoReturnable<Integer> cir) {
if (parseResults.getContext().getSource().getEntity() instanceof ServerPlayerEntity player) {
DebugKt.debugLogger.invoke("Start monitoring of CHAIN - Command");
UpdateMonitorHelper.playerStartRecording(player);
UpdateMonitorHelper.playerStartRecording(player, PlayerData.UndoRecord.Cause.COMMAND);
}
}

Expand All @@ -40,7 +39,6 @@ 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");
UpdateMonitorHelper.playerStopRecording(player);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager;
Expand Down Expand Up @@ -29,49 +29,41 @@ public class MixinPlayerMode {

@Inject(method = "tryBreakBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerWorld;removeBlock(Lnet/minecraft/util/math/BlockPos;Z)Z"))
private void onDestroy(BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
DebugKt.debugLogger.invoke("Start monitoring of CHAIN - Break block");
UpdateMonitorHelper.playerStartRecording(player);
UpdateMonitorHelper.playerStartRecording(player, PlayerData.UndoRecord.Cause.BREAK_BLOCK);
}

@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");
UpdateMonitorHelper.playerStopRecording(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;"))
private void onUseBlock(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
DebugKt.debugLogger.invoke("Start monitoring of CHAIN - Interact block");
UpdateMonitorHelper.playerStartRecording(player);
UpdateMonitorHelper.playerStartRecording(player, PlayerData.UndoRecord.Cause.USE_BLOCK);
}

@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");
UpdateMonitorHelper.playerStopRecording(player);
}

@Inject(method = "interactBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;"))
private void onUseItemOnBlock(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> cir) {
DebugKt.debugLogger.invoke("Start monitoring of CHAIN - Interact block with item");
UpdateMonitorHelper.playerStartRecording(player);
UpdateMonitorHelper.playerStartRecording(player, PlayerData.UndoRecord.Cause.USE_BLOCK);
}

@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");
UpdateMonitorHelper.playerStopRecording(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;"))
private void onUseItem(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
DebugKt.debugLogger.invoke("Start monitoring of CHAIN - Interact item");
UpdateMonitorHelper.playerStartRecording(player);
UpdateMonitorHelper.playerStartRecording(player, PlayerData.UndoRecord.Cause.USE_ITEM);
}

@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");
UpdateMonitorHelper.playerStopRecording(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ object UpdateMonitorHelper {
*
* 此缓存可能在没有确认的情况下不经检查直接调用
*/
private fun addRecord(): PlayerData.UndoRecord {
private fun addRecord(
cause: PlayerData.UndoRecord.Cause
): PlayerData.UndoRecord {
if (undoRecords.size != 0) {
throw IllegalStateException("Cannot add record when there is already one.")
}
val undoRecord = PlayerData.UndoRecord(
id = recordId,
lastChangedTick = server.ticks,
cause = cause
)
undoRecordsMap[recordId] = undoRecord
recordId++
Expand All @@ -85,15 +88,21 @@ object UpdateMonitorHelper {

internal fun removeRecord(id: Long) = undoRecordsMap.remove(id)

@Suppress("unused")
@JvmStatic
fun playerStartRecording(player: ServerPlayerEntity) {
fun playerStartRecording(player: ServerPlayerEntity) = playerStartRecording(player, PlayerData.UndoRecord.Cause.UNKNOWN)
@JvmStatic
fun playerStartRecording(
player: ServerPlayerEntity,
cause: PlayerData.UndoRecord.Cause
) {
val playerView = player.data()
if (!playerView.canRecord) return
if (!playerView.isRecording) {
playerView.isRecording = true
val record = addRecord()
val record = addRecord(cause)
playerView.undo.add(record)
pushRecord(record.id) { "player recording/${player.entityName}" }
pushRecord(record.id) { "player recording/${player.entityName}/$cause" }
}
}

Expand All @@ -102,7 +111,7 @@ object UpdateMonitorHelper {
val playerView = player.data()
if (playerView.isRecording) {
playerView.isRecording = false
popRecord { "player recording/${player.entityName}" }
popRecord { "player recording/${player.entityName}/${recording?.cause}" }
playerView.redo
.onEach { removeRecord(it.id) }
.clear()
Expand Down
25 changes: 6 additions & 19 deletions src/main/java/com/github/zly2006/reden/report/Report.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import com.google.gson.Gson
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.MinecraftVersion
import net.minecraft.client.MinecraftClient
import net.minecraft.client.network.MultiplayerServerListPinger
import net.minecraft.client.option.ServerList
import net.minecraft.text.ClickEvent
import net.minecraft.text.Text
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.net.http.HttpClient
import java.util.*
import java.util.concurrent.CompletableFuture

class ClientMetadataReq(
val online_mode: Boolean,
Expand All @@ -26,35 +23,25 @@ class ClientMetadataReq(
) {
class Server(
val name: String,
val ip: String,
val motd: String
val ip: String
)
}

fun initReport() {
val httpClient = HttpClient.newHttpClient()
val mc = MinecraftClient.getInstance()
val servers = ServerList(mc)
servers.loadFile()
val serverPinger = MultiplayerServerListPinger()
val metadata = ClientMetadataReq(
online_mode = mc.session.accessToken != "FabricMC",
uuid = mc.session.uuidOrNull,
name = mc.session.username,
mcversion = mc.gameVersion + " " + MinecraftVersion.create().name,
servers = (0 until servers.size()).map { servers[it] }.map {
val future = CompletableFuture<ClientMetadataReq.Server>()
serverPinger.add(it) {
future.complete(
ClientMetadataReq.Server(
name = it.name,
ip = it.address,
motd = "${it.label}, online=${it.online}, players=${it.playerCountLabel}"
)
)
}
future
}.map { it.join() }
ClientMetadataReq.Server(
name = it.name,
ip = it.address
)
}
)
try {
if (!FabricLoader.getInstance().isDevelopmentEnvironment) {
Expand Down

0 comments on commit 491442a

Please sign in to comment.