2014-05-02 21:31:20 +02:00
|
|
|
/*******************************************************************************
|
2014-08-07 08:29:25 +02:00
|
|
|
* Copyright (c) 2011-2014 SirSengir.
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the GNU Lesser Public License v3
|
|
|
|
|
* which accompanies this distribution, and is available at
|
2014-08-07 02:56:58 -07:00
|
|
|
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
2014-05-02 21:31:20 +02:00
|
|
|
*
|
2014-08-07 08:29:25 +02:00
|
|
|
* Various Contributors including, but not limited to:
|
|
|
|
|
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
2014-05-02 21:31:20 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
package forestry.plugins;
|
|
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
import com.google.common.collect.ImmutableList;
|
2014-08-21 11:00:52 -04:00
|
|
|
import com.google.common.collect.Lists;
|
2014-05-02 21:31:20 +02:00
|
|
|
import cpw.mods.fml.common.IFuelHandler;
|
2014-09-20 22:43:13 -07:00
|
|
|
import cpw.mods.fml.common.event.FMLInterModComms;
|
2014-05-02 21:31:20 +02:00
|
|
|
import cpw.mods.fml.common.network.IGuiHandler;
|
|
|
|
|
import cpw.mods.fml.common.registry.GameRegistry;
|
2014-09-20 22:43:13 -07:00
|
|
|
import forestry.Forestry;
|
2014-05-02 21:31:20 +02:00
|
|
|
import forestry.core.interfaces.IOreDictionaryHandler;
|
|
|
|
|
import forestry.core.interfaces.IPacketHandler;
|
|
|
|
|
import forestry.core.interfaces.IPickupHandler;
|
|
|
|
|
import forestry.core.interfaces.IResupplyHandler;
|
|
|
|
|
import forestry.core.interfaces.ISaveEventHandler;
|
|
|
|
|
import forestry.core.proxy.Proxies;
|
2014-09-20 22:43:13 -07:00
|
|
|
import net.minecraft.command.CommandHandler;
|
|
|
|
|
import net.minecraft.command.ICommand;
|
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraftforge.common.config.Configuration;
|
|
|
|
|
import net.minecraftforge.common.config.Property;
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
2014-05-02 21:31:20 +02:00
|
|
|
public class PluginManager {
|
|
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static final String MODULE_CONFIG_FILE_NAME = "modules.cfg";
|
|
|
|
|
public static final String CATEGORY_MODULES = "modules";
|
|
|
|
|
|
2014-08-21 11:00:52 -04:00
|
|
|
public static ArrayList<IGuiHandler> guiHandlers = Lists.newArrayList();
|
|
|
|
|
public static ArrayList<IPacketHandler> packetHandlers = Lists.newArrayList();
|
|
|
|
|
public static ArrayList<IPickupHandler> pickupHandlers = Lists.newArrayList();
|
|
|
|
|
public static ArrayList<ISaveEventHandler> saveEventHandlers = Lists.newArrayList();
|
|
|
|
|
public static ArrayList<IResupplyHandler> resupplyHandlers = Lists.newArrayList();
|
|
|
|
|
public static ArrayList<IOreDictionaryHandler> dictionaryHandlers = Lists.newArrayList();
|
|
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
private static ArrayList<PluginModule> loadedModules;
|
|
|
|
|
private static ArrayList<PluginModule> unloadedModules;
|
2014-09-20 22:43:13 -07:00
|
|
|
private static Stage stage = Stage.SETUP;
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public enum Stage {
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
SETUP, PRE_INIT, INIT, POST_INIT, INIT_DISABLED, FINISHED;
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
private static HashMap<Class<? extends ForestryPlugin>, PluginModule> pluginModules = new HashMap<Class<? extends ForestryPlugin>, PluginModule>();
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
ArrayList<Class<? extends ForestryPlugin>> pluginClasses = new ArrayList<Class<? extends ForestryPlugin>>();
|
|
|
|
|
pluginClasses.add(PluginCore.class);
|
|
|
|
|
pluginClasses.add(PluginApiculture.class);
|
|
|
|
|
pluginClasses.add(PluginArboriculture.class);
|
|
|
|
|
pluginClasses.add(PluginEnergy.class);
|
|
|
|
|
pluginClasses.add(PluginFactory.class);
|
|
|
|
|
pluginClasses.add(PluginFarming.class);
|
|
|
|
|
pluginClasses.add(PluginFood.class);
|
|
|
|
|
pluginClasses.add(PluginLepidopterology.class);
|
|
|
|
|
pluginClasses.add(PluginMail.class);
|
|
|
|
|
pluginClasses.add(PluginStorage.class);
|
|
|
|
|
pluginClasses.add(PluginBuildCraft.class);
|
|
|
|
|
pluginClasses.add(PluginPropolisPipe.class);
|
|
|
|
|
pluginClasses.add(PluginEE.class);
|
|
|
|
|
pluginClasses.add(PluginFarmCraftory.class);
|
|
|
|
|
pluginClasses.add(PluginIC2.class);
|
|
|
|
|
pluginClasses.add(PluginNatura.class);
|
|
|
|
|
|
|
|
|
|
unloadedModules = new ArrayList<PluginModule>();
|
|
|
|
|
loadedModules = new ArrayList<PluginModule>();
|
|
|
|
|
|
|
|
|
|
for (Class<? extends ForestryPlugin>pluginClass : pluginClasses) {
|
|
|
|
|
PluginModule pluginModule = new PluginModule(pluginClass);
|
|
|
|
|
pluginModules.put(pluginClass, pluginModule);
|
|
|
|
|
unloadedModules.add(pluginModule);
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static Stage getStage() {
|
|
|
|
|
return stage;
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
public static PluginModule getPluginModule(Class<? extends ForestryPlugin> pluginClass) {
|
|
|
|
|
return pluginModules.get(pluginClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<PluginModule> getLoadedModules() {
|
|
|
|
|
return new ArrayList<PluginModule>(loadedModules);
|
2014-09-20 22:43:13 -07:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
public static boolean isModuleLoaded(Class<? extends ForestryPlugin> pluginClass) {
|
|
|
|
|
PluginModule pluginModule = pluginModules.get(pluginClass);
|
|
|
|
|
return loadedModules.contains(pluginModule);
|
2014-09-20 22:43:13 -07:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static void addPlugin(ForestryPlugin plugin) {
|
2014-05-02 21:31:20 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static void runPreInit() {
|
|
|
|
|
stage = Stage.SETUP;
|
|
|
|
|
Locale locale = Locale.getDefault();
|
|
|
|
|
Locale.setDefault(Locale.ENGLISH);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
Configuration config = new Configuration(new File(Forestry.instance.getConfigFolder(), MODULE_CONFIG_FILE_NAME));
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
config.load();
|
|
|
|
|
config.addCustomCategoryComment(CATEGORY_MODULES, "Disabling these modules can greatly change how the mod functions.\n"
|
|
|
|
|
+ "Your milage may vary, please report any issues.");
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
ArrayList<PluginModule> toLoad = new ArrayList<PluginModule>(unloadedModules);
|
|
|
|
|
Iterator<PluginModule> it = toLoad.iterator();
|
2014-09-20 22:43:13 -07:00
|
|
|
while (it.hasNext()) {
|
2014-09-22 22:41:25 -07:00
|
|
|
PluginModule m = it.next();
|
|
|
|
|
if (m == getPluginModule(PluginCore.class))
|
2014-09-20 22:43:13 -07:00
|
|
|
continue;
|
|
|
|
|
if (!isEnabled(config, m)) {
|
|
|
|
|
it.remove();
|
|
|
|
|
Proxies.log.info("Module disabled: {0}", m);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-09-22 22:41:25 -07:00
|
|
|
if (m.instance() == null) {
|
2014-09-20 22:43:13 -07:00
|
|
|
it.remove();
|
2014-09-22 22:41:25 -07:00
|
|
|
Proxies.log.info("Module {0} failed to load.", m);
|
2014-09-20 22:43:13 -07:00
|
|
|
continue;
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-09-20 22:43:13 -07:00
|
|
|
boolean changed;
|
|
|
|
|
do {
|
|
|
|
|
changed = false;
|
|
|
|
|
it = toLoad.iterator();
|
|
|
|
|
while (it.hasNext()) {
|
2014-09-22 22:41:25 -07:00
|
|
|
PluginModule m = it.next();
|
|
|
|
|
|
|
|
|
|
Set<Class<? extends ForestryPlugin>> dependenciesClasses = m.instance().getDependencies();
|
|
|
|
|
Set<PluginModule> dependencies = new HashSet<PluginModule>();
|
|
|
|
|
for (Class<? extends ForestryPlugin> depClass : dependenciesClasses)
|
|
|
|
|
dependencies.add(getPluginModule(depClass));
|
|
|
|
|
|
|
|
|
|
if (!toLoad.containsAll(dependencies)) {
|
2014-09-20 22:43:13 -07:00
|
|
|
it.remove();
|
|
|
|
|
changed = true;
|
2014-09-22 22:41:25 -07:00
|
|
|
Proxies.log.warning("Module {0} is missing dependencies: {1}", m, dependencies);
|
2014-09-20 22:43:13 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (changed);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
unloadedModules.removeAll(toLoad);
|
|
|
|
|
loadedModules.addAll(toLoad);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
if (config.hasChanged())
|
|
|
|
|
config.save();
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
Locale.setDefault(locale);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
stage = Stage.PRE_INIT;
|
2014-09-22 22:41:25 -07:00
|
|
|
for (PluginModule m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance();
|
2014-09-20 22:43:13 -07:00
|
|
|
loadPlugin(plugin);
|
|
|
|
|
Proxies.log.fine("Pre-Init Start: {0}", plugin);
|
|
|
|
|
plugin.preInit();
|
|
|
|
|
plugin.registerItems();
|
|
|
|
|
Proxies.log.fine("Pre-Init Complete: {0}", plugin);
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
private static void loadPlugin(ForestryPlugin plugin) {
|
|
|
|
|
Proxies.log.fine("Loading Plugin: {0}", plugin);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 20:30:25 -07:00
|
|
|
IGuiHandler guiHandler = plugin.getGuiHandler();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (guiHandler != null)
|
|
|
|
|
guiHandlers.add(guiHandler);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 20:30:25 -07:00
|
|
|
IPacketHandler packetHandler = plugin.getPacketHandler();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (packetHandler != null)
|
|
|
|
|
packetHandlers.add(packetHandler);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 20:30:25 -07:00
|
|
|
IPickupHandler pickupHandler = plugin.getPickupHandler();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (pickupHandler != null)
|
|
|
|
|
pickupHandlers.add(pickupHandler);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 20:30:25 -07:00
|
|
|
ISaveEventHandler saveHandler = plugin.getSaveEventHandler();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (saveHandler != null)
|
|
|
|
|
saveEventHandlers.add(saveHandler);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 20:30:25 -07:00
|
|
|
IResupplyHandler resupplyHandler = plugin.getResupplyHandler();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (resupplyHandler != null)
|
|
|
|
|
resupplyHandlers.add(resupplyHandler);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 20:30:25 -07:00
|
|
|
IOreDictionaryHandler dictionaryHandler = plugin.getDictionaryHandler();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (dictionaryHandler != null)
|
|
|
|
|
dictionaryHandlers.add(dictionaryHandler);
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 20:30:25 -07:00
|
|
|
IFuelHandler fuelHandler = plugin.getFuelHandler();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (fuelHandler != null)
|
|
|
|
|
GameRegistry.registerFuelHandler((fuelHandler));
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static void runInit() {
|
|
|
|
|
stage = Stage.INIT;
|
2014-09-22 22:41:25 -07:00
|
|
|
for (PluginModule m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance();
|
2014-09-20 22:43:13 -07:00
|
|
|
Proxies.log.fine("Init Start: {0}", plugin);
|
|
|
|
|
plugin.registerBackpackItems();
|
|
|
|
|
plugin.registerCrates();
|
|
|
|
|
plugin.doInit();
|
|
|
|
|
Proxies.log.fine("Init Complete: {0}", plugin);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static void runPostInit() {
|
|
|
|
|
stage = Stage.POST_INIT;
|
2014-09-22 22:41:25 -07:00
|
|
|
for (PluginModule m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance();
|
2014-09-20 22:43:13 -07:00
|
|
|
Proxies.log.fine("Post-Init Start: {0}", plugin);
|
|
|
|
|
plugin.registerRecipes();
|
|
|
|
|
plugin.postInit();
|
|
|
|
|
Proxies.log.fine("Post-Init Complete: {0}", plugin);
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
stage = Stage.INIT_DISABLED;
|
2014-09-22 22:41:25 -07:00
|
|
|
for (PluginModule m : unloadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance();
|
|
|
|
|
Proxies.log.fine("Disabled-Init Start: {0}", m);
|
|
|
|
|
if (plugin != null)
|
|
|
|
|
plugin.disabledInit();
|
|
|
|
|
Proxies.log.fine("Disabled-Init Complete: {0}", m);
|
2014-09-20 22:43:13 -07:00
|
|
|
}
|
|
|
|
|
stage = Stage.FINISHED;
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static void serverStarting(MinecraftServer server) {
|
|
|
|
|
CommandHandler commandManager = (CommandHandler) server.getCommandManager();
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
for (PluginModule m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance();
|
2014-09-20 22:43:13 -07:00
|
|
|
ICommand[] commands = plugin.getConsoleCommands();
|
|
|
|
|
if (commands == null)
|
|
|
|
|
continue;
|
|
|
|
|
for (ICommand command : commands)
|
|
|
|
|
commandManager.registerCommand(command);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static void processIMCMessages(ImmutableList<FMLInterModComms.IMCMessage> messages) {
|
|
|
|
|
for (FMLInterModComms.IMCMessage message : messages)
|
2014-09-22 22:41:25 -07:00
|
|
|
for (PluginModule m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance();
|
2014-09-20 22:43:13 -07:00
|
|
|
if (plugin.processIMCMessage(message))
|
|
|
|
|
break;
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
2014-09-20 22:43:13 -07:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
public static void generateSurface(World world, Random rand, int chunkX, int chunkZ) {
|
2014-09-22 22:41:25 -07:00
|
|
|
for (PluginModule m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance();
|
2014-09-20 22:43:13 -07:00
|
|
|
plugin.generateSurface(world, rand, chunkX, chunkZ);
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
2014-09-20 22:43:13 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-22 22:41:25 -07:00
|
|
|
private static boolean isEnabled(Configuration config, PluginModule m) {
|
2014-09-20 22:43:13 -07:00
|
|
|
boolean defaultValue = true;
|
|
|
|
|
Property prop = config.get(CATEGORY_MODULES, m.toString().toLowerCase(Locale.ENGLISH).replace('_', '.'), defaultValue);
|
|
|
|
|
return prop.getBoolean(true);
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|