From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samsuik <40902469+Samsuik@users.noreply.github.com> Date: Thu, 30 Nov 2023 15:54:49 +0000 Subject: [PATCH] Reduce entity tracker player updates diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java index 8a817dda325898b759de63ab6e4300b050286bf7..64bf4398a8a0b429e5a7483cf8a24a02c58b7fb3 100644 --- a/src/main/java/net/minecraft/server/level/ChunkMap.java +++ b/src/main/java/net/minecraft/server/level/ChunkMap.java @@ -1145,6 +1145,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider try { for (TrackedEntity tracker : this.entityMap.values()) { // update tracker entry + if (!tracker.shouldLookForPlayers()) continue; // Sakura - delay entities looking for nearby players tracker.updatePlayers(tracker.entity.getPlayersInTrackRange()); } } finally { @@ -1311,14 +1312,34 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider private final int range; SectionPos lastSectionPos; public final Set seenBy = new it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet<>(); // Paper - Perf: optimise map impl + private final int playerSearchInterval; // Sakura - reduce entity tracker player updates + private Vec3 entityPosition; // Sakura - reduce entity tracker player updates public TrackedEntity(Entity entity, int i, int j, boolean flag) { this.serverEntity = new ServerEntity(ChunkMap.this.level, entity, j, flag, this::broadcast, this.seenBy); // CraftBukkit this.entity = entity; this.range = i; this.lastSectionPos = SectionPos.of((EntityAccess) entity); + // Sakura start - reduce entities looking for nearby players + // Use a maximum of 20 ticks because stationary entities use Integer.MAX_VALUE + // which causes them to turn invisible after untracking. + this.playerSearchInterval = Math.min(j, 20); + this.entityPosition = entity.position(); } + public final boolean shouldLookForPlayers() { + // We have to always update players otherwise they can turn invisible on teleports (why?) + if (this.entity instanceof net.minecraft.world.entity.player.Player || this.entity.tickCount % this.playerSearchInterval == 0) { + return true; + } + + Vec3 lastPosition = this.entityPosition; + this.entityPosition = this.entity.position(); + + return this.entity.position().distanceToSqr(lastPosition) >= (double) this.range / 2.0; + } + // Sakura end - reduce entities looking for nearby players + // Paper start - use distance map to optimise tracker com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet lastTrackerCandidates;