SPIGOT-4587: Enforce at least one valid choice for FurnaceRecipe

This commit is contained in:
md_5 2019-01-21 10:39:24 +11:00
parent 7fc66b28db
commit 1a64d4ae2b
4 changed files with 19 additions and 12 deletions

View File

@ -25,6 +25,6 @@ public class CraftFurnaceRecipe extends FurnaceRecipe implements CraftRecipe {
public void addToCraftingManager() { public void addToCraftingManager() {
ItemStack result = this.getResult(); 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()));
} }
} }

View File

@ -14,19 +14,26 @@ public interface CraftRecipe extends Recipe {
void addToCraftingManager(); void addToCraftingManager();
default net.minecraft.server.RecipeItemStack toNMS(RecipeChoice bukkit) { default RecipeItemStack toNMS(RecipeChoice bukkit, boolean requireNotEmpty) {
if (bukkit == null) { RecipeItemStack stack;
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;
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 { } else {
throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit); 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) { public static RecipeChoice toBukkit(RecipeItemStack list) {

View File

@ -53,7 +53,7 @@ public class CraftShapedRecipe extends ShapedRecipe implements CraftRecipe {
for (int i = 0; i < shape.length; i++) { for (int i = 0; i < shape.length; i++) {
String row = shape[i]; String row = shape[i];
for (int j = 0; j < row.length(); j++) { 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));
} }
} }

View File

@ -42,7 +42,7 @@ public class CraftShapelessRecipe extends ShapelessRecipe implements CraftRecipe
List<org.bukkit.inventory.RecipeChoice> ingred = this.getChoiceList(); List<org.bukkit.inventory.RecipeChoice> ingred = this.getChoiceList();
NonNullList<RecipeItemStack> data = NonNullList.a(ingred.size(), RecipeItemStack.a); NonNullList<RecipeItemStack> data = NonNullList.a(ingred.size(), RecipeItemStack.a);
for (int i = 0; i < ingred.size(); i++) { 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)); MinecraftServer.getServer().getCraftingManager().a(new ShapelessRecipes(CraftNamespacedKey.toMinecraft(this.getKey()), this.getGroup(), CraftItemStack.asNMSCopy(this.getResult()), data));