Search in sources :

Example 1 with RegisterCatalog

use of org.spongepowered.api.registry.util.RegisterCatalog in project SpongeCommon by SpongePowered.

the class RegistryModuleLoader method tryModulePhaseRegistration.

public static void tryModulePhaseRegistration(RegistryModule module) {
    try {
        if (requiresCustomRegistration(module)) {
            if (isCustomProperPhase(module)) {
                Method method = getCustomRegistration(module);
                invokeCustomRegistration(module, checkNotNull(method, "Custom registration module was null!"));
            }
        } else if (isDefaultProperPhase(module)) {
            module.registerDefaults();
            if (hasCatalogRegistration(module)) {
                Map<String, ?> map = getCatalogMap(module);
                if (map.isEmpty()) {
                    return;
                }
                RegisterCatalog regAnnot = getRegisterCatalogAnnot(module);
                Set<String> ignored = regAnnot.ignoredFields().length == 0 ? null : Sets.newHashSet(regAnnot.ignoredFields());
                RegistryHelper.mapFields(regAnnot.value(), map, ignored);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error trying to initialize module: " + module.getClass().getCanonicalName(), e);
    }
}
Also used : Set(java.util.Set) RegisterCatalog(org.spongepowered.api.registry.util.RegisterCatalog) Method(java.lang.reflect.Method) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with RegisterCatalog

use of org.spongepowered.api.registry.util.RegisterCatalog in project LanternServer by LanternPowered.

the class LanternGameRegistry method getCatalogMappingData.

@SuppressWarnings("unchecked")
private List<CatalogMappingData> getCatalogMappingData(RegistryModule module) {
    Map<String, ?> mappings = null;
    if (module instanceof AlternateCatalogRegistryModule) {
        mappings = checkNotNull(((AlternateCatalogRegistryModule) module).provideCatalogMap());
    }
    final List<CatalogMappingData> data = new ArrayList<>();
    for (Field field : module.getClass().getDeclaredFields()) {
        RegisterCatalog annotation = field.getAnnotation(RegisterCatalog.class);
        if (annotation != null) {
            if (mappings == null) {
                try {
                    field.setAccessible(true);
                    mappings = (Map<String, ?>) field.get(module);
                    checkState(!mappings.isEmpty(), "The registered module: " + module.getClass().getSimpleName() + " cannot have an empty mapping during registration!");
                } catch (Exception e) {
                    this.game.getLogger().error("Failed to retrieve a registry field from module: " + module.getClass().getCanonicalName(), e);
                }
            }
            data.add(new CatalogMappingData(annotation, mappings));
        }
    }
    if (module instanceof CatalogMappingDataHolder) {
        data.addAll(((CatalogMappingDataHolder) module).getCatalogMappings());
    }
    return data;
}
Also used : Field(java.lang.reflect.Field) AlternateCatalogRegistryModule(org.spongepowered.api.registry.AlternateCatalogRegistryModule) ArrayList(java.util.ArrayList) RegisterCatalog(org.spongepowered.api.registry.util.RegisterCatalog) CatalogMappingData(org.lanternpowered.server.game.registry.CatalogMappingData) CyclicGraphException(org.lanternpowered.server.util.graph.CyclicGraphException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CatalogMappingDataHolder(org.lanternpowered.server.game.registry.CatalogMappingDataHolder)

Example 3 with RegisterCatalog

use of org.spongepowered.api.registry.util.RegisterCatalog in project SpongeCommon by SpongePowered.

the class RegistryModuleLoader method getCatalogMap.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map<String, ?> getCatalogMap(RegistryModule module) {
    if (module instanceof AlternateCatalogRegistryModule) {
        return checkNotNull(((AlternateCatalogRegistryModule) module).provideCatalogMap());
    }
    for (Field field : module.getClass().getDeclaredFields()) {
        RegisterCatalog annotation = field.getAnnotation(RegisterCatalog.class);
        if (annotation != null) {
            try {
                field.setAccessible(true);
                Map<String, ?> map = (Map<String, ?>) field.get(module);
                checkState(!map.isEmpty(), "The registered module: " + module.getClass().getSimpleName() + " cannot have an empty mapping during registration!");
                return checkNotNull(map);
            } catch (Exception e) {
                SpongeImpl.getLogger().error("Failed to retrieve a registry field from module: " + module.getClass().getCanonicalName());
            }
        }
    }
    throw new IllegalStateException("Registry module does not have a catalog map! Registry: " + module.getClass().getCanonicalName());
}
Also used : Field(java.lang.reflect.Field) AlternateCatalogRegistryModule(org.spongepowered.api.registry.AlternateCatalogRegistryModule) RegisterCatalog(org.spongepowered.api.registry.util.RegisterCatalog) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)3 RegisterCatalog (org.spongepowered.api.registry.util.RegisterCatalog)3 Field (java.lang.reflect.Field)2 Map (java.util.Map)2 AlternateCatalogRegistryModule (org.spongepowered.api.registry.AlternateCatalogRegistryModule)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 CatalogMappingData (org.lanternpowered.server.game.registry.CatalogMappingData)1 CatalogMappingDataHolder (org.lanternpowered.server.game.registry.CatalogMappingDataHolder)1 CyclicGraphException (org.lanternpowered.server.util.graph.CyclicGraphException)1