Skip to content

Commit

Permalink
RecipeInput
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Jun 8, 2024
1 parent 42ee003 commit aff1c56
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@
*/
package org.spongepowered.api.block.entity.carrier;

import org.spongepowered.api.item.recipe.crafting.RecipeInput;

import java.util.List;

public interface Campfire extends CarrierBlockEntity {

List<RecipeInput.Single> recipeInputs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.spongepowered.api.data.Keys;
import org.spongepowered.api.data.value.Value;
import org.spongepowered.api.item.recipe.Recipe;
import org.spongepowered.api.item.recipe.crafting.RecipeInput;
import org.spongepowered.api.util.Ticks;

/**
Expand Down Expand Up @@ -82,4 +83,6 @@ default Value.Mutable<Ticks> maxCookTime() {
return this.requireValue(Keys.MAX_COOK_TIME).asMutable();
}

RecipeInput.Single asRecipeInput();

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.type.GridInventory;
import org.spongepowered.api.item.recipe.crafting.CraftingRecipe;
import org.spongepowered.api.item.recipe.crafting.RecipeInput;
import org.spongepowered.api.world.server.ServerWorld;

import java.util.Optional;
Expand All @@ -50,4 +51,11 @@ public interface CraftingGridInventory extends Inventory {
* @return the grid inventory
*/
GridInventory asGrid();

/**
* Returns this inventory as a {@link RecipeInput.Crafting}.
*
* @return the recipe input
*/
RecipeInput.Crafting asRecipeInput();
}
24 changes: 12 additions & 12 deletions src/main/java/org/spongepowered/api/item/recipe/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import org.spongepowered.api.block.entity.carrier.furnace.Furnace;
import org.spongepowered.api.block.entity.carrier.furnace.Smoker;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.inventory.crafting.CraftingInventory;
import org.spongepowered.api.item.recipe.cooking.CookingRecipe;
import org.spongepowered.api.item.recipe.crafting.CraftingRecipe;
import org.spongepowered.api.item.recipe.crafting.Ingredient;
import org.spongepowered.api.item.recipe.crafting.RecipeInput;
import org.spongepowered.api.item.recipe.crafting.RecipeResult;
import org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe;
import org.spongepowered.api.item.recipe.crafting.ShapelessCraftingRecipe;
Expand All @@ -57,7 +57,7 @@
* <p>{@link StoneCutterRecipe} for recipes in a {@link BlockTypes#STONECUTTER} block</p>
* <p>{@link SmithingRecipe} for recipes in a {@link BlockTypes#SMITHING_TABLE} block</p>
*/
public interface Recipe {
public interface Recipe<T extends RecipeInput> {

/**
* Checks if the given inventory fits the required constraints to make a valid recipe
Expand All @@ -67,11 +67,11 @@ public interface Recipe {
*
* @return True if the given input matches this recipe's requirements
*/
boolean isValid(Inventory inventory, ServerWorld world);
boolean isValid(T inventory, ServerWorld world);

/**
* The result of this recipe. This method should only be called if
* {@link #isValid(Inventory, ServerWorld)} returns {@code true}.
* {@link #isValid(RecipeInput, ServerWorld)} returns {@code true}.
*
* <p>This method is preferred over the {@link CraftingRecipe#exemplaryResult()} method,
* as it may customize the result further depending on the context.</p>
Expand All @@ -80,19 +80,19 @@ public interface Recipe {
*
* @return The result of this recipe
*/
ItemStackSnapshot result(Inventory inventory);
ItemStackSnapshot result(T inventory);

/**
* A general result of this recipe. This result may be customized depending on the context.
* See {@link #result(Inventory)}
* See {@link #result(RecipeInput)}
*
* @return The exemplary result of this recipe
*/
ItemStackSnapshot exemplaryResult();

/**
* The remaining items result of this recipe.
* This method should only be called if {@link #isValid(Inventory, ServerWorld)} returns {@code true}.
* This method should only be called if {@link #isValid(RecipeInput, ServerWorld)} returns {@code true}.
*
* <p>A list of items to be added to the inventory of the player when they craft the result.
* For example, if a player crafts a {@link ItemTypes#CAKE}, the empty buckets are returned to
Expand All @@ -102,22 +102,22 @@ public interface Recipe {
* @return The list of items to be added to the inventory of the player
* when the recipe has been fulfilled (possibly empty)
*/
List<ItemStackSnapshot> remainingItems(Inventory inventory);
List<ItemStackSnapshot> remainingItems(T inventory);

/**
* Returns the {@link RecipeResult} for the given inventory and world.
*
* <p>Returns {@link Optional#empty()} if the arguments do not satisfy
* {@link #isValid(Inventory, ServerWorld)}.</p>
* {@link #isValid(RecipeInput, ServerWorld)}.</p>
*
* @param inventory The input inventory
* @param world The world this recipe would be used in
*
* @return A {@link RecipeResult} if the arguments satisfy
* {@link #isValid(Inventory, ServerWorld)}, or
* {@link #isValid(RecipeInput, ServerWorld)}, or
* {@link Optional#empty()} if not
*/
default Optional<RecipeResult> result(Inventory inventory, ServerWorld world) {
default Optional<RecipeResult> result(T inventory, ServerWorld world) {
if (this.isValid(inventory, world)) {
return Optional.of(new RecipeResult(this.result(inventory), this.remainingItems(inventory)));
}
Expand All @@ -144,6 +144,6 @@ default Optional<RecipeResult> result(Inventory inventory, ServerWorld world) {
*
* @return The recipe type.
*/
RecipeType<? extends Recipe> type();
RecipeType<? extends Recipe<?>> type();

}
53 changes: 14 additions & 39 deletions src/main/java/org/spongepowered/api/item/recipe/RecipeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
package org.spongepowered.api.item.recipe;

import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.recipe.cooking.CookingRecipe;
import org.spongepowered.api.item.recipe.crafting.RecipeResult;
import org.spongepowered.api.item.recipe.crafting.RecipeInput;
import org.spongepowered.api.world.server.ServerWorld;

import java.util.Collection;
Expand All @@ -50,30 +49,30 @@ public interface RecipeManager {
*
* @return The recipe if available
*/
Optional<Recipe> byKey(ResourceKey key);
Optional<Recipe<?>> byKey(ResourceKey key);

/**
* Gets all registered recipes.
*
* @return All registered recipes.
*/
Collection<Recipe> all();
Collection<Recipe<?>> all();

/**
* Returns all registered recipes of given type
* @param type The recipe type
*
* @return All recipes of given type
*/
<T extends Recipe> Collection<T> allOfType(RecipeType<T> type);
<T extends Recipe<?>> Collection<T> allOfType(RecipeType<T> type);

/**
* Returns all registered recipes of given type
* @param supplier The recipe type
*
* @return All recipes of given type
*/
default <T extends Recipe> Collection<T> allOfType(Supplier<? extends RecipeType<T>> supplier) {
default <T extends Recipe<?>> Collection<T> allOfType(Supplier<? extends RecipeType<T>> supplier) {
return this.allOfType(supplier.get());
}

Expand All @@ -85,7 +84,7 @@ default <T extends Recipe> Collection<T> allOfType(Supplier<? extends RecipeType
*
* @return The recipes resulting in given item.
*/
<T extends Recipe> Collection<T> findByResult(RecipeType<T> type, ItemStackSnapshot result);
<T extends Recipe<?>> Collection<T> findByResult(RecipeType<T> type, ItemStackSnapshot result);

/**
* Gets all recipes with given item as a result.
Expand All @@ -94,43 +93,32 @@ default <T extends Recipe> Collection<T> allOfType(Supplier<? extends RecipeType
*
* @return All recipes resulting in given item.
*/
default <T extends Recipe> Collection<T> findByResult(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot result) {
default <T extends Recipe<?>> Collection<T> findByResult(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot result) {
return this.findByResult(supplier.get(), result);
}

/**
* Finds a matching recipe for given inventory and world.
*
* @param inventory The input inventory
* @param world The world
*
* @return The found {@link Recipe}, or {@link Optional#empty()}
* if no recipe was found for this configuration
*/
Optional<Recipe> findMatchingRecipe(Inventory inventory, ServerWorld world);

/**
* Finds a matching recipe for given type, inventory and world
* Finds a matching recipe for given type, recipe input and world
*
* @param type The recipe type
* @param inventory The input inventory
* @param input The recipe input
* @param world The world
*
* @return The matching recipes.
*/
<T extends Recipe> Optional<T> findMatchingRecipe(RecipeType<T> type, Inventory inventory, ServerWorld world);
<I extends RecipeInput, T extends Recipe<I>> Optional<T> findMatchingRecipe(RecipeType<T> type, I input, ServerWorld world);

/**
* Finds a matching recipe for given type, inventory and world
* Finds a matching recipe for given type, recipe input and world
*
* @param supplier The recipe type
* @param inventory The input inventory
* @param input The recipe input
* @param world The world
*
* @return The matching recipes.
*/
default <T extends Recipe> Optional<T> findMatchingRecipe(Supplier<? extends RecipeType<T>> supplier, Inventory inventory, ServerWorld world) {
return this.findMatchingRecipe(supplier.get(), inventory, world);
default <I extends RecipeInput, T extends Recipe<I>> Optional<T> findMatchingRecipe(Supplier<? extends RecipeType<T>> supplier, I input, ServerWorld world) {
return this.findMatchingRecipe(supplier.get(), input, world);
}

/**
Expand All @@ -155,17 +143,4 @@ default <T extends CookingRecipe> Optional<T> findCookingRecipe(Supplier<? exten
return this.findCookingRecipe(supplier.get(), ingredient);
}

/**
* Finds the matching recipe and creates the {@link RecipeResult},
* which is then returned.
*
* @param inventory The crafting grid
* @param world The world
* @return The {@link RecipeResult} if a recipe was found, or
* {@link Optional#empty()} if not
*/
default Optional<RecipeResult> result(Inventory inventory, ServerWorld world) {
return this.findMatchingRecipe(inventory, world)
.flatMap(recipe -> recipe.result(inventory, world));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
* <p>All registrations through the API will generate into the Vanilla data pack system</p>
*/
public interface RecipeRegistration extends DataPackEntry<RecipeRegistration> {
Recipe recipe();
Recipe<?> recipe();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
*/
package org.spongepowered.api.item.recipe;

import org.spongepowered.api.item.recipe.crafting.RecipeInput;
import org.spongepowered.api.registry.DefaultedRegistryValue;
import org.spongepowered.api.util.annotation.CatalogedBy;

@CatalogedBy(RecipeTypes.class)
public interface RecipeType<T extends Recipe> extends DefaultedRegistryValue {
public interface RecipeType<T extends Recipe<? extends RecipeInput>> extends DefaultedRegistryValue {


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public final class RecipeTypes {
private RecipeTypes() {
}

public static Registry<RecipeType<? extends Recipe>> registry() {
public static Registry<RecipeType<? extends Recipe<?>>> registry() {
return Sponge.game().registry(RegistryTypes.RECIPE_TYPE);
}

private static <T extends Recipe> DefaultedRegistryReference<RecipeType<T>> key(final ResourceKey location) {
private static <T extends Recipe<?>> DefaultedRegistryReference<RecipeType<T>> key(final ResourceKey location) {
return RegistryKey.of(RegistryTypes.RECIPE_TYPE, location).asDefaultedReference(Sponge::game);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import org.spongepowered.api.Sponge;
import org.spongepowered.api.datapack.DataPack;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.recipe.Recipe;
import org.spongepowered.api.item.recipe.RecipeRegistration;
import org.spongepowered.api.item.recipe.RecipeType;
import org.spongepowered.api.item.recipe.crafting.Ingredient;
import org.spongepowered.api.item.recipe.crafting.RecipeInput;
import org.spongepowered.api.util.ResourceKeyedBuilder;
import org.spongepowered.api.util.Ticks;

Expand All @@ -45,7 +45,7 @@
/**
* A general interface for cooking-type/furnace recipes.
*/
public interface CookingRecipe extends Recipe {
public interface CookingRecipe extends Recipe<RecipeInput.Single> {

/**
* Builds a cooking recipe.
Expand Down Expand Up @@ -210,7 +210,7 @@ default EndStep result(Supplier<? extends ItemType> result) {
*
* @return The builder
*/
EndStep result(final Function<Inventory, ItemStack> resultFunction, final org.spongepowered.api.item.inventory.ItemStack exemplaryResult);
EndStep result(final Function<RecipeInput.Single, ItemStack> resultFunction, final ItemStack exemplaryResult);
}

interface EndStep extends Builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* <p>{@link ShapedCraftingRecipe} for recipes with simple ingredients/result in a pattern in a {@link CraftingInventory}</p>
* <p>{@link SpecialCraftingRecipe} for recipes with complex ingredients and result in a {@link CraftingInventory}</p>
*/
public interface CraftingRecipe extends Recipe {
public interface CraftingRecipe extends Recipe<RecipeInput.Crafting> {

@Override
RecipeType<? extends CraftingRecipe> type();
Expand Down
Loading

0 comments on commit aff1c56

Please sign in to comment.