SPIGOT-7770: Fix certain shaped recipes not registering

By: 2008Choco <hawkeboyz2@hotmail.com>
This commit is contained in:
CraftBukkit/Spigot 2024-06-18 19:06:07 +10:00
parent 5f8b82d6cc
commit ac95e0b210

View file

@ -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<Character, org.bukkit.inventory.RecipeChoice> ingred = this.getChoiceMap();
int width = shape[0].length();
NonNullList<RecipeItemStack> data = NonNullList.withSize(shape.length * width, RecipeItemStack.EMPTY);
String[] shape = replaceUndefinedIngredientsWithEmpty(this.getShape(), ingred);
ingred.values().removeIf(Objects::isNull);
Map<Character, RecipeItemStack> 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<Character, org.bukkit.inventory.RecipeChoice> 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;
}
}