Skip to content

Commit

Permalink
Change enchantments to ingredients
Browse files Browse the repository at this point in the history
Relieves enchantments from relying on IDs.
  • Loading branch information
naqaden authored Nov 8, 2018
1 parent e20c3a5 commit 71a8f7b
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/api/java/mezz/jei/api/ingredients/VanillaTypes.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,6 +11,7 @@
*/
public final class VanillaTypes {
public static final IIngredientType<ItemStack> ITEM = () -> ItemStack.class;
public static final IIngredientType<EnchantmentData> ENCHANT = () -> EnchantmentData.class;
public static final IIngredientType<FluidStack> FLUID = () -> FluidStack.class;

private VanillaTypes() {
Expand Down
31 changes: 6 additions & 25 deletions src/main/java/mezz/jei/plugins/vanilla/VanillaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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<String> 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
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<EnchantmentData> {
@Override
public List<EnchantmentData> expandSubtypes(List<EnchantmentData> contained) {
return contained;
}

@Override
@Nullable
public EnchantmentData getMatch(Iterable<EnchantmentData> 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<Color> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<EnchantmentData> create() {
List<EnchantmentData> enchantData = new ArrayList<EnchantmentData>();

Collection<Enchantment> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<EnchantmentData> {

@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<String> getTooltip(Minecraft minecraft, EnchantmentData ingredient, ITooltipFlag tooltipFlag) {
EntityPlayer player = minecraft.player;
ItemStack enchantedBook = new ItemStack(Items.ENCHANTED_BOOK);
ItemEnchantedBook.addEnchantment(enchantedBook, ingredient);
List<String> 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;
}
}

0 comments on commit 71a8f7b

Please sign in to comment.