Skip to content

Commit

Permalink
Migrate recipes to ItemStackLike
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHell228 committed Sep 14, 2024
1 parent c55e532 commit 6ddddd4
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 97 deletions.
41 changes: 37 additions & 4 deletions src/main/java/org/spongepowered/api/item/recipe/RecipeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.api.item.recipe;

import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.recipe.cooking.CookingRecipe;
import org.spongepowered.api.item.recipe.crafting.RecipeInput;
Expand Down Expand Up @@ -76,6 +77,14 @@ default <T extends Recipe<?>> Collection<T> allOfType(Supplier<? extends RecipeT
return this.allOfType(supplier.get());
}

/**
* @deprecated Use {@link #findByResult(RecipeType, ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default <T extends Recipe<?>> Collection<T> findByResult(RecipeType<T> type, ItemStackSnapshot result) {
return this.findByResult(type, (ItemStackLike) result);
}

/**
* Returns all registered recipes of given type and with given item as a result.
*
Expand All @@ -84,7 +93,15 @@ default <T extends Recipe<?>> Collection<T> allOfType(Supplier<? extends RecipeT
*
* @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, ItemStackLike result);

/**
* @deprecated Use {@link #findByResult(Supplier, ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default <T extends Recipe<?>> Collection<T> findByResult(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot result) {
return this.findByResult(supplier, (ItemStackLike) result);
}

/**
* Gets all recipes with given item as a result.
Expand All @@ -93,7 +110,7 @@ default <T extends Recipe<?>> Collection<T> allOfType(Supplier<? extends RecipeT
*
* @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, ItemStackLike result) {
return this.findByResult(supplier.get(), result);
}

Expand Down Expand Up @@ -121,6 +138,14 @@ default <I extends RecipeInput, T extends Recipe<I>> Optional<T> findMatchingRec
return this.findMatchingRecipe(supplier.get(), input, world);
}

/**
* @deprecated Use {@link #findCookingRecipe(RecipeType, ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default <T extends CookingRecipe> Optional<T> findCookingRecipe(RecipeType<T> type, ItemStackSnapshot ingredient) {
return this.findCookingRecipe(type, (ItemStackLike) ingredient);
}

/**
* Finds a matching cooking recipe for given type and ingredient
*
Expand All @@ -129,7 +154,15 @@ default <I extends RecipeInput, T extends Recipe<I>> Optional<T> findMatchingRec
*
* @return The matching recipe.
*/
<T extends CookingRecipe> Optional<T> findCookingRecipe(RecipeType<T> type, ItemStackSnapshot ingredient);
<T extends CookingRecipe> Optional<T> findCookingRecipe(RecipeType<T> type, ItemStackLike ingredient);

