mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-28 07:20:24 +01:00
Fix issues with Recipe API
This commit is contained in:
parent
977543c545
commit
5d843f3120
4 changed files with 45 additions and 4 deletions
|
@ -70,7 +70,7 @@
|
||||||
+ char c = 'a';
|
+ char c = 'a';
|
||||||
+ for (Optional<Ingredient> list : this.pattern.ingredients()) {
|
+ for (Optional<Ingredient> list : this.pattern.ingredients()) {
|
||||||
+ RecipeChoice choice = CraftRecipe.toBukkit(list);
|
+ RecipeChoice choice = CraftRecipe.toBukkit(list);
|
||||||
+ if (choice != null) {
|
+ if (choice != RecipeChoice.empty()) { // Paper
|
||||||
+ recipe.setIngredient(c, choice);
|
+ recipe.setIngredient(c, choice);
|
||||||
+ }
|
+ }
|
||||||
+
|
+
|
||||||
|
|
|
@ -19,7 +19,7 @@ public interface CraftRecipe extends Recipe {
|
||||||
void addToCraftingManager();
|
void addToCraftingManager();
|
||||||
|
|
||||||
default Optional<Ingredient> toNMSOptional(RecipeChoice bukkit, boolean requireNotEmpty) {
|
default Optional<Ingredient> toNMSOptional(RecipeChoice bukkit, boolean requireNotEmpty) {
|
||||||
return (bukkit == null) ? Optional.empty() : Optional.of(this.toNMS(bukkit, requireNotEmpty));
|
return (bukkit == null || bukkit == RecipeChoice.empty()) ? Optional.empty() : Optional.of(this.toNMS(bukkit, requireNotEmpty)); // Paper - support "empty" choices
|
||||||
}
|
}
|
||||||
|
|
||||||
default Ingredient toNMS(RecipeChoice bukkit, boolean requireNotEmpty) {
|
default Ingredient toNMS(RecipeChoice bukkit, boolean requireNotEmpty) {
|
||||||
|
@ -36,6 +36,13 @@ public interface CraftRecipe extends Recipe {
|
||||||
stack = Ingredient.of(((RecipeChoice.MaterialChoice) bukkit).getChoices().stream().map((mat) -> CraftItemType.bukkitToMinecraft(mat)));
|
stack = Ingredient.of(((RecipeChoice.MaterialChoice) bukkit).getChoices().stream().map((mat) -> CraftItemType.bukkitToMinecraft(mat)));
|
||||||
} else if (bukkit instanceof RecipeChoice.ExactChoice) {
|
} else if (bukkit instanceof RecipeChoice.ExactChoice) {
|
||||||
stack = Ingredient.ofStacks(((RecipeChoice.ExactChoice) bukkit).getChoices().stream().map((mat) -> CraftItemStack.asNMSCopy(mat)).toList());
|
stack = Ingredient.ofStacks(((RecipeChoice.ExactChoice) bukkit).getChoices().stream().map((mat) -> CraftItemStack.asNMSCopy(mat)).toList());
|
||||||
|
// Paper start - support "empty" choices - legacy method that spigot might incorrectly call
|
||||||
|
// Their impl of Ingredient.of() will error, ingredients need at least one entry.
|
||||||
|
// Callers running into this exception may have passed an incorrect empty() recipe choice to a non-empty slot or
|
||||||
|
// spigot calls this method in a wrong place.
|
||||||
|
} else if (bukkit == RecipeChoice.empty()) {
|
||||||
|
throw new IllegalArgumentException("This ingredient cannot be empty");
|
||||||
|
// Paper end - support "empty" choices
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit);
|
throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit);
|
||||||
}
|
}
|
||||||
|
@ -48,12 +55,12 @@ public interface CraftRecipe extends Recipe {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RecipeChoice toBukkit(Optional<Ingredient> list) {
|
public static RecipeChoice toBukkit(Optional<Ingredient> list) {
|
||||||
return list.map(CraftRecipe::toBukkit).orElse(null);
|
return list.map(CraftRecipe::toBukkit).orElse(RecipeChoice.empty()); // Paper - fix issue with recipe API
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RecipeChoice toBukkit(Ingredient list) {
|
public static RecipeChoice toBukkit(Ingredient list) {
|
||||||
if (list.isEmpty()) {
|
if (list.isEmpty()) {
|
||||||
return null;
|
return RecipeChoice.empty(); // Paper - null breaks API contracts
|
||||||
}
|
}
|
||||||
|
|
||||||
if (list.isExact()) {
|
if (list.isExact()) {
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package io.papermc.paper.inventory.recipe;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.inventory.Recipe;
|
||||||
|
import org.bukkit.support.environment.AllFeatures;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@AllFeatures
|
||||||
|
class TestRecipeChoice {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRecipeChoices() {
|
||||||
|
final Iterator<Recipe> iter = Bukkit.recipeIterator();
|
||||||
|
boolean foundRecipes = false;
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
foundRecipes = true;
|
||||||
|
assertDoesNotThrow(iter::next, "Failed to convert a recipe to Bukkit recipe!");
|
||||||
|
}
|
||||||
|
assertTrue(foundRecipes, "No recipes found!");
|
||||||
|
}
|
||||||
|
}
|
|
@ -92,6 +92,15 @@ public final class DummyServerHelper {
|
||||||
// Paper end - testing additions
|
// Paper end - testing additions
|
||||||
|
|
||||||
io.papermc.paper.configuration.GlobalConfigTestingBase.setupGlobalConfigForTest(); // Paper - configuration files - setup global configuration test base
|
io.papermc.paper.configuration.GlobalConfigTestingBase.setupGlobalConfigForTest(); // Paper - configuration files - setup global configuration test base
|
||||||
|
|
||||||
|
// Paper start - add test for recipe conversion
|
||||||
|
when(instance.recipeIterator()).thenAnswer(ignored ->
|
||||||
|
com.google.common.collect.Iterators.transform(
|
||||||
|
RegistryHelper.getDataPack().getRecipeManager().recipes.byType.entries().iterator(),
|
||||||
|
input -> input.getValue().toBukkitRecipe()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
// Paper end - add test for recipe conversion
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue