diff --git a/src/main/java/forestry/api/modules/IForestryModule.java b/src/main/java/forestry/api/modules/IForestryModule.java index 098298772..90397c081 100644 --- a/src/main/java/forestry/api/modules/IForestryModule.java +++ b/src/main/java/forestry/api/modules/IForestryModule.java @@ -16,6 +16,7 @@ import net.minecraft.util.ResourceLocation; * Defines a Forestry module. * Any class implementing this interface and annotated by {@link ForestryModule} to be loaded by * the model manager of Forestry. + * */ public interface IForestryModule { default boolean isAvailable() { @@ -40,22 +41,31 @@ public interface IForestryModule { /** * Can be used to setup the api. * Will only be called if the module is active if not {@link #disabledSetupAPI()} will be called. + * + * Must be called by the mod that registers the container. */ default void setupAPI() { } /** * Called to setup the api if this module is disabled in the config or has missing dependencies. + * + * Must be called by the mod that registers the container. */ default void disabledSetupAPI() { } /** * Can be used to register items and blocks. Called before {@link #preInit()}. + * + * Must be called by the mod that registers the container. */ default void registerItemsAndBlocks() { } + /** + * Must be called by the mod that registers the container. + */ default void preInit() { } diff --git a/src/main/java/forestry/api/modules/IModuleContainer.java b/src/main/java/forestry/api/modules/IModuleContainer.java index 3363e8be4..e977659ac 100644 --- a/src/main/java/forestry/api/modules/IModuleContainer.java +++ b/src/main/java/forestry/api/modules/IModuleContainer.java @@ -5,8 +5,6 @@ ******************************************************************************/ package forestry.api.modules; -import java.util.Collection; - import net.minecraftforge.common.config.Configuration; public interface IModuleContainer { @@ -26,9 +24,9 @@ public interface IModuleContainer { /** * Called after configured the modules. - * - * @param modules All active modules */ - default void onConfiguredModules(Collection modules){ + default void onConfiguredModules(IModuleHandler handler){ } + + } diff --git a/src/main/java/forestry/api/modules/IModuleHandler.java b/src/main/java/forestry/api/modules/IModuleHandler.java new file mode 100644 index 000000000..31c2ca3bf --- /dev/null +++ b/src/main/java/forestry/api/modules/IModuleHandler.java @@ -0,0 +1,16 @@ +package forestry.api.modules; + +import java.util.Collection; + +public interface IModuleHandler { + + void runSetup(); + + void runPreInit(); + + void runInit(); + + void runPostInit(); + + Collection getModules(); +} diff --git a/src/main/java/forestry/modules/ModuleHandler.java b/src/main/java/forestry/modules/ModuleHandler.java new file mode 100644 index 000000000..3b9ad0bbc --- /dev/null +++ b/src/main/java/forestry/modules/ModuleHandler.java @@ -0,0 +1,81 @@ +package forestry.modules; + +import com.google.common.collect.ImmutableSet; + +import java.util.Collection; +import java.util.Set; + +import net.minecraftforge.fml.common.FMLCommonHandler; + +import forestry.api.modules.IForestryModule; +import forestry.api.modules.IModuleHandler; +import forestry.core.config.Constants; +import forestry.core.utils.Log; + +public class ModuleHandler implements IModuleHandler { + + private final Set modules; + private final ModuleManager manager; + + public ModuleHandler(Collection modules, ModuleManager manager) { + this.modules = ImmutableSet.copyOf(modules); + this.manager = manager; + } + + public void runSetup() { + for (IForestryModule module : modules) { + Log.debug("Setup API Start: {}", module); + module.setupAPI(); + Log.debug("Setup API Complete: {}", module); + } + + for (IForestryModule module : modules) { + Log.debug("Disabled-Setup Start: {}", module); + module.disabledSetupAPI(); + Log.debug("Disabled-Setup Complete: {}", module); + } + + for (IForestryModule module : modules) { + Log.debug("Register Items and Blocks Start: {}", module); + module.registerItemsAndBlocks(); + Log.debug("Register Items and Blocks Complete: {}", module); + } + } + + public void runPreInit() { + for (IForestryModule module : modules) { + Log.debug("Pre-Init Start: {}", module); + if(module instanceof BlankForestryModule) { + BlankForestryModule moduleInternal = (BlankForestryModule) module; + ModuleManager.registerHandlers(moduleInternal, FMLCommonHandler.instance().getEffectiveSide()); + } + module.preInit(); + if (manager.isModuleEnabled(Constants.MOD_ID, ForestryModuleUids.BUILDCRAFT_STATEMENTS)) { + module.registerTriggers(); + } + Log.debug("Pre-Init Complete: {}", module); + } + } + + public void runInit() { + for (IForestryModule module : modules) { + Log.debug("Init Start: {}", module); + module.doInit(); + module.registerRecipes(); + Log.debug("Init Complete: {}", module); + } + } + + public void runPostInit() { + for (IForestryModule module : modules) { + Log.debug("Post-Init Start: {}", module); + module.postInit(); + Log.debug("Post-Init Complete: {}", module); + } + } + + @Override + public Set getModules() { + return modules; + } +} diff --git a/src/main/java/forestry/modules/ModuleManager.java b/src/main/java/forestry/modules/ModuleManager.java index 1ef2f0085..b06308337 100644 --- a/src/main/java/forestry/modules/ModuleManager.java +++ b/src/main/java/forestry/modules/ModuleManager.java @@ -49,6 +49,7 @@ import forestry.api.core.ForestryAPI; import forestry.api.modules.ForestryModule; import forestry.api.modules.IForestryModule; import forestry.api.modules.IModuleContainer; +import forestry.api.modules.IModuleHandler; import forestry.api.modules.IModuleManager; import forestry.core.IPickupHandler; import forestry.core.IResupplyHandler; @@ -56,6 +57,7 @@ import forestry.core.ISaveEventHandler; import forestry.core.config.Constants; import forestry.core.network.IPacketRegistry; import forestry.core.utils.Log; +import forestry.plugins.ForestryCompatPlugins; public class ModuleManager implements IModuleManager { @@ -69,6 +71,7 @@ public class ModuleManager implements IModuleManager { private static final HashMap sortedModules = new LinkedHashMap<>(); private static final Set loadedModules = new LinkedHashSet<>(); private static final Set internalModules = new LinkedHashSet<>(); + private static final Set internalHandles = new LinkedHashSet<>(); private static final Set unloadedModules = new LinkedHashSet<>(); private static final HashMap moduleContainers = new HashMap<>(); public static final Set configDisabledModules = new HashSet<>(); @@ -118,7 +121,7 @@ public class ModuleManager implements IModuleManager { return ImmutableSet.copyOf(sortedModules.values()); } - private static void registerHandlers(BlankForestryModule module, Side side) { + static void registerHandlers(BlankForestryModule module, Side side) { Log.debug("Registering Handlers for Module: {}", module); IPacketRegistry packetRegistry = module.getPacketRegistry(); @@ -155,16 +158,6 @@ public class ModuleManager implements IModuleManager { return null; } - private static IForestryModule getCoreModule(List modules, String containerID) { - for (IForestryModule module : modules) { - ForestryModule info = module.getClass().getAnnotation(ForestryModule.class); - if (info.coreModule()) { - return module; - } - } - throw new IllegalStateException("Could not find core module for the container " + containerID); - } - private static void configureModules(Map> modules) { Locale locale = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); @@ -263,7 +256,11 @@ public class ModuleManager implements IModuleManager { return info.containerID().equals(container.getID()); } ).collect(Collectors.toList()); - container.onConfiguredModules(containerModules); + IModuleHandler handler = new ModuleHandler(containerModules, getInstance()); + if(container instanceof ForestryModules || handler instanceof ForestryCompatPlugins){ + internalHandles.add(handler); + } + container.onConfiguredModules(handler); } loadedModules.addAll(sortedModules.values()); @@ -288,40 +285,15 @@ public class ModuleManager implements IModuleManager { stage = Stage.SETUP; configureModules(forestryModules); - for (IForestryModule module : loadedModules) { - Log.debug("Setup API Start: {}", module); - module.setupAPI(); - Log.debug("Setup API Complete: {}", module); - } - - stage = Stage.SETUP_DISABLED; - for (IForestryModule module : unloadedModules) { - Log.debug("Disabled-Setup Start: {}", module); - module.disabledSetupAPI(); - Log.debug("Disabled-Setup Complete: {}", module); - } - - stage = Stage.REGISTER; - for (IForestryModule module : loadedModules) { - Log.debug("Register Items and Blocks Start: {}", module); - module.registerItemsAndBlocks(); - Log.debug("Register Items and Blocks Complete: {}", module); + for(IModuleHandler handler : internalHandles){ + handler.runSetup(); } } public static void runPreInit(Side side) { stage = Stage.PRE_INIT; - for (IForestryModule module : loadedModules) { - Log.debug("Pre-Init Start: {}", module); - if(module instanceof BlankForestryModule) { - BlankForestryModule moduleInternal = (BlankForestryModule) module; - registerHandlers(moduleInternal, side); - } - module.preInit(); - if (getInstance().isModuleEnabled(Constants.MOD_ID, ForestryModuleUids.BUILDCRAFT_STATEMENTS)) { - module.registerTriggers(); - } - Log.debug("Pre-Init Complete: {}", module); + for(IModuleHandler handler : internalHandles){ + handler.runPreInit(); } } @@ -339,20 +311,15 @@ public class ModuleManager implements IModuleManager { public static void runInit() { stage = Stage.INIT; - for (IForestryModule module : loadedModules) { - Log.debug("Init Start: {}", module); - module.doInit(); - module.registerRecipes(); - Log.debug("Init Complete: {}", module); + for(IModuleHandler handler : internalHandles){ + handler.runInit(); } } public static void runPostInit() { stage = Stage.POST_INIT; - for (IForestryModule module : loadedModules) { - Log.debug("Post-Init Start: {}", module); - module.postInit(); - Log.debug("Post-Init Complete: {}", module); + for(IModuleHandler handler : internalHandles){ + handler.runPostInit(); } stage = Stage.FINISHED;