Fix skull mix-up by not reusing skulls (#5206)

This commit is contained in:
Valaphee The Meerkat 2024-12-11 03:04:33 +01:00 committed by GitHub
parent 8b232d7900
commit b2045a5b3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 47 deletions

View file

@ -102,17 +102,6 @@ public class SkullPlayerEntity extends PlayerEntity {
session.sendUpstreamPacket(addPlayerPacket); session.sendUpstreamPacket(addPlayerPacket);
} }
/**
* Hide the player entity so that it can be reused for a different skull.
*/
public void free() {
setFlag(EntityFlag.INVISIBLE, true);
updateBedrockMetadata();
// Move skull entity out of the way
moveAbsolute(session.getPlayerEntity().getPosition().up(128), 0, 0, 0, false, true);
}
public void updateSkull(SkullCache.Skull skull) { public void updateSkull(SkullCache.Skull skull) {
skullPosition = skull.getPosition(); skullPosition = skull.getPosition();

View file

@ -50,20 +50,14 @@ import java.util.*;
public class SkullCache { public class SkullCache {
private final int maxVisibleSkulls; private final int maxVisibleSkulls;
private final boolean cullingEnabled; private final boolean cullingEnabled;
private final int skullRenderDistanceSquared; private final int skullRenderDistanceSquared;
/**
* The time in milliseconds before unused skull entities are despawned
*/
private static final long CLEANUP_PERIOD = 10000;
@Getter @Getter
private final Map<Vector3i, Skull> skulls = new Object2ObjectOpenHashMap<>(); private final Map<Vector3i, Skull> skulls = new Object2ObjectOpenHashMap<>();
private final List<Skull> inRangeSkulls = new ArrayList<>(); private final List<Skull> inRangeSkulls = new ArrayList<>();
private final Deque<SkullPlayerEntity> unusedSkullEntities = new ArrayDeque<>();
private int totalSkullEntities = 0; private int totalSkullEntities = 0;
private final GeyserSession session; private final GeyserSession session;
@ -188,43 +182,26 @@ public class SkullCache {
} }
} }
} }
// Occasionally clean up unused entities as we want to keep skull
// entities around for later use, to reduce "player" pop-in
if ((System.currentTimeMillis() - lastCleanup) > CLEANUP_PERIOD) {
lastCleanup = System.currentTimeMillis();
for (SkullPlayerEntity entity : unusedSkullEntities) {
entity.despawnEntity();
totalSkullEntities--;
}
unusedSkullEntities.clear();
}
} }
private void assignSkullEntity(Skull skull) { private void assignSkullEntity(Skull skull) {
if (skull.entity != null) { if (skull.entity != null) {
return; return;
} }
if (unusedSkullEntities.isEmpty()) { if (!cullingEnabled || totalSkullEntities < maxVisibleSkulls) {
if (!cullingEnabled || totalSkullEntities < maxVisibleSkulls) { // Create a new entity
// Create a new entity long geyserId = session.getEntityCache().getNextEntityId().incrementAndGet();
long geyserId = session.getEntityCache().getNextEntityId().incrementAndGet(); skull.entity = new SkullPlayerEntity(session, geyserId);
skull.entity = new SkullPlayerEntity(session, geyserId); skull.entity.spawnEntity();
skull.entity.spawnEntity();
skull.entity.updateSkull(skull);
totalSkullEntities++;
}
} else {
// Reuse an entity
skull.entity = unusedSkullEntities.removeFirst();
skull.entity.updateSkull(skull); skull.entity.updateSkull(skull);
totalSkullEntities++;
} }
} }
private void freeSkullEntity(Skull skull) { private void freeSkullEntity(Skull skull) {
if (skull.entity != null) { if (skull.entity != null) {
skull.entity.free(); skull.entity.despawnEntity();
unusedSkullEntities.addFirst(skull.entity); totalSkullEntities--;
skull.entity = null; skull.entity = null;
} }
} }
@ -250,10 +227,6 @@ public class SkullCache {
} }
skulls.clear(); skulls.clear();
inRangeSkulls.clear(); inRangeSkulls.clear();
for (SkullPlayerEntity skull : unusedSkullEntities) {
skull.despawnEntity();
}
unusedSkullEntities.clear();
totalSkullEntities = 0; totalSkullEntities = 0;
lastPlayerPosition = null; lastPlayerPosition = null;
} }