Search in sources :

Example 1 with IModuleContainer

use of forestry.api.modules.IModuleContainer in project ForestryMC by ForestryMC.

the class ModuleManager method configureModules.

private static void configureModules(Map<String, List<IForestryModule>> modules) {
    Locale locale = Locale.getDefault();
    Locale.setDefault(Locale.ENGLISH);
    Set<ResourceLocation> toLoad = new HashSet<>();
    Set<IForestryModule> modulesToLoad = new HashSet<>();
    ImmutableList<IForestryModule> allModules = ImmutableList.copyOf(modules.values().stream().flatMap(Collection::stream).collect(Collectors.toList()));
    for (IModuleContainer container : moduleContainers.values()) {
        String containerID = container.getID();
        List<IForestryModule> containerModules = modules.get(containerID);
        Configuration config = container.getModulesConfig();
        config.load();
        config.addCustomCategoryComment(CONFIG_CATEGORY, "Disabling these modules can greatly change how the mod functions.\n" + "Your mileage may vary, please report any issues.");
        IForestryModule coreModule = getModuleCore(containerModules);
        if (coreModule != null) {
            containerModules.remove(coreModule);
            containerModules.add(0, coreModule);
        } else {
            Log.debug("Could not find core module for the module container: {}", containerID);
        }
        Iterator<IForestryModule> iterator = containerModules.iterator();
        while (iterator.hasNext()) {
            IForestryModule module = iterator.next();
            if (!container.isAvailable()) {
                iterator.remove();
                Log.info("Module disabled: {}", module);
                continue;
            }
            if (module.canBeDisabled()) {
                if (!container.isModuleEnabled(module)) {
                    configDisabledModules.add(module);
                    iterator.remove();
                    Log.info("Module disabled: {}", module);
                    continue;
                }
                if (!module.isAvailable()) {
                    iterator.remove();
                    Log.info("Module {} failed to load: {}", module, module.getFailMessage());
                    continue;
                }
            }
            ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
            toLoad.add(new ResourceLocation(containerID, info.moduleID()));
            modulesToLoad.add(module);
        }
    }
    // Check Dependencies
    Iterator<IForestryModule> iterator;
    boolean changed;
    do {
        changed = false;
        iterator = modulesToLoad.iterator();
        while (iterator.hasNext()) {
            IForestryModule module = iterator.next();
            Set<ResourceLocation> dependencies = module.getDependencyUids();
            if (!toLoad.containsAll(dependencies)) {
                iterator.remove();
                changed = true;
                ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
                String moduleId = info.moduleID();
                toLoad.remove(new ResourceLocation(moduleId));
                Log.warning("Module {} is missing dependencies: {}", moduleId, dependencies);
            }
        }
    } while (changed);
    // Sort Modules
    do {
        changed = false;
        iterator = modulesToLoad.iterator();
        while (iterator.hasNext()) {
            IForestryModule module = iterator.next();
            if (sortedModules.keySet().containsAll(module.getDependencyUids())) {
                iterator.remove();
                ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
                sortedModules.put(new ResourceLocation(info.containerID(), info.moduleID()), module);
                changed = true;
                break;
            }
        }
    } while (changed);
    for (IModuleContainer container : moduleContainers.values()) {
        Configuration config = container.getModulesConfig();
        if (config.hasChanged()) {
            config.save();
        }
    }
    loadedModules.addAll(sortedModules.values());
    unloadedModules.addAll(allModules);
    unloadedModules.removeAll(sortedModules.values());
    for (IModuleContainer container : moduleContainers.values()) {
        Collection<IForestryModule> loadedModules = sortedModules.values().stream().filter(m -> {
            ForestryModule info = m.getClass().getAnnotation(ForestryModule.class);
            return info.containerID().equals(container.getID());
        }).collect(Collectors.toList());
        Collection<IForestryModule> unloadedModules = ModuleManager.unloadedModules.stream().filter(m -> {
            ForestryModule info = m.getClass().getAnnotation(ForestryModule.class);
            return info.containerID().equals(container.getID());
        }).collect(Collectors.toList());
        container.onConfiguredModules(loadedModules, unloadedModules);
    }
    ForestryAPI.enabledModules = new HashSet<>();
    ForestryAPI.enabledPlugins = new HashSet<>();
    for (IForestryModule module : sortedModules.values()) {
        ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
        ForestryAPI.enabledModules.add(new ResourceLocation(info.containerID(), info.moduleID()));
        if (module instanceof BlankForestryModule) {
            ForestryAPI.enabledPlugins.add(info.containerID() + "." + info.moduleID());
        }
    }
    Locale.setDefault(locale);
}
Also used : Locale(java.util.Locale) CommandHandler(net.minecraft.command.CommandHandler) ForestryModule(forestry.api.modules.ForestryModule) IResupplyHandler(forestry.core.IResupplyHandler) HashMap(java.util.HashMap) Log(forestry.core.utils.Log) IForestryModule(forestry.api.modules.IForestryModule) ArrayList(java.util.ArrayList) ASMDataTable(net.minecraftforge.fml.common.discovery.ASMDataTable) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) MinecraftServer(net.minecraft.server.MinecraftServer) Lists(com.google.common.collect.Lists) IModuleManager(forestry.api.modules.IModuleManager) ImmutableList(com.google.common.collect.ImmutableList) ISaveEventHandler(forestry.core.ISaveEventHandler) Locale(java.util.Locale) Map(java.util.Map) FMLPreInitializationEvent(net.minecraftforge.fml.common.event.FMLPreInitializationEvent) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) IPickupHandler(forestry.core.IPickupHandler) Collectors(java.util.stream.Collectors) ICommand(net.minecraft.command.ICommand) IModuleContainer(forestry.api.modules.IModuleContainer) List(java.util.List) ForestryAPI(forestry.api.core.ForestryAPI) ResourceLocation(net.minecraft.util.ResourceLocation) Preconditions(com.google.common.base.Preconditions) Configuration(net.minecraftforge.common.config.Configuration) ForestryModule(forestry.api.modules.ForestryModule) IForestryModule(forestry.api.modules.IForestryModule) Configuration(net.minecraftforge.common.config.Configuration) IModuleContainer(forestry.api.modules.IModuleContainer) ResourceLocation(net.minecraft.util.ResourceLocation) Collection(java.util.Collection) IForestryModule(forestry.api.modules.IForestryModule) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with IModuleContainer

