Skip to content

Commit 6ddddd4

Browse files
committed
Migrate recipes to ItemStackLike
1 parent c55e532 commit 6ddddd4

File tree

11 files changed

+304
-97
lines changed

11 files changed

+304
-97
lines changed

src/main/java/org/spongepowered/api/item/recipe/RecipeManager.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package org.spongepowered.api.item.recipe;
2626

2727
import org.spongepowered.api.ResourceKey;
28+
import org.spongepowered.api.item.inventory.ItemStackLike;
2829
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
2930
import org.spongepowered.api.item.recipe.cooking.CookingRecipe;
3031
import org.spongepowered.api.item.recipe.crafting.RecipeInput;
@@ -76,6 +77,14 @@ default <T extends Recipe<?>> Collection<T> allOfType(Supplier<? extends RecipeT
7677
return this.allOfType(supplier.get());
7778
}
7879

80+
/**
81+
* @deprecated Use {@link #findByResult(RecipeType, ItemStackLike)} instead.
82+
*/
83+
@Deprecated(forRemoval = true)
84+
default <T extends Recipe<?>> Collection<T> findByResult(RecipeType<T> type, ItemStackSnapshot result) {
85+
return this.findByResult(type, (ItemStackLike) result);
86+
}
87+
7988
/**
8089
* Returns all registered recipes of given type and with given item as a result.
8190
*
@@ -84,7 +93,15 @@ default <T extends Recipe<?>> Collection<T> allOfType(Supplier<? extends RecipeT
8493
*
8594
* @return The recipes resulting in given item.
8695
*/
87-
<T extends Recipe<?>> Collection<T> findByResult(RecipeType<T> type, ItemStackSnapshot result);
96+
<T extends Recipe<?>> Collection<T> findByResult(RecipeType<T> type, ItemStackLike result);
97+
98+
/**
99+
* @deprecated Use {@link #findByResult(Supplier, ItemStackLike)} instead.
100+
*/
101+
@Deprecated(forRemoval = true)
102+
default <T extends Recipe<?>> Collection<T> findByResult(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot result) {
103+
return this.findByResult(supplier, (ItemStackLike) result);
104+
}
88105

