Added JEI support

This commit is contained in:
Sollace 2019-03-09 18:24:16 +02:00
parent 0e54cbf29a
commit aed4352a24
8 changed files with 225 additions and 1 deletions

View file

@ -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
}
}

View file

@ -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();
}
}

View file

@ -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);
}
}

View file

@ -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<List<ItemStack>> 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());
}
}

View file

@ -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<IRecipeWrapper> {
@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));
}
}

View file

@ -80,6 +80,7 @@ public abstract class AbstractSpecialRecipe extends Impl<IRecipe> implements IRe
}
}
}
return toMatch.isEmpty();
}
@ -101,6 +102,14 @@ public abstract class AbstractSpecialRecipe extends Impl<IRecipe> implements IRe
return width * height < ingredients.size();
}
public SpellIngredient getSpellItem() {
return spellitem;
}
public NonNullList<SpellIngredient> getSpellIngredients() {
return ingredients;
}
@Override
public ItemStack getRecipeOutput() {
return spellitem.getStack();

View file

@ -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<ItemStack> getStacks() {
return instance().getIngredient(res).getStacks();
}
@Override
public boolean matches(ItemStack other, int materialMult) {
return instance().getIngredient(res).matches(other, materialMult);

View file

@ -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<ItemStack> getStacks();
class Compound implements SpellIngredient {
private final List<SpellIngredient> items;
@ -38,6 +45,16 @@ public interface SpellIngredient {
this.items = items;
}
public Stream<ItemStack> getStacks() {
Stream<ItemStack> stacks = Lists.<ItemStack>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<ItemStack> getStacks() {
if (ignoreMeta && !contained.isEmpty()) {
NonNullList<ItemStack> 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")) {