Skip to content

Commit

Permalink
Always remove entities from cache (GeyserMC#4577)
Browse files Browse the repository at this point in the history
* Always remove entities from cache

* Despawn throwable entities in the void
  • Loading branch information
AJ-Ferguson authored and XingLingQAQ committed Sep 25, 2024
1 parent a2a74e1 commit 5a23971
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,9 @@ public void addAdditionalSpawnData(AddEntityPacket addEntityPacket) {

/**
* Despawns the entity
*
* @return can be deleted
*/
public boolean despawnEntity() {
if (!valid) return true;
public void despawnEntity() {
if (!valid) return;

for (Entity passenger : passengers) { // Make sure all passengers on the despawned entity are updated
if (passenger == null) continue;
Expand All @@ -218,7 +216,6 @@ public boolean despawnEntity() {
session.sendUpstreamPacket(removeEntityPacket);

valid = false;
return true;
}

public void moveRelative(double relX, double relY, double relZ, float yaw, float pitch, boolean isOnGround) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ protected void moveAbsoluteImmediate(Vector3f position, float yaw, float pitch,

@Override
public void tick() {
if (removedInVoid()) {
return;
}
moveAbsoluteImmediate(tickMovement(position), getYaw(), getPitch(), getHeadYaw(), false, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ private void sendSplashSound(GeyserSession session) {

@Override
public void tick() {
if (removedInVoid()) {
return;
}
if (hooked || !isInAir() && !isInWater() || isOnGround()) {
motion = Vector3f.ZERO;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void spawnEntity() {

@Override
public void tick() {
if (isInWater()) {
if (removedInVoid() || isInWater()) {
return;
}
if (!isOnGround() || (motion.getX() * motion.getX() + motion.getZ() * motion.getZ()) > 0.00001) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void setItemRotation(IntEntityMetadata entityMetadata) {
}

@Override
public boolean despawnEntity() {
public void despawnEntity() {
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
updateBlockPacket.setDataLayer(0);
updateBlockPacket.setBlockPosition(bedrockPosition);
Expand All @@ -161,7 +161,6 @@ public boolean despawnEntity() {
session.getItemFrameCache().remove(bedrockPosition, this);

valid = false;
return true;
}

private NbtMap getDefaultTag() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public ThrowableEntity(GeyserSession session, int entityId, long geyserId, UUID
*/
@Override
public void tick() {
if (removedInVoid()) {
return;
}
moveAbsoluteImmediate(position.add(motion), getYaw(), getPitch(), getHeadYaw(), isOnGround(), false);
float drag = getDrag();
float gravity = getGravity();
Expand Down Expand Up @@ -170,14 +173,14 @@ protected boolean isInWater() {
}

@Override
public boolean despawnEntity() {
public void despawnEntity() {
if (definition.entityType() == EntityType.ENDER_PEARL) {
LevelEventPacket particlePacket = new LevelEventPacket();
particlePacket.setType(LevelEvent.PARTICLE_TELEPORT);
particlePacket.setPosition(position);
session.sendUpstreamPacket(particlePacket);
}
return super.despawnEntity();
super.despawnEntity();
}

@Override
Expand All @@ -191,4 +194,17 @@ public void moveAbsolute(Vector3f position, float yaw, float pitch, float headYa
moveAbsoluteImmediate(position, yaw, pitch, headYaw, isOnGround, teleported);
lastJavaPosition = position;
}

/**
* Removes the entity if it is 64 blocks below the world.
*
* @return true if the entity was removed
*/
public boolean removedInVoid() {
if (position.getY() < session.getDimensionType().minY() - 64) {
session.getEntityCache().removeEntity(this);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ public void spawnEntity() {
}

@Override
public boolean despawnEntity() {
public void despawnEntity() {
if (secondEntity != null) {
secondEntity.despawnEntity();
}
return super.despawnEntity();
super.despawnEntity();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ public void addAdditionalSpawnData(AddEntityPacket addEntityPacket) {
}

@Override
public boolean despawnEntity() {
public void despawnEntity() {
for (EnderDragonPartEntity part : allParts) {
part.despawnEntity();
}
return super.despawnEntity();
super.despawnEntity();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,29 @@ public boolean cacheEntity(Entity entity) {
return false;
}

public boolean removeEntity(Entity entity, boolean force) {
public void removeEntity(Entity entity) {
if (entity instanceof PlayerEntity player) {
session.getPlayerWithCustomHeads().remove(player.getUuid());
}

if (entity != null && entity.isValid() && (force || entity.despawnEntity())) {
if (entity != null) {
if (entity.isValid()) {
entity.despawnEntity();
}

long geyserId = entityIdTranslations.remove(entity.getEntityId());
entities.remove(geyserId);

if (entity instanceof Tickable) {
tickableEntities.remove(entity);
}
return true;
}
return false;
}

public void removeAllEntities() {
List<Entity> entities = new ArrayList<>(this.entities.values());
for (Entity entity : entities) {
removeEntity(entity, false);
removeEntity(entity);
}

session.getPlayerWithCustomHeads().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void translate(GeyserSession session, ClientboundRemoveEntitiesPacket pac
for (int entityId : packet.getEntityIds()) {
Entity entity = session.getEntityCache().getEntityByJavaId(entityId);
if (entity != null) {
session.getEntityCache().removeEntity(entity, false);
session.getEntityCache().removeEntity(entity);
}
}
}
Expand Down

0 comments on commit 5a23971

Please sign in to comment.