Correctly support RecipeChoice.empty (#11550)

The previous implementation was based off of spigots logic in
CraftRecipe#toIngredient, which is completely incorrect as
nms.Ingredient.of() is a throwing call.

Correctly insert handling for the empty() choice in the toNMSOptional
logic.
This commit is contained in:
Bjarne Koll 2024-11-01 11:30:40 +01:00
parent 2b04a0a412
commit 69b9c693b5

View File

@ -21,14 +21,26 @@ diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java b/s
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java
@@ -0,0 +0,0 @@ public interface CraftRecipe extends Recipe {
void addToCraftingManager();
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) {
@@ -0,0 +0,0 @@ public interface CraftRecipe extends Recipe { @@ -0,0 +0,0 @@ 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 + // 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()) { + } else if (bukkit == RecipeChoice.empty()) {
+ stack = Ingredient.of(); + throw new IllegalArgumentException("This ingredient cannot be empty");
+ // Paper end + // Paper end - support "empty" choices
} else { } else {
throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit); throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit);
} }