use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class Actor method getComponentField.
public Object getComponentField(ComponentFieldUri uri) {
ComponentLibrary componentLibrary = CoreRegistry.get(EntitySystemLibrary.class).getComponentLibrary();
ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(new ResourceUrn(uri.getComponentUri().toString()));
if (metadata == null) {
return null;
}
Component component = entity.getComponent(metadata.getType());
if (component == null) {
return null;
}
FieldMetadata<?, ?> fieldMetadata = metadata.getField(uri.getFieldName());
if (fieldMetadata == null) {
return null;
}
Field field = fieldMetadata.getField();
try {
return field.get(component);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class PrefabData method createFromPrefab.
public static PrefabData createFromPrefab(Prefab prefab) {
PrefabData result = new PrefabData();
for (Component component : prefab.iterateComponents()) {
result.addComponent(component);
}
result.setAlwaysRelevant(prefab.isAlwaysRelevant());
result.setParent(prefab.getParent());
result.setPersisted(prefab.isPersisted());
return result;
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class EventSystemImpl method registerEventHandler.
@Override
public void registerEventHandler(ComponentSystem handler) {
Class handlerClass = handler.getClass();
if (!Modifier.isPublic(handlerClass.getModifiers())) {
logger.error("Cannot register handler {}, must be public", handler.getClass().getName());
return;
}
logger.debug("Registering event handler " + handlerClass.getName());
for (Method method : handlerClass.getMethods()) {
ReceiveEvent receiveEventAnnotation = method.getAnnotation(ReceiveEvent.class);
if (receiveEventAnnotation != null) {
if (!receiveEventAnnotation.netFilter().isValidFor(isAutority, false)) {
continue;
}
Set<Class<? extends Component>> requiredComponents = Sets.newLinkedHashSet();
method.setAccessible(true);
Class<?>[] types = method.getParameterTypes();
logger.debug("Found method: " + method.toString());
if (!Event.class.isAssignableFrom(types[0]) || !EntityRef.class.isAssignableFrom(types[1])) {
logger.error("Invalid event handler method: {}", method.getName());
return;
}
requiredComponents.addAll(Arrays.asList(receiveEventAnnotation.components()));
List<Class<? extends Component>> componentParams = Lists.newArrayList();
for (int i = 2; i < types.length; ++i) {
if (!Component.class.isAssignableFrom(types[i])) {
logger.error("Invalid event handler method: {} - {} is not a component class", method.getName(), types[i]);
return;
}
requiredComponents.add((Class<? extends Component>) types[i]);
componentParams.add((Class<? extends Component>) types[i]);
}
ByteCodeEventHandlerInfo handlerInfo = new ByteCodeEventHandlerInfo(handler, method, receiveEventAnnotation.priority(), receiveEventAnnotation.activity(), requiredComponents, componentParams);
addEventHandler((Class<? extends Event>) types[0], handlerInfo, requiredComponents);
}
}
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class ParticleUpdaterImpl method configureEmitter.
@Override
public void configureEmitter(final ParticleEmitterComponent emitter) {
emitter.generatorFunctionMap.clear();
emitter.affectorFunctionMap.clear();
for (Component c : emitter.ownerEntity.iterateComponents()) {
if (registeredGeneratorFunctions.containsKey(c.getClass())) {
emitter.generatorFunctionMap.put(c, registeredGeneratorFunctions.get(c.getClass()));
} else if (registeredAffectorFunctions.containsKey(c.getClass())) {
emitter.affectorFunctionMap.put(c, registeredAffectorFunctions.get(c.getClass()));
}
}
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class EntityRestorer method restore.
public Map<String, EntityRef> restore(EntityData.EntityStore store) {
EntitySerializer serializer = new EntitySerializer(entityManager);
serializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
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