Skip to content

Commit

Permalink
fix: prevent normal tracker from tracking staging area
Browse files Browse the repository at this point in the history
Fixes #68
  • Loading branch information
ishland committed Jul 11, 2024
1 parent 2500a90 commit 77c4204
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +72,10 @@ public static void init() {
private final ObjectLinkedOpenHashSet<StagedTracker> 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(
Expand All @@ -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();
Expand Down Expand Up @@ -199,7 +209,7 @@ private void tickStaging(ServerChunkLoadingManager.TicketManager ticketManager)
}
}

final List<ServerPlayerEntity> players = List.copyOf(this.playerTrackers.keySet());
final List<ServerPlayerEntity> players = ((IThreadedAnvilChunkStorage) ((IThreadedAnvilChunkStorageTicketManager) ticketManager).getField_17443()).getWorld().getPlayers();
for(StagedTracker staged : this.stagingTrackers) {
final ServerChunkLoadingManager.EntityTracker entityTracker = staged.tracker();
ChunkSectionPos chunkSectionPos = ((IThreadedAnvilChunkStorageEntityTracker) entityTracker).getTrackedSection();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerPlayerEntity> players) {
if (((IThreadedAnvilChunkStorageEntityTracker) instance).getEntity() instanceof ServerPlayerEntity player) {
this.nearbyEntityTracking.addPlayer(player);
}
this.nearbyEntityTracking.addEntityTracker(instance);
}

Expand All @@ -52,9 +49,6 @@ private <T> ObjectCollection<T> nullifyTrackerListOnRemoveEntity(Int2ObjectMap<T

@Redirect(method = "unloadEntity", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerChunkLoadingManager$EntityTracker;stopTracking()V"))
private void redirectUpdateOnRemoveEntity(ServerChunkLoadingManager.EntityTracker instance) {
if (((IThreadedAnvilChunkStorageEntityTracker) instance).getEntity() instanceof ServerPlayerEntity player) {
this.nearbyEntityTracking.removePlayer(player);
}
this.nearbyEntityTracking.removeEntityTracker(instance);
instance.stopTracking();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/vmp.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"access.ISpreadPlayersCommandPile",
"access.IThreadedAnvilChunkStorage",
"access.IThreadedAnvilChunkStorageEntityTracker",
"access.IThreadedAnvilChunkStorageTicketManager",
"access.ITrackedPosition",
"carpet.MixinEntityPlayerMPFake",
"chunk.iteration.MixinServerChunkManager",
Expand Down

0 comments on commit 77c4204

Please sign in to comment.