Change the plugin system to a module system and slit up modules

-Moved the woodpile from the arboriculture module to the charcoal module
-Moved the backpacks from the storage module to the backpacks module
-Moved the crates from the storage module to the creates module
-Added the database module with a new storage for individuals
-Added a gui element system to improve the alyzer api
-Added a species plugin interface to handle the database gui
-Added some alyzer methods to the species plugin to move the alyzer
integration later to it
This commit is contained in:
Nedelosk committed 2017-11-02 16:03:33 +01:00
1 parent ee272de168
commit 88fd031fb4
277 files changed
+7361 -1930

No files matched your search

@@ -0,0 +1,28 @@
package forestry.modules;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import net.minecraft.util.ResourceLocation;
import forestry.core.config.Constants;
public class BlankForestryModule implements IForestryModule {
/**
* The ForestryModule.moduleID()s of any other modules this module depends on.
*/
public Set<ResourceLocation> getDependencyUids(){
return ImmutableSet.of(new ResourceLocation(Constants.MOD_ID, ForestryModuleUids.CORE));
}
@Override
public String toString() {
ForestryModule forestryModule = getClass().getAnnotation(ForestryModule.class);
if (forestryModule == null) {
return getClass().getSimpleName();
}
return forestryModule.name() + " Module";
}
}
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.modules;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Annotation to provide additional information on IForestryModules. This information will be available via the "/forestry module info {@link #moduleID()}" command ingame.
*
* @author Nedelosk
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ForestryModule {
/**
* @return Unique identifier for the module, no spaces!
*/
String moduleID();
/**
* @return The unique identifier of the module container.
*/
String containerID();
/**
* @return Nice and readable module name.
*/
String name();
/**
* @return ForestryModule author's name.
*/
String author() default "";
/**
* @return URL of plugin homepage.
*/
String url() default "";
/**
* @return Not used (yet?).
*/
String help() default "";
/**
* @return The name of the loot table file, if any.
*/
String lootTable() default "";
/**
* @return True if this is a core module.
*/
boolean coreModule() default false;
/**
* @return Version of the module, if any.
*/
String version() default "";
/**
* @return Localization key for a short description what the module does.
*/
String unlocalizedDescription() default "";
}
@@ -0,0 +1,39 @@
package forestry.modules;
public class ForestryModuleUids {
//Forestry
public static final String APICULTURE = "apiculture";
public static final String ARBORICULTURE = "arboriculture";
public static final String CHARCOAL = "charcoal";
public static final String CORE = "core";
public static final String ENERGY = "energy";
public static final String FACTORY = "factory";
public static final String FARMING = "farming";
public static final String GREENHOUSE = "greenhouse";
public static final String FLUIDS = "fluids";
public static final String FOOD = "food";
public static final String LEPIDOPTEROLOGY = "lepidopterology";
public static final String MAIL = "mail";
public static final String CRATE = "crates";
public static final String BACKPACKS = "backpacks";
public static final String DATABASE = "database";
//Mod Plugins
public static final String BIOMES_O_PLENTY = "biomes.o.plenty";
public static final String BUILDCRAFT_FUELS = "buildcraft.fuels";
public static final String BUILDCRAFT_RECIPES = "buildcraft.recipes";
public static final String BUILDCRAFT_STATEMENTS = "buildcraft.statements";
public static final String BUILDCRAFT_TRANSPORT = "buildcraft.transport";
public static final String CHISEL = "chisel";
public static final String ENDER_IO = "ender.io";
public static final String EXTRA_UTILITIES = "extra.utilities";
public static final String NATURA = "natura";
public static final String INDUSTRIALCRAFT2 = "industrialcraft2";
public static final String ACT_ADD = "actuallyadditions";
public static final String HARVESTCRAFT = "harvestcraft";
public static final String IMMERSIVE_ENGINEERING = "immersiveengineering";
public static final String MAGICAL_AGRICULTURE = "mysticalagriculture";
public static final String RUSTIC = "rustic";
public static final String ROOTS = "roots";
public static final String BETTER_WITH_MODS = "betterwithmods";
}
@@ -0,0 +1,46 @@
package forestry.modules;
import java.io.File;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import forestry.Forestry;
import forestry.core.config.Constants;
public class ForestryModules implements IModuleContainer {
private static final String MODULE_CONFIG_FILE_NAME = "modules.cfg";
private static final String CONFIG_CATEGORY = "modules";
private static Configuration config;
@Override
public String getID() {
return Constants.MOD_ID;
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public Configuration getModulesConfig() {
return getModulesConfiguration();
}
public static final Configuration getModulesConfiguration(){
if(config == null){
config = new Configuration(new File(Forestry.instance.getConfigFolder(), MODULE_CONFIG_FILE_NAME));
}
return config;
}
@Override
public boolean isModuleEnabled(IForestryModule module) {
ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
String comment = ForestryPluginUtil.getComment(module);
Property prop = getModulesConfig().get(CONFIG_CATEGORY, info.moduleID(), true, comment);
return prop.getBoolean();
}
}
@@ -0,0 +1,71 @@
package forestry.modules;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.translation.I18n;
import net.minecraftforge.fml.common.discovery.ASMDataTable;
import forestry.core.utils.Log;
public class ForestryPluginUtil {
private ForestryPluginUtil() {
}
public static Map<String, List<IForestryModule>> getForestryModules(ASMDataTable asmDataTable) {
List<IForestryModule> instances = getInstances(asmDataTable, ForestryModule.class, IForestryModule.class);
Map<String, List<IForestryModule>> modules = new LinkedHashMap<>();
for(IForestryModule module : instances){
ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
modules.computeIfAbsent(info.containerID(), k-> new ArrayList<>()).add(module);
}
return modules;
}
public static String getComment(IForestryModule module) {
ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
String comment = I18n.translateToLocal(info.unlocalizedDescription());
Set<ResourceLocation> dependencies = module.getDependencyUids();
if(!dependencies.isEmpty()){
Iterator<ResourceLocation> iDependencies = dependencies.iterator();
StringBuilder builder = new StringBuilder(comment);
builder.append("\n");
builder.append("Dependencies: [ ");
builder.append(iDependencies.next());
while(iDependencies.hasNext()){
ResourceLocation uid = iDependencies.next();
builder.append(", ").append(uid.toString());
}
builder.append(" ]");
comment = builder.toString();
}
return comment;
}
private static <T> List<T> getInstances(ASMDataTable asmDataTable, Class annotationClass, Class<T> instanceClass) {
String annotationClassName = annotationClass.getCanonicalName();
Set<ASMDataTable.ASMData> asmDatas = asmDataTable.getAll(annotationClassName);
List<T> instances = new ArrayList<>();
for (ASMDataTable.ASMData asmData : asmDatas) {
try {
Class<?> asmClass = Class.forName(asmData.getClassName());
Class<? extends T> asmInstanceClass = asmClass.asSubclass(instanceClass);
T instance = asmInstanceClass.newInstance();
instances.add(instance);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
Log.error("Failed to load: {}", asmData.getClassName(), e);
}
}
return instances;
}
}
@@ -0,0 +1,116 @@
package forestry.modules;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Set;
import net.minecraft.command.ICommand;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.IChunkGenerator;
import net.minecraftforge.fml.common.event.FMLInterModComms;
import forestry.core.IPickupHandler;
import forestry.core.IResupplyHandler;
import forestry.core.ISaveEventHandler;
import forestry.core.network.IPacketRegistry;
public interface IForestryModule {
default boolean isAvailable() {
return true;
}
default boolean canBeDisabled() {
return true;
}
default String getFailMessage() {
return "";
}
/**
* The ForestryModule.moduleID()s of any other modules this module depends on.
*/
default Set<ResourceLocation> getDependencyUids(){
return Collections.emptySet();
}
default void setupAPI() {
}
default void disabledSetupAPI() {
}
default void registerItemsAndBlocks() {
}
default void preInit() {
}
default void registerTriggers() {
}
default void registerBackpackItems() {
}
default void registerCrates() {
}
default void doInit() {
}
default void registerRecipes(){
}
default void addLootPoolNames(Set<String> lootPoolNames){
}
default void postInit(){
}
default boolean processIMCMessage(FMLInterModComms.IMCMessage message){
return false;
}
default void populateChunk(IChunkGenerator chunkGenerator, World world, Random rand, int chunkX, int chunkZ, boolean hasVillageGenerated){
}
default void populateChunkRetroGen(World world, Random rand, int chunkX, int chunkZ){
}
default void decorateBiome(World world, Random rand, BlockPos pos){
}
default void getHiddenItems(List<ItemStack> hiddenItems){
}
@Nullable
default ISaveEventHandler getSaveEventHandler(){
return null;
}
@Nullable
default IPacketRegistry getPacketRegistry(){
return null;
}
@Nullable
default IPickupHandler getPickupHandler(){
return null;
}
@Nullable
default IResupplyHandler getResupplyHandler(){
return null;
}
@Nullable
default ICommand[] getConsoleCommands(){
return null;
}
}
@@ -0,0 +1,19 @@
package forestry.modules;
import net.minecraftforge.common.config.Configuration;
public interface IModuleContainer {
/**
* @return Unique identifier for the module container, no spaces!
*/
String getID();
boolean isAvailable();
Configuration getModulesConfig();
/**
* @return true if the module is enabled in the config file of this container.
*/
boolean isModuleEnabled(IForestryModule module);
}
@@ -0,0 +1,18 @@
package forestry.modules;
import java.util.Collection;
import net.minecraft.util.ResourceLocation;
public interface IModuleManager {
default boolean isModuleEnabled(String containerID, String moduleID){
return isModuleEnabled(new ResourceLocation(containerID, moduleID));
}
boolean isModuleEnabled(ResourceLocation id);
void registerContainers(IModuleContainer... container);
Collection<IModuleContainer> getContainers();
}
@@ -0,0 +1,417 @@
/*******************************************************************************
* 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
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.modules;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
import net.minecraft.command.CommandHandler;
import net.minecraft.command.ICommand;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.IChunkGenerator;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.discovery.ASMDataTable;
import net.minecraftforge.fml.common.event.FMLInterModComms;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;
import forestry.api.core.ForestryAPI;
import forestry.core.IPickupHandler;
import forestry.core.IResupplyHandler;
import forestry.core.ISaveEventHandler;
import forestry.core.config.Constants;
import forestry.core.network.IPacketRegistry;
import forestry.core.utils.Log;
public class ModuleManager implements IModuleManager{
private static final String CONFIG_CATEGORY = "modules";
private static ModuleManager ourInstance = new ModuleManager();
public static final ArrayList<IPickupHandler> pickupHandlers = Lists.newArrayList();
public static final ArrayList<ISaveEventHandler> saveEventHandlers = Lists.newArrayList();
public static final ArrayList<IResupplyHandler> resupplyHandlers = Lists.newArrayList();
private static final HashMap<ResourceLocation, IForestryModule> sortedModules = new LinkedHashMap<>();
private static final Set<IForestryModule> loadedModules = new LinkedHashSet<>();
private static final Set<IForestryModule> unloadedModules = new LinkedHashSet<>();
private static final HashMap<String, IModuleContainer> moduleContainers = new HashMap<>();
public static final Set<IForestryModule> configDisabledModules = new HashSet<>();
private static Stage stage = Stage.SETUP;
public enum Stage {
SETUP, // setup API to make it functional. GameMode Configs are not yet accessible
SETUP_DISABLED, // setup fallback API to avoid crashes
REGISTER, // register basic blocks and items
PRE_INIT, // register handlers, triggers, definitions, and anything that depends on basic items
BACKPACKS_CRATES, // backpacks, crates
INIT, // anything that depends on PreInit stages, recipe registration
POST_INIT, // stubborn mod integration, dungeon loot, and finalization of things that take input from mods
FINISHED
}
private ModuleManager() {
}
public static ModuleManager getInstance() {
return ourInstance;
}
@Override
public boolean isModuleEnabled(ResourceLocation id) {
return sortedModules.get(id) != null;
}
@Override
public void registerContainers(IModuleContainer... containers) {
for(IModuleContainer container : containers) {
Preconditions.checkNotNull(container);
moduleContainers.put(container.getID(), container);
}
}
@Override
public Collection<IModuleContainer> getContainers() {
return moduleContainers.values();
}
public static Stage getStage() {
return stage;
}
public static Set<IForestryModule> getLoadedModules() {
return ImmutableSet.copyOf(sortedModules.values());
}
private static void registerHandlers(IForestryModule module, Side side) {
Log.debug("Registering Handlers for Module: {}", module);
IPacketRegistry packetRegistry = module.getPacketRegistry();
if (packetRegistry != null) {
packetRegistry.registerPacketsServer();
if (side == Side.CLIENT) {
packetRegistry.registerPacketsClient();
}
}
IPickupHandler pickupHandler = module.getPickupHandler();
if (pickupHandler != null) {
pickupHandlers.add(pickupHandler);
}
ISaveEventHandler saveHandler = module.getSaveEventHandler();
if (saveHandler != null) {
saveEventHandlers.add(saveHandler);
}
IResupplyHandler resupplyHandler = module.getResupplyHandler();
if (resupplyHandler != null) {
resupplyHandlers.add(resupplyHandler);
}
}
private static IForestryModule getModuleCore(List<IForestryModule> forestryModules) {
for (IForestryModule module : forestryModules) {
ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
if (module.isAvailable() && info.coreModule()) {
return module;
}
}
return null;
}
private static IForestryModule getCoreModule(List<IForestryModule> 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<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(x -> x.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(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());
ForestryAPI.enabledModules = new HashSet<>();
for (IForestryModule module : sortedModules.values()) {
ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
ForestryAPI.enabledModules.add(new ResourceLocation(info.containerID(), info.moduleID()));
}
Locale.setDefault(locale);
}
public static void runSetup(FMLPreInitializationEvent event) {
ASMDataTable asmDataTable = event.getAsmData();
Map<String, List<IForestryModule>> forestryModules = ForestryPluginUtil.getForestryModules(asmDataTable);
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);
}
}
public static void runPreInit(Side side) {
stage = Stage.PRE_INIT;
for (IForestryModule module : loadedModules) {
Log.debug("Pre-Init Start: {}", module);
registerHandlers(module, side);
module.preInit();
if (getInstance().isModuleEnabled(Constants.MOD_ID, ForestryModuleUids.BUILDCRAFT_STATEMENTS)) {
module.registerTriggers();
}
Log.debug("Pre-Init Complete: {}", module);
}
}
public static void runRegisterBackpacksAndCrates() {
stage = Stage.BACKPACKS_CRATES;
for (IForestryModule module : loadedModules) {
if (getInstance().isModuleEnabled(Constants.MOD_ID, ForestryModuleUids.CRATE)) {
Log.debug("BackpacksAndCrates Start: {}", module);
module.registerBackpackItems();
module.registerCrates();
Log.debug("BackpacksAndCrates Complete: {}", module);
}
}
}
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);
}
}
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);
}
stage = Stage.FINISHED;
}
public static void serverStarting(MinecraftServer server) {
CommandHandler commandManager = (CommandHandler) server.getCommandManager();
for (IForestryModule module : loadedModules) {
ICommand[] commands = module.getConsoleCommands();
if (commands == null) {
continue;
}
for (ICommand command : commands) {
commandManager.registerCommand(command);
}
}
}
public static void processIMCMessages(ImmutableList<FMLInterModComms.IMCMessage> messages) {
for (FMLInterModComms.IMCMessage message : messages) {
for (IForestryModule module : loadedModules) {
if (module.processIMCMessage(message)) {
break;
}
}
}
}
public static void populateChunk(IChunkGenerator chunkProvider, World world, Random rand, int chunkX, int chunkZ, boolean hasVillageGenerated) {
for (IForestryModule module : loadedModules) {
module.populateChunk(chunkProvider, world, rand, chunkX, chunkZ, hasVillageGenerated);
}
}
public static void decorateBiome(World world, Random rand, BlockPos pos) {
for (IForestryModule module : loadedModules) {
module.decorateBiome(world, rand, pos);
}
}
public static void populateChunkRetroGen(World world, Random rand, int chunkX, int chunkZ) {
for (IForestryModule module : loadedModules) {
module.populateChunkRetroGen(world, rand, chunkX, chunkZ);
}
}
public static List<ItemStack> getHiddenItems() {
List<ItemStack> hiddenItems = new ArrayList<>();
for (IForestryModule module : loadedModules) {
module.getHiddenItems(hiddenItems);
}
return hiddenItems;
}
public static Set<String> getLootPoolNames() {
Set<String> lootPoolNames = new HashSet<>();
for (IForestryModule module : loadedModules) {
module.addLootPoolNames(lootPoolNames);
}
return lootPoolNames;
}
public static Set<String> getLootTableFiles() {
Set<String> lootTableNames = new HashSet<>();
for (IForestryModule module : loadedModules) {
ForestryModule info = module.getClass().getAnnotation(ForestryModule.class);
String lootTableFolder = info.lootTable();
if (!lootTableFolder.isEmpty()) {
lootTableNames.add(lootTableFolder);
}
}
return lootTableNames;
}
}
@@ -0,0 +1,9 @@
@ParametersAreNonnullByDefault
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
package forestry.modules;
import javax.annotation.ParametersAreNonnullByDefault;
import forestry.core.utils.FieldsAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;