Add RecipeChoice.ExactChoice API for NBT matches on ingredients

This commit is contained in:
md_5 2019-01-02 15:56:21 +11:00
parent 8e65d8df6c
commit 756c38d1e4
5 changed files with 71 additions and 32 deletions

View File

@ -9,30 +9,23 @@
+import java.util.List;
+import org.bukkit.craftbukkit.inventory.CraftFurnaceRecipe;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.craftbukkit.inventory.CraftRecipe;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
+import org.bukkit.inventory.Recipe;
+import org.bukkit.inventory.RecipeChoice;
+// CraftBukkit end
public class FurnaceRecipe implements IRecipe {
@@ -56,6 +66,23 @@
@@ -56,6 +66,16 @@
return this.key;
}
+ @Override
+ public Recipe toBukkitRecipe() {
+ CraftItemStack result = CraftItemStack.asCraftMirror(this.result);
+ RecipeItemStack list = this.ingredient;
+ list.buildChoices();
+
+ List<org.bukkit.Material> choices = new ArrayList<>(list.choices.length);
+ for (ItemStack i : list.choices) {
+ choices.add(CraftMagicNumbers.getMaterial(i.getItem()));
+ }
+
+ CraftFurnaceRecipe recipe = new CraftFurnaceRecipe(CraftNamespacedKey.fromMinecraft(this.key), result, new RecipeChoice.MaterialChoice(choices), this.experience, this.cookingTime);
+ CraftFurnaceRecipe recipe = new CraftFurnaceRecipe(CraftNamespacedKey.fromMinecraft(this.key), result, CraftRecipe.toBukkit(this.ingredient), this.experience, this.cookingTime);
+ recipe.setGroup(this.group);
+
+ return recipe;

View File

@ -0,0 +1,26 @@
--- a/net/minecraft/server/RecipeItemStack.java
+++ b/net/minecraft/server/RecipeItemStack.java
@@ -28,6 +28,7 @@
private final RecipeItemStack.Provider[] c;
public ItemStack[] choices;
private IntList e;
+ public boolean exact; // CraftBukkit
public RecipeItemStack(Stream<? extends RecipeItemStack.Provider> stream) {
this.c = (RecipeItemStack.Provider[]) stream.filter(RecipeItemStack.b).toArray((i) -> {
@@ -59,6 +60,15 @@
for (int j = 0; j < i; ++j) {
ItemStack itemstack1 = aitemstack[j];
+ // CraftBukkit start
+ if (exact) {
+ if (ItemStack.equals(itemstack, itemstack1)) {
+ return true;
+ }
+
+ continue;
+ }
+ // CraftBukkit end
if (itemstack1.getItem() == itemstack.getItem()) {
return true;
}

View File

@ -1,6 +1,6 @@
--- a/net/minecraft/server/ShapedRecipes.java
+++ b/net/minecraft/server/ShapedRecipes.java
@@ -12,6 +12,13 @@
@@ -12,6 +12,15 @@
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
@ -8,13 +8,15 @@
+import java.util.ArrayList;
+import java.util.List;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.craftbukkit.inventory.CraftRecipe;
+import org.bukkit.craftbukkit.inventory.CraftShapedRecipe;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+import org.bukkit.inventory.RecipeChoice;
+// CraftBukkit end
public class ShapedRecipes implements IRecipe {
@@ -31,6 +38,70 @@
@@ -31,6 +40,66 @@
this.result = itemstack;
}
@ -67,15 +69,11 @@
+ }
+ char c = 'a';
+ for (RecipeItemStack list : this.items) {
+ list.buildChoices();
+ if (list.choices.length > 0) {
+ List<org.bukkit.Material> choices = new ArrayList<>(list.choices.length);
+ for (ItemStack i : list.choices) {
+ choices.add(CraftMagicNumbers.getMaterial(i.getItem()));
+ RecipeChoice choice = CraftRecipe.toBukkit(list);
+ if (choice != null) {
+ recipe.setIngredient(c, choice);
+ }
+
+ recipe.setIngredient(c, new org.bukkit.inventory.RecipeChoice.MaterialChoice(choices));
+ }
+ c++;
+ }
+ return recipe;

View File

@ -1,20 +1,18 @@
--- a/net/minecraft/server/ShapelessRecipes.java
+++ b/net/minecraft/server/ShapelessRecipes.java
@@ -5,6 +5,13 @@
@@ -5,6 +5,11 @@
import com.google.gson.JsonParseException;
import it.unimi.dsi.fastutil.ints.IntList;
import java.util.Iterator;
+// CraftBukkit start
+import java.util.ArrayList;
+import java.util.List;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.craftbukkit.inventory.CraftRecipe;
+import org.bukkit.craftbukkit.inventory.CraftShapelessRecipe;
+import org.bukkit.craftbukkit.util.CraftMagicNumbers;
+// CraftBukkit end
public class ShapelessRecipes implements IRecipe {
@@ -20,6 +27,26 @@
@@ -20,6 +25,20 @@
this.ingredients = nonnulllist;
}
@ -26,13 +24,7 @@
+ recipe.setGroup(this.group);
+
+ for (RecipeItemStack list : this.ingredients) {
+ list.buildChoices();
+
+ List<org.bukkit.Material> choices = new ArrayList<>(list.choices.length);
+ for (ItemStack i : list.choices) {
+ choices.add(CraftMagicNumbers.getMaterial(i.getItem()));
+ }
+ recipe.addIngredient(new org.bukkit.inventory.RecipeChoice.MaterialChoice(choices));
+ recipe.addIngredient(CraftRecipe.toBukkit(list));
+ }
+ return recipe;
+ }

View File

@ -1,6 +1,11 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import net.minecraft.server.RecipeItemStack;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.RecipeChoice;
@ -14,8 +19,33 @@ public interface CraftRecipe extends Recipe {
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;
} else {
throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit);
}
}
public static RecipeChoice toBukkit(RecipeItemStack list) {
list.buildChoices();
if (list.choices.length == 0) {
return null;
}
if (list.exact) {
Preconditions.checkState(list.choices.length == 1, "Exact recipe must have 1 choice");
return new RecipeChoice.ExactChoice(CraftItemStack.asBukkitCopy(list.choices[0]));
}
List<org.bukkit.Material> choices = new ArrayList<>(list.choices.length);
for (net.minecraft.server.ItemStack i : list.choices) {
choices.add(CraftMagicNumbers.getMaterial(i.getItem()));
}
return new RecipeChoice.MaterialChoice(choices);
}
}