From 71a8f7b5389c3b40ac9d3bcfb9d7fdb3d2b9a737 Mon Sep 17 00:00:00 2001 From: naqaden Date: Wed, 7 Nov 2018 19:19:34 -0700 Subject: [PATCH 1/5] Change enchantments to ingredients Relieves enchantments from relying on IDs. --- .../jei/api/ingredients/VanillaTypes.java | 2 + .../jei/plugins/vanilla/VanillaPlugin.java | 31 ++----- .../ingredients/EnchantDataHelper.java | 87 +++++++++++++++++++ .../ingredients/EnchantDataListFactory.java | 28 ++++++ .../ingredients/EnchantDataRenderer.java | 83 ++++++++++++++++++ 5 files changed, 206 insertions(+), 25 deletions(-) create mode 100644 src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java create mode 100644 src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataListFactory.java create mode 100644 src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java 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 From eb4a0020c72551c610a71af3e571883d5dec1ef3 Mon Sep 17 00:00:00 2001 From: naqaden Date: Thu, 8 Nov 2018 19:43:01 -0700 Subject: [PATCH 2/5] Hide ID-based enchanted books --- src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java b/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java index d90c18a43..7e6c42930 100644 --- a/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java +++ b/src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java @@ -87,7 +87,7 @@ public void registerItemSubtypes(ISubtypeRegistry subtypeRegistry) { return resourceLocation == null ? ISubtypeRegistry.ISubtypeInterpreter.NONE : resourceLocation.toString(); }); // Hide ID-based enchanted book items to render as ingredients instead - subtypeRegistry.registerSubtypeInterpreter(Items.ENCHANTED_BOOK, itemStack -> null); + subtypeRegistry.registerSubtypeInterpreter(Items.ENCHANTED_BOOK, itemStack -> ""); } @Override From 910034ff39dc6fa4efe716bb9f84a2c0117ea1d3 Mon Sep 17 00:00:00 2001 From: naqaden Date: Thu, 29 Nov 2018 15:09:25 -0700 Subject: [PATCH 3/5] Use ItemStackRenderer for EnchantDataRenderer and minor clean up with 'enchant' abbreviation consistency and ItemEnchantedBook's existing enchanted book function. --- .../ingredients/EnchantDataRenderer.java | 43 +++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java index abb773248..4cd29e32a 100644 --- a/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java +++ b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java @@ -9,13 +9,9 @@ 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; @@ -23,31 +19,33 @@ public class EnchantDataRenderer implements IIngredientRenderer { + private IIngredientRenderer itemRenderer; + + public EnchantDataRenderer() { + this(new ItemStackRenderer()); + } + + public EnchantDataRenderer(IIngredientRenderer itemRenderer) { + this.itemRenderer = itemRenderer; + } + @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(); + ItemStack enchantBook = ItemEnchantedBook.getEnchantedItemStack(ingredient); + itemRenderer.render(minecraft, xPosition, yPosition, enchantBook); } } @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); + ItemStack enchantBook = ItemEnchantedBook.getEnchantedItemStack(ingredient); List list; try { - list = enchantedBook.getTooltip(player, tooltipFlag); + list = enchantBook.getTooltip(player, tooltipFlag); } catch (RuntimeException | LinkageError e) { - String itemStackInfo = ErrorUtil.getItemStackInfo(enchantedBook); + String itemStackInfo = ErrorUtil.getItemStackInfo(enchantBook); Log.get().error("Failed to get tooltip: {}", itemStackInfo, e); list = new ArrayList<>(); list.add(TextFormatting.RED + Translator.translateToLocal("jei.tooltip.error.crash")); @@ -71,13 +69,4 @@ public List getTooltip(Minecraft minecraft, EnchantmentData ingredient, 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 +} From c4e7d148888b57aea0520cc762bbc47ccec75ba5 Mon Sep 17 00:00:00 2001 From: naqaden Date: Thu, 29 Nov 2018 15:10:57 -0700 Subject: [PATCH 4/5] Use ItemEnchantedBook#getEnchantedItemStack --- .../jei/plugins/vanilla/ingredients/EnchantDataHelper.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java index fd3bef935..085c3d082 100644 --- a/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java +++ b/src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java @@ -8,7 +8,6 @@ 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; @@ -62,9 +61,7 @@ public String getResourceId(EnchantmentData ingredient) { @Override public ItemStack getCheatItemStack(EnchantmentData ingredient) { - ItemStack enchantedBook = new ItemStack(Items.ENCHANTED_BOOK); - ItemEnchantedBook.addEnchantment(enchantedBook, ingredient); - return enchantedBook; + return ItemEnchantedBook.getEnchantedItemStack(ingredient); } @Override @@ -84,4 +81,4 @@ public String getErrorInfo(@Nullable EnchantmentData ingredient) { return toStringHelper.toString(); } -} \ No newline at end of file +} From f57d55c9fea787f65acc6b519b664c6126f9e5ea Mon Sep 17 00:00:00 2001 From: naqaden Date: Thu, 29 Nov 2018 15:55:05 -0700 Subject: [PATCH 5/5] Added special cases for EnchantmentData --- .../java/mezz/jei/recipes/RecipeRegistry.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/mezz/jei/recipes/RecipeRegistry.java b/src/main/java/mezz/jei/recipes/RecipeRegistry.java index 5d08d8ea3..43d587a1a 100644 --- a/src/main/java/mezz/jei/recipes/RecipeRegistry.java +++ b/src/main/java/mezz/jei/recipes/RecipeRegistry.java @@ -35,9 +35,11 @@ import mezz.jei.util.Log; import net.minecraft.block.Block; import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.enchantment.EnchantmentData; import net.minecraft.inventory.Container; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemEnchantedBook; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidRegistry; @@ -540,6 +542,20 @@ public RecipeClickableArea getRecipeClickableArea(GuiContainer gui, int mouseX, return null; } + /** + * Special case for EnchantmentData which replaces enchanted book items. + */ + @Nullable + private static ItemStack getEnchantBookFromEnchantData(IFocus focus) { + Object ingredient = focus.getValue(); + if (ingredient instanceof EnchantmentData) { + EnchantmentData enchantData = (EnchantmentData) ingredient; + return ItemEnchantedBook.getEnchantedItemStack(enchantData); + } + + return null; + } + /** * Special case for ItemBlocks containing fluid blocks. * Nothing crafts those, the player probably wants to look up fluids. @@ -566,6 +582,10 @@ private static FluidStack getFluidFromItemBlock(IFocus focus) { public List getRecipeCategories(IFocus focus) { focus = Focus.check(focus); + ItemStack enchantBook = getEnchantBookFromEnchantData(focus); + if (enchantBook != null) { + return getRecipeCategories(createFocus(focus.getMode(), enchantBook)); + } FluidStack fluidStack = getFluidFromItemBlock(focus); if (fluidStack != null) { return getRecipeCategories(createFocus(focus.getMode(), fluidStack)); @@ -599,6 +619,10 @@ public List getRecipeWrappers(IRecipeCategory