Expand Recipe API to allow multiple Materials per slot
By: md_5 <git@md-5.net>
This commit is contained in:
@@ -1,7 +1,18 @@
|
||||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
import org.bukkit.inventory.RecipeChoice;
|
||||
|
||||
public interface CraftRecipe extends Recipe {
|
||||
|
||||
void addToCraftingManager();
|
||||
|
||||
default net.minecraft.server.RecipeItemStack toNMS(RecipeChoice bukkit) {
|
||||
if (bukkit instanceof RecipeChoice.MaterialChoice) {
|
||||
return new net.minecraft.server.RecipeItemStack(((RecipeChoice.MaterialChoice) bukkit).getChoices().stream().map((mat) -> new net.minecraft.server.RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(new ItemStack(mat)))));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.NonNullList;
|
||||
@@ -11,6 +10,7 @@ import net.minecraft.server.ShapedRecipes;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.RecipeChoice;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
|
||||
public class CraftShapedRecipe extends ShapedRecipe implements CraftRecipe {
|
||||
@@ -34,11 +34,11 @@ public class CraftShapedRecipe extends ShapedRecipe implements CraftRecipe {
|
||||
ret.setGroup(recipe.getGroup());
|
||||
String[] shape = recipe.getShape();
|
||||
ret.shape(shape);
|
||||
Map<Character, ItemStack> ingredientMap = recipe.getIngredientMap();
|
||||
Map<Character, RecipeChoice> ingredientMap = recipe.getChoiceMap();
|
||||
for (char c : ingredientMap.keySet()) {
|
||||
ItemStack stack = ingredientMap.get(c);
|
||||
RecipeChoice stack = ingredientMap.get(c);
|
||||
if (stack != null) {
|
||||
ret.setIngredient(c, stack.getType(), stack.getDurability());
|
||||
ret.setIngredient(c, stack);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@@ -46,14 +46,14 @@ public class CraftShapedRecipe extends ShapedRecipe implements CraftRecipe {
|
||||
|
||||
public void addToCraftingManager() {
|
||||
String[] shape = this.getShape();
|
||||
Map<Character, ItemStack> ingred = this.getIngredientMap();
|
||||
Map<Character, org.bukkit.inventory.RecipeChoice> ingred = this.getChoiceMap();
|
||||
int width = shape[0].length();
|
||||
NonNullList<RecipeItemStack> data = NonNullList.a(shape.length * width, RecipeItemStack.a);
|
||||
|
||||
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, new RecipeItemStack(Stream.of(new RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(ingred.get(row.charAt(j)))))));
|
||||
data.set(i * width + j, toNMS(ingred.get(row.charAt(j))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.bukkit.craftbukkit.inventory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.NonNullList;
|
||||
@@ -11,6 +10,7 @@ import net.minecraft.server.ShapelessRecipes;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.RecipeChoice;
|
||||
import org.bukkit.inventory.ShapelessRecipe;
|
||||
|
||||
public class CraftShapelessRecipe extends ShapelessRecipe implements CraftRecipe {
|
||||
@@ -32,17 +32,17 @@ public class CraftShapelessRecipe extends ShapelessRecipe implements CraftRecipe
|
||||
}
|
||||
CraftShapelessRecipe ret = new CraftShapelessRecipe(recipe.getKey(), recipe.getResult());
|
||||
ret.setGroup(recipe.getGroup());
|
||||
for (ItemStack ingred : recipe.getIngredientList()) {
|
||||
ret.addIngredient(ingred.getType(), ingred.getDurability());
|
||||
for (RecipeChoice ingred : recipe.getChoiceList()) {
|
||||
ret.addIngredient(ingred);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void addToCraftingManager() {
|
||||
List<ItemStack> ingred = this.getIngredientList();
|
||||
List<org.bukkit.inventory.RecipeChoice> ingred = this.getChoiceList();
|
||||
NonNullList<RecipeItemStack> data = NonNullList.a(ingred.size(), RecipeItemStack.a);
|
||||
for (int i = 0; i < ingred.size(); i++) {
|
||||
data.set(i, new RecipeItemStack(Stream.of(new RecipeItemStack.StackProvider(CraftItemStack.asNMSCopy(ingred.get(i))))));
|
||||
data.set(i, toNMS(ingred.get(i)));
|
||||
}
|
||||
|
||||
MinecraftServer.getServer().getCraftingManager().a(new ShapelessRecipes(CraftNamespacedKey.toMinecraft(this.getKey()), this.getGroup(), CraftItemStack.asNMSCopy(this.getResult()), data));
|
||||
|
||||
Reference in New Issue
Block a user