diff options
Diffstat (limited to 'src/main/java/org/bukkit/inventory/RecipeChoice.java')
-rw-r--r-- | src/main/java/org/bukkit/inventory/RecipeChoice.java | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/inventory/RecipeChoice.java b/src/main/java/org/bukkit/inventory/RecipeChoice.java new file mode 100644 index 00000000..2dd72242 --- /dev/null +++ b/src/main/java/org/bukkit/inventory/RecipeChoice.java @@ -0,0 +1,114 @@ +package org.bukkit.inventory; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; +import org.bukkit.Material; + +/** + * Represents a potential item match within a recipe. All choices within a + * recipe must be satisfied for it to be craftable. + * + * <b>This class is not legal for implementation by plugins!</b> + * + * @deprecated draft API + */ +@Deprecated +public interface RecipeChoice extends Predicate<ItemStack>, Cloneable { + + /** + * Gets a single item stack representative of this stack choice. + * + * @return a single representative item + * @deprecated for compatability only + */ + @Deprecated + ItemStack getItemStack(); + + RecipeChoice clone(); + + /** + * Represents a choice of multiple matching Materials. + */ + public static class MaterialChoice implements RecipeChoice { + + private List<Material> choices; + + public MaterialChoice(List<Material> choices) { + Preconditions.checkArgument(choices != null, "choices"); + Preconditions.checkArgument(!choices.isEmpty(), "Must have at least one choice"); + this.choices = choices; + } + + @Override + public boolean test(ItemStack t) { + for (Material match : choices) { + if (t.getType() == match) { + return true; + } + } + + return false; + } + + @Override + public ItemStack getItemStack() { + ItemStack stack = new ItemStack(choices.get(0)); + + // For compat + if (choices.size() > 1) { + stack.setDurability(Short.MAX_VALUE); + } + + return stack; + } + + public List<Material> getChoices() { + return Collections.unmodifiableList(choices); + } + + @Override + public MaterialChoice clone() { + try { + MaterialChoice clone = (MaterialChoice) super.clone(); + clone.choices = new ArrayList<>(choices); + return clone; + } catch (CloneNotSupportedException ex) { + throw new AssertionError(ex); + } + } + + @Override + public int hashCode() { + int hash = 3; + hash = 37 * 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 MaterialChoice other = (MaterialChoice) obj; + if (!Objects.equals(this.choices, other.choices)) { + return false; + } + return true; + } + + @Override + public String toString() { + return "MaterialChoice{" + "choices=" + choices + '}'; + } + } +} |