diff --git a/src/main/java/slimeknights/mantle/recipe/AbstractRecipeBuilder.java b/src/main/java/slimeknights/mantle/recipe/AbstractRecipeBuilder.java new file mode 100644 index 0000000..5e97ab3 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/AbstractRecipeBuilder.java @@ -0,0 +1,79 @@ +package slimeknights.mantle.recipe; + +import net.minecraft.advancements.Advancement; +import net.minecraft.advancements.AdvancementRewards; +import net.minecraft.advancements.ICriterionInstance; +import net.minecraft.advancements.IRequirementsStrategy; +import net.minecraft.advancements.criterion.RecipeUnlockedTrigger; +import net.minecraft.data.IFinishedRecipe; +import net.minecraft.util.ResourceLocation; + +import javax.annotation.Nullable; +import java.util.function.Consumer; + +/** + * Common logic to create a recipe builder class + * @param + */ +@SuppressWarnings({"WeakerAccess", "unused"}) +public abstract class AbstractRecipeBuilder> { + /** Advancement builder for this class */ + protected final Advancement.Builder advancementBuilder = Advancement.Builder.builder(); + /** Group for this recipe */ + @Nullable + protected String group; + + /** + * Adds a criteria to the recipe + * @param name Criteria name + * @param criteria Criteria instance + * @return Builder + */ + @SuppressWarnings("unchecked") + public T addCriterion(String name, ICriterionInstance criteria) { + this.advancementBuilder.withCriterion(name, criteria); + return (T)this; + } + + /** + * Sets the group for this recipe + * @param group Recipe group + * @return Builder + */ + @SuppressWarnings("unchecked") + public T setGroup(String group) { + this.group = group; + return (T)this; + } + + /** + * Builds the recipe with a default recipe ID, typically based on the output + * @param consumerIn Recipe consumer + */ + public abstract void build(Consumer consumerIn); + + /** + * Builds the recipe + * @param consumerIn Recipe consumer + * @param id Recipe ID + */ + public abstract void build(Consumer consumerIn, ResourceLocation id); + + /** + * Builds and validates the advancement, intended to be called in {@link #build(Consumer, ResourceLocation)} + * @param id Recipe ID + * @param folder Group folder for saving recipes. Vanilla typically uses item groups, but for mods might as well base on the recipe + * @return Advancement ID + */ + protected ResourceLocation buildAdvancement(ResourceLocation id, String folder) { + if (this.advancementBuilder.getCriteria().isEmpty()) { + throw new IllegalStateException("No way of obtaining recipe " + id); + } + this.advancementBuilder + .withParentId(new ResourceLocation("recipes/root")) + .withCriterion("has_the_recipe", RecipeUnlockedTrigger.func_235675_a_(id)) + .withRewards(AdvancementRewards.Builder.recipe(id)) + .withRequirementsStrategy(IRequirementsStrategy.OR); + return new ResourceLocation(id.getNamespace(), "recipes/" + folder + "/" + id.getPath()); + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/FluidIngredient.java b/src/main/java/slimeknights/mantle/recipe/FluidIngredient.java new file mode 100644 index 0000000..5b75df2 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/FluidIngredient.java @@ -0,0 +1,410 @@ +package slimeknights.mantle.recipe; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import net.minecraft.fluid.Fluid; +import net.minecraft.fluid.Fluids; +import net.minecraft.network.PacketBuffer; +import net.minecraft.tags.FluidTags; +import net.minecraft.tags.ITag; +import net.minecraft.tags.Tag; +import net.minecraft.tags.TagCollectionManager; +import net.minecraft.util.JSONUtils; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.registries.ForgeRegistries; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +@SuppressWarnings("unused") +public abstract class FluidIngredient { + /** Empty fluid ingredient, matches nothing */ + public static final FluidIngredient EMPTY = new Empty(); + + /** + * Checks if the given fluid matches this ingredient + * @param fluid Fluid to check + * @return True if the fluid matches + */ + public abstract boolean test(Fluid fluid); + + /** + * Gets the amount of the given fluid needed for the recipe + * @param fluid Fluid to check + * @return Amount of the fluid needed + */ + public abstract int getAmount(Fluid fluid); + + /** + * Checks if the given fluid stack argument matches this ingredient + * @param stack Fluid stack to check + * @return True if the fluid matches this ingredient and the amount is equal or greater than this + */ + public boolean test(FluidStack stack) { + Fluid fluid = stack.getFluid(); + return stack.getAmount() >= getAmount(fluid) && test(stack.getFluid()); + } + + /** + * Gets a list of fluid stacks contained in this ingredient for display + * @return List of fluid stacks for this ingredient + */ + public abstract List getFluids(); + + /** + * Serializes the Fluid Ingredient into JSON + * @return FluidIngredient JSON + */ + public abstract JsonElement serialize(); + + /** + * Writes the ingredient into the packet buffer + * @param buffer Packet buffer instance + */ + public void write(PacketBuffer buffer) { + Collection fluids = getFluids(); + buffer.writeInt(fluids.size()); + for (FluidStack stack : fluids) { + buffer.writeString(Objects.requireNonNull(stack.getFluid().getRegistryName()).toString()); + buffer.writeInt(stack.getAmount()); + } + } + + + /* + * Instance creation + */ + + /** + * Creates a new ingredient using the given fluid and amount + * @param fluid Fluid to check + * @param amount Minimum fluid amount + * @return Fluid ingredient for this fluid + */ + public static FluidIngredient of(Fluid fluid, int amount) { + return new FluidIngredient.FluidMatch(fluid, amount); + } + + /** + * Creates a new ingredient using the given fluidstack + * @param stack Fluid stack + * @return Fluid ingredient for this fluid stack + */ + public static FluidIngredient of(FluidStack stack) { + return of(stack.getFluid(), stack.getAmount()); + } + + /** + * Creates a new fluid ingredient from the given tag + * @param fluid Fluid tag + * @param amount Minimum fluid amount + * @return Fluid ingredient from a tag + */ + public static FluidIngredient of(Tag fluid, int amount) { + return new FluidIngredient.TagMatch(fluid, amount); + } + + /** + * Creates a new compound ingredient from the given list of ingredients + * @param ingredients Ingredient list + * @return Compound ingredient + */ + public static FluidIngredient of(FluidIngredient... ingredients) { + return new FluidIngredient.Compound(ingredients); + } + + + /* + * JSON deserializing + */ + + /** + * Deserializes the fluid ingredient from JSON + * @param parent Parent containing the fluid JSON + * @param name Name of the key to fetch from the parent object + * @return Fluid ingredient instance + * @throws JsonSyntaxException if syntax is invalid + */ + public static FluidIngredient deserialize(JsonObject parent, String name) { + JsonElement json = parent.get(name); + + // single ingredient object + if (json.isJsonObject()) { + return deserializeObject(json.getAsJsonObject()); + } + + // array + if (json.isJsonArray()) { + return Compound.deserialize(json.getAsJsonArray(), name); + } + + throw new JsonSyntaxException("Fluid ingredient " + name + " must be either an object or array"); + } + + /** + * Deserializes the fluid ingredient from JSON + * @param json JSON object + * @return Fluid Ingredient + * @throws JsonSyntaxException if syntax is invalid + */ + private static FluidIngredient deserializeObject(JsonObject json) { + if (json.entrySet().isEmpty()) { + return EMPTY; + } + + // fluid match + if (json.has("name")) { + // don't set both, obviously an error + if (json.has("tag")) { + throw new JsonSyntaxException("An ingredient entry is either a tag or an fluid, not both"); + } + + // parse a fluid + return FluidMatch.deserialize(json); + } + + // tag match + if (json.has("tag")) { + return TagMatch.deserialize(json); + } + + throw new JsonSyntaxException("An ingredient entry needs either a tag or an fluid"); + } + + + /* + * Packet buffers + */ + + /** + * Reads a fluid ingredient from the packet buffer + * @param buffer Buffer instance + * @return Fluid ingredient instance + */ + public static FluidIngredient read(PacketBuffer buffer) { + int count = buffer.readInt(); + FluidIngredient[] ingredients = new FluidIngredient[count]; + for (int i = 0; i < count; i++) { + Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(buffer.readString(32767))); + if (fluid == null) { + fluid = Fluids.EMPTY; + } + int amount = buffer.readInt(); + ingredients[i] = of(fluid, amount); + } + // if a single ingredient, do not wrap in compound + if (count == 1) { + return ingredients[0]; + } + // compound for anything else + return of(ingredients); + } + + /** + * Empty fluid ingredient, matches only empty fluid stacks + */ + @NoArgsConstructor(access = AccessLevel.PRIVATE) + private static class Empty extends FluidIngredient { + @Override + public boolean test(Fluid fluid) { + return fluid == Fluids.EMPTY; + } + @Override + public boolean test(FluidStack fluid) { + return fluid.isEmpty(); + } + + @Override + public int getAmount(Fluid fluid) { + return 0; + } + + @Override + public List getFluids() { + return Collections.emptyList(); + } + + @Override + public JsonElement serialize() { + return new JsonObject(); + } + } + + /** + * Fluid ingredient that matches a single fluid + */ + @AllArgsConstructor(access=AccessLevel.PRIVATE) + private static class FluidMatch extends FluidIngredient { + private final Fluid fluid; + private final int amount; + + @Override + public boolean test(Fluid fluid) { + return fluid == this.fluid; + } + + @Override + public int getAmount(Fluid fluid) { + return amount; + } + + @Override + public List getFluids() { + return Collections.singletonList(new FluidStack(fluid, amount)); + } + + @Override + public JsonElement serialize() { + JsonObject object = new JsonObject(); + object.addProperty("name", Objects.requireNonNull(fluid.getRegistryName()).toString()); + object.addProperty("amount", amount); + return object; + } + + @Override + public void write(PacketBuffer buffer) { + // count + buffer.writeInt(1); + // single fluid + buffer.writeString(Objects.requireNonNull(fluid.getRegistryName()).toString()); + buffer.writeInt(amount); + } + + /** + * Deserailizes the ingredient from JSON + * @param json JSON object + * @return Fluid ingredient instance + */ + private static FluidMatch deserialize(JsonObject json) { + String fluidName = JSONUtils.getString(json, "name"); + Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(fluidName)); + if (fluid == null || fluid == Fluids.EMPTY) { + throw new JsonSyntaxException("Unknown fluid '" + fluidName + "'"); + } + int amount = JSONUtils.getInt(json, "amount"); + return new FluidMatch(fluid, amount); + } + } + + /** + * Fluid ingredient that matches a tag + */ + @AllArgsConstructor(access=AccessLevel.PRIVATE) + private static class TagMatch extends FluidIngredient { + private final ITag tag; + private final int amount; + + @Override + public boolean test(Fluid fluid) { + return tag.contains(fluid); + } + + @Override + public int getAmount(Fluid fluid) { + return amount; + } + + @Override + public List getFluids() { + return tag.getAllElements().stream().map((fluid) -> new FluidStack(fluid, amount)).collect(Collectors.toList()); + } + + @Override + public JsonElement serialize() { + JsonObject object = new JsonObject(); + object.addProperty("tag", TagCollectionManager.func_232928_e_().func_232926_c_().func_232975_b_(this.tag).toString()); + object.addProperty("amount", amount); + return object; + } + + /** + * Deseralizes the ingredient from JSON + * @param json JSON object + * @return Fluid ingredient instance + */ + private static TagMatch deserialize(JsonObject json) { + String tagName = JSONUtils.getString(json, "tag"); + ITag tag = FluidTags.getCollection().get(new ResourceLocation(tagName)); + if (tag == null) { + throw new JsonSyntaxException("Unknown fluid tag '" + tagName + "'"); + } + int amount = JSONUtils.getInt(json, "amount"); + return new TagMatch(tag, amount); + } + } + + /** + * Fluid ingredient that matches a list of ingredients + */ + private static class Compound extends FluidIngredient { + private final List ingredients; + private Compound(FluidIngredient[] ingredients) { + this.ingredients = Arrays.asList(ingredients); + } + + @Override + public boolean test(Fluid fluid) { + return ingredients.stream().anyMatch(ingredient -> ingredient.test(fluid)); + } + + @Override + public boolean test(FluidStack stack) { + return ingredients.stream().anyMatch(ingredient -> ingredient.test(stack)); + } + + @Override + public int getAmount(Fluid fluid) { + return ingredients.stream() + .filter(ingredient -> ingredient.test(fluid)) + .mapToInt(ingredient -> ingredient.getAmount(fluid)) + .findFirst() + .orElse(0); + } + + @Override + public List getFluids() { + return ingredients.stream() + .flatMap(ingredient -> ingredient.getFluids().stream()) + .collect(Collectors.toList()); + } + + @Override + public JsonElement serialize() { + return ingredients.stream() + .map(FluidIngredient::serialize) + .collect(JsonArray::new, JsonArray::add, JsonArray::addAll); + } + + /** + * Deserializes a compound ingredient from JSON + * @param array JSON array + * @param name Array key + * @return Compound fluid ingredient instance + */ + private static Compound deserialize(JsonArray array, String name) { + // size must be valid + int size = array.size(); + if (size == 0) { + throw new JsonSyntaxException("Fluid array cannot be empty, at least one fluid must be defined"); + } + + // parse all ingredients + FluidIngredient[] ingredients = new FluidIngredient[size]; + for (int i = 0; i < size; i++) { + // no reason to an array in an array + ingredients[i] = deserializeObject(JSONUtils.getJsonObject(array.get(i), name + "[" + i + "]")); + } + return new Compound(ingredients); + } + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/IMultiRecipe.java b/src/main/java/slimeknights/mantle/recipe/IMultiRecipe.java new file mode 100644 index 0000000..2a60df7 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/IMultiRecipe.java @@ -0,0 +1,17 @@ +package slimeknights.mantle.recipe; + +import net.minecraft.item.crafting.IRecipe; + +import java.util.List; + +/** + * This interface is intended to be used on dynamic recipes to return a full list of valid recipes. + * @param Recipe type for the return + */ +public interface IMultiRecipe> { + /** + * Gets a list of recipes for display in JEI + * @return List of recipes + */ + List getRecipes(); +} diff --git a/src/main/java/slimeknights/mantle/recipe/RecipeHelper.java b/src/main/java/slimeknights/mantle/recipe/RecipeHelper.java new file mode 100644 index 0000000..f6329d4 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/RecipeHelper.java @@ -0,0 +1,212 @@ +package slimeknights.mantle.recipe; + +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import io.netty.handler.codec.DecoderException; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import net.minecraft.fluid.Fluid; +import net.minecraft.fluid.Fluids; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.Item; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.IRecipeType; +import net.minecraft.item.crafting.RecipeManager; +import net.minecraft.network.PacketBuffer; +import net.minecraft.util.IItemProvider; +import net.minecraft.util.JSONUtils; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.registries.ForgeRegistries; + +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Helpers used in creation of recipes + */ +@SuppressWarnings({"WeakerAccess", "unused"}) +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class RecipeHelper { + + /* Recipe manager utils */ + + /** + * Gets a recipe of a specific class type by name from the manager + * @param manager Recipe manager + * @param name Recipe name + * @param clazz Output class + * @param Return type + * @return Optional of the recipe, or empty if the recipe is missing + */ + public static > Optional getRecipe(RecipeManager manager, ResourceLocation name, Class clazz) { + return manager.getRecipe(name).filter(clazz::isInstance).map(clazz::cast); + } + + /** + * Gets a list of all recipes from the manager, safely casting to the specified type. Multi Recipes are kept as a single recipe instance + * @param manager Recipe manager + * @param type Recipe type + * @param clazz Preferred recipe class type + * @param Inventory interface type + * @param Recipe class + * @param Return type + * @return List of recipes from the manager + */ + public static , C extends T> List getRecipes(RecipeManager manager, IRecipeType type, Class clazz) { + return manager.getRecipes(type).values().stream() + .filter(clazz::isInstance) + .map(clazz::cast) + .collect(Collectors.toList()); + } + + /** + * Gets a list of recipes for display in a UI list, such as UI buttons. Will be sorted to keep the order the same on both sides, and filtered based on the given predicate and class + * @param manager Recipe manager + * @param type Recipe type + * @param clazz Preferred recipe class type + * @param filter Filter for which recipes to add to the list + * @param Inventory interface type + * @param Recipe class + * @param Return type + * @return Recipe list + */ + public static , C extends T> List getUIRecipes(RecipeManager manager, IRecipeType type, Class clazz, Predicate filter) { + return manager.getRecipes(type).values().stream() + .filter(clazz::isInstance) + .map(clazz::cast) + .filter(filter) + .sorted(Comparator.comparing(IRecipe::getId)) + .collect(Collectors.toList()); + } + + /** + * Gets a list of all recipes from the manager, expanding multi recipes. Intended for use in recipe display such as JEI + * @param manager Recipe manager + * @param type Recipe type + * @param clazz Preferred recipe class type + * @param Inventory interface type + * @param Recipe class + * @param Return type + * @return List of flattened recipes from the manager + */ + public static , C extends T> List getJEIRecipes(RecipeManager manager, IRecipeType type, Class clazz) { + return manager.getRecipes(type).values().stream() + .sorted((r1, r2) -> { + // if one is multi, and the other not, the multi recipe is larger + boolean m1 = r1 instanceof IMultiRecipe; + boolean m2 = r2 instanceof IMultiRecipe; + if (m1 && !m2) { + return 1; + } + if (!m1 && m2) { + return -1; + } + // fall back to recipe ID + return r1.getId().compareTo(r2.getId()); + }) + .flatMap((recipe) -> { + // if its a multi recipe, extract child recipes and stream those + if (recipe instanceof IMultiRecipe) { + return ((IMultiRecipe)recipe).getRecipes().stream(); + } + return Stream.of(recipe); + }) + .filter(clazz::isInstance) + .map(clazz::cast) + .collect(Collectors.toList()); + } + + + /* JSON */ + + /** + * Serializes the fluid stack into JSON + * @param stack Stack to serialize + * @return JSON data + */ + public static JsonObject serializeFluidStack(FluidStack stack) { + JsonObject json = new JsonObject(); + json.addProperty("fluid", Objects.requireNonNull(stack.getFluid().getRegistryName()).toString()); + json.addProperty("amount", stack.getAmount()); + return json; + } + + /** + * Deserializes the fluid stack from JSON + * @param json JSON data + * @return Fluid stack instance + * @throws JsonSyntaxException if syntax is invalid + */ + public static FluidStack deserializeFluidStack(JsonObject json) { + String fluidName = JSONUtils.getString(json, "fluid"); + Fluid fluid = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(fluidName)); + if (fluid == null || fluid == Fluids.EMPTY) { + throw new JsonSyntaxException("Unknown fluid " + fluidName); + } + int amount = JSONUtils.getInt(json, "amount"); + return new FluidStack(fluid, amount); + } + + /** + * Gets an item from JSON and validates it class type + * @param name String containing an item name + * @param key Key to use for errors + * @param clazz Output class + * @param Class type + * @return Item read from JSON with the given class type + * @throws JsonSyntaxException If the key is missing, or the value is not the right class + */ + public static C deserializeItem(String name, String key, Class clazz) { + Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(name)); + if (item == null) { + throw new JsonSyntaxException("Invalid " + key + ": Unknown item " + name + "'"); + } + if (!clazz.isInstance(item)) { + throw new JsonSyntaxException("Invalid " + key + ": must be " + clazz.getSimpleName()); + } + return clazz.cast(item); + } + + + /* Packet buffer utils */ + + /** + * Reads an item from the packet buffer + * @param buffer Buffer instance + * @return Item read from the buffer + */ + public static Item readItem(PacketBuffer buffer) { + return Item.getItemById(buffer.readVarInt()); + } + + /** + * Reads an item from the packet buffer and validates its class type + * @param buffer Buffer instance + * @param clazz Output class + * @param Class type + * @return Item read from the buffer with the given class type + * @throws DecoderException If the value is not the right class + */ + public static T readItem(PacketBuffer buffer, Class clazz) { + Item item = readItem(buffer); + if (!clazz.isInstance(item)) { + throw new DecoderException("Invalid item '" + item.getRegistryName() + "', must be " + clazz.getSimpleName()); + } + return clazz.cast(item); + } + + /** + * Writes an item to the packet buffer + * @param buffer Buffer instance + * @param item Item to write + */ + public static void writeItem(PacketBuffer buffer, IItemProvider item) { + buffer.writeVarInt(Item.getIdFromItem(item.asItem())); + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/inventory/IEmptyInventory.java b/src/main/java/slimeknights/mantle/recipe/inventory/IEmptyInventory.java new file mode 100644 index 0000000..d479696 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/inventory/IEmptyInventory.java @@ -0,0 +1,29 @@ +package slimeknights.mantle.recipe.inventory; + +import net.minecraft.item.ItemStack; + +/** + * Base inventory for inventories that do not use items + */ +public interface IEmptyInventory extends IReadOnlyInventory { + /** @deprecated unused method */ + @Deprecated + @Override + default ItemStack getStackInSlot(int index) { + return ItemStack.EMPTY; + } + + /** @deprecated unused method */ + @Deprecated + @Override + default boolean isEmpty() { + return true; + } + + /** @deprecated always 0, not useful */ + @Deprecated + @Override + default int getSizeInventory() { + return 0; + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/inventory/IReadOnlyInventory.java b/src/main/java/slimeknights/mantle/recipe/inventory/IReadOnlyInventory.java new file mode 100644 index 0000000..b1bcc54 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/inventory/IReadOnlyInventory.java @@ -0,0 +1,55 @@ +package slimeknights.mantle.recipe.inventory; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +/** + * IInventory extension for a recipe that only needs read access. + * Used to control which slots an recipe gets and to prevent the need to implement IInventory to get the recipe. + */ +public interface IReadOnlyInventory extends IInventory { + /* Unsupported operations */ + + /** @deprecated unsupported method */ + @Deprecated + @Override + default ItemStack decrStackSize(int index, int count) { + throw new UnsupportedOperationException(); + } + + /** @deprecated unsupported method */ + @Deprecated + @Override + default ItemStack removeStackFromSlot(int index) { + throw new UnsupportedOperationException(); + } + + /** @deprecated unsupported method */ + @Deprecated + @Override + default void setInventorySlotContents(int index, ItemStack stack) { + throw new UnsupportedOperationException(); + } + + /** @deprecated unsupported method */ + @Deprecated + @Override + default void clear() { + throw new UnsupportedOperationException(); + } + + /* Unused */ + + /** @deprecated unused method */ + @Deprecated + @Override + default void markDirty() {} + + /** @deprecated unused method */ + @Deprecated + @Override + default boolean isUsableByPlayer(PlayerEntity player) { + return true; + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/inventory/ISingleItemInventory.java b/src/main/java/slimeknights/mantle/recipe/inventory/ISingleItemInventory.java new file mode 100644 index 0000000..3308397 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/inventory/ISingleItemInventory.java @@ -0,0 +1,56 @@ +package slimeknights.mantle.recipe.inventory; + +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; + +import java.util.Set; + +/** + * IInventory extension for an inventory wrapper containing a single item. Primarily used for furnace like recipes. + */ +public interface ISingleItemInventory extends IReadOnlyInventory { + /** + * Gets the relevant item in this inventory + * @return Contained item + */ + ItemStack getStack(); + + /* Multistack methods, redundant now */ + + /** @deprecated use {{@link #getStack()}} */ + @Deprecated + @Override + default ItemStack getStackInSlot(int index) { + return index == 0 ? getStack() : ItemStack.EMPTY; + } + + @Override + default boolean isEmpty() { + return getStack().isEmpty(); + } + + /** @deprecated always 1, not useful */ + @Deprecated + @Override + default int getSizeInventory() { + return 1; + } + + + /* Utils made more efficient */ + + @Override + default int count(Item itemIn) { + ItemStack stack = getStack(); + if (stack.getItem() == itemIn) { + return stack.getCount(); + } + return 0; + } + + @Override + default boolean hasAny(Set set) { + ItemStack stack = getStack(); + return !stack.isEmpty() && set.contains(getStack().getItem()); + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/inventory/InventorySlotWrapper.java b/src/main/java/slimeknights/mantle/recipe/inventory/InventorySlotWrapper.java new file mode 100644 index 0000000..042224a --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/inventory/InventorySlotWrapper.java @@ -0,0 +1,19 @@ +package slimeknights.mantle.recipe.inventory; + +import lombok.AllArgsConstructor; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; + +/** + * Implementation of {@link ISingleItemInventory} to wrap another {@link IInventory} + */ +@AllArgsConstructor +public class InventorySlotWrapper implements ISingleItemInventory { + private final IInventory parent; + private final int index; + + @Override + public ItemStack getStack() { + return parent.getStackInSlot(index); + } +} diff --git a/src/main/java/slimeknights/mantle/recipe/inventory/package-info.java b/src/main/java/slimeknights/mantle/recipe/inventory/package-info.java new file mode 100644 index 0000000..c05aad5 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/inventory/package-info.java @@ -0,0 +1,7 @@ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package slimeknights.mantle.recipe.inventory; + +import mcp.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/src/main/java/slimeknights/mantle/recipe/package-info.java b/src/main/java/slimeknights/mantle/recipe/package-info.java new file mode 100644 index 0000000..333a3e6 --- /dev/null +++ b/src/main/java/slimeknights/mantle/recipe/package-info.java @@ -0,0 +1,7 @@ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package slimeknights.mantle.recipe; + +import mcp.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index fb7ccc9..8366ea0 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -21,3 +21,6 @@ public net.minecraft.client.gui.screen.Screen buttons # buttons # Model utils public net.minecraft.client.renderer.model.WeightedBakedModel field_177566_c # baseModel public net.minecraft.client.renderer.model.MultipartBakedModel field_188626_f # selectors + +# Recipe helper +public net.minecraft.item.crafting.RecipeManager func_215366_a(Lnet/minecraft/item/crafting/IRecipeType;)Ljava/util/Map; # getRecipes