Commit 68c6a67a authored by primetoxinz's avatar primetoxinz

More work on new recipe output system, especially JEI

parent 5c08f2f1
......@@ -42,7 +42,7 @@ repositories {
mavenCentral()
}
dependencies {
deobfCompile "mezz.jei:jei_1.12.2:4.8.3.128"
deobfCompile "mezz.jei:jei_1.12.2:+"
deobfCompile "applecore:AppleCore:1.12.2-3.1.0:deobf"
deobfCompile "team.chisel.ctm:CTM:+"
compile "MCMultiPart2:MCMultiPart:2.4.1"
......
package betterwithmods.api.recipe;
import com.google.common.collect.Lists;
import net.minecraft.item.ItemStack;
import java.util.List;
public interface IOutput{
List<IOutput> ALL_OUTPUTS = Lists.newArrayList();
ItemStack getOutput();
String getTooltip();
boolean equals(IOutput output);
IOutput copy();
}
......@@ -4,18 +4,28 @@ import com.google.common.collect.Lists;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public interface IRecipeOutput {
public interface IRecipeOutputs {
NonNullList<ItemStack> getOutputs();
default List<List<ItemStack>> getDisplayOutputs() {
return Lists.<List<ItemStack>>newArrayList(getOutputs());
}
List<IOutput> getDisplayOutputs();
boolean matches(List<ItemStack> outputs);
boolean matchesFuzzy(List<ItemStack> outputs);
boolean isInvalid();
default List<List<IOutput>> getExpandedOutputs() {
return Lists.<List<IOutput>>newArrayList(new ArrayList<>(getDisplayOutputs()));
}
default List<IOutput> cast(List<? extends IOutput> list) {
return list.stream().map(o -> (IOutput)o).collect(Collectors.toList());
}
}
package betterwithmods.api.recipe.impl;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.api.recipe.IRecipeOutputs;
import betterwithmods.util.InvUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import java.util.List;
import java.util.stream.Collectors;
public class ListOutputs implements IRecipeOutputs {
protected List<StackOutput> outputs;
public ListOutputs(List<ItemStack> outputs) {
this.outputs = outputs.stream().filter(s -> !s.isEmpty()).map(StackOutput::new).collect(Collectors.toList());
}
@Override
public NonNullList<ItemStack> getOutputs() {
return InvUtils.asNonnullList(this.outputs.stream().map(StackOutput::getOutput).map(ItemStack::copy).collect(Collectors.toList()));
}
@Override
public List<IOutput> getDisplayOutputs() {
return cast(outputs);
}
@Override
public boolean matches(List<ItemStack> outputs) {
return InvUtils.matchesExact(getOutputs(), outputs);
}
@Override
public boolean matchesFuzzy(List<ItemStack> outputs) {
return InvUtils.matches(getOutputs(), outputs);
}
@Override
public boolean isInvalid() {
return outputs.isEmpty();
}
}
package betterwithmods.api.recipe.impl;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.api.recipe.IRecipeOutputs;
import betterwithmods.util.InvUtils;
import com.google.common.collect.Lists;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import java.util.List;
import java.util.stream.Collectors;
public class RandomCountOutputs implements IRecipeOutputs {
private List<RandomOutput> outputs;
public RandomCountOutputs(RandomOutput... outputs) {
this(Lists.newArrayList(outputs));
}
public RandomCountOutputs(List<RandomOutput> outputs) {
this.outputs = outputs;
}
@Override
public List<IOutput> getDisplayOutputs() {
return cast(outputs);
}
@Override
public NonNullList<ItemStack> getOutputs() {
return findResult();
}
@Override
public boolean matches(List<ItemStack> outputs) {
return false;
}
@Override
public boolean matchesFuzzy(List<ItemStack> outputs) {
return false;
}
@Override
public boolean isInvalid() {
return outputs.isEmpty();
}
private NonNullList<ItemStack> findResult() {
return InvUtils.asNonnullList(outputs.stream().map(RandomOutput::getRandomStack).collect(Collectors.toList()));
}
}
package betterwithmods.api.recipe.impl;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.util.InvUtils;
import net.minecraft.item.ItemStack;
import java.util.Random;
public class RandomOutput extends StackOutput {
private static final Random RANDOM = new Random();
private int min, max;
public RandomOutput(ItemStack stack, int min, int max) {
super(stack);
this.min = min;
this.max = max;
}
public int getMin() {
return min;
}
public int getMax() {
return max;
}
private int rand() {
return RANDOM.nextInt((max - min) + 1) + min;
}
public ItemStack getRandomStack() {
return InvUtils.setCount(getOutput(), rand());
}
public RandomOutput copy() {
return new RandomOutput(getOutput(), min, max);
}
public String getTooltip() {
return String.format("%s-%s", min, max);
}
@Override
public boolean equals(IOutput output) {
if(output instanceof RandomOutput) {
RandomOutput other = (RandomOutput) output;
return this.getMax() == other.getMax() && this.getMin() == other.getMin() && InvUtils.matches(other.getOutput(), this.getOutput());
}
return false;
}
}
package betterwithmods.api.recipe.impl;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.util.InvUtils;
import net.minecraft.item.ItemStack;
public class StackOutput implements IOutput {
protected ItemStack output;
public StackOutput(ItemStack stack) {
this.output = stack;
ALL_OUTPUTS.add(this);
}
@Override
public ItemStack getOutput() {
return output;
}
@Override
public String getTooltip() {
return "";
}
@Override
public boolean equals(IOutput output) {
return InvUtils.matches(output.getOutput(),this.getOutput());
}
@Override
public IOutput copy() {
return new StackOutput(output);
}
}
package betterwithmods.api.recipe.impl;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.util.InvUtils;
import net.minecraft.item.ItemStack;
public class WeightedOutput extends StackOutput {
private double weight;
public WeightedOutput(ItemStack stack, double weight) {
super(stack);
this.weight = weight;
}
public double getWeight() {
return weight;
}
@Override
public String getTooltip() {
//TODO lang?
return String.format("%s", weight);
}
@Override
public boolean equals(IOutput output) {
if (output instanceof WeightedOutput) {
WeightedOutput other = (WeightedOutput) output;
return other.getWeight() == this.getWeight() && InvUtils.matches(other.getOutput(), this.getOutput());
}
return false;
}
}
package betterwithmods.api.recipe;
package betterwithmods.api.recipe.impl;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.api.recipe.IRecipeOutputs;
import betterwithmods.util.InvUtils;
import com.google.common.collect.Lists;
import net.minecraft.item.ItemStack;
......@@ -7,24 +9,24 @@ import net.minecraft.util.NonNullList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class WeightedOutput implements IRecipeOutput {
public class WeightedOutputs implements IRecipeOutputs {
private static final Random RANDOM = new Random();
protected List<WeightedItemStack> weightedItemStacks;
protected List<WeightedOutput> weightedItemStacks;
public WeightedOutput(WeightedItemStack... weightedItemStacks) {
public WeightedOutputs(WeightedOutput... weightedItemStacks) {
this(Lists.newArrayList(weightedItemStacks));
}
public WeightedOutput(List<WeightedItemStack> weightedItemStacks) {
public WeightedOutputs(List<WeightedOutput> weightedItemStacks) {
this.weightedItemStacks = weightedItemStacks;
}
@SuppressWarnings("unchecked")
@Override
public List<List<ItemStack>> getDisplayOutputs() {
return weightedItemStacks.stream().map(WeightedItemStack::getStack).map(Lists::newArrayList).collect(Collectors.toList());
public List<IOutput> getDisplayOutputs() {
return cast(weightedItemStacks);
}
@Override
......@@ -50,32 +52,15 @@ public class WeightedOutput implements IRecipeOutput {
private ItemStack findResult() {
ItemStack result = ItemStack.EMPTY;
double bestValue = Double.MAX_VALUE;
for (WeightedItemStack element : weightedItemStacks) {
for (WeightedOutput element : weightedItemStacks) {
double value = -Math.log(RANDOM.nextDouble()) / element.getWeight();
if (value < bestValue) {
bestValue = value;
result = element.getStack();
result = element.getOutput();
}
}
return result;
}
public static class WeightedItemStack {
private ItemStack stack;
private double weight;
public WeightedItemStack(ItemStack stack, double weight) {
this.stack = stack;
this.weight = weight;
}
public ItemStack getStack() {
return stack;
}
public double getWeight() {
return weight;
}
}
}
package betterwithmods.common.registry.block.recipe;
import betterwithmods.api.recipe.IRecipeOutput;
import betterwithmods.api.recipe.ListOutput;
import betterwithmods.api.recipe.IRecipeOutputs;
import betterwithmods.api.recipe.impl.ListOutputs;
import betterwithmods.util.InvUtils;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;
......@@ -19,13 +19,13 @@ import java.util.List;
*/
public class BlockRecipe {
private final BlockIngredient input;
private final IRecipeOutput recipeOutput;
private final IRecipeOutputs recipeOutput;
public BlockRecipe(BlockIngredient input, List<ItemStack> outputs) {
this(input, new ListOutput(outputs));
this(input, new ListOutputs(outputs));
}
public BlockRecipe(BlockIngredient input, IRecipeOutput recipeOutput) {
public BlockRecipe(BlockIngredient input, IRecipeOutputs recipeOutput) {
this.input = input;
this.recipeOutput = recipeOutput;
}
......@@ -46,7 +46,7 @@ public class BlockRecipe {
return input;
}
public IRecipeOutput getRecipeOutput() {
public IRecipeOutputs getRecipeOutput() {
return recipeOutput;
}
......
package betterwithmods.common.registry.block.recipe;
import betterwithmods.api.recipe.IRecipeOutput;
import betterwithmods.api.recipe.IRecipeOutputs;
import net.minecraft.item.ItemStack;
import java.util.List;
......@@ -14,7 +14,7 @@ public class SawRecipe extends BlockRecipe {
super(input, outputs);
}
public SawRecipe(BlockIngredient input, IRecipeOutput recipeOutput) {
public SawRecipe(BlockIngredient input, IRecipeOutputs recipeOutput) {
super(input, recipeOutput);
}
}
package betterwithmods.common.registry.bulk.recipes;
import betterwithmods.api.recipe.IRecipeOutput;
import betterwithmods.api.recipe.ListOutput;
import betterwithmods.api.recipe.IRecipeOutputs;
import betterwithmods.api.recipe.impl.ListOutputs;
import betterwithmods.util.InvUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
......@@ -18,10 +18,10 @@ import java.util.List;
public class BulkRecipe implements Comparable<BulkRecipe> {
protected NonNullList<Ingredient> inputs;
protected IRecipeOutput recipeOutput;
protected IRecipeOutputs recipeOutput;
protected int priority;
public BulkRecipe(List<Ingredient> inputs, IRecipeOutput outputs, int priority) {
public BulkRecipe(List<Ingredient> inputs, IRecipeOutputs outputs, int priority) {
this.inputs = InvUtils.asNonnullList(inputs);
this.recipeOutput = outputs;
this.priority = priority;
......@@ -32,7 +32,7 @@ public class BulkRecipe implements Comparable<BulkRecipe> {
}
public BulkRecipe(List<Ingredient> inputs, @Nonnull List<ItemStack> outputs, int priority) {
this(inputs, new ListOutput(outputs), priority);
this(inputs, new ListOutputs(outputs), priority);
}
public NonNullList<ItemStack> onCraft(World world, TileEntity tile, ItemStackHandler inv) {
......@@ -44,7 +44,7 @@ public class BulkRecipe implements Comparable<BulkRecipe> {
return NonNullList.create();
}
public IRecipeOutput getRecipeOutput() {
public IRecipeOutputs getRecipeOutput() {
return recipeOutput;
}
......
package betterwithmods.common.registry.bulk.recipes;
import betterwithmods.api.recipe.IRecipeOutput;
import betterwithmods.api.recipe.IRecipeOutputs;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.ResourceLocation;
......@@ -18,7 +18,7 @@ public class MillRecipe extends BulkRecipe {
private int ticks;
public MillRecipe(List<Ingredient> inputs, IRecipeOutput outputs, int priority, SoundEvent sound, int ticks) {
public MillRecipe(List<Ingredient> inputs, IRecipeOutputs outputs, int priority, SoundEvent sound, int ticks) {
super(inputs, outputs, priority);
this.sound = sound;
this.ticks = ticks;
......
package betterwithmods.module.compat.jei;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.client.container.anvil.ContainerSteelAnvil;
import betterwithmods.client.container.anvil.GuiSteelAnvil;
import betterwithmods.client.container.bulk.GuiCauldron;
......@@ -26,11 +27,15 @@ import betterwithmods.common.registry.crafting.ToolBaseRecipe;
import betterwithmods.common.registry.crafting.ToolDamageRecipe;
import betterwithmods.common.registry.heat.BWMHeatRegistry;
import betterwithmods.module.compat.jei.category.*;
import betterwithmods.module.compat.jei.ingredient.OutputHelper;
import betterwithmods.module.compat.jei.ingredient.OutputRenderer;
import betterwithmods.module.compat.jei.wrapper.*;
import betterwithmods.module.gameplay.miniblocks.MiniBlocks;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import mezz.jei.Internal;
import mezz.jei.api.*;
import mezz.jei.api.ingredients.IModIngredientRegistration;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.recipe.IRecipeCategoryRegistration;
import mezz.jei.api.recipe.IVanillaRecipeFactory;
......@@ -38,6 +43,7 @@ import mezz.jei.api.recipe.VanillaRecipeCategoryUid;
import mezz.jei.api.recipe.transfer.IRecipeTransferRegistry;
import mezz.jei.gui.Focus;
import mezz.jei.plugins.vanilla.crafting.ShapelessRecipeWrapper;
import mezz.jei.startup.StackHelper;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
......@@ -126,6 +132,12 @@ public class JEI implements IModPlugin {
return list;
}
@Override
public void registerIngredients(IModIngredientRegistration registry) {
StackHelper stackHelper = Internal.getStackHelper();
registry.register(IOutput.class, Collections.emptySet(), new OutputHelper(stackHelper), new OutputRenderer()) ;
}
@Override
public void register(@Nonnull IModRegistry reg) {
HELPER = reg.getJeiHelpers();
......
......@@ -8,7 +8,6 @@ import mezz.jei.api.IGuiHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiItemStackGroup;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.gui.ITooltipCallback;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.util.Translator;
......
package betterwithmods.module.compat.jei.category;
import betterwithmods.BWMod;
import betterwithmods.api.recipe.IOutput;
import betterwithmods.module.compat.jei.wrapper.BlockRecipeWrapper;
import betterwithmods.util.InvUtils;
import mezz.jei.api.IGuiHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiIngredientGroup;
import mezz.jei.api.gui.IGuiItemStackGroup;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.util.Translator;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull;
import java.util.List;
/**
* Created by primetoxinz on 9/5/16.
......@@ -62,15 +61,12 @@ public class SawRecipeCategory implements IRecipeCategory<BlockRecipeWrapper> {
@Override
public void setRecipe(@Nonnull IRecipeLayout layout, @Nonnull BlockRecipeWrapper wrapper, @Nonnull IIngredients ingredients) {
IGuiItemStackGroup guiItemStacks = layout.getItemStacks();
IGuiIngredientGroup<IOutput> guiOutputs = layout.getIngredientsGroup(IOutput.class);
guiItemStacks.init(0, true, 8, 9);
guiItemStacks.init(1, false, 57, 9);
guiItemStacks.init(2, false, 75, 9);
guiItemStacks.init(3, false, 93, 9);
// guiItemStacks.init(4, false, 32, 27);
guiOutputs.init(1, false, 58, 10);
guiOutputs.init(2, false, 74, 10);
guiOutputs.init(3, false, 90, 10);
guiItemStacks.set(ingredients);
int index = 0;
for (List<ItemStack> outputStacks : InvUtils.splitIntoBoxes(wrapper.recipe.getOutputs(),3))
guiItemStacks.set(1 + index++, outputStacks);
// guiItemStacks.set(4, new ItemStack(BWMBlocks.SAW));
guiOutputs.set(ingredients);
}
}
......@@ -7,7 +7,6 @@ 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.recipe.BlankRecipeCategory;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeWrapper;
import mezz.jei.api.recipe.wrapper.IShapedCraftingRecipeWrapper;
......
package betterwithmods.module.compat.jei.ingredient;
import betterwithmods.api.recipe.IOutput;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.color.ColorGetter;
import mezz.jei.startup.StackHelper;
import mezz.jei.util.ErrorUtil;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nullable;
import java.awt.*;
import java.util.List;
public class OutputHelper implements IIngredientHelper<IOutput> {
private final StackHelper stackHelper;
public OutputHelper(StackHelper stackHelper) {
this.stackHelper = stackHelper;
}
@Override
public List<IOutput> expandSubtypes(List<IOutput> contained) {
return contained;
}
@Nullable
@Override
public IOutput getMatch(Iterable<IOutput> ingredients, IOutput ingredientToMatch) {
for (IOutput r : ingredients) {
if (r.equals(ingredientToMatch)) {
return r;
}
}
return null;
}
@Override
public String getDisplayName(IOutput ingredient) {
return ErrorUtil.checkNotNull(ingredient.getOutput().getDisplayName(), "itemStack.getDisplayName()");
}
@Override
public String getUniqueId(IOutput ingredient) {
ErrorUtil.checkNotEmpty(ingredient.getOutput());
return stackHelper.getUniqueIdentifierForStack(ingredient.getOutput());
}
@Override
public String getWildcardId(IOutput ingredient) {
ErrorUtil.checkNotEmpty(ingredient.getOutput());
return stackHelper.getUniqueIdentifierForStack(ingredient.getOutput(), StackHelper.UidMode.WILDCARD);
}
@Override
public String getModId(IOutput ingredient) {
ErrorUtil.checkNotEmpty(ingredient.getOutput());
Item item = ingredient.getOutput().getItem();
ResourceLocation itemName = item.getRegistryName();
if (itemName == null) {
String stackInfo = getErrorInfo(ingredient);
throw new IllegalStateException("item.getRegistryName() returned null for: " + stackInfo);
}
return itemName.getResourceDomain();
}
@Override
public Iterable<Color> getColors(IOutput ingredient) {
return ColorGetter.getColors(ingredient.getOutput(), 2);
}
@Override
public String getResourceId(IOutput ingredient) {
ErrorUtil.checkNotEmpty(ingredient.getOutput());
Item item = ingredient.getOutput().getItem();
ResourceLocation itemName = item.getRegistryName();
if (itemName == null) {
String stackInfo = getErrorInfo(ingredient);
throw new IllegalStateException("item.getRegistryName() returned null for: " + stackInfo);
}
return itemName.getResourcePath();
}
@Override
public IOutput copyIngredient(IOutput ingredient) {
return ingredient.copy();
}
@Override
public String getErrorInfo(IOutput ingredient) {
return ErrorUtil.getItemStackInfo(ingredient.getOutput());
}
}
package betterwithmods.module.compat.jei.ingredient;
import betterwithmods.api.recipe.IOutput;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.plugins.vanilla.ingredients.ItemStackRenderer;