From aed4352a245c126a3a7bb39ad484cee9b756b75a Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 9 Mar 2019 18:24:16 +0200 Subject: [PATCH] Added JEI support --- build.gradle | 4 ++ .../unicopia/jei/BlendedDrawable.java | 32 +++++++++ .../unicopia/jei/JEIUnicopia.java | 42 ++++++++++++ .../unicopia/jei/SpellRecipeWrapper.java | 37 +++++++++++ .../unicopia/jei/SpellbookCategory.java | 66 +++++++++++++++++++ .../enchanting/AbstractSpecialRecipe.java | 9 +++ .../enchanting/AffineIngredients.java | 6 ++ .../unicopia/enchanting/SpellIngredient.java | 30 ++++++++- 8 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 src/jei/java/com/minelittlepony/unicopia/jei/BlendedDrawable.java create mode 100644 src/jei/java/com/minelittlepony/unicopia/jei/JEIUnicopia.java create mode 100644 src/jei/java/com/minelittlepony/unicopia/jei/SpellRecipeWrapper.java create mode 100644 src/jei/java/com/minelittlepony/unicopia/jei/SpellbookCategory.java diff --git a/build.gradle b/build.gradle index 911fee13..b5f954fd 100644 --- a/build.gradle +++ b/build.gradle @@ -41,11 +41,15 @@ sourceCompatibility = 1.8 targetCompatibility = 1.8 sourceSets { + jei { + compileClasspath += main.compileClasspath + } external { compileClasspath += main.compileClasspath } main { compileClasspath += external.output + compileClasspath += jei.output ext.refMap = project.refCore } } diff --git a/src/jei/java/com/minelittlepony/unicopia/jei/BlendedDrawable.java b/src/jei/java/com/minelittlepony/unicopia/jei/BlendedDrawable.java new file mode 100644 index 00000000..0036e00c --- /dev/null +++ b/src/jei/java/com/minelittlepony/unicopia/jei/BlendedDrawable.java @@ -0,0 +1,32 @@ +package com.minelittlepony.unicopia.jei; + +import mezz.jei.api.gui.IDrawable; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.GlStateManager; + +class BlendedDrawable implements IDrawable { + + private final IDrawable wrapped; + + public BlendedDrawable(IDrawable wrapped) { + this.wrapped = wrapped; + } + + @Override + public int getWidth() { + return wrapped.getWidth(); + } + + @Override + public int getHeight() { + return wrapped.getHeight(); + } + + @Override + public void draw(Minecraft minecraft, int xOffset, int yOffset) { + GlStateManager.enableBlend(); + wrapped.draw(minecraft, xOffset, yOffset); + GlStateManager.disableBlend(); + } + +} diff --git a/src/jei/java/com/minelittlepony/unicopia/jei/JEIUnicopia.java b/src/jei/java/com/minelittlepony/unicopia/jei/JEIUnicopia.java new file mode 100644 index 00000000..3ea7682b --- /dev/null +++ b/src/jei/java/com/minelittlepony/unicopia/jei/JEIUnicopia.java @@ -0,0 +1,42 @@ +package com.minelittlepony.unicopia.jei; + +import com.minelittlepony.unicopia.Unicopia; +import com.minelittlepony.unicopia.enchanting.SpecialRecipe; +import com.minelittlepony.unicopia.enchanting.SpellRecipe; +import com.minelittlepony.unicopia.init.UItems; +import com.minelittlepony.unicopia.spell.SpellRegistry; + +import mezz.jei.api.IGuiHelper; +import mezz.jei.api.IModPlugin; +import mezz.jei.api.IModRegistry; +import mezz.jei.api.ISubtypeRegistry; +import mezz.jei.api.JEIPlugin; +import mezz.jei.api.recipe.IRecipeCategoryRegistration; + +@JEIPlugin +public class JEIUnicopia implements IModPlugin { + + static IGuiHelper GUI_HELPER; + + static final String RECIPE_UID = "unicopia:spellbook_2"; + + @Override + public void registerItemSubtypes(ISubtypeRegistry registry) { + registry.registerSubtypeInterpreter(UItems.spell, SpellRegistry::getKeyFromStack); + registry.registerSubtypeInterpreter(UItems.curse, SpellRegistry::getKeyFromStack); + } + + @Override + public void registerCategories(IRecipeCategoryRegistration registry) { + registry.addRecipeCategories(new SpellbookCategory()); + } + + @Override + public void register(IModRegistry registry) { + GUI_HELPER = registry.getJeiHelpers().getGuiHelper(); + + registry.handleRecipes(SpellRecipe.class, SpellRecipeWrapper::new, RECIPE_UID); + registry.handleRecipes(SpecialRecipe.class, SpellRecipeWrapper::new, RECIPE_UID); + registry.addRecipes(Unicopia.getCraftingManager().getRecipes(), RECIPE_UID); + } +} diff --git a/src/jei/java/com/minelittlepony/unicopia/jei/SpellRecipeWrapper.java b/src/jei/java/com/minelittlepony/unicopia/jei/SpellRecipeWrapper.java new file mode 100644 index 00000000..66e77950 --- /dev/null +++ b/src/jei/java/com/minelittlepony/unicopia/jei/SpellRecipeWrapper.java @@ -0,0 +1,37 @@ +package com.minelittlepony.unicopia.jei; + +import java.util.List; +import java.util.stream.Collectors; + +import com.minelittlepony.unicopia.enchanting.AbstractSpecialRecipe; + +import mezz.jei.api.ingredients.IIngredients; +import mezz.jei.api.ingredients.VanillaTypes; +import mezz.jei.api.recipe.IRecipeWrapper; +import net.minecraft.item.ItemStack; + +public class SpellRecipeWrapper implements IRecipeWrapper { + + private final AbstractSpecialRecipe recipe; + + public SpellRecipeWrapper(AbstractSpecialRecipe recipe) { + this.recipe = recipe; + } + + public AbstractSpecialRecipe getRecipe() { + return recipe; + } + + @Override + public void getIngredients(IIngredients ingredients) { + + List> ingreds = recipe.getSpellIngredients().stream().map(ingredient -> { + return ingredient.getStacks().collect(Collectors.toList()); + }).collect(Collectors.toList()); + + ingredients.setInputLists(VanillaTypes.ITEM, ingreds); + + ingredients.setOutput(VanillaTypes.ITEM, recipe.getRecipeOutput()); + } + +} diff --git a/src/jei/java/com/minelittlepony/unicopia/jei/SpellbookCategory.java b/src/jei/java/com/minelittlepony/unicopia/jei/SpellbookCategory.java new file mode 100644 index 00000000..b97ef241 --- /dev/null +++ b/src/jei/java/com/minelittlepony/unicopia/jei/SpellbookCategory.java @@ -0,0 +1,66 @@ +package com.minelittlepony.unicopia.jei; + +import com.minelittlepony.unicopia.Unicopia; +import com.minelittlepony.unicopia.inventory.gui.GuiSpellBook; + +import mezz.jei.api.gui.IDrawable; +import mezz.jei.api.gui.IGuiItemStackGroup; +import mezz.jei.api.gui.IRecipeLayout; +import mezz.jei.api.ingredients.IIngredients; +import mezz.jei.api.ingredients.VanillaTypes; +import mezz.jei.api.recipe.IRecipeCategory; +import mezz.jei.api.recipe.IRecipeWrapper; +import net.minecraft.util.ResourceLocation; + +public class SpellbookCategory implements IRecipeCategory { + + @Override + public String getUid() { + return JEIUnicopia.RECIPE_UID; + } + + @Override + public String getTitle() { + return "Spellbook"; + } + + @Override + public IDrawable getIcon() { + return JEIUnicopia.GUI_HELPER.drawableBuilder( + new ResourceLocation(Unicopia.MODID, "textures/items/spellbook.png"), 0, 0, 16, 16) + .setTextureSize(16, 16) + .build(); + } + + @Override + public String getModName() { + return "Unicopia"; + } + + @Override + public IDrawable getBackground() { + return new BlendedDrawable( + JEIUnicopia.GUI_HELPER.drawableBuilder(GuiSpellBook.spellBookGuiTextures, 405, 0, 105, 108) + .setTextureSize(512, 256) + .build()); + } + + @Override + public void setRecipe(IRecipeLayout recipeLayout, IRecipeWrapper recipeWrapper, @Deprecated IIngredients unused) { + recipeLayout.setShapeless(); + + IGuiItemStackGroup stacks = recipeLayout.getItemStacks(); + + stacks.init(0, true, 29, 3); + stacks.init(1, true, 3, 46); + stacks.init(2, true, 30, 86); + stacks.init(3, true, 80, 72); + stacks.init(4, true, 82, 15); + + stacks.init(5, false, 46, 44); + + stacks.set(unused); + + stacks.set(5, unused.getOutputs(VanillaTypes.ITEM).get(0)); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/enchanting/AbstractSpecialRecipe.java b/src/main/java/com/minelittlepony/unicopia/enchanting/AbstractSpecialRecipe.java index 665583e8..9d2dc87b 100644 --- a/src/main/java/com/minelittlepony/unicopia/enchanting/AbstractSpecialRecipe.java +++ b/src/main/java/com/minelittlepony/unicopia/enchanting/AbstractSpecialRecipe.java @@ -80,6 +80,7 @@ public abstract class AbstractSpecialRecipe extends Impl implements IRe } } } + return toMatch.isEmpty(); } @@ -101,6 +102,14 @@ public abstract class AbstractSpecialRecipe extends Impl implements IRe return width * height < ingredients.size(); } + public SpellIngredient getSpellItem() { + return spellitem; + } + + public NonNullList getSpellIngredients() { + return ingredients; + } + @Override public ItemStack getRecipeOutput() { return spellitem.getStack(); diff --git a/src/main/java/com/minelittlepony/unicopia/enchanting/AffineIngredients.java b/src/main/java/com/minelittlepony/unicopia/enchanting/AffineIngredients.java index 90060399..19c6b5ae 100644 --- a/src/main/java/com/minelittlepony/unicopia/enchanting/AffineIngredients.java +++ b/src/main/java/com/minelittlepony/unicopia/enchanting/AffineIngredients.java @@ -1,6 +1,7 @@ package com.minelittlepony.unicopia.enchanting; import java.util.Map; +import java.util.stream.Stream; import javax.annotation.Nonnull; @@ -63,6 +64,11 @@ public class AffineIngredients { return instance().getIngredient(res).getStack(); } + @Override + public Stream getStacks() { + return instance().getIngredient(res).getStacks(); + } + @Override public boolean matches(ItemStack other, int materialMult) { return instance().getIngredient(res).matches(other, materialMult); diff --git a/src/main/java/com/minelittlepony/unicopia/enchanting/SpellIngredient.java b/src/main/java/com/minelittlepony/unicopia/enchanting/SpellIngredient.java index 3bd2a816..1527fb53 100644 --- a/src/main/java/com/minelittlepony/unicopia/enchanting/SpellIngredient.java +++ b/src/main/java/com/minelittlepony/unicopia/enchanting/SpellIngredient.java @@ -1,18 +1,23 @@ package com.minelittlepony.unicopia.enchanting; import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; import javax.annotation.Nullable; import com.google.common.collect.Lists; +import com.google.common.collect.Streams; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.minelittlepony.unicopia.enchanting.AffineIngredients.AffineIngredient; import com.minelittlepony.unicopia.spell.SpellRegistry; +import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.NonNullList; public interface SpellIngredient { @@ -31,6 +36,8 @@ public interface SpellIngredient { ItemStack getStack(); + Stream getStacks(); + class Compound implements SpellIngredient { private final List items; @@ -38,6 +45,16 @@ public interface SpellIngredient { this.items = items; } + public Stream getStacks() { + Stream stacks = Lists.newArrayList().stream(); + + for (SpellIngredient i : items) { + stacks = Streams.concat(stacks, i.getStacks()); + } + + return stacks.distinct(); + } + @Override public ItemStack getStack() { return items.get((int)(Math.random() * items.size())).getStack(); @@ -80,6 +97,17 @@ public interface SpellIngredient { ignoreMeta = meta; } + public Stream getStacks() { + if (ignoreMeta && !contained.isEmpty()) { + NonNullList subItems = NonNullList.create(); + contained.getItem().getSubItems(CreativeTabs.SEARCH, subItems); + + return subItems.stream(); + } + + return Streams.stream(Optional.ofNullable(contained)); + } + @Override public ItemStack getStack() { return contained; @@ -121,7 +149,7 @@ public interface SpellIngredient { stack = SpellRegistry.instance().enchantStack(stack, spell); } - return new Single(stack, !json.has("data")); + return new Single(stack, !(json.has("spell") || json.has("data"))); } if (json.has("id")) {