Search in sources :

Example 1 with ContextCalculator

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
                }
            }
        }
    }
}
Also used : Subject(org.spongepowered.api.service.permission.Subject) RegisterService(io.github.nucleuspowered.nucleus.internal.annotations.RegisterService) ContextCalculator(org.spongepowered.api.service.context.ContextCalculator) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)

Aggregations

RegisterService (io.github.nucleuspowered.nucleus.internal.annotations.RegisterService)1 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)1 ContextCalculator (org.spongepowered.api.service.context.ContextCalculator)1 Subject (org.spongepowered.api.service.permission.Subject)1