use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetworkSystemImpl method getOwnerEntity.
@Override
public EntityRef getOwnerEntity(EntityRef entity) {
EntityRef owner = entity;
// NetworkComponent ownerNetComp = entity.getComponent(NetworkComponent.class);
int i = 0;
while (i++ < OWNER_DEPTH_LIMIT) {
EntityRef nextOwner = owner.getOwner();
if (nextOwner.exists()) {
owner = nextOwner;
} else {
break;
}
}
return owner;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class ServerImpl method processEvent.
private void processEvent(NetData.EventMessage message) {
try {
Event event = eventSerializer.deserialize(message.getEvent());
EntityRef target = EntityRef.NULL;
if (message.hasTargetBlockPos()) {
target = blockEntityRegistry.getBlockEntityAt(NetMessageUtil.convert(message.getTargetBlockPos()));
} else if (message.hasTargetId()) {
target = networkSystem.getEntity(message.getTargetId());
}
if (target.exists()) {
target.send(event);
} else {
logger.info("Dropping event {} for unavailable entity {}", event.getClass().getSimpleName(), target);
}
} catch (DeserializationException e) {
logger.error("Failed to deserialize event", e);
}
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetEntityRefTypeHandler method serializeCollection.
@Override
public PersistedData serializeCollection(Collection<EntityRef> value, SerializationContext context) {
List<PersistedData> items = Lists.newArrayList();
for (EntityRef ref : value) {
BlockComponent blockComponent = ref.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3i blockPos = blockComponent.getPosition();
items.add(context.create(blockPos.x, blockPos.y, blockPos.z));
} else {
NetworkComponent netComponent = ref.getComponent(NetworkComponent.class);
if (netComponent != null) {
items.add(context.create(netComponent.getNetworkId()));
} else {
items.add(context.createNull());
}
}
}
return context.create(items);
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class AbstractStorageManager method getEntitiesOfChunk.
protected Collection<EntityRef> getEntitiesOfChunk(Chunk chunk) {
List<EntityRef> entitiesToStore = Lists.newArrayList();
AABB aabb = chunk.getAABB();
for (EntityRef entity : getEntityManager().getEntitiesWith(LocationComponent.class)) {
if (!entity.getOwner().exists() && !entity.isAlwaysRelevant() && !entity.hasComponent(ClientComponent.class)) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
if (loc != null) {
if (aabb.contains(loc.getWorldPosition())) {
entitiesToStore.add(entity);
}
}
}
}
return entitiesToStore;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class EntityRestorer method restore.
public Map<String, EntityRef> restore(EntityData.EntityStore store) {
EntitySerializer serializer = new EntitySerializer(entityManager);
Map<Class<? extends Component>, Integer> idMap = Maps.newHashMap();
for (int i = 0; i < store.getComponentClassCount(); ++i) {
ComponentMetadata<?> metadata = entityManager.getComponentLibrary().resolve(store.getComponentClass(i));
if (metadata != null) {
idMap.put(metadata.getType(), i);
}
}
serializer.setComponentIdMapping(idMap);
store.getEntityList().forEach(serializer::deserialize);
Map<String, EntityRef> namedEntities = Maps.newHashMap();
for (int i = 0; i < store.getEntityNameCount() && i < store.getEntityNamedCount(); ++i) {
namedEntities.put(store.getEntityName(i), entityManager.getEntity(store.getEntityNamed(i)));
}
return namedEntities;
}
Aggregations