/**
* @deprecated Use {@link #findCookingRecipe(Supplier, ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default <T extends CookingRecipe> Optional<T> findCookingRecipe(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot ingredient) {
return this.findCookingRecipe(supplier, (ItemStackLike) ingredient);
}

/**
* Finds a matching cooking recipe for given type and ingredient
Expand All @@ -139,7 +172,7 @@ default <I extends RecipeInput, T extends Recipe<I>> Optional<T> findMatchingRec
*
* @return The matching recipe.
*/
default <T extends CookingRecipe> Optional<T> findCookingRecipe(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot ingredient) {
default <T extends CookingRecipe> Optional<T> findCookingRecipe(Supplier<? extends RecipeType<T>> supplier, ItemStackLike ingredient) {
return this.findCookingRecipe(supplier.get(), ingredient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.spongepowered.api.datapack.DataPack;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.recipe.Recipe;
import org.spongepowered.api.item.recipe.RecipeRegistration;
Expand Down Expand Up @@ -64,25 +65,41 @@ static Builder builder() {
Ingredient ingredient();

/**
* Checks if the given {@link ItemStackSnapshot} fits the required
* @deprecated Use {@link #isValid(ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default boolean isValid(ItemStackSnapshot ingredient) {
return this.isValid((ItemStackLike) ingredient);
}

/**
* Checks if the given {@link ItemStackLike} fits the required
* constraints to craft this {@link CookingRecipe}.
*
* @param ingredient The ingredient to check against
*
* @return Whether this ingredient can be used to craft the result
*/
boolean isValid(ItemStackSnapshot ingredient);
boolean isValid(ItemStackLike ingredient);

/**
* @deprecated Use {@link #result(ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default Optional<CookingResult> result(ItemStackSnapshot ingredient) {
return this.result((ItemStackLike) ingredient);
}

/**
* <p>Returns the {@link CookingResult} containing the resulting
* {@link ItemStackSnapshot} and the amount of experience released.</p>
* {@link ItemStackLike} and the amount of experience released.</p>
*
* @param ingredient The {@link ItemStackSnapshot} currently being cooked
* @param ingredient The {@link ItemStackLike} currently being cooked
* @return The {@link CookingResult}, or {@link Optional#empty()}
* if the recipe is not valid according to
* {@link #isValid(ItemStackSnapshot)}.
* {@link #isValid(ItemStackLike)}.
*/
Optional<CookingResult> result(ItemStackSnapshot ingredient);
Optional<CookingResult> result(ItemStackLike ingredient);

/**
* Returns the cooking time in ticks.
Expand Down Expand Up @@ -183,24 +200,38 @@ default EndStep result(Supplier<? extends ItemType> result) {
}

/**
* Changes the result and returns this builder. The result is the
* {@link ItemStack} created when the recipe is fulfilled.
*
* @param result The output of this recipe
*
* @return This builder, for chaining
* @deprecated Use {@link #result(ItemStackLike)} instead.
*/
EndStep result(ItemStack result);
@Deprecated(forRemoval = true)
default EndStep result(ItemStack result) {
return this.result((ItemStackLike) result);
}

/**
* @deprecated Use {@link #result(ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default EndStep result(ItemStackSnapshot result) {
return this.result((ItemStackLike) result);
}

/**
* Changes the result and returns this builder. The result is the
* {@link ItemStack} created when the recipe is fulfilled.
* {@link ItemStackLike} created when the recipe is fulfilled.
*
* @param result The output of this recipe
*
* @return This builder, for chaining
*/
EndStep result(ItemStackSnapshot result);
EndStep result(ItemStackLike result);

/**
* @deprecated Use {@link #result(Function, ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
default EndStep result(final Function<RecipeInput.Single, ItemStack> resultFunction, final ItemStack exemplaryResult) {
return this.result(resultFunction, (ItemStackLike) exemplaryResult);
}

/**
* Sets the result function and an exemplary result.
Expand All @@ -210,7 +241,7 @@ default EndStep result(Supplier<? extends ItemType> result) {
*
* @return The builder
*/
EndStep result(final Function<RecipeInput.Single, ItemStack> resultFunction, final ItemStack exemplaryResult);
EndStep result(final Function<RecipeInput.Single, ? extends ItemStackLike> resultFunction, final ItemStackLike exemplaryResult);
}

interface EndStep extends Builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package org.spongepowered.api.item.recipe.cooking;

import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;

import java.util.Objects;
Expand All @@ -37,6 +38,14 @@ public final class CookingResult {
private final ItemStackSnapshot result;
private final double experience;

/**
* @deprecated Use {@link #CookingResult(ItemStackLike, double)} instead.
*/
@Deprecated(forRemoval = true)
public CookingResult(final ItemStackSnapshot result, final double experience) {
this((ItemStackLike) result, experience);
}

/**
* Creates a new {@link CookingResult}.
*
Expand All @@ -45,16 +54,16 @@ public final class CookingResult {
* @param result The result of the cooking recipe
* @param experience The experience that should be created from this result
*/
public CookingResult(final ItemStackSnapshot result, final double experience) {
public CookingResult(final ItemStackLike result, final double experience) {
Objects.requireNonNull(result, "result");
if (result.isEmpty()) {
throw new IllegalArgumentException("The resulting snapshot must not be empty");
throw new IllegalArgumentException("The result must not be empty");
}
if (experience < 0) {
throw new IllegalArgumentException("The experience must be non-negative.");
}

this.result = result;
this.result = result.asImmutable();
this.experience = experience;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.spongepowered.api.Sponge;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.registry.DefaultedRegistryReference;

Expand All @@ -53,11 +54,19 @@ static Ingredient empty() {
return Sponge.game().factoryProvider().provide(Factory.class).empty();
}

/**
* @deprecated Use {@link #test(ItemStackLike)} instead.
*/
@Deprecated(forRemoval = true)
@Override
boolean test(ItemStack itemStack);
default boolean test(ItemStack itemStack) {
return this.test((ItemStackLike) itemStack);
}

boolean test(ItemStackLike item);

/**
* Returns the list of {@link ItemStack}s used to display the ingredient in a recipe.
* Returns the list of {@link ItemStackSnapshot}s used to display the ingredient in a recipe.
* These are not necessarily all the items that this Ingredient can match.
*
* @return The list of items to display the Ingredient in a recipe.
Expand Down Expand Up @@ -87,25 +96,28 @@ static Ingredient of(ItemType @Nullable ... itemTypes) {
}

/**
* Creates a new {@link Ingredient} for the provided {@link ItemStack}s.
*
* @param items The items
* @return The new ingredient
* @deprecated Use {@link #of(ItemStackLike...)} instead.
*/
@Deprecated(forRemoval = true)
static Ingredient of(ItemStack @Nullable ... items) {
if (items == null || items.length == 0) {
return Ingredient.empty();
}
return Ingredient.builder().with(items).build();
return Ingredient.of((ItemStackLike[]) items);
}

/**
* Creates a new {@link Ingredient} for the provided {@link ItemStackSnapshot}s.
* @deprecated Use {@link #of(ItemStackLike...)} instead.
*/
@Deprecated(forRemoval = true)
static Ingredient of(ItemStackSnapshot @Nullable ... items) {
return Ingredient.of((ItemStackLike[]) items);
}

/**
* Creates a new {@link Ingredient} for the provided {@link ItemStackLike}s.
*
* @param items The item
* @return The new ingredient
*/
static Ingredient of(ItemStackSnapshot @Nullable ... items) {
static Ingredient of(ItemStackLike @Nullable ... items) {
if (items == null) {
return Ingredient.empty();
}
Expand All @@ -127,7 +139,15 @@ static Ingredient of(DefaultedRegistryReference<? extends ItemType> @Nullable ..
}

/**
* Creates a new {@link Ingredient} for the provided {@link Predicate} and exemplary {@link ItemStack}s.
* @deprecated Use {@link #of(ResourceKey, Predicate, ItemStackLike...)} instead.
*/
@Deprecated(forRemoval = true)
static Ingredient of(ResourceKey key, Predicate<ItemStack> predicate, ItemStack... exemplaryStacks) {
return Ingredient.of(key, itemStack -> predicate.test(itemStack.asMutable()), (ItemStackLike[]) exemplaryStacks);
}

/**
* Creates a new {@link Ingredient} for the provided {@link Predicate} and exemplary {@link ItemStackLike}s.
* <p>Note: Predicate ingredients may not be fully supported for all recipe types</p>
*
* @param key A unique resource key
Expand All @@ -136,7 +156,7 @@ static Ingredient of(DefaultedRegistryReference<? extends ItemType> @Nullable ..
*
* @return The new ingredient
*/
static Ingredient of(ResourceKey key, Predicate<ItemStack> predicate, ItemStack... exemplaryStacks) {
static Ingredient of(ResourceKey key, Predicate<? super ItemStackLike> predicate, ItemStackLike... exemplaryStacks) {
if (exemplaryStacks.length == 0) {
throw new IllegalArgumentException("At least exemplary stack is required");
}
Expand Down Expand Up @@ -178,12 +198,28 @@ interface Builder extends org.spongepowered.api.util.Builder<Ingredient, Builder
Builder with(Supplier<? extends ItemType>... types);

/**
* Sets one ore more ItemStack for matching the ingredient.
* @deprecated Use {@link #with(ItemStackLike...)} instead
*/
@Deprecated(forRemoval = true)
default Builder with(ItemStack... types) {
return this.with((ItemStackLike[]) types);
}

/**
* Sets one or more ItemStackLike for matching the ingredient.
*
* @param types The items
* @return This Builder, for chaining
*/
Builder with(ItemStack... types);
Builder with(ItemStackLike... types);

/**
* @deprecated Use {@link #with(ResourceKey, Predicate, ItemStackLike...)} instead.
*/
@Deprecated(forRemoval = true)
default Builder with(ResourceKey resourceKey, Predicate<ItemStack> predicate, ItemStack... exemplaryTypes) {
return this.with(resourceKey, itemStack -> predicate.test(itemStack.asMutable()), (ItemStackLike[]) exemplaryTypes);
}

/**
* Sets a Predicate for matching the ingredient.
Expand All @@ -194,15 +230,15 @@ interface Builder extends org.spongepowered.api.util.Builder<Ingredient, Builder
* @param exemplaryTypes The items
* @return This Builder, for chaining
*/
Builder with(ResourceKey resourceKey, Predicate<ItemStack> predicate, ItemStack... exemplaryTypes);
Builder with(ResourceKey resourceKey, Predicate<? super ItemStackLike> predicate, ItemStackLike... exemplaryTypes);

/**
* Sets one ItemStack for matching the ingredient.
*
* @param types The items
* @return This Builder, for chaining
* @deprecated Use {@link #with(ItemStackLike...)} instead
*/
Builder with(ItemStackSnapshot... types);
@Deprecated(forRemoval = true)
default Builder with(ItemStackSnapshot... types) {
return this.with((ItemStackLike[]) types);
}

/**
* Sets the item tag for matching the ingredient.
Expand Down
Loading

0 comments on commit 6ddddd4

Please sign in to comment.