Skip to content

Commit

Permalink
Improve display of many colored recipes
Browse files Browse the repository at this point in the history
Skipping potions for now, no good way to show them without listing all potions. Maybe will make it a config in the future
  • Loading branch information
KnightMiner committed Sep 5, 2020
1 parent 17e4d34 commit f414a92
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 23 deletions.
Expand Up @@ -2,20 +2,18 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredient;
import knightminer.inspirations.library.recipe.cauldron.inventory.ICauldronState;
import knightminer.inspirations.library.recipe.cauldron.util.DisplayCauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.util.LevelPredicate;
import knightminer.inspirations.library.recipe.cauldron.util.TemperaturePredicate;
import net.minecraft.util.JSONUtils;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -64,22 +62,13 @@ public List<ICauldronContents> getContentInputs() {
return ingredient.getMatchingContents();
}

/**
* Gets an optional fluidstack from the given contents
* @param contents Contents instance
* @return Fluid stack if its a fluid contents
*/
private static Optional<FluidStack> getFluid(ICauldronContents contents) {
return contents.get(CauldronContentTypes.FLUID).map(fluid -> new FluidStack(fluid, FluidAttributes.BUCKET_VOLUME));
}

@Override
public List<FluidStack> getFluidInputs() {
if (fluidInputs == null) {
// map fluids to fluid stacks, skip any non-fluids
List<ICauldronContents> inputs = getContentInputs();
fluidInputs = inputs.stream()
.flatMap(contents -> getFluid(contents).map(Stream::of).orElseGet(Stream::empty))
.flatMap(contents -> DisplayCauldronRecipe.getFluid(contents).map(Stream::of).orElseGet(Stream::empty))
.collect(Collectors.toList());
// ensure size is the same, potions are cross registered
if (fluidInputs.size() != inputs.size()) {
Expand Down Expand Up @@ -109,7 +98,7 @@ public ICauldronContents getContentOutput() {
@Override
public FluidStack getFluidOutput() {
if (displayFluid == null) {
displayFluid = getFluid(getContentOutput()).orElse(FluidStack.EMPTY);
displayFluid = DisplayCauldronRecipe.getFluid(getContentOutput()).orElse(FluidStack.EMPTY);
}
return displayFluid;
}
Expand Down
Expand Up @@ -44,7 +44,9 @@ default List<FluidStack> getFluidInputs() {
* Gets the temperature required for input
* @return Input temperature
*/
TemperaturePredicate getTemperature();
default TemperaturePredicate getTemperature() {
return TemperaturePredicate.ANY;
}

/**
* Gets the duration of this recipe. Return -1 for no duration
Expand Down Expand Up @@ -82,4 +84,12 @@ default int getTime() {
default FluidStack getFluidOutput() {
return FluidStack.EMPTY;
}

/**
* Method to override for recipes that are simple, but have a non-simple variant
* @return True if the recipe is simple and should be shown
*/
default boolean isSimple() {
return true;
}
}
Expand Up @@ -9,26 +9,35 @@
import knightminer.inspirations.library.recipe.cauldron.inventory.ICauldronInventory;
import knightminer.inspirations.library.recipe.cauldron.inventory.IModifyableCauldronInventory;
import knightminer.inspirations.library.recipe.cauldron.recipe.ICauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.util.DisplayCauldronRecipe;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.DyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvents;
import net.minecraft.world.World;
import slimeknights.mantle.recipe.IMultiRecipe;
import slimeknights.mantle.util.JsonHelper;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Shared logic between dyeing and clearing dye
*/
public abstract class DyeableCauldronRecipe implements ICauldronRecipe {
public abstract class DyeableCauldronRecipe implements ICauldronRecipe, IMultiRecipe<DisplayCauldronRecipe> {
private final ResourceLocation id;
private final Ingredient ingredient;
private List<DisplayCauldronRecipe> displayRecipes;

/**
* Recipe to remove dye from an item
Expand Down Expand Up @@ -87,10 +96,28 @@ public ResourceLocation getId() {
return id;
}

/* Display */

/**
* Gets a stream of recipes for the given input stack
* @param stack Input stack
* @return Stream of recipes for display
*/
protected abstract Stream<DisplayCauldronRecipe> getDisplayRecipes(ItemStack stack);

@Override
public List<DisplayCauldronRecipe> getRecipes() {
if (displayRecipes == null) {
displayRecipes = Arrays.stream(ingredient.getMatchingStacks()).flatMap(this::getDisplayRecipes).collect(Collectors.toList());
}
return displayRecipes;
}

/**
* Recipe to dye a dyeable
*/
public static class Dye extends DyeableCauldronRecipe {
private List<DisplayCauldronRecipe> displayRecipes;
public Dye(ResourceLocation id, Ingredient ingredient) {
super(id, ingredient);
}
Expand All @@ -106,6 +133,17 @@ protected ItemStack updateColor(ICauldronContents contents, ItemStack stack) {
return Util.setColor(stack, color);
}

@Override
protected Stream<DisplayCauldronRecipe> getDisplayRecipes(ItemStack stack) {
List<ItemStack> inputs = Collections.singletonList(stack);
return Arrays.stream(DyeColor.values())
.map(color -> DisplayCauldronRecipe.builder(1, 0)
.setItemInputs(inputs)
.setContentInputs(CauldronContentTypes.DYE.of(color))
.setItemOutput(Util.setColor(stack.copy(), color.getColorValue()))
.build());
}

@Override
public IRecipeSerializer<?> getSerializer() {
return RecipeSerializers.CAULDRON_DYE_DYEABLE;
Expand All @@ -130,6 +168,16 @@ protected ItemStack updateColor(ICauldronContents contents, ItemStack stack) {
return Util.clearColor(stack);
}

@Override
protected Stream<DisplayCauldronRecipe> getDisplayRecipes(ItemStack stack) {
List<ItemStack> inputs = Arrays.stream(DyeColor.values()).map(color -> Util.setColor(stack.copy(), color.getColorValue())).collect(Collectors.toList());
return Stream.of(DisplayCauldronRecipe.builder(1, 0)
.setItemInputs(inputs)
.setContentInputs(DisplayCauldronRecipe.WATER_CONTENTS.getValue())
.setItemOutput(Util.clearColor(stack.copy()))
.build());
}

@Override
public IRecipeSerializer<?> getSerializer() {
return RecipeSerializers.CAULDRON_CLEAR_DYEABLE;
Expand Down

0 comments on commit f414a92

Please sign in to comment.