Skip to content

Commit

Permalink
Fix entity untracking when removeall
Browse files Browse the repository at this point in the history
  • Loading branch information
senseiwells committed Feb 1, 2024
1 parent a056a8d commit 9b92ad3
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.mojang.authlib.GameProfile;
import me.senseiwells.replay.config.ReplayConfig;
import me.senseiwells.replay.player.PlayerRecorder;
import me.senseiwells.replay.player.PlayerRecorders;
import me.senseiwells.replay.player.predicates.ReplayPlayerContext;
import me.senseiwells.replay.recorder.ReplayRecorder;
import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginPacketListenerImpl;
Expand Down Expand Up @@ -35,7 +35,7 @@ private void onLoggedIn(
) {
GameProfile profile = this.authenticatedProfile;
if (profile != null && ReplayConfig.getEnabled() && ReplayConfig.predicate.test(new ReplayPlayerContext(this.server, profile))) {
PlayerRecorder recorder = PlayerRecorders.create(this.server, profile);
ReplayRecorder recorder = PlayerRecorders.create(this.server, profile);
recorder.logStart();
recorder.afterLogin();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import me.senseiwells.replay.chunk.ChunkRecorder;
import me.senseiwells.replay.ducks.ServerReplay$ChunkRecordable;
import me.senseiwells.replay.recorder.ReplayRecorder;
import net.minecraft.network.protocol.Packet;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.server.level.ServerPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ private void onRemoved(CallbackInfo ci) {
public void replay$removeAllRecorders() {
ClientboundRemoveEntitiesPacket packet = new ClientboundRemoveEntitiesPacket(this.entity.getId());
for (ChunkRecorder recorder : this.replay$recorders) {
recorder.onEntityUntracked(this.entity);

recorder.record(packet);
}
this.replay$recorders.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.senseiwells.replay.mixin.studio;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.replaymod.replaystudio.viaversion.CustomViaPlatform;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.logging.Logger;

@Mixin(value = CustomViaPlatform.class, remap = false)
public class CustomViaPlatformMixin {
@ModifyReturnValue(
method = "getLogger",
at = @At("RETURN")
)
private Logger onGetLogger(Logger original) {
// We don't care, shut up, please :)
original.setFilter(log -> false);
return original;
}
}
26 changes: 6 additions & 20 deletions src/main/kotlin/me/senseiwells/replay/chunk/ChunkRecorder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import net.minecraft.server.level.ClientInformation
import net.minecraft.server.level.ServerLevel
import net.minecraft.server.level.ServerPlayer
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.boss.wither.WitherBoss
import net.minecraft.world.level.ChunkPos
import net.minecraft.world.level.levelgen.Heightmap
import net.minecraft.world.phys.Vec3
import org.jetbrains.annotations.ApiStatus.Internal
import java.nio.file.Path
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -83,20 +81,7 @@ class ChunkRecorder internal constructor(
}

override fun spawnPlayer() {
val center = this.chunks.center
this.record(ClientboundAddEntityPacket(
this.dummy.id,
this.dummy.uuid,
center.middleBlockX.toDouble(),
100.0,
center.middleBlockZ.toDouble(),
0.0F,
0.0F,
EntityType.PLAYER,
0,
Vec3.ZERO,
0.0
))
this.record(ClientboundAddEntityPacket(this.dummy))
val tracked = this.dummy.entityData.nonDefaultValues
if (tracked != null) {
this.record(ClientboundSetEntityDataPacket(this.dummy.id, tracked))
Expand Down Expand Up @@ -149,14 +134,16 @@ class ChunkRecorder internal constructor(
@Internal
fun onEntityTracked(entity: Entity) {
if (entity is WitherBoss) {
((entity as WitherBossAccessor).bossEvent as ChunkRecordable).addRecorder(this)
val recordable = ((entity as WitherBossAccessor).bossEvent as ChunkRecordable)
recordable.addRecorder(this)
}
}

@Internal
fun onEntityUntracked(entity: Entity) {
if (entity is WitherBoss) {
((entity as WitherBossAccessor).bossEvent as ChunkRecordable).removeRecorder(this)
val recordable = ((entity as WitherBossAccessor).bossEvent as ChunkRecordable)
recordable.removeRecorder(this)
}
}

Expand Down Expand Up @@ -216,8 +203,7 @@ class ChunkRecorder internal constructor(
return 0L
}

@Internal
internal fun paused(): Boolean {
private fun paused(): Boolean {
return this.lastPaused != 0L
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/me/senseiwells/replay/chunk/ChunkRecorders.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.senseiwells.replay.chunk

import me.senseiwells.replay.config.ReplayConfig
import me.senseiwells.replay.recorder.ReplayRecorder
import net.minecraft.resources.ResourceKey
import net.minecraft.server.MinecraftServer
import net.minecraft.server.level.ServerLevel
Expand All @@ -15,13 +16,13 @@ object ChunkRecorders {
private val closing = HashMap<String, CompletableFuture<Long>>()

@JvmStatic
fun create(level: ServerLevel, from: ChunkPos, to: ChunkPos, name: String): ChunkRecorder {
fun create(level: ServerLevel, from: ChunkPos, to: ChunkPos, name: String): ReplayRecorder {
return this.create(ChunkArea(level, from, to), name)
}

@JvmStatic
@JvmOverloads
fun create(area: ChunkArea, name: String = generateName(area)): ChunkRecorder {
fun create(area: ChunkArea, name: String = generateName(area)): ReplayRecorder {
if (this.chunks.containsKey(area)) {
throw IllegalArgumentException("Recorder for chunk area already exists")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ object ReplayCommand {
}
val recorder = ChunkRecorders.create(area, id)
recorder.tryStart()
context.source.sendSuccess({ Component.literal("Successfully started chunk replay: ${recorder.recorderName}") }, true)
context.source.sendSuccess({ Component.literal("Successfully started chunk replay: ${recorder.getName()}") }, true)
return 1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.senseiwells.replay.player

import com.mojang.authlib.GameProfile
import me.senseiwells.replay.config.ReplayConfig
import me.senseiwells.replay.recorder.ReplayRecorder
import me.senseiwells.replay.rejoin.RejoinedReplayPlayer
import net.minecraft.server.MinecraftServer
import net.minecraft.server.level.ServerPlayer
Expand All @@ -13,15 +14,15 @@ object PlayerRecorders {
private val closing = HashMap<UUID, CompletableFuture<Long>>()

@JvmStatic
fun create(player: ServerPlayer): PlayerRecorder {
fun create(player: ServerPlayer): ReplayRecorder {
if (player is RejoinedReplayPlayer) {
throw IllegalArgumentException("Cannot create a replay for a rejoining player")
}
return this.create(player.server, player.gameProfile)
}

@JvmStatic
fun create(server: MinecraftServer, profile: GameProfile): PlayerRecorder {
fun create(server: MinecraftServer, profile: GameProfile): ReplayRecorder {
if (this.players.containsKey(profile.id)) {
throw IllegalArgumentException("Player already has a recorder")
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/serverreplay.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"rejoin.ServerConfigurationPacketListenerImplAccessor",
"rejoin.ServerPlayerMixin",
"rejoin.TrackedEntityAccessor",
"studio.CustomViaPlatformMixin",
"studio.ZipReplayFileAccessor"
],
"injectors": {
Expand Down

0 comments on commit 9b92ad3

Please sign in to comment.