Skip to content

Commit

Permalink
TDynstruct.cfg can now be used to toggle compat plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunstrike committed Nov 29, 2013
1 parent bd213f6 commit 4698b28
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/main/java/tconstruct/plugins/ICompatPlugin.java
@@ -1,5 +1,11 @@
package tconstruct.plugins;

/**
* Interface for TiC compat plugins.
*
* Do not include mod API usage directly in this file except for IMC! This must be constructable even
* when the target mod isn't available due to Java not allowing static abstracts.
*/
public interface ICompatPlugin {

// Mod ID the plugin handles
Expand Down
51 changes: 44 additions & 7 deletions src/main/java/tconstruct/plugins/PluginController.java
@@ -1,14 +1,18 @@
package tconstruct.plugins;

import cpw.mods.fml.common.Loader;
import net.minecraftforge.common.Configuration;
import tconstruct.TConstruct;
import tconstruct.plugins.fmp.ForgeMultiPart;
import tconstruct.plugins.minefactoryreloaded.MineFactoryReloaded;
import tconstruct.plugins.nei.NotEnoughItems;
import tconstruct.plugins.waila.Waila;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PluginController
{
Expand All @@ -18,23 +22,47 @@ private enum Phase {
}

private static PluginController instance;
private List<ICompatPlugin> plugins = new ArrayList<ICompatPlugin>();
private Configuration conf = null;
private Map<ICompatPlugin, Boolean> plugins = new HashMap<ICompatPlugin, Boolean>();
private Phase currPhase = Phase.PRELAUNCH;

private PluginController() {}
private PluginController() {
String path = Loader.instance().getConfigDir().toString() + File.separator + "TDynstruct.cfg";
TConstruct.logger.info("[PluginController] Using config path: " + path);
conf = new Configuration(new File(path));
}

public static PluginController getController()
{
if (instance == null) instance = new PluginController();
return instance;
}

/**
* Register a plugin with the controller.
*
* Warning: Make sure your plugin class imports no APIs directly! Any API interaction should be done by handlers called in pre/init/post so
* merely creating an instance to check the mod ID isn't a hazard to the controller.
*
* @param plugin Plugin to register
*/
public void registerPlugin(ICompatPlugin plugin)
{
if (Loader.isModLoaded(plugin.getModId()))
{
TConstruct.logger.info("Registering compat plugin for " + plugin.getModId());
plugins.add(plugin);
TConstruct.logger.info("[PluginController] Attempting registration of compat plugin for " + plugin.getModId());
plugins.put(plugin, true);

conf.load();
boolean shouldLoad = conf.get("Plugins", plugin.getModId(), true).getBoolean(true);
plugins.put(plugin, shouldLoad);
conf.save();

if (!shouldLoad)
{
TConstruct.logger.info("[PluginController] Aborting registration of compat plugin for " + plugin.getModId() + "; disabled in configuration.");
return;
}

switch (currPhase) // Play catch-up if plugin is registered late
{
Expand All @@ -60,19 +88,28 @@ public void registerPlugin(ICompatPlugin plugin)
public void preInit()
{
currPhase = Phase.PREINIT;
for (ICompatPlugin plugin : plugins) plugin.preInit();
for (Map.Entry<ICompatPlugin, Boolean> entry : plugins.entrySet()){
if (entry.getValue())
entry.getKey().preInit();
}
}

public void init()
{
currPhase = Phase.INIT;
for (ICompatPlugin plugin : plugins) plugin.init();
for (Map.Entry<ICompatPlugin, Boolean> entry : plugins.entrySet()){
if (entry.getValue())
entry.getKey().init();
}
}

public void postInit()
{
currPhase = Phase.POSTINIT;
for (ICompatPlugin plugin : plugins) plugin.postInit();
for (Map.Entry<ICompatPlugin, Boolean> entry : plugins.entrySet()){
if (entry.getValue())
entry.getKey().postInit();
}
currPhase = Phase.DONE;
}

Expand Down

0 comments on commit 4698b28

Please sign in to comment.