Skip to content

Commit

Permalink
Speed up building the item filter on reload (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Nov 11, 2016
1 parent d66a438 commit fb4f3ca
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 30 deletions.
68 changes: 68 additions & 0 deletions src/main/java/mezz/jei/IngredientInformation.java
@@ -0,0 +1,68 @@
package mezz.jei;

import javax.annotation.Nullable;
import java.awt.Color;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Joiner;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.util.color.ColorNamer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.Language;
import net.minecraft.util.text.TextFormatting;

public class IngredientInformation {
@Nullable
private static Language previousLanguage;

private static Map<Object, String> tooltipCache = new HashMap<Object, String>();
private static Map<Object, String> colorCache = new HashMap<Object, String>();

public static <T> String getTooltipString(T ingredient, IIngredientRenderer<T> ingredientRenderer, String modId, String modName, String displayName) {
String tooltipString = tooltipCache.get(ingredient);
if (tooltipString == null) {
List<String> tooltip = ingredientRenderer.getTooltip(Minecraft.getMinecraft(), ingredient);
tooltipString = Joiner.on(' ').join(tooltip).toLowerCase();
tooltipString = removeChatFormatting(tooltipString);
tooltipString = tooltipString.replace(modId, "");
tooltipString = tooltipString.replace(modName, "");
tooltipString = tooltipString.replace(displayName, "");
tooltipCache.put(ingredient, tooltipString);
}

return tooltipString;
}

private static String removeChatFormatting(String string) {
String withoutFormattingCodes = TextFormatting.getTextWithoutFormattingCodes(string);
return (withoutFormattingCodes == null) ? string : withoutFormattingCodes;
}

public static <V> String getColorString(V ingredient, IIngredientHelper<V> ingredientHelper) {
String colorString = colorCache.get(ingredient);
if (colorString == null) {
Iterable<Color> colors = ingredientHelper.getColors(ingredient);
ColorNamer colorNamer = Internal.getColorNamer();
Collection<String> colorNames = colorNamer.getColorNames(colors);
colorString = Joiner.on(' ').join(colorNames).toLowerCase();
colorCache.put(ingredient, colorString);
}
return colorString;
}

public static void onStart(boolean resourceReload) {
Language language = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage();
if (previousLanguage != null && !previousLanguage.equals(language)) {
tooltipCache.clear();
}
previousLanguage = language;

if (resourceReload) {
colorCache.clear();
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/mezz/jei/JeiStarter.java
Expand Up @@ -22,7 +22,7 @@ public class JeiStarter {
@Nullable
private GuiEventHandler guiEventHandler;

public void start(List<IModPlugin> plugins, boolean showProgressBar) {
public void start(List<IModPlugin> plugins, boolean showProgressBar, boolean resourceReload) {
long jeiStartTime = System.currentTimeMillis();

Log.info("Starting JEI...");
Expand Down Expand Up @@ -52,6 +52,8 @@ public void start(List<IModPlugin> plugins, boolean showProgressBar) {
RecipeRegistry recipeRegistry = modRegistry.createRecipeRegistry(stackHelper, ingredientRegistry);
Log.info("Built recipe registry in {} ms", System.currentTimeMillis() - start_time);

IngredientInformation.onStart(resourceReload);

ItemFilter itemFilter = new ItemFilter(showProgressBar);

Log.info("Building runtime...");
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/mezz/jei/ProxyCommonClient.java
Expand Up @@ -102,13 +102,13 @@ public void loadComplete(FMLLoadCompleteEvent event) {
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
if (SessionData.hasJoinedWorld()) {
restartJEI();
restartJEI(true);
}
}
});

try {
this.starter.start(plugins, true);
this.starter.start(plugins, true, false);
} catch (Exception e) {
Log.error("Exception on load", e);
}
Expand All @@ -124,9 +124,13 @@ public void onEntityJoinedWorld(EntityJoinWorldEvent event) {

@Override
public void restartJEI() {
restartJEI(false);
}

private void restartJEI(boolean resourceReload) {
// check that JEI has been started before. if not, do nothing
if (this.starter.hasStarted()) {
this.starter.start(this.plugins, false);
this.starter.start(this.plugins, false, resourceReload);
}
}

Expand Down
29 changes: 3 additions & 26 deletions src/main/java/mezz/jei/util/IngredientListElement.java
@@ -1,24 +1,18 @@
package mezz.jei.util;

import javax.annotation.Nullable;
import java.awt.Color;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import com.google.common.base.Joiner;
import mezz.jei.IngredientInformation;
import mezz.jei.Internal;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.config.Config;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.util.color.ColorNamer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.oredict.OreDictionary;

public class IngredientListElement<V> implements IIngredientListElement<V> {
Expand Down Expand Up @@ -59,13 +53,10 @@ protected IngredientListElement(V ingredient, IIngredientHelper<V> ingredientHel

this.modNameString = modId.replaceAll(" ", "") + ' ' + modName.replaceAll(" ", "");

this.tooltipString = getTooltipString(ingredient, ingredientRenderer, modId, modName, displayName);
this.tooltipString = IngredientInformation.getTooltipString(ingredient, ingredientRenderer, modId, modName, displayName);

if (Config.getColorSearchMode() != Config.SearchMode.DISABLED) {
Iterable<Color> colors = ingredientHelper.getColors(ingredient);
ColorNamer colorNamer = Internal.getColorNamer();
Collection<String> colorNames = colorNamer.getColorNames(colors);
this.colorString = Joiner.on(' ').join(colorNames).toLowerCase();
this.colorString = IngredientInformation.getColorString(ingredient, ingredientHelper);
} else {
this.colorString = "";
}
Expand Down Expand Up @@ -119,20 +110,6 @@ protected IngredientListElement(V ingredient, IIngredientHelper<V> ingredientHel
this.searchString = searchStringBuilder.toString();
}

private static <T> String getTooltipString(T ingredient, IIngredientRenderer<T> ingredientRenderer, String modId, String modName, String displayName) {
List<String> tooltip = ingredientRenderer.getTooltip(Minecraft.getMinecraft(), ingredient);
String tooltipString = Joiner.on(' ').join(tooltip).toLowerCase();
tooltipString = removeChatFormatting(tooltipString);
tooltipString = tooltipString.replace(modId, "");
tooltipString = tooltipString.replace(modName, "");
return tooltipString.replace(displayName, "");
}

private static String removeChatFormatting(String string) {
String withoutFormattingCodes = TextFormatting.getTextWithoutFormattingCodes(string);
return (withoutFormattingCodes == null) ? string : withoutFormattingCodes;
}

@Override
public final V getIngredient() {
return ingredient;
Expand Down

0 comments on commit fb4f3ca

Please sign in to comment.