diff --git a/src/api/java/mezz/jei/api/ingredients/VanillaTypes.java b/src/api/java/mezz/jei/api/ingredients/VanillaTypes.java index 79ff40618..a1b76c3ba 100644 --- a/src/api/java/mezz/jei/api/ingredients/VanillaTypes.java +++ b/src/api/java/mezz/jei/api/ingredients/VanillaTypes.java @@ -1,6 +1,7 @@ package mezz.jei.api.ingredients; import mezz.jei.api.recipe.IIngredientType; +import net.minecraft.enchantment.EnchantmentData; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -10,6 +11,7 @@ */ public final class VanillaTypes { public static final IIngredientType ITEM = () -> ItemStack.class; + public static final IIngredientType ENCHANT = () -> EnchantmentData.class; public static final IIngredientType FLUID = () -> FluidStack.class; private VanillaTypes() { diff --git a/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java b/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java index da7684f3f..d90c18a43 100644 --- a/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java +++ b/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java @@ -31,6 +31,9 @@ import mezz.jei.plugins.vanilla.furnace.FurnaceFuelCategory; import mezz.jei.plugins.vanilla.furnace.FurnaceSmeltingCategory; import mezz.jei.plugins.vanilla.furnace.SmeltingRecipeMaker; +import mezz.jei.plugins.vanilla.ingredients.EnchantDataHelper; +import mezz.jei.plugins.vanilla.ingredients.EnchantDataListFactory; +import mezz.jei.plugins.vanilla.ingredients.EnchantDataRenderer; import mezz.jei.plugins.vanilla.ingredients.FluidStackHelper; import mezz.jei.plugins.vanilla.ingredients.FluidStackListFactory; import mezz.jei.plugins.vanilla.ingredients.FluidStackRenderer; @@ -44,7 +47,6 @@ import net.minecraft.client.gui.inventory.GuiCrafting; import net.minecraft.client.gui.inventory.GuiFurnace; import net.minecraft.client.gui.inventory.GuiInventory; -import net.minecraft.enchantment.Enchantment; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.inventory.ContainerBrewingStand; @@ -53,21 +55,15 @@ import net.minecraft.inventory.ContainerWorkbench; import net.minecraft.item.EnumDyeColor; import net.minecraft.item.ItemBanner; -import net.minecraft.item.ItemEnchantedBook; import net.minecraft.item.ItemMonsterPlacer; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.ShapedRecipes; import net.minecraft.item.crafting.ShapelessRecipes; -import net.minecraft.nbt.NBTBase; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import net.minecraft.util.ResourceLocation; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.List; @JEIPlugin public class VanillaPlugin implements IModPlugin { @@ -90,24 +86,8 @@ public void registerItemSubtypes(ISubtypeRegistry subtypeRegistry) { ResourceLocation resourceLocation = ItemMonsterPlacer.getNamedIdFrom(itemStack); return resourceLocation == null ? ISubtypeRegistry.ISubtypeInterpreter.NONE : resourceLocation.toString(); }); - subtypeRegistry.registerSubtypeInterpreter(Items.ENCHANTED_BOOK, itemStack -> { - List enchantmentNames = new ArrayList<>(); - NBTTagList enchantments = ItemEnchantedBook.getEnchantments(itemStack); - for (NBTBase nbt : enchantments) { - if (nbt instanceof NBTTagCompound) { - NBTTagCompound nbttagcompound = (NBTTagCompound) nbt; - int j = nbttagcompound.getShort("id"); - Enchantment enchantment = Enchantment.getEnchantmentByID(j); - if (enchantment != null) - { - String enchantmentUid = enchantment.getName() + ".lvl" + nbttagcompound.getShort("lvl"); - enchantmentNames.add(enchantmentUid); - } - } - } - enchantmentNames.sort(null); - return enchantmentNames.toString(); - }); + // Hide ID-based enchanted book items to render as ingredients instead + subtypeRegistry.registerSubtypeInterpreter(Items.ENCHANTED_BOOK, itemStack -> null); } @Override @@ -117,6 +97,7 @@ public void registerIngredients(IModIngredientRegistration ingredientRegistratio ItemStackListFactory itemStackListFactory = new ItemStackListFactory(this.subtypeRegistry); ingredientRegistration.register(VanillaTypes.ITEM, itemStackListFactory.create(stackHelper), new ItemStackHelper(stackHelper), new ItemStackRenderer()); + ingredientRegistration.register(VanillaTypes.ENCHANT, EnchantDataListFactory.create(), new EnchantDataHelper(), new EnchantDataRenderer()); ingredientRegistration.register(VanillaTypes.FLUID, FluidStackListFactory.create(), new FluidStackHelper(), new FluidStackRenderer()); } diff --git a/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java new file mode 100644 index 000000000..fd3bef935 --- /dev/null +++ b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java @@ -0,0 +1,87 @@ +package mezz.jei.plugins.vanilla.ingredients; + +import javax.annotation.Nullable; +import java.awt.Color; +import java.util.Collections; +import java.util.List; + +import com.google.common.base.MoreObjects; +import mezz.jei.api.ingredients.IIngredientHelper; +import net.minecraft.enchantment.EnchantmentData; +import net.minecraft.init.Items; +import net.minecraft.item.ItemEnchantedBook; +import net.minecraft.item.ItemStack; + +public class EnchantDataHelper implements IIngredientHelper { + @Override + public List expandSubtypes(List contained) { + return contained; + } + + @Override + @Nullable + public EnchantmentData getMatch(Iterable ingredients, EnchantmentData toMatch) { + for (EnchantmentData enchantData : ingredients) { + if (enchantData.enchantment.getRegistryName() == toMatch.enchantment.getRegistryName() + && enchantData.enchantmentLevel == toMatch.enchantmentLevel) { + return enchantData; + } + } + return null; + } + + @Override + public String getDisplayName(EnchantmentData ingredient) { + return ingredient.enchantment.getTranslatedName(ingredient.enchantmentLevel); + } + + @Override + public String getUniqueId(EnchantmentData ingredient) { + return "enchantment:" + ingredient.enchantment.getName() + ".lvl" + ingredient.enchantmentLevel; + } + + @Override + public String getWildcardId(EnchantmentData ingredient) { + return getUniqueId(ingredient); + } + + @Override + public String getModId(EnchantmentData ingredient) { + return ingredient.enchantment.getRegistryName().getNamespace(); + } + + @Override + public Iterable getColors(EnchantmentData ingredient) { + return Collections.emptyList(); + } + + @Override + public String getResourceId(EnchantmentData ingredient) { + return ingredient.enchantment.getRegistryName().getPath(); + } + + @Override + public ItemStack getCheatItemStack(EnchantmentData ingredient) { + ItemStack enchantedBook = new ItemStack(Items.ENCHANTED_BOOK); + ItemEnchantedBook.addEnchantment(enchantedBook, ingredient); + return enchantedBook; + } + + @Override + public EnchantmentData copyIngredient(EnchantmentData ingredient) { + return new EnchantmentData(ingredient.enchantment, ingredient.enchantmentLevel); + } + + @Override + public String getErrorInfo(@Nullable EnchantmentData ingredient) { + if (ingredient == null) { + return "null"; + } + MoreObjects.ToStringHelper toStringHelper = MoreObjects.toStringHelper(EnchantmentData.class); + + toStringHelper.add("Enchantment", ingredient.enchantment.getName()); + toStringHelper.add("Level", ingredient.enchantmentLevel); + + return toStringHelper.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataListFactory.java b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataListFactory.java new file mode 100644 index 000000000..6a9db60af --- /dev/null +++ b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataListFactory.java @@ -0,0 +1,28 @@ +package mezz.jei.plugins.vanilla.ingredients; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import net.minecraftforge.fml.common.registry.ForgeRegistries; +import net.minecraft.enchantment.Enchantment; +import net.minecraft.enchantment.EnchantmentData; + +public final class EnchantDataListFactory { + private EnchantDataListFactory() { + + } + + public static List create() { + List enchantData = new ArrayList(); + + Collection enchants = ForgeRegistries.ENCHANTMENTS.getValuesCollection(); + for(Enchantment enchant : enchants) { + for(int lvl = enchant.getMinLevel(); lvl <= enchant.getMaxLevel(); lvl++) { + enchantData.add(new EnchantmentData(enchant, lvl)); + } + } + + return enchantData; + } +} \ No newline at end of file diff --git a/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java new file mode 100644 index 000000000..abb773248 --- /dev/null +++ b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java @@ -0,0 +1,83 @@ +package mezz.jei.plugins.vanilla.ingredients; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; + +import mezz.jei.api.ingredients.IIngredientRenderer; +import mezz.jei.util.ErrorUtil; +import mezz.jei.util.Log; +import mezz.jei.util.Translator; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.client.util.ITooltipFlag; +import net.minecraft.enchantment.EnchantmentData; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.EnumRarity; +import net.minecraft.item.ItemEnchantedBook; +import net.minecraft.item.ItemStack; +import net.minecraft.util.text.TextFormatting; + +public class EnchantDataRenderer implements IIngredientRenderer { + + @Override + public void render(Minecraft minecraft, int xPosition, int yPosition, @Nullable EnchantmentData ingredient) { + if (ingredient != null) { + GlStateManager.enableDepth(); + RenderHelper.enableGUIStandardItemLighting(); + FontRenderer font = getFontRenderer(minecraft, ingredient); + ItemStack enchantedBook = new ItemStack(Items.ENCHANTED_BOOK); + ItemEnchantedBook.addEnchantment(enchantedBook, ingredient); + minecraft.getRenderItem().renderItemAndEffectIntoGUI(null, enchantedBook, xPosition, yPosition); + minecraft.getRenderItem().renderItemOverlayIntoGUI(font, enchantedBook, xPosition, yPosition, null); + GlStateManager.disableBlend(); + RenderHelper.disableStandardItemLighting(); + } + } + + @Override + public List getTooltip(Minecraft minecraft, EnchantmentData ingredient, ITooltipFlag tooltipFlag) { + EntityPlayer player = minecraft.player; + ItemStack enchantedBook = new ItemStack(Items.ENCHANTED_BOOK); + ItemEnchantedBook.addEnchantment(enchantedBook, ingredient); + List list; + try { + list = enchantedBook.getTooltip(player, tooltipFlag); + } catch (RuntimeException | LinkageError e) { + String itemStackInfo = ErrorUtil.getItemStackInfo(enchantedBook); + Log.get().error("Failed to get tooltip: {}", itemStackInfo, e); + list = new ArrayList<>(); + list.add(TextFormatting.RED + Translator.translateToLocal("jei.tooltip.error.crash")); + return list; + } + + EnumRarity rarity; + if (ingredient.enchantment.isTreasureEnchantment()) { + rarity = EnumRarity.RARE; + } else { + rarity = EnumRarity.UNCOMMON; + } + + for (int k = 0; k < list.size(); ++k) { + if (k == 0) { + list.set(k, rarity.color + list.get(k)); + } else { + list.set(k, TextFormatting.GRAY + list.get(k)); + } + } + + return list; + } + + @Override + public FontRenderer getFontRenderer(Minecraft minecraft, EnchantmentData ingredient) { + FontRenderer fontRenderer = Items.ENCHANTED_BOOK.getFontRenderer(new ItemStack(Items.ENCHANTED_BOOK)); + if (fontRenderer == null) { + fontRenderer = minecraft.fontRenderer; + } + return minecraft.fontRenderer; + } +} \ No newline at end of file