package mezz.jei.plugins.vanilla.ingredients.item;

import com.mojang.blaze3d.matrix.MatrixStack;
import javax.annotation.Nullable;
import javax.xml.soap.Text;
import java.util.ArrayList;
import java.util.List;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Rarity;
import net.minecraft.util.text.IFormattableTextComponent;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;

import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.util.ErrorUtil;
import net.minecraft.util.text.TranslationTextComponent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ItemStackRenderer implements IIngredientRenderer<ItemStack> {
	private static final Logger LOGGER = LogManager.getLogger();

	@Override
	@SuppressWarnings("deprecation")
	public void render(MatrixStack matrixStack, int xPosition, int yPosition, @Nullable ItemStack ingredient) {
		if (ingredient != null) {
			RenderSystem.pushMatrix();
			RenderSystem.multMatrix(matrixStack.getLast().getMatrix());
			RenderSystem.enableDepthTest();
			RenderHelper.enableStandardItemLighting();
			Minecraft minecraft = Minecraft.getInstance();
			FontRenderer font = getFontRenderer(minecraft, ingredient);
			ItemRenderer itemRenderer = minecraft.getItemRenderer();
			itemRenderer.renderItemAndEffectIntoGUI(null, ingredient, xPosition, yPosition);
			itemRenderer.renderItemOverlayIntoGUI(font, ingredient, xPosition, yPosition, null);
			RenderSystem.disableBlend();
			RenderHelper.disableStandardItemLighting();
			RenderSystem.popMatrix();
		}
	}

	@Override
	public List<ITextComponent> getTooltip(ItemStack ingredient, ITooltipFlag tooltipFlag) {
		Minecraft minecraft = Minecraft.getInstance();
		PlayerEntity player = minecraft.player;
		List<ITextComponent> list;
		try {
			list = ingredient.getTooltip(player, tooltipFlag);
		} catch (RuntimeException | LinkageError e) {
			String itemStackInfo = ErrorUtil.getItemStackInfo(ingredient);
			LOGGER.error("Failed to get tooltip: {}", itemStackInfo, e);
			list = new ArrayList<>();
			TranslationTextComponent crash = new TranslationTextComponent("jei.tooltip.error.crash");
			list.add(crash.mergeStyle(TextFormatting.RED));
			return list;
		}

		Rarity rarity;
		try {
			rarity = ingredient.getRarity();
		} catch (RuntimeException | LinkageError e) {
			String itemStackInfo = ErrorUtil.getItemStackInfo(ingredient);
			LOGGER.error("Failed to get rarity: {}", itemStackInfo, e);
			rarity = Rarity.COMMON;
		}

		for (int k = 0; k < list.size(); ++k) {
			final TextFormatting style;
			if (k == 0) {
				style = rarity.color;
			} else {
				style = TextFormatting.GRAY;
			}
			ITextComponent textComponent = list.get(k);
			IFormattableTextComponent textComponentCopy = textComponent.deepCopy();
			list.set(k, textComponentCopy.mergeStyle(style));
		}

		return list;
	}

	@Override
	public FontRenderer getFontRenderer(Minecraft minecraft, ItemStack ingredient) {
		FontRenderer fontRenderer = ingredient.getItem().getFontRenderer(ingredient);
		if (fontRenderer == null) {
			fontRenderer = minecraft.fontRenderer;
		}
		return fontRenderer;
	}
}

@@ -129,36 +129,8 @@
 le;%0A
-import javax.xml.soap.Text;%0A
 impo
@@ -549,100 +549,8 @@
 ck;%0A
-import net.minecraft.item.Rarity;%0Aimport net.minecraft.util.text.IFormattableTextComponent;%0A
 impo
@@ -2018,37 +2018,8 @@
 er;%0A
-%09%09List%3CITextComponent%3E list;%0A
 %09%09tr
@@ -2025,22 +2025,22 @@
 ry %7B%0A%09%09%09
-list =
+return
  ingredi
@@ -2253,24 +2253,45 @@
 nfo, e);%0A%09%09%09
+List%3CITextComponent%3E 
 list = new A
@@ -2471,640 +2471,8 @@
 %0A%09%09%7D
-%0A%0A%09%09Rarity rarity;%0A%09%09try %7B%0A%09%09%09rarity = ingredient.getRarity();%0A%09%09%7D catch (RuntimeException %7C LinkageError e) %7B%0A%09%09%09String itemStackInfo = ErrorUtil.getItemStackInfo(ingredient);%0A%09%09%09LOGGER.error(%22Failed to get rarity: %7B%7D%22, itemStackInfo, e);%0A%09%09%09rarity = Rarity.COMMON;%0A%09%09%7D%0A%0A%09%09for (int k = 0; k %3C list.size(); ++k) %7B%0A%09%09%09final TextFormatting style;%0A%09%09%09if (k == 0) %7B%0A%09%09%09%09style = rarity.color;%0A%09%09%09%7D else %7B%0A%09%09%09%09style = TextFormatting.GRAY;%0A%09%09%09%7D%0A%09%09%09ITextComponent textComponent = list.get(k);%0A%09%09%09IFormattableTextComponent textComponentCopy = textComponent.deepCopy();%0A%09%09%09list.set(k, textComponentCopy.mergeStyle(style));%0A%09%09%7D%0A%0A%09%09return list;
 %0A%09%7D%0A

Fix #2039 All item tooltips turn gray