use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.
the class WorldImplementation method addEntity.
@Override
public void addEntity(final Entity entity) {
// Assign an UUID to entities lacking one
if (this instanceof WorldMaster && entity.getUUID() == -1) {
long nextUUID = nextEntityId();
entity.setUUID(nextUUID);
}
Entity check = this.getEntityByUUID(entity.getUUID());
if (check != null) {
logger().error("Added an entity twice " + check + " conflits with " + entity + " UUID: " + entity.getUUID());
// logger().save();
Thread.dumpStack();
// System.exit(-1);
return;
}
// Add it to the world
((EntityBase) entity).markHasSpawned();
assert entity.getWorld() == this;
Chunk chunk = this.getChunkWorldCoordinates(entity.getLocation());
if (chunk != null) {
((EntityBase) entity).positionComponent.trySnappingToChunk();
}
this.entities.insertEntity(entity);
}
use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.
the class ServerPlayer method unsubscribeAll.
@Override
public void unsubscribeAll() {
Iterator<Entity> iterator = getSubscribedToList();
while (iterator.hasNext()) {
Entity entity = iterator.next();
// If one of the entities is controllable ...
if (entity instanceof EntityControllable) {
EntityControllable controllableEntity = (EntityControllable) entity;
Controller entityController = controllableEntity.getControllerComponent().getController();
// If said entity is controlled by this subscriber/player
if (entityController == this) {
// Set the controller to null
controllableEntity.getControllerComponent().setController(null);
}
}
((EntityBase) entity).unsubscribe(this);
iterator.remove();
}
}
use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.
the class WorldImplementation method tick.
@Override
public void tick() {
// Iterates over every entity
try {
entitiesLock.writeLock().lock();
Iterator<Entity> iter = this.getAllLoadedEntities();
Entity entity;
while (iter.hasNext()) {
entity = iter.next();
// Check entity's region is loaded
if (entity.getChunk() != null)
entity.tick();
else
// Tries to snap the entity to the region if it ends up being loaded
((EntityBase) entity).positionComponent.trySnappingToChunk();
}
} finally {
entitiesLock.writeLock().unlock();
}
// Increase the ticks counter
worldTicksCounter++;
// Time cycle
if (this instanceof WorldMaster && internalData.getBoolean("doTimeCycle", true))
if (worldTicksCounter % 60 == 0)
worldTime++;
}
use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.
the class WorldImplementation method removeEntity.
@Override
public boolean removeEntity(Entity entity) {
try {
entitiesLock.writeLock().lock();
if (entity != null) {
EntityBase ent = (EntityBase) entity;
// Only once
if (ent.getComponentExistence().exists()) {
// Destroys it
ent.getComponentExistence().destroyEntity();
// Removes it's reference within the region
if (ent.positionComponent.getChunkWithin() != null)
ent.positionComponent.getChunkWithin().removeEntity(entity);
// Actually removes it from the world list
removeEntityFromList(entity);
return true;
}
}
return false;
} finally {
entitiesLock.writeLock().unlock();
}
}
Aggregations