use of forestry.api.modules.IModuleContainer in project ForestryMC by ForestryMC.

the class ModuleManager method registerContainers.

@Override
public void registerContainers(IModuleContainer... containers) {
    for (IModuleContainer container : containers) {
        Preconditions.checkNotNull(container);
        moduleContainers.put(container.getID(), container);
    }
}
Also used : IModuleContainer(forestry.api.modules.IModuleContainer)

Example 3 with IModuleContainer

use of forestry.api.modules.IModuleContainer in project Binnie by ForestryMC.

the class BinnieCore method preInit.

@Mod.EventHandler
public void preInit(final FMLPreInitializationEvent evt) {
    MinecraftForge.EVENT_BUS.register(Binnie.LIQUID);
    MinecraftForge.EVENT_BUS.register(ModuleManager.class);
    Binnie.CONFIGURATION.registerConfiguration(ConfigurationMods.class, this);
    for (IModuleContainer container : ForestryAPI.moduleManager.getContainers()) {
        if (!(container instanceof ModuleContainer)) {
            continue;
        }
        ((ModuleContainer) container).setupAPI();
    }
    for (FluidContainerType container : FluidContainerType.getBinnieContainers()) {
        Item item = new ItemFluidContainer(container);
        getProxy().registerItem(item);
    }
    for (CoreErrorCode errorCode : CoreErrorCode.values()) {
        ErrorStateRegistry.registerErrorState(errorCode);
    }
    super.preInit(evt);
}
Also used : Item(net.minecraft.item.Item) ItemFluidContainer(binnie.core.liquid.ItemFluidContainer) FluidContainerType(binnie.core.liquid.FluidContainerType) IModuleContainer(forestry.api.modules.IModuleContainer) IModuleContainer(forestry.api.modules.IModuleContainer) ModuleContainer(binnie.core.modules.ModuleContainer) CoreErrorCode(binnie.core.machines.errors.CoreErrorCode)

Aggregations

IModuleContainer (forestry.api.modules.IModuleContainer)3 FluidContainerType (binnie.core.liquid.FluidContainerType)1 ItemFluidContainer (binnie.core.liquid.ItemFluidContainer)1 CoreErrorCode (binnie.core.machines.errors.CoreErrorCode)1 ModuleContainer (binnie.core.modules.ModuleContainer)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 ForestryAPI (forestry.api.core.ForestryAPI)1 ForestryModule (forestry.api.modules.ForestryModule)1 IForestryModule (forestry.api.modules.IForestryModule)1 IModuleManager (forestry.api.modules.IModuleManager)1 IPickupHandler (forestry.core.IPickupHandler)1 IResupplyHandler (forestry.core.IResupplyHandler)1 ISaveEventHandler (forestry.core.ISaveEventHandler)1 Log (forestry.core.utils.Log)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1