From 1a64d4ae2b77fe76d4e6d72ce19749fc427804ee Mon Sep 17 00:00:00 2001 From: md_5 Date: Mon, 21 Jan 2019 10:39:24 +1100 Subject: [PATCH] SPIGOT-4587: Enforce at least one valid choice for FurnaceRecipe --- .../inventory/CraftFurnaceRecipe.java | 2 +- .../craftbukkit/inventory/CraftRecipe.java | 25 ++++++++++++------- .../inventory/CraftShapedRecipe.java | 2 +- .../inventory/CraftShapelessRecipe.java | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java index 4ae2fafbc5..a38d08a153 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java @@ -25,6 +25,6 @@ public class CraftFurnaceRecipe extends FurnaceRecipe implements CraftRecipe { public void addToCraftingManager() { ItemStack result = this.getResult(); - MinecraftServer.getServer().getCraftingManager().a(new net.minecraft.server.FurnaceRecipe(CraftNamespacedKey.toMinecraft(this.getKey()), this.getGroup(), toNMS(this.getInputChoice()), CraftItemStack.asNMSCopy(result), getExperience(), getCookingTime())); + MinecraftServer.getServer().getCraftingManager().a(new net.minecraft.server.FurnaceRecipe(CraftNamespacedKey.toMinecraft(this.getKey()), this.getGroup(), toNMS(this.getInputChoice(), true), CraftItemStack.asNMSCopy(result), getExperience(), getCookingTime())); } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java index 3309f32691..62f475158e 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java @@ -14,19 +14,26 @@ public interface CraftRecipe extends Recipe { void addToCraftingManager(); - default net.minecraft.server.RecipeItemStack toNMS(RecipeChoice bukkit) { - if (bukkit == null) { - return RecipeItemStack.a; - } else if (bukkit instanceof RecipeChoice.MaterialChoice) { - return new RecipeItemStack(((RecipeChoice.MaterialChoice) bukkit).getChoices().stream().map((mat) -> new net.minecraft.server.RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(new ItemStack(mat))))); - } else if (bukkit instanceof RecipeChoice.ExactChoice) { - RecipeItemStack stack = new RecipeItemStack(Stream.of(new net.minecraft.server.RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(((RecipeChoice.ExactChoice) bukkit).getItemStack())))); - stack.exact = true; + default RecipeItemStack toNMS(RecipeChoice bukkit, boolean requireNotEmpty) { + RecipeItemStack stack; - return stack; + if (bukkit == null) { + stack= RecipeItemStack.a; + } else if (bukkit instanceof RecipeChoice.MaterialChoice) { + stack= new RecipeItemStack(((RecipeChoice.MaterialChoice) bukkit).getChoices().stream().map((mat) -> new net.minecraft.server.RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(new ItemStack(mat))))); + } else if (bukkit instanceof RecipeChoice.ExactChoice) { + stack = new RecipeItemStack(Stream.of(new net.minecraft.server.RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(((RecipeChoice.ExactChoice) bukkit).getItemStack())))); + stack.exact = true; } else { throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit); } + + stack.buildChoices(); + if (requireNotEmpty && stack.choices.length == 0) { + throw new IllegalArgumentException("Recipe requires at least one non-air choice!"); + } + + return stack; } public static RecipeChoice toBukkit(RecipeItemStack list) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java index 9e313cc418..1d06a59769 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java @@ -53,7 +53,7 @@ public class CraftShapedRecipe extends ShapedRecipe implements CraftRecipe { for (int i = 0; i < shape.length; i++) { String row = shape[i]; for (int j = 0; j < row.length(); j++) { - data.set(i * width + j, toNMS(ingred.get(row.charAt(j)))); + data.set(i * width + j, toNMS(ingred.get(row.charAt(j)), false)); } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java index 8e3446fc3c..0773b13596 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java @@ -42,7 +42,7 @@ public class CraftShapelessRecipe extends ShapelessRecipe implements CraftRecipe List ingred = this.getChoiceList(); NonNullList data = NonNullList.a(ingred.size(), RecipeItemStack.a); for (int i = 0; i < ingred.size(); i++) { - data.set(i, toNMS(ingred.get(i))); + data.set(i, toNMS(ingred.get(i), true)); } MinecraftServer.getServer().getCraftingManager().a(new ShapelessRecipes(CraftNamespacedKey.toMinecraft(this.getKey()), this.getGroup(), CraftItemStack.asNMSCopy(this.getResult()), data));