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-09-22 23:20:58 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2014-08-21 11:00:52 -04:00
|
|
|
import com.google.common.collect.Lists;
|
2014-09-22 23:20:58 -07:00
|
|
|
|
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-09-22 23:20:58 -07:00
|
|
|
|
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-22 23:20:58 -07:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.EnumSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.Set;
|
2014-09-20 22:43:13 -07:00
|
|
|
import net.minecraft.command.CommandHandler;
|
|
|
|
|
import net.minecraft.command.ICommand;
|
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
2014-09-27 12:50:24 -07:00
|
|
|
import net.minecraft.util.StatCollector;
|
2014-09-20 22:43:13 -07:00
|
|
|
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
|
|
|
|
|
|
|
|
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 23:20:58 -07:00
|
|
|
private static final Set<Module> loadedModules = EnumSet.noneOf(Module.class);
|
|
|
|
|
private static final Set<Module> unloadedModules = EnumSet.allOf(Module.class);
|
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-11-28 18:09:51 -08:00
|
|
|
SETUP, PRE_INIT, INIT, POST_INIT, INIT_DISABLED, FINISHED
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-22 23:20:58 -07:00
|
|
|
public enum Module {
|
|
|
|
|
|
|
|
|
|
CORE(new PluginCore()),
|
|
|
|
|
APICULTURE(new PluginApiculture()),
|
|
|
|
|
ARBORICULTURE(new PluginArboriculture()),
|
|
|
|
|
ENERGY(new PluginEnergy()),
|
|
|
|
|
FACTORY(new PluginFactory()),
|
|
|
|
|
FARMING(new PluginFarming()),
|
|
|
|
|
FOOD(new PluginFood()),
|
|
|
|
|
LEPIDOPTEROLOGY(new PluginLepidopterology()),
|
|
|
|
|
MAIL(new PluginMail()),
|
|
|
|
|
STORAGE(new PluginStorage()),
|
|
|
|
|
BUILDCRAFT(new PluginBuildCraft()),
|
|
|
|
|
PROPOLIS_PIPE(new PluginPropolisPipe()),
|
|
|
|
|
EQUIVELENT_EXCHANGE(new PluginEE()),
|
|
|
|
|
FARM_CRAFTORY(new PluginFarmCraftory()),
|
|
|
|
|
INDUSTRIALCRAFT(new PluginIC2()),
|
|
|
|
|
NATURA(new PluginNatura()),;
|
|
|
|
|
|
|
|
|
|
private final ForestryPlugin instance;
|
|
|
|
|
|
|
|
|
|
private Module(ForestryPlugin plugin) {
|
|
|
|
|
this.instance = plugin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ForestryPlugin instance() {
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isEnabled() {
|
|
|
|
|
return isModuleLoaded(this);
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|
2014-09-22 23:20:58 -07:00
|
|
|
|
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 23:20:58 -07:00
|
|
|
public static EnumSet<Module> getLoadedModules() {
|
|
|
|
|
return EnumSet.copyOf(loadedModules);
|
2014-09-20 22:43:13 -07:00
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 23:20:58 -07:00
|
|
|
public static boolean isModuleLoaded(Module module) {
|
|
|
|
|
return loadedModules.contains(module);
|
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"
|
2014-09-27 12:50:24 -07:00
|
|
|
+ "Your mileage may vary, please report any issues.");
|
2014-05-02 21:31:20 +02:00
|
|
|
|
2014-09-22 23:20:58 -07:00
|
|
|
Set<Module> toLoad = EnumSet.allOf(Module.class);
|
|
|
|
|
Iterator<Module> it = toLoad.iterator();
|
2014-09-20 22:43:13 -07:00
|
|
|
while (it.hasNext()) {
|
2014-09-22 23:20:58 -07:00
|
|
|
Module m = it.next();
|
|
|
|
|
if (m == Module.CORE)
|
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 23:20:58 -07:00
|
|
|
ForestryPlugin plugin = m.instance;
|
|
|
|
|
if (!plugin.isAvailable()) {
|
2014-09-20 22:43:13 -07:00
|
|
|
it.remove();
|
2014-09-22 23:20:58 -07:00
|
|
|
Proxies.log.info("Module {0} failed to load: {1}", plugin, plugin.getFailMessage());
|
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 23:20:58 -07:00
|
|
|
Module m = it.next();
|
|
|
|
|
Set<Module> deps = m.instance().getDependancies();
|
|
|
|
|
if (!toLoad.containsAll(deps)) {
|
2014-09-20 22:43:13 -07:00
|
|
|
it.remove();
|
|
|
|
|
changed = true;
|
2014-09-22 23:20:58 -07:00
|
|
|
Proxies.log.warning("Module {0} is missing dependancies: {1}", m, deps);
|
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 23:20:58 -07:00
|
|
|
for (Module m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance;
|
2014-09-20 22:43:13 -07:00
|
|
|
loadPlugin(plugin);
|
2014-09-25 18:08:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Module m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance;
|
2014-09-20 22:43:13 -07:00
|
|
|
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) {
|
2014-09-25 18:08:36 -07:00
|
|
|
Proxies.log.fine("Registering Handlers for 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 23:20:58 -07:00
|
|
|
for (Module m : loadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance;
|
2014-09-20 22:43:13 -07:00
|
|
|
Proxies.log.fine("Init Start: {0}", plugin);
|
2014-10-01 19:41:18 -07:00
|
|
|
|
|
|
|
|
if (Module.STORAGE.isEnabled()) {
|
|
|
|
|
plugin.registerBackpackItems();
|
|
|
|
|
plugin.registerCrates();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
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 23:20:58 -07:00
|
|
|
for (Module 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 23:20:58 -07:00
|
|
|
for (Module m : unloadedModules) {
|
|
|
|
|
ForestryPlugin plugin = m.instance;
|
|
|
|
|
Proxies.log.fine("Disabled-Init Start: {0}", plugin);
|
|
|
|
|
plugin.disabledInit();
|
|
|
|
|
Proxies.log.fine("Disabled-Init Complete: {0}", plugin);
|
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 23:20:58 -07:00
|
|
|
for (Module 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 23:20:58 -07:00
|
|
|
for (Module 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 23:20:58 -07:00
|
|
|
for (Module 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 23:20:58 -07:00
|
|
|
private static boolean isEnabled(Configuration config, Module m) {
|
2014-09-27 12:50:24 -07:00
|
|
|
Plugin info = m.instance().getClass().getAnnotation(Plugin.class);
|
|
|
|
|
|
2014-09-20 22:43:13 -07:00
|
|
|
boolean defaultValue = true;
|
|
|
|
|
Property prop = config.get(CATEGORY_MODULES, m.toString().toLowerCase(Locale.ENGLISH).replace('_', '.'), defaultValue);
|
2014-09-27 12:50:24 -07:00
|
|
|
prop.comment = StatCollector.translateToLocal(info.unlocalizedDescription());
|
2014-09-20 22:43:13 -07:00
|
|
|
return prop.getBoolean(true);
|
|
|
|
|
}
|
2014-05-02 21:31:20 +02:00
|
|
|
}
|