Search in sources :

Example 1 with ModuleData

use of uk.co.drnaylor.quickstart.annotations.ModuleData in project QuickStartModuleLoader by NucleusPowered.

the class ModuleContainer method startDiscover.

public final void startDiscover() throws QuickStartModuleDiscoveryException {
    try {
        Preconditions.checkState(currentPhase == ConstructionPhase.INITALISED);
        currentPhase = ConstructionPhase.DISCOVERING;
        Set<Class<? extends Module>> modules = discoverModules();
        HashMap<String, ModuleSpec> discovered = Maps.newHashMap();
        for (Class<? extends Module> s : modules) {
            // If we have a module annotation, we are golden.
            String id;
            ModuleSpec ms;
            if (s.isAnnotationPresent(ModuleData.class)) {
                ModuleData md = s.getAnnotation(ModuleData.class);
                id = md.id().toLowerCase();
                ms = new ModuleSpec(s, md);
            } else if (this.requireAnnotation) {
                loggerProxy.warn(MessageFormat.format("The module class {0} does not have a ModuleData annotation associated with it. " + "It is not being loaded as the module container requires the annotation to be present.", s.getClass().getName()));
                continue;
            } else {
                id = s.getClass().getName().toLowerCase();
                loggerProxy.warn(MessageFormat.format("The module {0} does not have a ModuleData annotation associated with it. We're just assuming an ID of {0}.", id));
                ms = new ModuleSpec(s, id, id, LoadingStatus.ENABLED, false);
            }
            if (discovered.containsKey(id)) {
                throw new QuickStartModuleDiscoveryException("Duplicate module ID \"" + id + "\" was discovered - loading cannot continue.");
            }
            discovered.put(id, ms);
        }
        // Create the dependency map.
        resolveDependencyOrder(discovered);
        // Modules discovered. Create the Module Config adapter.
        List<ModuleSpec> moduleSpecList = this.discoveredModules.entrySet().stream().filter(x -> !x.getValue().isMandatory()).map(Map.Entry::getValue).collect(Collectors.toList());
        // Attaches config adapter and loads in the defaults.
        config.attachModulesConfig(moduleSpecList, this.descriptionProcessor, this.moduleSection, this.moduleSectionHeader);
        config.saveAdapterDefaults(false);
        // Load what we have in config into our discovered modules.
        try {
            config.getConfigAdapter().getNode().forEach((k, v) -> {
                try {
                    ModuleSpec ms = discoveredModules.get(k);
                    if (ms != null) {
                        ms.setStatus(v);
                    } else {
                        loggerProxy.warn(String.format("Ignoring module entry %s in the configuration file: module does not exist.", k));
                    }
                } catch (IllegalStateException ex) {
                    loggerProxy.warn("A mandatory module can't have its status changed by config. Falling back to FORCELOAD for " + k);
                }
            });
        } catch (ObjectMappingException e) {
            loggerProxy.warn("Could not load modules config, falling back to defaults.");
            e.printStackTrace();
        }
        // Modules have been discovered.
        currentPhase = ConstructionPhase.DISCOVERED;
    } catch (QuickStartModuleDiscoveryException ex) {
        throw ex;
    } catch (Exception e) {
        throw new QuickStartModuleDiscoveryException("Unable to discover QuickStart modules", e);
    }
}
Also used : QuickStartModuleDiscoveryException(uk.co.drnaylor.quickstart.exceptions.QuickStartModuleDiscoveryException) NoModuleException(uk.co.drnaylor.quickstart.exceptions.NoModuleException) QuickStartModuleLoaderException(uk.co.drnaylor.quickstart.exceptions.QuickStartModuleLoaderException) IncorrectAdapterTypeException(uk.co.drnaylor.quickstart.exceptions.IncorrectAdapterTypeException) IOException(java.io.IOException) UndisableableModuleException(uk.co.drnaylor.quickstart.exceptions.UndisableableModuleException) QuickStartModuleDiscoveryException(uk.co.drnaylor.quickstart.exceptions.QuickStartModuleDiscoveryException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) MissingDependencyException(uk.co.drnaylor.quickstart.exceptions.MissingDependencyException) ModuleData(uk.co.drnaylor.quickstart.annotations.ModuleData) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ObjectMappingException (ninja.leaping.configurate.objectmapping.ObjectMappingException)1 ModuleData (uk.co.drnaylor.quickstart.annotations.ModuleData)1 IncorrectAdapterTypeException (uk.co.drnaylor.quickstart.exceptions.IncorrectAdapterTypeException)1 MissingDependencyException (uk.co.drnaylor.quickstart.exceptions.MissingDependencyException)1 NoModuleException (uk.co.drnaylor.quickstart.exceptions.NoModuleException)1 QuickStartModuleDiscoveryException (uk.co.drnaylor.quickstart.exceptions.QuickStartModuleDiscoveryException)1 QuickStartModuleLoaderException (uk.co.drnaylor.quickstart.exceptions.QuickStartModuleLoaderException)1 UndisableableModuleException (uk.co.drnaylor.quickstart.exceptions.UndisableableModuleException)1