Skip to content

Commit

Permalink
chores: minor code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jan 22, 2025
1 parent 8ed3d6f commit f31752d
Showing 1 changed file with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.allaymc.server.world.service;

import io.netty.util.internal.PlatformDependent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.eventbus.event.entity.EntityDespawnEvent;
Expand All @@ -17,10 +16,14 @@
* @author Cool_Loong
*/
@Slf4j
@RequiredArgsConstructor
public class AllayEntityService implements EntityService {
protected final AllayEntityPhysicsService entityPhysicsService;
protected final Queue<EntityUpdateOperation> entityUpdateOperationQueue = PlatformDependent.newMpscQueue();
protected final Queue<EntityUpdateOperation> entityUpdateOperationQueue;

public AllayEntityService(AllayEntityPhysicsService entityPhysicsService) {
this.entityPhysicsService = entityPhysicsService;
this.entityUpdateOperationQueue = PlatformDependent.newMpscQueue();
}

public void tick() {
while (!entityUpdateOperationQueue.isEmpty()) {
Expand All @@ -47,8 +50,9 @@ private void removeEntityImmediately(Entity entity) {
entityPhysicsService.removeEntity(entity);

entity.despawnFromAll();
((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent()).setWillBeDespawnedNextTick(false);
((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent()).setSpawned(false);
var baseComponent = ((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent());
baseComponent.setWillBeDespawnedNextTick(false);
baseComponent.setSpawned(false);
}

private void addEntityImmediately(Entity entity) {
Expand All @@ -63,8 +67,9 @@ private void addEntityImmediately(Entity entity) {
entity.spawnTo(chunk.getPlayerChunkLoaders());

entityPhysicsService.addEntity(entity);
((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent()).setWillBeSpawnedNextTick(false);
((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent()).setSpawned(true);
var baseComponent = ((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent());
baseComponent.setWillBeSpawnedNextTick(false);
baseComponent.setSpawned(true);
}

@Override
Expand All @@ -75,11 +80,7 @@ public void addEntity(Entity entity, Runnable callback) {
}

((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent()).setWillBeSpawnedNextTick(true);
entityUpdateOperationQueue.add(new EntityUpdateOperation(
entity,
EntityUpdateType.ADD,
callback
));
entityUpdateOperationQueue.add(new EntityUpdateOperation(entity, EntityUpdateType.ADD, callback));
}

@Override
Expand All @@ -90,11 +91,7 @@ public void removeEntity(Entity entity, Runnable callback) {
}

((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent()).setWillBeDespawnedNextTick(true);
entityUpdateOperationQueue.add(new EntityUpdateOperation(
entity,
EntityUpdateType.REMOVE,
callback
));
entityUpdateOperationQueue.add(new EntityUpdateOperation(entity, EntityUpdateType.REMOVE, callback));
}

protected enum EntityUpdateType {
Expand Down

0 comments on commit f31752d

Please sign in to comment.