From 98d49a9175ac7c4ccad233a06037a037545c5521 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Fri, 25 Jan 2019 13:58:30 +1100 Subject: [PATCH] Consistent multiple choice APIs for RecipeChoice. By: md_5 --- .../org/bukkit/inventory/RecipeChoice.java | 82 +++++++++++++++++-- 1 file changed, 74 insertions(+), 8 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java b/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java index 5d254ca829..8a29ab4bd6 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java +++ b/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java @@ -2,6 +2,7 @@ package org.bukkit.inventory; import com.google.common.base.Preconditions; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -34,9 +35,21 @@ public interface RecipeChoice extends Predicate, Cloneable { private List choices; + public MaterialChoice(Material choice) { + this(Arrays.asList(choice)); + } + + public MaterialChoice(Material... choices) { + this(Arrays.asList(choices)); + } + public MaterialChoice(List choices) { Preconditions.checkArgument(choices != null, "choices"); Preconditions.checkArgument(!choices.isEmpty(), "Must have at least one choice"); + for (Material choice : choices) { + Preconditions.checkArgument(choice != null, "Cannot have null choice"); + } + this.choices = choices; } @@ -110,8 +123,8 @@ public interface RecipeChoice extends Predicate, Cloneable { } /** - * Represents a choice that will be valid only if a stack is exactly matched - * (aside from stack size). + * Represents a choice that will be valid only one of the stacks is exactly + * matched (aside from stack size). *
* Not valid for shapeless recipes * @@ -120,23 +133,40 @@ public interface RecipeChoice extends Predicate, Cloneable { @Deprecated public static class ExactChoice implements RecipeChoice { - private ItemStack stack; + private List choices; public ExactChoice(ItemStack stack) { - Preconditions.checkArgument(stack != null, "stack"); - this.stack = stack; + this(Arrays.asList(stack)); + } + + public ExactChoice(ItemStack... stacks) { + this(Arrays.asList(stacks)); + } + + public ExactChoice(List choices) { + Preconditions.checkArgument(choices != null, "choices"); + Preconditions.checkArgument(!choices.isEmpty(), "Must have at least one choice"); + for (ItemStack choice : choices) { + Preconditions.checkArgument(choice != null, "Cannot have null choice"); + } + + this.choices = choices; } @Override public ItemStack getItemStack() { - return stack; + return choices.get(0).clone(); + } + + public List getChoices() { + return Collections.unmodifiableList(choices); } @Override public ExactChoice clone() { try { ExactChoice clone = (ExactChoice) super.clone(); - clone.stack = stack.clone(); + clone.choices = new ArrayList<>(choices); return clone; } catch (CloneNotSupportedException ex) { throw new AssertionError(ex); @@ -145,7 +175,43 @@ public interface RecipeChoice extends Predicate, Cloneable { @Override public boolean test(ItemStack t) { - return stack.equals(t); + for (ItemStack match : choices) { + if (t.isSimilar(match)) { + return true; + } + } + + return false; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 41 * hash + Objects.hashCode(this.choices); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final ExactChoice other = (ExactChoice) obj; + if (!Objects.equals(this.choices, other.choices)) { + return false; + } + return true; + } + + @Override + public String toString() { + return "ExactChoice{" + "choices=" + choices + '}'; } } }