use of org.spongepowered.api.service.context.ContextCalculator in project Nucleus by NucleusPowered.
the class StandardModule method registerServices.
@SuppressWarnings("unchecked")
private void registerServices() throws Exception {
RegisterService[] annotations = getClass().getAnnotationsByType(RegisterService.class);
if (annotations != null && annotations.length > 0) {
// for each annotation, attempt to register the service.
for (RegisterService service : annotations) {
Class<?> impl = service.value();
// create the impl
Object serviceImpl;
try {
serviceImpl = impl.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
String error = "ERROR: Cannot instantiate " + impl.getName();
Nucleus.getNucleus().getLogger().error(error);
throw new IllegalStateException(error, e);
}
Class<?> api = service.apiService();
if (api != Object.class) {
if (api.isInstance(serviceImpl)) {
// OK
register((Class) api, (Class) impl, serviceImpl);
} else {
String error = "ERROR: " + api.getName() + " does not inherit from " + impl.getName();
Nucleus.getNucleus().getLogger().error(error);
throw new IllegalStateException(error);
}
} else {
register((Class) impl, serviceImpl);
}
if (serviceImpl instanceof Reloadable) {
Reloadable reloadable = (Reloadable) serviceImpl;
Nucleus.getNucleus().registerReloadable(reloadable);
reloadable.onReload();
}
if (serviceImpl instanceof ContextCalculator) {
try {
// boolean matches(Context context, T calculable);
serviceImpl.getClass().getMethod("matches", Context.class, Subject.class);
// register it
ServiceChangeListener.getInstance().registerCalculator((ContextCalculator<Subject>) serviceImpl);
} catch (NoSuchMethodException e) {
// ignored
}
}
}
}
}
Aggregations