89106
/**
90107
* Gets all recipes with given item as a result.
@@ -93,7 +110,7 @@ default <T extends Recipe<?>> Collection<T> allOfType(Supplier<? extends RecipeT
93110
*
94111
* @return All recipes resulting in given item.
95112
*/
96-
default <T extends Recipe<?>> Collection<T> findByResult(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot result) {
113+
default <T extends Recipe<?>> Collection<T> findByResult(Supplier<? extends RecipeType<T>> supplier, ItemStackLike result) {
97114
return this.findByResult(supplier.get(), result);
98115
}
99116

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

141+
/**
142+
* @deprecated Use {@link #findCookingRecipe(RecipeType, ItemStackLike)} instead.
143+
*/
144+
@Deprecated(forRemoval = true)
145+
default <T extends CookingRecipe> Optional<T> findCookingRecipe(RecipeType<T> type, ItemStackSnapshot ingredient) {
146+
return this.findCookingRecipe(type, (ItemStackLike) ingredient);
147+
}
148+
124149
/**
125150
* Finds a matching cooking recipe for given type and ingredient
126151
*
@@ -129,7 +154,15 @@ default <I extends RecipeInput, T extends Recipe<I>> Optional<T> findMatchingRec
129154
*
130155
* @return The matching recipe.
131156
*/
132-
<T extends CookingRecipe> Optional<T> findCookingRecipe(RecipeType<T> type, ItemStackSnapshot ingredient);
157+
<T extends CookingRecipe> Optional<T> findCookingRecipe(RecipeType<T> type, ItemStackLike ingredient);
158+
159+
/**
160+
* @deprecated Use {@link #findCookingRecipe(Supplier, ItemStackLike)} instead.
161+
*/
162+
@Deprecated(forRemoval = true)
163+
default <T extends CookingRecipe> Optional<T> findCookingRecipe(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot ingredient) {
164+
return this.findCookingRecipe(supplier, (ItemStackLike) ingredient);
165+
}
133166

134167
/**
135168
* Finds a matching cooking recipe for given type and ingredient
@@ -139,7 +172,7 @@ default <I extends RecipeInput, T extends Recipe<I>> Optional<T> findMatchingRec
139172
*
140173
* @return The matching recipe.
141174
*/
142-
default <T extends CookingRecipe> Optional<T> findCookingRecipe(Supplier<? extends RecipeType<T>> supplier, ItemStackSnapshot ingredient) {
175+
default <T extends CookingRecipe> Optional<T> findCookingRecipe(Supplier<? extends RecipeType<T>> supplier, ItemStackLike ingredient) {
143176
return this.findCookingRecipe(supplier.get(), ingredient);
144177
}
145178

src/main/java/org/spongepowered/api/item/recipe/cooking/CookingRecipe.java

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.spongepowered.api.datapack.DataPack;
3030
import org.spongepowered.api.item.ItemType;
3131
import org.spongepowered.api.item.inventory.ItemStack;
32+
import org.spongepowered.api.item.inventory.ItemStackLike;
3233
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
3334
import org.spongepowered.api.item.recipe.Recipe;
3435
import org.spongepowered.api.item.recipe.RecipeRegistration;
@@ -64,25 +65,41 @@ static Builder builder() {
6465
Ingredient ingredient();
6566

6667
/**
67-
* Checks if the given {@link ItemStackSnapshot} fits the required
68+
* @deprecated Use {@link #isValid(ItemStackLike)} instead.
69+
*/
70+
@Deprecated(forRemoval = true)
71+
default boolean isValid(ItemStackSnapshot ingredient) {
72+
return this.isValid((ItemStackLike) ingredient);
73+
}
74+
75+
/**
76+
* Checks if the given {@link ItemStackLike} fits the required
6877
* constraints to craft this {@link CookingRecipe}.
6978
*
7079
* @param ingredient The ingredient to check against
7180
*
7281
* @return Whether this ingredient can be used to craft the result
7382
*/
74-
boolean isValid(ItemStackSnapshot ingredient);
83+
boolean isValid(ItemStackLike ingredient);
84+
85+
/**
86+
* @deprecated Use {@link #result(ItemStackLike)} instead.
87+
*/
88+
@Deprecated(forRemoval = true)
89+
default Optional<CookingResult> result(ItemStackSnapshot ingredient) {
90+
return this.result((ItemStackLike) ingredient);
91+
}
7592

7693
/**
7794
* <p>Returns the {@link CookingResult} containing the resulting
78-
* {@link ItemStackSnapshot} and the amount of experience released.</p>
95+
* {@link ItemStackLike} and the amount of experience released.</p>
7996
*
80-
* @param ingredient The {@link ItemStackSnapshot} currently being cooked
97+
* @param ingredient The {@link ItemStackLike} currently being cooked
8198
* @return The {@link CookingResult}, or {@link Optional#empty()}
8299
* if the recipe is not valid according to
83-
* {@link #isValid(ItemStackSnapshot)}.
100+
* {@link #isValid(ItemStackLike)}.
84101
*/
85-
Optional<CookingResult> result(ItemStackSnapshot ingredient);
102+
Optional<CookingResult> result(ItemStackLike ingredient);
86103

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

185202
/**
186-
* Changes the result and returns this builder. The result is the
187-
* {@link ItemStack} created when the recipe is fulfilled.
188-
*
189-
* @param result The output of this recipe
190-
*
191-
* @return This builder, for chaining
203+
* @deprecated Use {@link #result(ItemStackLike)} instead.
192204
*/
193-
EndStep result(ItemStack result);
205+
@Deprecated(forRemoval = true)
206+
default EndStep result(ItemStack result) {
207+
return this.result((ItemStackLike) result);
208+
}
209+
210+
/**
211+
* @deprecated Use {@link #result(ItemStackLike)} instead.
212+
*/
213+
@Deprecated(forRemoval = true)
214+
default EndStep result(ItemStackSnapshot result) {
215+
return this.result((ItemStackLike) result);
216+
}
194217

195218
/**
196219
* Changes the result and returns this builder. The result is the
197-
* {@link ItemStack} created when the recipe is fulfilled.
220+
* {@link ItemStackLike} created when the recipe is fulfilled.
198221
*
199222
* @param result The output of this recipe
200223
*
201224
* @return This builder, for chaining
202225
*/
203-
EndStep result(ItemStackSnapshot result);
226+
EndStep result(ItemStackLike result);
227+
228+
/**
229+
* @deprecated Use {@link #result(Function, ItemStackLike)} instead.
230+
*/
231+
@Deprecated(forRemoval = true)
232+
default EndStep result(final Function<RecipeInput.Single, ItemStack> resultFunction, final ItemStack exemplaryResult) {
233+
return this.result(resultFunction, (ItemStackLike) exemplaryResult);
234+
}
204235

205236
/**
206237
* Sets the result function and an exemplary result.
@@ -210,7 +241,7 @@ default EndStep result(Supplier<? extends ItemType> result) {
210241
*
211242
* @return The builder
212243
*/
213-
EndStep result(final Function<RecipeInput.Single, ItemStack> resultFunction, final ItemStack exemplaryResult);
244+
EndStep result(final Function<RecipeInput.Single, ? extends ItemStackLike> resultFunction, final ItemStackLike exemplaryResult);
214245
}
215246

216247
interface EndStep extends Builder,

src/main/java/org/spongepowered/api/item/recipe/cooking/CookingResult.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package org.spongepowered.api.item.recipe.cooking;
2626

27+
import org.spongepowered.api.item.inventory.ItemStackLike;
2728
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
2829

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

41+
/**
42+
* @deprecated Use {@link #CookingResult(ItemStackLike, double)} instead.
43+
*/
44+
@Deprecated(forRemoval = true)
45+
public CookingResult(final ItemStackSnapshot result, final double experience) {
46+
this((ItemStackLike) result, experience);
47+
}
48+
4049
/**
4150
* Creates a new {@link CookingResult}.
4251
*
@@ -45,16 +54,16 @@ public final class CookingResult {
4554
* @param result The result of the cooking recipe
4655
* @param experience The experience that should be created from this result
4756
*/
48-
public CookingResult(final ItemStackSnapshot result, final double experience) {
57+
public CookingResult(final ItemStackLike result, final double experience) {
4958
Objects.requireNonNull(result, "result");
5059
if (result.isEmpty()) {
51-
throw new IllegalArgumentException("The resulting snapshot must not be empty");
60+
throw new IllegalArgumentException("The result must not be empty");
5261
}
5362
if (experience < 0) {
5463
throw new IllegalArgumentException("The experience must be non-negative.");
5564
}
5665

57-
this.result = result;
66+
this.result = result.asImmutable();
5867
this.experience = experience;
5968
}
6069

src/main/java/org/spongepowered/api/item/recipe/crafting/Ingredient.java

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.spongepowered.api.Sponge;
3030
import org.spongepowered.api.item.ItemType;
3131
import org.spongepowered.api.item.inventory.ItemStack;
32+
import org.spongepowered.api.item.inventory.ItemStackLike;
3233
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
3334
import org.spongepowered.api.registry.DefaultedRegistryReference;
3435

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

57+
/**
58+
* @deprecated Use {@link #test(ItemStackLike)} instead.
59+
*/
60+
@Deprecated(forRemoval = true)
5661
@Override
57-
boolean test(ItemStack itemStack);
62+
default boolean test(ItemStack itemStack) {
63+
return this.test((ItemStackLike) itemStack);
64+
}
65+
66+
boolean test(ItemStackLike item);
5867

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

8998
/**
90-
* Creates a new {@link Ingredient} for the provided {@link ItemStack}s.
91-
*
92-
* @param items The items
93-
* @return The new ingredient
99+
* @deprecated Use {@link #of(ItemStackLike...)} instead.
94100
*/
101+
@Deprecated(forRemoval = true)
95102
static Ingredient of(ItemStack @Nullable ... items) {
96-
if (items == null || items.length == 0) {
97-
return Ingredient.empty();
98-
}
99-
return Ingredient.builder().with(items).build();
103+
return Ingredient.of((ItemStackLike[]) items);
100104
}
101105

102106
/**
103-
* Creates a new {@link Ingredient} for the provided {@link ItemStackSnapshot}s.
107+
* @deprecated Use {@link #of(ItemStackLike...)} instead.
108+
*/
109+
@Deprecated(forRemoval = true)
110+
static Ingredient of(ItemStackSnapshot @Nullable ... items) {
111+
return Ingredient.of((ItemStackLike[]) items);
112+
}
113+
114+
/**
115+
* Creates a new {@link Ingredient} for the provided {@link ItemStackLike}s.
104116
*
105117
* @param items The item
106118
* @return The new ingredient
107119
*/
108-
static Ingredient of(ItemStackSnapshot @Nullable ... items) {
120+
static Ingredient of(ItemStackLike @Nullable ... items) {
109121
if (items == null) {
110122
return Ingredient.empty();
111123
}
@@ -127,7 +139,15 @@ static Ingredient of(DefaultedRegistryReference<? extends ItemType> @Nullable ..
127139
}
128140

129141
/**
130-
* Creates a new {@link Ingredient} for the provided {@link Predicate} and exemplary {@link ItemStack}s.
142+
* @deprecated Use {@link #of(ResourceKey, Predicate, ItemStackLike...)} instead.
143+
*/
144+
@Deprecated(forRemoval = true)
145+
static Ingredient of(ResourceKey key, Predicate<ItemStack> predicate, ItemStack... exemplaryStacks) {
146+
return Ingredient.of(key, itemStack -> predicate.test(itemStack.asMutable()), (ItemStackLike[]) exemplaryStacks);
147+
}
148+
149+
/**
150+
* Creates a new {@link Ingredient} for the provided {@link Predicate} and exemplary {@link ItemStackLike}s.
131151
* <p>Note: Predicate ingredients may not be fully supported for all recipe types</p>
132152
*
133153
* @param key A unique resource key
@@ -136,7 +156,7 @@ static Ingredient of(DefaultedRegistryReference<? extends ItemType> @Nullable ..
136156
*
137157
* @return The new ingredient
138158
*/
139-
static Ingredient of(ResourceKey key, Predicate<ItemStack> predicate, ItemStack... exemplaryStacks) {
159+
static Ingredient of(ResourceKey key, Predicate<? super ItemStackLike> predicate, ItemStackLike... exemplaryStacks) {
140160
if (exemplaryStacks.length == 0) {
141161
throw new IllegalArgumentException("At least exemplary stack is required");
142162
}
@@ -178,12 +198,28 @@ interface Builder extends org.spongepowered.api.util.Builder<Ingredient, Builder
178198
Builder with(Supplier<? extends ItemType>... types);
179199

180200
/**
181-
* Sets one ore more ItemStack for matching the ingredient.
201+
* @deprecated Use {@link #with(ItemStackLike...)} instead
202+
*/
203+
@Deprecated(forRemoval = true)
204+
default Builder with(ItemStack... types) {
205+
return this.with((ItemStackLike[]) types);
206+
}
207+
208+
/**
209+
* Sets one or more ItemStackLike for matching the ingredient.
182210
*
183211
* @param types The items
184212
* @return This Builder, for chaining
185213
*/
186-
Builder with(ItemStack... types);
214+
Builder with(ItemStackLike... types);
215+
216+
/**
217+
* @deprecated Use {@link #with(ResourceKey, Predicate, ItemStackLike...)} instead.
218+
*/
219+
@Deprecated(forRemoval = true)
220+
default Builder with(ResourceKey resourceKey, Predicate<ItemStack> predicate, ItemStack... exemplaryTypes) {
221+
return this.with(resourceKey, itemStack -> predicate.test(itemStack.asMutable()), (ItemStackLike[]) exemplaryTypes);
222+
}
187223

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

199235
/**
200-
* Sets one ItemStack for matching the ingredient.
201-
*
202-
* @param types The items
203-
* @return This Builder, for chaining
236+
* @deprecated Use {@link #with(ItemStackLike...)} instead
204237
*/
205-
Builder with(ItemStackSnapshot... types);
238+
@Deprecated(forRemoval = true)
239+
default Builder with(ItemStackSnapshot... types) {
240+
return this.with((ItemStackLike[]) types);
241+
}
206242

207243
/**
208244
* Sets the item tag for matching the ingredient.

0 commit comments

Comments
 (0)