use of org.terasology.engine.entitySystem.metadata.ComponentLibrary 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.engine.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.
the class EnvironmentSwitchHandler method handleSwitchToPreviewEnvironment.
public void handleSwitchToPreviewEnvironment(Context context, ModuleEnvironment environment) {
cheapAssetManagerUpdate(context, environment);
ComponentLibrary library = new ComponentLibrary(environment, context.get(ReflectFactory.class), context.get(CopyStrategyLibrary.class));
context.put(ComponentLibrary.class, library);
registerComponents(library, environment);
}
use of org.terasology.engine.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.
the class EnvironmentSwitchHandler method handleSwitchToGameEnvironment.
public void handleSwitchToGameEnvironment(Context context) {
ModuleManager moduleManager = context.get(ModuleManager.class);
ModuleEnvironment environment = moduleManager.getEnvironment();
ModuleTypeRegistry typeRegistry = context.get(ModuleTypeRegistry.class);
typeRegistry.reload(environment);
CopyStrategyLibrary copyStrategyLibrary = context.get(CopyStrategyLibrary.class);
copyStrategyLibrary.clear();
for (CopyStrategyEntry<?> entry : typesWithCopyConstructors) {
entry.registerWith(copyStrategyLibrary);
}
// TODO: find a permanent fix over just creating a new typehandler
// https://github.com/Terasology/JoshariasSurvival/issues/31
// TypeHandlerLibrary typeHandlerLibrary = context.get(TypeHandlerLibrary.class);
// typeHandlerLibrary.addTypeHandler(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
TypeHandlerLibrary typeHandlerLibrary = TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager, typeRegistry);
typeHandlerLibrary.addTypeHandler(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
context.put(TypeHandlerLibrary.class, typeHandlerLibrary);
// Entity System Library
EntitySystemLibrary library = new EntitySystemLibrary(context, typeHandlerLibrary);
context.put(EntitySystemLibrary.class, library);
ComponentLibrary componentLibrary = library.getComponentLibrary();
context.put(ComponentLibrary.class, componentLibrary);
context.put(EventLibrary.class, library.getEventLibrary());
context.put(ClassMetaLibrary.class, new ClassMetaLibraryImpl(context));
registerComponents(componentLibrary, environment);
registerTypeHandlers(context, typeHandlerLibrary, environment);
// Load configs for the new environment
AutoConfigManager autoConfigManager = context.get(AutoConfigManager.class);
autoConfigManager.loadConfigsIn(context);
ModuleAwareAssetTypeManager assetTypeManager = context.get(ModuleAwareAssetTypeManager.class);
/*
* The registering of the prefab formats is done in this method, because it needs to be done before
* the environment of the asset manager gets changed.
*
* It can't be done before this method gets called because the ComponentLibrary isn't
* existing then yet.
*/
unregisterPrefabFormats(assetTypeManager);
registeredPrefabFormat = new PrefabFormat(componentLibrary, typeHandlerLibrary);
assetTypeManager.getAssetFileDataProducer(assetTypeManager.getAssetType(Prefab.class).orElseThrow(() -> new RuntimeException("Cannot get Prefab Asset typee"))).addAssetFormat(registeredPrefabFormat);
registeredPrefabDeltaFormat = new PrefabDeltaFormat(componentLibrary, typeHandlerLibrary);
assetTypeManager.getAssetFileDataProducer(assetTypeManager.getAssetType(Prefab.class).orElseThrow(() -> new RuntimeException("Cannot get Prefab Asset type"))).addDeltaFormat(registeredPrefabDeltaFormat);
assetTypeManager.switchEnvironment(environment);
assetTypeManager.reloadAssets();
}
use of org.terasology.engine.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.
the class PrefabTest method setup.
@BeforeEach
public void setup() throws Exception {
ContextImpl context = new ContextImpl();
context.put(RecordAndReplayCurrentStatus.class, new RecordAndReplayCurrentStatus());
CoreRegistry.setContext(context);
ModuleManager moduleManager = ModuleManagerFactory.create();
context.put(ModuleManager.class, moduleManager);
EntitySystemSetupUtil.addReflectionBasedLibraries(context);
ModuleAwareAssetTypeManager assetTypeManager = new ModuleAwareAssetTypeManagerImpl();
AssetType<Prefab, PrefabData> prefabDataAssetType = assetTypeManager.createAssetType(Prefab.class, PojoPrefab::new, "prefabs");
ComponentLibrary componentLibrary = context.get(ComponentLibrary.class);
TypeHandlerLibrary typeHandlerLibrary = context.get(TypeHandlerLibrary.class);
PrefabFormat prefabFormat = new PrefabFormat(componentLibrary, typeHandlerLibrary);
assetTypeManager.getAssetFileDataProducer(prefabDataAssetType).addAssetFormat(prefabFormat);
assetTypeManager.switchEnvironment(moduleManager.getEnvironment());
context.put(AssetManager.class, assetTypeManager.getAssetManager());
NetworkSystem networkSystem = mock(NetworkSystem.class);
when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
context.put(NetworkSystem.class, networkSystem);
EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
prefabManager = new PojoPrefabManager(context);
}
use of org.terasology.engine.entitySystem.metadata.ComponentLibrary in project Terasology by MovingBlocks.
the class ComponentSerializerTest method setup.
@BeforeEach
public void setup() {
context = new ContextImpl();
context.put(RecordAndReplayCurrentStatus.class, new RecordAndReplayCurrentStatus());
context.put(ModuleManager.class, moduleManager);
CoreRegistry.setContext(context);
Reflections reflections = new Reflections(getClass().getClassLoader());
TypeHandlerLibrary serializationLibrary = new TypeHandlerLibraryImpl(reflections);
serializationLibrary.addTypeHandler(Vector3f.class, new Vector3fTypeHandler());
serializationLibrary.addTypeHandler(Quaternionf.class, new QuaternionfTypeHandler());
NetworkSystem networkSystem = mock(NetworkSystem.class);
when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
context.put(NetworkSystem.class, networkSystem);
EntitySystemSetupUtil.addReflectionBasedLibraries(context);
EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
EngineEntityManager entityManager = context.get(EngineEntityManager.class);
entityManager.getComponentLibrary().register(new ResourceUrn("test", "gettersetter"), GetterSetterComponent.class);
entityManager.getComponentLibrary().register(new ResourceUrn("test", "string"), StringComponent.class);
entityManager.getComponentLibrary().register(new ResourceUrn("test", "integer"), IntegerComponent.class);
ComponentLibrary componentLibrary = entityManager.getComponentLibrary();
componentSerializer = new ComponentSerializer(componentLibrary, serializationLibrary);
}
Aggregations