Skip to content

Commit

Permalink
feat: entity undo
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Aug 14, 2023
1 parent 0d5b078 commit 33486df
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import java.text.SimpleDateFormat

plugins {
id 'fabric-loom' version '1.2-SNAPSHOT'
id 'maven-publish'
id "org.ajoberstar.grgit" version "4.1.0"
id 'org.jetbrains.kotlin.jvm' version '1.8.10'
}

version = project.mod_version
version = project.mod_version + "+" + new SimpleDateFormat("MMddhhmm").format(new Date())
group = project.maven_group

repositories {
Expand Down Expand Up @@ -33,7 +35,7 @@ dependencies {
testImplementation "net.fabricmc:fabric-loader-junit:${project.loader_version}"
include(implementation("org.bouncycastle:bcprov-jdk18on:1.76"))
include(implementation("org.bouncycastle:bcpg-jdk18on:1.76"))
modImplementation "curse.maven:litematica-308892:4626718"
modRuntimeOnly "curse.maven:litematica-308892:4626718"
}

test {
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/com/github/zly2006/reden/access/PlayerData.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.zly2006.reden.access

import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper
import net.minecraft.command.EntitySelector
import net.minecraft.entity.EntityType
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtHelper
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import java.util.*


class PlayerData(
Expand All @@ -23,14 +26,33 @@ class PlayerData(

data class Entry(
val blockState: NbtCompound,
val blockEntity: NbtCompound?
val blockEntity: NbtCompound?,
val entities: MutableMap<UUID, EntityEntry> = hashMapOf()
) {
class EntityEntry(
val entity: EntityType<*>,
val nbt: NbtCompound,
val pos: BlockPos
)

companion object {
fun fromWorld(world: World, pos: BlockPos): Entry {
return Entry(
NbtHelper.fromBlockState(world.getBlockState(pos)),
world.getBlockEntity(pos)?.createNbt()
)
).apply {
if (world.getBlockState(pos).getCollisionShape(world, pos).boundingBoxes.size != 0) {
val list = world.getEntitiesByType(
EntitySelector.PASSTHROUGH_FILTER,
world.getBlockState(pos).getCollisionShape(world, pos).boundingBox
.offset(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble())
.expand(0.1),
) { true }
list.forEach {
entities[it.uuid] = EntityEntry(it.type, NbtCompound().apply(it::writeNbt), it.blockPos)
}
}
}
}
}
}
Expand All @@ -49,7 +71,8 @@ class PlayerData(
val data: MutableMap<Long, Entry>
) {
fun getMemorySize() = data.asSequence()
.map { it.value.blockState.sizeInBytes + (it.value.blockEntity?.sizeInBytes ?: 0) }
.map { it.value }
.map { it.blockState.sizeInBytes + (it.blockEntity?.sizeInBytes ?: 0) + it.entities.map { it.value.nbt.sizeInBytes }.sum() }
.sum()
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/github/zly2006/reden/mixin/undo/MixinEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.zly2006.reden.mixin.undo;

import com.github.zly2006.reden.mixinhelper.UpdateMonitorHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.MovementType;
import net.minecraft.util.math.Vec3d;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Entity.class)
public class MixinEntity {
@Inject(method = "move", at = @At("HEAD"))
private void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
UpdateMonitorHelper.tryAddRelatedEntity((Entity) (Object) this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import com.github.zly2006.reden.access.PlayerData
import com.github.zly2006.reden.access.PlayerData.Companion.data
import com.github.zly2006.reden.carpet.CarpetSettings
import com.github.zly2006.reden.malilib.DEBUG_LOGGER
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.client.MinecraftClient
import net.minecraft.entity.Entity
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.server.world.ServerWorld
import net.minecraft.text.Text
Expand All @@ -19,6 +21,12 @@ object UpdateMonitorHelper {
private val chainFinishListeners = mutableMapOf<World.() -> Unit, LifeTime>()
private var recordId = 20060210L
val undoRecordsMap: MutableMap<Long, PlayerData.UndoRecord> = HashMap()
data class Changed(
val record: PlayerData.UndoRecord,
val pos: BlockPos
)
var lastTickChanged: MutableSet<Changed> = hashSetOf(); private set
var thisTickChanged: MutableSet<Changed> = hashSetOf(); private set

/**
* 非常非常危险的变量,如果没有十足把握请不要直接操作
Expand Down Expand Up @@ -134,7 +142,17 @@ object UpdateMonitorHelper {
private fun playerQuit(player: ServerPlayerEntity) =
player.data().undo.forEach { removeRecord(it.id) }

@JvmStatic
fun tryAddRelatedEntity(entity: Entity) {
if (entity.noClip) return
if (entity is ServerPlayerEntity) return
}

init {
ServerPlayConnectionEvents.DISCONNECT.register { handler, _ -> playerQuit(handler.player) }
ServerTickEvents.START_SERVER_TICK.register {
lastTickChanged = thisTickChanged
thisTickChanged = hashSetOf()
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/zly2006/reden/network/Rollback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.fabricmc.fabric.api.networking.v1.FabricPacket
import net.fabricmc.fabric.api.networking.v1.PacketType
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
import net.minecraft.block.Block
import net.minecraft.entity.SpawnReason
import net.minecraft.nbt.NbtHelper
import net.minecraft.network.PacketByteBuf
import net.minecraft.registry.Registries
Expand Down Expand Up @@ -55,6 +56,10 @@ class Rollback(
entry.blockEntity?.let { be ->
player.world.getBlockEntity(BlockPos.fromLong(pos))?.readNbt(be)
}
entry.entities.forEach {
val entity = player.serverWorld.getEntity(it.key)?.readNbt(it.value.nbt)
?: it.value.entity.spawn(player.serverWorld, it.value.nbt, null, it.value.pos, SpawnReason.COMMAND, false, false)
}
}
return ret
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/reden.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ extendable class net/minecraft/world/tick/OrderedTick
extendable class net/minecraft/server/world/BlockEvent
accessible method net/minecraft/world/World getDestructionType (Lnet/minecraft/world/GameRules$Key;)Lnet/minecraft/world/explosion/Explosion$DestructionType;
accessible field net/minecraft/world/chunk/Chunk heightmaps Ljava/util/Map;
accessible field net/minecraft/command/EntitySelector PASSTHROUGH_FILTER Lnet/minecraft/util/TypeFilter;
1 change: 1 addition & 0 deletions src/main/resources/reden.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"tick.MixinWatchdog",
"undo.MixinBlockEvent",
"undo.MixinCommands",
"undo.MixinEntity",
"undo.MixinExplosion",
"undo.MixinPlayerMode",
"undo.MixinSchedule",
Expand Down

0 comments on commit 33486df

Please sign in to comment.