forked from mezz/JustEnoughItems
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relieves enchantments from relying on IDs.
- Loading branch information
Showing
5 changed files
with
206 additions
and
25 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
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
87 changes: 87 additions & 0 deletions
87
src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataHelper.java
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,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(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataListFactory.java
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,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; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/mezz/jei/plugins/vanilla/ingredients/EnchantDataRenderer.java
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,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; | ||
} | ||
} |