diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java index f1391b926f..e6d413ce91 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java @@ -1,8 +1,8 @@ package org.bukkit.craftbukkit.inventory; +import com.google.common.collect.Maps; import java.util.Map; -import java.util.Optional; -import net.minecraft.core.NonNullList; +import java.util.Objects; import net.minecraft.server.MinecraftServer; import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.item.crafting.RecipeItemStack; @@ -48,17 +48,27 @@ public class CraftShapedRecipe extends ShapedRecipe implements CraftRecipe { @Override public void addToCraftingManager() { - String[] shape = this.getShape(); Map ingred = this.getChoiceMap(); - int width = shape[0].length(); - NonNullList data = NonNullList.withSize(shape.length * width, RecipeItemStack.EMPTY); + String[] shape = replaceUndefinedIngredientsWithEmpty(this.getShape(), ingred); + ingred.values().removeIf(Objects::isNull); + Map data = Maps.transformValues(ingred, (bukkit) -> toNMS(bukkit, false)); + ShapedRecipePattern pattern = ShapedRecipePattern.of(data, shape); + MinecraftServer.getServer().getRecipeManager().addRecipe(new RecipeHolder<>(CraftNamespacedKey.toMinecraft(this.getKey()), new ShapedRecipes(this.getGroup(), CraftRecipe.getCategory(this.getCategory()), pattern, CraftItemStack.asNMSCopy(this.getResult())))); + } + + private static String[] replaceUndefinedIngredientsWithEmpty(String[] shape, Map ingredients) { 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)), false)); + StringBuilder filteredRow = new StringBuilder(row.length()); + + for (char character : row.toCharArray()) { + filteredRow.append(ingredients.get(character) == null ? ' ' : character); } + + shape[i] = filteredRow.toString(); } - MinecraftServer.getServer().getRecipeManager().addRecipe(new RecipeHolder<>(CraftNamespacedKey.toMinecraft(this.getKey()), new ShapedRecipes(this.getGroup(), CraftRecipe.getCategory(this.getCategory()), new ShapedRecipePattern(width, shape.length, data, Optional.empty()), CraftItemStack.asNMSCopy(this.getResult())))); + + return shape; } }