Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.inventory;
2
3import org.bukkit.Material;
4import org.bukkit.material.MaterialData;
5
6import java.util.ArrayList;
7
8/**
9 * Represents a shapeless recipe, where the arrangement of the ingredients on the crafting grid
10 * does not matter.
11 */
12public class ShapelessRecipe implements Recipe {
13 private ItemStack output;
14 private ArrayList<MaterialData> ingredients = new ArrayList<MaterialData>();
15
16 /**
17 * Create a shapeless recipe to craft the specified ItemStack. The constructor merely determines the
18 * result and type; to set the actual recipe, you'll need to call the appropriate methods.
19 *
20 * @param result The item you want the recipe to create.
21 * @see ShapelessRecipe#addIngredient(Material)
22 * @see ShapelessRecipe#addIngredient(MaterialData)
23 */
24 public ShapelessRecipe(ItemStack result) {
25 this.output = result;
26 }
27
28 /**
29 * Adds the specified ingredient.
30 *
31 * @param ingredient The ingredient to add.
32 * @return The changed recipe, so you can chain calls.
33 */
34 public ShapelessRecipe addIngredient(MaterialData ingredient) {
35 return addIngredient(1, ingredient);
36 }
37
38 /**
39 * Adds the specified ingredient.
40 *
41 * @param ingredient The ingredient to add.
42 * @return The changed recipe, so you can chain calls.
43 */
44 public ShapelessRecipe addIngredient(Material ingredient) {
45 return addIngredient(1, ingredient, 0);
46 }
47
48 /**
49 * Adds the specified ingredient.
50 *
51 * @param ingredient The ingredient to add.
52 * @param rawdata The data value.
53 * @return The changed recipe, so you can chain calls.
54 */
55 public ShapelessRecipe addIngredient(Material ingredient, int rawdata) {
56 return addIngredient(1, ingredient, rawdata);
57 }
58
59 /**
60 * Adds multiples of the specified ingredient.
61 *
62 * @param count How many to add (can't be more than 9!)
63 * @param ingredient The ingredient to add.
64 * @return The changed recipe, so you can chain calls.
65 */
66 public ShapelessRecipe addIngredient(int count, MaterialData ingredient) {
67 if (ingredients.size() + count > 9) {
68 throw new IllegalArgumentException("Shapeless recipes cannot have more than 9 ingredients");
69 }
70 while (count-- > 0) {
71 ingredients.add(ingredient);
72 }
73 return this;
74 }
75
76 /**
77 * Adds multiples of the specified ingredient.
78 *
79 * @param count How many to add (can't be more than 9!)
80 * @param ingredient The ingredient to add.
81 * @return The changed recipe, so you can chain calls.
82 */
83 public ShapelessRecipe addIngredient(int count, Material ingredient) {
84 return addIngredient(count, ingredient, 0);
85 }
86
87 /**
88 * Adds multiples of the specified ingredient.
89 *
90 * @param count How many to add (can't be more than 9!)
91 * @param ingredient The ingredient to add.
92 * @param rawdata The data value.
93 * @return The changed recipe, so you can chain calls.
94 */
95 public ShapelessRecipe addIngredient(int count, Material ingredient, int rawdata) {
96 MaterialData data = ingredient.getNewData((byte) rawdata);
97
98 if (data == null) {
99 data = new MaterialData(ingredient, (byte) rawdata);
100 }
101 return addIngredient(count, data);
102 }
103
104 /**
105 * Removes an ingredient from the list. If the ingredient occurs multiple times,
106 * only one instance of it is removed.
107 *
108 * @param ingredient The ingredient to remove
109 * @return The changed recipe.
110 */
111 public ShapelessRecipe removeIngredient(MaterialData ingredient) {
112 this.ingredients.remove(ingredient);
113 return this;
114 }
115
116 /**
117 * Get the result of this recipe.
118 *
119 * @return The result stack.
120 */
121 public ItemStack getResult() {
122 return output;
123 }
124
125 /**
126 * Get the list of ingredients used for this recipe.
127 *
128 * @return The input list
129 */
130 public ArrayList<MaterialData> getIngredientList() {
131 return ingredients;
132 }
133}