Skip to content

Commit

Permalink
Load plugins during postInit
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Oct 23, 2016
1 parent 6be170e commit 85b4c51
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 69 deletions.
11 changes: 9 additions & 2 deletions src/main/java/mezz/jei/IngredientBaseListFactory.java
Expand Up @@ -18,13 +18,16 @@
import mezz.jei.util.Java6Helper;
import mezz.jei.util.Log;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.ProgressManager;

public class IngredientBaseListFactory {
private IngredientBaseListFactory() {

}

public static ImmutableList<IIngredientListElement> create(IIngredientRegistry ingredientRegistry, JeiHelpers jeiHelpers) {
public static ImmutableList<IIngredientListElement> create() {
IIngredientRegistry ingredientRegistry = Internal.getIngredientRegistry();
JeiHelpers jeiHelpers = Internal.getHelpers();
IngredientChecker ingredientChecker = new IngredientChecker(jeiHelpers);

List<IIngredientListElement> ingredientListElements = new LinkedList<IIngredientListElement>();
Expand All @@ -41,7 +44,10 @@ private static <V> List<IIngredientListElement> addToBaseList(List<IIngredientLi
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredientClass);
IIngredientRenderer<V> ingredientRenderer = ingredientRegistry.getIngredientRenderer(ingredientClass);

for (V ingredient : ingredientRegistry.getIngredients(ingredientClass)) {
ImmutableList<V> ingredients = ingredientRegistry.getIngredients(ingredientClass);
ProgressManager.ProgressBar progressBar = ProgressManager.push("Adding " + ingredientClass.getSimpleName() + " ingredients.", ingredients.size());
for (V ingredient : ingredients) {
progressBar.step("");
if (ingredient != null) {
if (!ingredientChecker.isIngredientHidden(ingredient, ingredientHelper)) {
IngredientListElement<V> ingredientListElement = IngredientListElement.create(ingredient, ingredientHelper, ingredientRenderer);
Expand All @@ -51,6 +57,7 @@ private static <V> List<IIngredientListElement> addToBaseList(List<IIngredientLi
}
}
}
ProgressManager.pop(progressBar);

return baseList;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mezz/jei/ItemFilter.java
Expand Up @@ -29,11 +29,11 @@ public class ItemFilter {

private ImmutableList<IIngredientListElement> baseList;

public void build(IIngredientRegistry ingredientRegistry, JeiHelpers jeiHelpers) {
public void build() {
Log.info("Building item filter...");
long start_time = System.currentTimeMillis();

this.baseList = IngredientBaseListFactory.create(ingredientRegistry, jeiHelpers);
this.baseList = IngredientBaseListFactory.create();
this.filteredItemMapsCache.invalidateAll();

Log.info("Built item filter in {} ms", System.currentTimeMillis() - start_time);
Expand Down
57 changes: 48 additions & 9 deletions src/main/java/mezz/jei/JeiStarter.java
@@ -1,5 +1,6 @@
package mezz.jei;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;

Expand All @@ -13,18 +14,28 @@
import mezz.jei.util.ModIdUtil;
import mezz.jei.util.ModRegistry;
import mezz.jei.util.StackHelper;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.ProgressManager;

public class JeiStarter {
public static JeiRuntime startJEI(List<IModPlugin> plugins) {
private final ModRegistry modRegistry;
private final StackHelper stackHelper;
private final IngredientRegistry ingredientRegistry;

private boolean started;
@Nullable
private GuiEventHandler guiEventHandler;

public JeiStarter(List<IModPlugin> plugins) {
SubtypeRegistry subtypeRegistry = new SubtypeRegistry();

registerItemSubtypes(plugins, subtypeRegistry);

StackHelper stackHelper = new StackHelper(subtypeRegistry);
stackHelper = new StackHelper(subtypeRegistry);
stackHelper.enableUidCache();
Internal.setStackHelper(stackHelper);

IngredientRegistry ingredientRegistry = registerIngredients(plugins);
ingredientRegistry = registerIngredients(plugins);
Internal.setIngredientRegistry(ingredientRegistry);

JeiHelpers jeiHelpers = new JeiHelpers(ingredientRegistry, stackHelper, subtypeRegistry);
Expand All @@ -33,21 +44,23 @@ public static JeiRuntime startJEI(List<IModPlugin> plugins) {
ModIdUtil modIdUtil = Internal.getModIdUtil();
ItemRegistry itemRegistry = new ItemRegistry(ingredientRegistry, modIdUtil);

ModRegistry modRegistry = new ModRegistry(jeiHelpers, itemRegistry, ingredientRegistry);
modRegistry = new ModRegistry(jeiHelpers, itemRegistry, ingredientRegistry);

registerPlugins(plugins, modRegistry);
}

public void start(List<IModPlugin> plugins) {
stackHelper.enableUidCache();

Log.info("Building recipe registry...");
long start_time = System.currentTimeMillis();
RecipeRegistry recipeRegistry = modRegistry.createRecipeRegistry(stackHelper, ingredientRegistry);
Log.info("Built recipe registry in {} ms", System.currentTimeMillis() - start_time);

ItemFilter itemFilter = new ItemFilter();
itemFilter.build(ingredientRegistry, jeiHelpers);

Log.info("Building runtime...");
start_time = System.currentTimeMillis();
List<IAdvancedGuiHandler<?>> advancedGuiHandlers = modRegistry.getAdvancedGuiHandlers();
ItemFilter itemFilter = new ItemFilter();
ItemListOverlay itemListOverlay = new ItemListOverlay(itemFilter, advancedGuiHandlers, ingredientRegistry);
RecipesGui recipesGui = new RecipesGui(recipeRegistry);
JeiRuntime jeiRuntime = new JeiRuntime(recipeRegistry, itemListOverlay, recipesGui, ingredientRegistry);
Expand All @@ -56,16 +69,30 @@ public static JeiRuntime startJEI(List<IModPlugin> plugins) {

stackHelper.disableUidCache();

itemFilter.build();

sendRuntime(plugins, jeiRuntime);

return jeiRuntime;
if (guiEventHandler != null) {
MinecraftForge.EVENT_BUS.unregister(guiEventHandler);
}
guiEventHandler = new GuiEventHandler(jeiRuntime);
MinecraftForge.EVENT_BUS.register(guiEventHandler);

started = true;
}

public boolean hasStarted() {
return started;
}

private static void registerItemSubtypes(List<IModPlugin> plugins, SubtypeRegistry subtypeRegistry) {
ProgressManager.ProgressBar progressBar = ProgressManager.push("Registering item subtypes", plugins.size());
Iterator<IModPlugin> iterator = plugins.iterator();
while (iterator.hasNext()) {
IModPlugin plugin = iterator.next();
try {
progressBar.step(plugin.getClass().getName());
plugin.registerItemSubtypes(subtypeRegistry);
} catch (RuntimeException e) {
Log.error("Failed to register item subtypes for mod plugin: {}", plugin.getClass(), e);
Expand All @@ -74,15 +101,18 @@ private static void registerItemSubtypes(List<IModPlugin> plugins, SubtypeRegist
// legacy mod plugins do not have registerItemSubtypes
}
}
ProgressManager.pop(progressBar);
}

private static IngredientRegistry registerIngredients(List<IModPlugin> plugins) {
ProgressManager.ProgressBar progressBar = ProgressManager.push("Registering ingredients", plugins.size());
ModIngredientRegistration modIngredientRegistry = new ModIngredientRegistration();

Iterator<IModPlugin> iterator = plugins.iterator();
while (iterator.hasNext()) {
IModPlugin plugin = iterator.next();
try {
progressBar.step(plugin.getClass().getName());
plugin.registerIngredients(modIngredientRegistry);
} catch (RuntimeException e) {
if (VanillaPlugin.class.isInstance(plugin)) {
Expand All @@ -98,15 +128,18 @@ private static IngredientRegistry registerIngredients(List<IModPlugin> plugins)
// legacy mod plugins do not have registerIngredients
}
}
ProgressManager.pop(progressBar);

return modIngredientRegistry.createIngredientRegistry();
}

private static void registerPlugins(List<IModPlugin> plugins, ModRegistry modRegistry) {
ProgressManager.ProgressBar progressBar = ProgressManager.push("Registering plugins", plugins.size());
Iterator<IModPlugin> iterator = plugins.iterator();
while (iterator.hasNext()) {
IModPlugin plugin = iterator.next();
try {
progressBar.step(plugin.getClass().getName());
long start_time = System.currentTimeMillis();
Log.info("Registering plugin: {} ...", plugin.getClass().getName());
plugin.register(modRegistry);
Expand All @@ -120,18 +153,23 @@ private static void registerPlugins(List<IModPlugin> plugins, ModRegistry modReg
iterator.remove();
}
}
ProgressManager.pop(progressBar);
}

private static void sendRuntime(List<IModPlugin> plugins, IJeiRuntime jeiRuntime) {
ProgressManager.ProgressBar progressBar = ProgressManager.push("Sending Runtime", plugins.size());
Iterator<IModPlugin> iterator = plugins.iterator();
while (iterator.hasNext()) {
IModPlugin plugin = iterator.next();
try {
progressBar.step(plugin.getClass().getName());
long start_time = System.currentTimeMillis();
Log.info("Sending runtime to plugin: {} ...", plugin.getClass().getName());
plugin.onRuntimeAvailable(jeiRuntime);
long timeElapsedMs = System.currentTimeMillis() - start_time;
Log.info("Sent runtime to plugin: {} in {} ms", plugin.getClass().getName(), timeElapsedMs);
if (timeElapsedMs > 100) {
Log.warning("Sending runtime to plugin: {} took {} ms", plugin.getClass().getName(), timeElapsedMs);
}
} catch (RuntimeException e) {
Log.error("Sending runtime to plugin failed: {}", plugin.getClass(), e);
iterator.remove();
Expand All @@ -140,5 +178,6 @@ private static void sendRuntime(List<IModPlugin> plugins, IJeiRuntime jeiRuntime
iterator.remove();
}
}
ProgressManager.pop(progressBar);
}
}
64 changes: 23 additions & 41 deletions src/main/java/mezz/jei/ProxyCommonClient.java
@@ -1,10 +1,10 @@
package mezz.jei;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

import mezz.jei.api.IModPlugin;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.config.Config;
import mezz.jei.config.Constants;
import mezz.jei.config.KeyBindings;
Expand All @@ -20,7 +20,6 @@
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.crash.CrashReport;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.ForgeVersion;
import net.minecraftforge.common.MinecraftForge;
Expand All @@ -36,9 +35,9 @@

@SuppressWarnings("unused")
public class ProxyCommonClient extends ProxyCommon {
private List<IModPlugin> plugins = new ArrayList<IModPlugin>();
@Nullable
private GuiEventHandler guiEventHandler;
private List<IModPlugin> plugins;
private JeiStarter starter;

private static void initVersionChecker() {
final NBTTagCompound compound = new NBTTagCompound();
Expand Down Expand Up @@ -102,59 +101,42 @@ public void postInit(FMLPostInitializationEvent event) {
reloadableResourceManager.registerReloadListener(new IResourceManagerReloadListener() {
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
restartJEI();
if (SessionData.hasJoinedWorld()) {
restartJEI();
}
}
});
}

@SubscribeEvent
public void onEntityJoinedWorld(EntityJoinWorldEvent event) {
if (event.getWorld().isRemote && !SessionData.isJeiStarted() && Minecraft.getMinecraft().thePlayer != null) {
try {
startJEI();
} catch (Throwable e) {
Minecraft.getMinecraft().displayCrashReport(new CrashReport("JEI failed to start:", e));
}
}
}

private void startJEI() {
long jeiStartTime = System.currentTimeMillis();
Log.info("Beginning startup...");
SessionData.setJeiStarted();
Log.info("Beginning postInit...");
this.starter = new JeiStarter(this.plugins);
Log.info("Finished postInit in {} ms", System.currentTimeMillis() - jeiStartTime);

Config.startJei();

JeiRuntime jeiRuntime = JeiStarter.startJEI(plugins);
this.starter.start(this.plugins);
}

if (guiEventHandler != null) {
MinecraftForge.EVENT_BUS.unregister(guiEventHandler);
guiEventHandler = null;
@SubscribeEvent
public void onEntityJoinedWorld(EntityJoinWorldEvent event) {
if (event.getWorld().isRemote && !SessionData.hasJoinedWorld() && Minecraft.getMinecraft().thePlayer != null) {
SessionData.setJoinedWorld();
Config.syncWorldConfig();
}
guiEventHandler = new GuiEventHandler(jeiRuntime);
MinecraftForge.EVENT_BUS.register(guiEventHandler);

Log.info("Finished startup in {} ms", System.currentTimeMillis() - jeiStartTime);
}

@Override
public void restartJEI() {
// check that JEI has been started before. if not, do nothing
if (SessionData.isJeiStarted()) {
startJEI();
if (this.starter != null && this.starter.hasStarted()) {
this.starter.start(this.plugins);
}
}

private static void reloadItemList() {
if (SessionData.isJeiStarted()) {
JeiRuntime runtime = Internal.getRuntime();
if (runtime != null) {
ItemListOverlay itemListOverlay = runtime.getItemListOverlay();
ItemFilter itemFilter = itemListOverlay.getItemFilter();
IIngredientRegistry ingredientRegistry = Internal.getIngredientRegistry();
JeiHelpers helpers = Internal.getHelpers();
itemFilter.build(ingredientRegistry, helpers);
}
JeiRuntime runtime = Internal.getRuntime();
if (runtime != null) {
ItemListOverlay itemListOverlay = runtime.getItemListOverlay();
ItemFilter itemFilter = itemListOverlay.getItemFilter();
itemFilter.build();
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/mezz/jei/RecipeRegistry.java
Expand Up @@ -49,6 +49,7 @@
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.ProgressManager;

public class RecipeRegistry implements IRecipeRegistry {
private final StackHelper stackHelper;
Expand Down Expand Up @@ -159,9 +160,12 @@ private void addRecipes(@Nullable List<Object> recipes) {
return;
}

ProgressManager.ProgressBar progressBar = ProgressManager.push("Adding recipes", recipes.size());
for (Object recipe : recipes) {
progressBar.step("");
addRecipe(recipe);
}
ProgressManager.pop(progressBar);
}

@Override
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/mezz/jei/api/IModPlugin.java
Expand Up @@ -5,31 +5,27 @@
/**
* The main class to implement to create a JEI plugin. Everything communicated between a mod and JEI is through this class.
* IModPlugins must have the {@link JEIPlugin} annotation to get loaded by JEI.
* This class must not import anything that could be missing at runtime (i.e. code from any other mod).
*
* @see BlankModPlugin
*/
public interface IModPlugin {

/**
* If your item has subtypes that depend on NBT or capabilities, use this to help JEI identify those subtypes correctly.
* Called when the player joins the world and any time JEI reloads (like for a config change).
*
* @since JEI 3.12.1
*/
void registerItemSubtypes(ISubtypeRegistry subtypeRegistry);

/**
* Register special ingredients, beyond the basic ItemStack and FluidStack.
* Called when the player joins the world and any time JEI reloads (like for a config change).
*
* @since JEI 3.11.0
*/
void registerIngredients(IModIngredientRegistration registry);

/**
* Register this mod plugin with the mod registry.
* Called when the player joins the world and any time JEI reloads (like for a config change).
*/
void register(IModRegistry registry);

Expand Down

0 comments on commit 85b4c51

Please sign in to comment.