From 77c4204aa3cde59eaa07926e6b9405107946fcab Mon Sep 17 00:00:00 2001 From: ishland Date: Thu, 11 Jul 2024 16:42:19 +0800 Subject: [PATCH] fix: prevent normal tracker from tracking staging area Fixes #68 --- .../common/playerwatching/NearbyEntityTracking.java | 12 +++++++++++- .../IThreadedAnvilChunkStorageTicketManager.java | 13 +++++++++++++ .../MixinThreadedAnvilChunkStorage.java | 6 ------ src/main/resources/vmp.mixins.json | 1 + 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/ishland/vmp/mixins/access/IThreadedAnvilChunkStorageTicketManager.java diff --git a/src/main/java/com/ishland/vmp/common/playerwatching/NearbyEntityTracking.java b/src/main/java/com/ishland/vmp/common/playerwatching/NearbyEntityTracking.java index 160d5ee..a7b596f 100644 --- a/src/main/java/com/ishland/vmp/common/playerwatching/NearbyEntityTracking.java +++ b/src/main/java/com/ishland/vmp/common/playerwatching/NearbyEntityTracking.java @@ -4,7 +4,9 @@ import com.ishland.vmp.common.maps.AreaMap; import com.ishland.vmp.common.playerwatching.compat.EntityPositionTransformer; import com.ishland.vmp.common.util.SimpleObjectPool; +import com.ishland.vmp.mixins.access.IThreadedAnvilChunkStorage; import com.ishland.vmp.mixins.access.IThreadedAnvilChunkStorageEntityTracker; +import com.ishland.vmp.mixins.access.IThreadedAnvilChunkStorageTicketManager; import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet; import it.unimi.dsi.fastutil.objects.ObjectListIterator; import it.unimi.dsi.fastutil.objects.Reference2LongMap; @@ -70,6 +72,10 @@ public static void init() { private final ObjectLinkedOpenHashSet stagingTrackers = new ObjectLinkedOpenHashSet<>(); private void addEntityTrackerAreaMap(ServerChunkLoadingManager.EntityTracker tracker) { + if (((IThreadedAnvilChunkStorageEntityTracker) tracker).getEntity() instanceof ServerPlayerEntity player) { + this.addPlayer(player); + } + // update is done lazily on next tickEntityMovement final ChunkPos pos = getEntityChunkPos(((IThreadedAnvilChunkStorageEntityTracker) tracker).getEntity()); this.areaMap.add( @@ -93,6 +99,10 @@ public void addEntityTracker(ServerChunkLoadingManager.EntityTracker tracker) { } public void removeEntityTracker(ServerChunkLoadingManager.EntityTracker tracker) { + if (((IThreadedAnvilChunkStorageEntityTracker) tracker).getEntity() instanceof ServerPlayerEntity player) { + this.removePlayer(player); + } + // remove from staging if (this.stagingTrackers.remove(new StagedTracker(tracker, 0L))) { // 0L is a dummy value (not used in equals(..) and hashCode()) tracker.stopTracking(); @@ -199,7 +209,7 @@ private void tickStaging(ServerChunkLoadingManager.TicketManager ticketManager) } } - final List players = List.copyOf(this.playerTrackers.keySet()); + final List players = ((IThreadedAnvilChunkStorage) ((IThreadedAnvilChunkStorageTicketManager) ticketManager).getField_17443()).getWorld().getPlayers(); for(StagedTracker staged : this.stagingTrackers) { final ServerChunkLoadingManager.EntityTracker entityTracker = staged.tracker(); ChunkSectionPos chunkSectionPos = ((IThreadedAnvilChunkStorageEntityTracker) entityTracker).getTrackedSection(); diff --git a/src/main/java/com/ishland/vmp/mixins/access/IThreadedAnvilChunkStorageTicketManager.java b/src/main/java/com/ishland/vmp/mixins/access/IThreadedAnvilChunkStorageTicketManager.java new file mode 100644 index 0000000..f838ab7 --- /dev/null +++ b/src/main/java/com/ishland/vmp/mixins/access/IThreadedAnvilChunkStorageTicketManager.java @@ -0,0 +1,13 @@ +package com.ishland.vmp.mixins.access; + +import net.minecraft.server.world.ServerChunkLoadingManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(ServerChunkLoadingManager.TicketManager.class) +public interface IThreadedAnvilChunkStorageTicketManager { + + @Accessor + ServerChunkLoadingManager getField_17443(); + +} diff --git a/src/main/java/com/ishland/vmp/mixins/playerwatching/optimize_nearby_entity_tracking_lookups/MixinThreadedAnvilChunkStorage.java b/src/main/java/com/ishland/vmp/mixins/playerwatching/optimize_nearby_entity_tracking_lookups/MixinThreadedAnvilChunkStorage.java index 9372062..ec27c82 100644 --- a/src/main/java/com/ishland/vmp/mixins/playerwatching/optimize_nearby_entity_tracking_lookups/MixinThreadedAnvilChunkStorage.java +++ b/src/main/java/com/ishland/vmp/mixins/playerwatching/optimize_nearby_entity_tracking_lookups/MixinThreadedAnvilChunkStorage.java @@ -32,9 +32,6 @@ public class MixinThreadedAnvilChunkStorage { @Redirect(method = "loadEntity", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerChunkLoadingManager$EntityTracker;updateTrackedStatus(Ljava/util/List;)V")) private void redirectUpdateOnAddEntity(ServerChunkLoadingManager.EntityTracker instance, List players) { - if (((IThreadedAnvilChunkStorageEntityTracker) instance).getEntity() instanceof ServerPlayerEntity player) { - this.nearbyEntityTracking.addPlayer(player); - } this.nearbyEntityTracking.addEntityTracker(instance); } @@ -52,9 +49,6 @@ private ObjectCollection nullifyTrackerListOnRemoveEntity(Int2ObjectMap