Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Speed up building the item filter on reload (#551)
- Loading branch information
Showing
4 changed files
with
81 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters