Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix skull mix-up by not reusing skulls #5206

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,6 @@ public void spawnEntity() {
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) {
skullPosition = skull.getPosition();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,14 @@
public class SkullCache {
private final int maxVisibleSkulls;
private final boolean cullingEnabled;

private final int skullRenderDistanceSquared;

/**
* The time in milliseconds before unused skull entities are despawned
*/
private static final long CLEANUP_PERIOD = 10000;

@Getter
private final Map<Vector3i, Skull> skulls = new Object2ObjectOpenHashMap<>();

private final List<Skull> inRangeSkulls = new ArrayList<>();

private final Deque<SkullPlayerEntity> unusedSkullEntities = new ArrayDeque<>();
private int totalSkullEntities = 0;

private final GeyserSession session;
Expand Down Expand Up @@ -188,43 +182,26 @@ public void updateVisibleSkulls() {
}
}
}

// 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) {
if (skull.entity != null) {
return;
}
if (unusedSkullEntities.isEmpty()) {
if (!cullingEnabled || totalSkullEntities < maxVisibleSkulls) {
// Create a new entity
long geyserId = session.getEntityCache().getNextEntityId().incrementAndGet();
skull.entity = new SkullPlayerEntity(session, geyserId);
skull.entity.spawnEntity();
skull.entity.updateSkull(skull);
totalSkullEntities++;
}
} else {
// Reuse an entity
skull.entity = unusedSkullEntities.removeFirst();
if (!cullingEnabled || totalSkullEntities < maxVisibleSkulls) {
// Create a new entity
long geyserId = session.getEntityCache().getNextEntityId().incrementAndGet();
skull.entity = new SkullPlayerEntity(session, geyserId);
skull.entity.spawnEntity();
skull.entity.updateSkull(skull);
totalSkullEntities++;
}
}

private void freeSkullEntity(Skull skull) {
if (skull.entity != null) {
skull.entity.free();
unusedSkullEntities.addFirst(skull.entity);
skull.entity.despawnEntity();
totalSkullEntities--;
skull.entity = null;
}
}
Expand All @@ -250,10 +227,6 @@ public void clear() {
}
skulls.clear();
inRangeSkulls.clear();
for (SkullPlayerEntity skull : unusedSkullEntities) {
skull.despawnEntity();
}
unusedSkullEntities.clear();
totalSkullEntities = 0;
lastPlayerPosition = null;
}
Expand Down
Loading