Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ShapelessRecipe.java
1 package org.bukkit.inventory;
2 
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 
7 import org.apache.commons.lang.Validate;
8 
9 import org.bukkit.Material;
11 
12 /**
13  * Represents a shapeless recipe, where the arrangement of the ingredients on
14  * the crafting grid does not matter.
15  */
16 public class ShapelessRecipe implements Recipe {
17  private ItemStack output;
18  private List<ItemStack> ingredients = new ArrayList<ItemStack>();
19 
20  /**
21  * Create a shapeless recipe to craft the specified ItemStack. The
22  * constructor merely determines the result and type; to set the actual
23  * recipe, you'll need to call the appropriate methods.
24  *
25  * @param result The item you want the recipe to create.
26  * @see ShapelessRecipe#addIngredient(Material)
27  * @see ShapelessRecipe#addIngredient(MaterialData)
28  * @see ShapelessRecipe#addIngredient(Material,int)
29  * @see ShapelessRecipe#addIngredient(int,Material)
30  * @see ShapelessRecipe#addIngredient(int,MaterialData)
31  * @see ShapelessRecipe#addIngredient(int,Material,int)
32  */
33  public ShapelessRecipe(ItemStack result) {
34  this.output = new ItemStack(result);
35  }
36 
37  /**
38  * Adds the specified ingredient.
39  *
40  * @param ingredient The ingredient to add.
41  * @return The changed recipe, so you can chain calls.
42  */
44  return addIngredient(1, ingredient);
45  }
46 
47  /**
48  * Adds the specified ingredient.
49  *
50  * @param ingredient The ingredient to add.
51  * @return The changed recipe, so you can chain calls.
52  */
53  public ShapelessRecipe addIngredient(Material ingredient) {
54  return addIngredient(1, ingredient, 0);
55  }
56 
57  /**
58  * Adds the specified ingredient.
59  *
60  * @param ingredient The ingredient to add.
61  * @param rawdata The data value, or -1 to allow any data value.
62  * @return The changed recipe, so you can chain calls.
63  * @deprecated Magic value
64  */
65  @Deprecated
66  public ShapelessRecipe addIngredient(Material ingredient, int rawdata) {
67  return addIngredient(1, ingredient, rawdata);
68  }
69 
70  /**
71  * Adds multiples of the specified ingredient.
72  *
73  * @param count How many to add (can't be more than 9!)
74  * @param ingredient The ingredient to add.
75  * @return The changed recipe, so you can chain calls.
76  */
77  public ShapelessRecipe addIngredient(int count, MaterialData ingredient) {
78  return addIngredient(count, ingredient.getItemType(), ingredient.getData());
79  }
80 
81  /**
82  * Adds multiples of the specified ingredient.
83  *
84  * @param count How many to add (can't be more than 9!)
85  * @param ingredient The ingredient to add.
86  * @return The changed recipe, so you can chain calls.
87  */
88  public ShapelessRecipe addIngredient(int count, Material ingredient) {
89  return addIngredient(count, ingredient, 0);
90  }
91 
92  /**
93  * Adds multiples of the specified ingredient.
94  *
95  * @param count How many to add (can't be more than 9!)
96  * @param ingredient The ingredient to add.
97  * @param rawdata The data value, or -1 to allow any data value.
98  * @return The changed recipe, so you can chain calls.
99  * @deprecated Magic value
100  */
101  @Deprecated
102  public ShapelessRecipe addIngredient(int count, Material ingredient, int rawdata) {
103  Validate.isTrue(ingredients.size() + count <= 9, "Shapeless recipes cannot have more than 9 ingredients");
104 
105  // -1 is the old wildcard, map to Short.MAX_VALUE as the new one
106  if (rawdata == -1) {
107  rawdata = Short.MAX_VALUE;
108  }
109 
110  while (count-- > 0) {
111  ingredients.add(new ItemStack(ingredient, 1, (short) rawdata));
112  }
113  return this;
114  }
115 
116  /**
117  * Removes an ingredient from the list. If the ingredient occurs multiple
118  * times, only one instance of it is removed. Only removes exact matches,
119  * with a data value of 0.
120  *
121  * @param ingredient The ingredient to remove
122  * @return The changed recipe.
123  */
125  return removeIngredient(ingredient, 0);
126  }
127 
128  /**
129  * Removes an ingredient from the list. If the ingredient occurs multiple
130  * times, only one instance of it is removed. If the data value is -1,
131  * only ingredients with a -1 data value will be removed.
132  *
133  * @param ingredient The ingredient to remove
134  * @return The changed recipe.
135  */
137  return removeIngredient(ingredient.getItemType(), ingredient.getData());
138  }
139 
140  /**
141  * Removes multiple instances of an ingredient from the list. If there are
142  * less instances then specified, all will be removed. Only removes exact
143  * matches, with a data value of 0.
144  *
145  * @param count The number of copies to remove.
146  * @param ingredient The ingredient to remove
147  * @return The changed recipe.
148  */
149  public ShapelessRecipe removeIngredient(int count, Material ingredient) {
150  return removeIngredient(count, ingredient, 0);
151  }
152 
153  /**
154  * Removes multiple instances of an ingredient from the list. If there are
155  * less instances then specified, all will be removed. If the data value
156  * is -1, only ingredients with a -1 data value will be removed.
157  *
158  * @param count The number of copies to remove.
159  * @param ingredient The ingredient to remove.
160  * @return The changed recipe.
161  */
162  public ShapelessRecipe removeIngredient(int count, MaterialData ingredient) {
163  return removeIngredient(count, ingredient.getItemType(), ingredient.getData());
164  }
165 
166  /**
167  * Removes an ingredient from the list. If the ingredient occurs multiple
168  * times, only one instance of it is removed. If the data value is -1,
169  * only ingredients with a -1 data value will be removed.
170  *
171  * @param ingredient The ingredient to remove
172  * @param rawdata The data value;
173  * @return The changed recipe.
174  * @deprecated Magic value
175  */
176  @Deprecated
177  public ShapelessRecipe removeIngredient(Material ingredient, int rawdata) {
178  return removeIngredient(1, ingredient, rawdata);
179  }
180 
181  /**
182  * Removes multiple instances of an ingredient from the list. If there are
183  * less instances then specified, all will be removed. If the data value
184  * is -1, only ingredients with a -1 data value will be removed.
185  *
186  * @param count The number of copies to remove.
187  * @param ingredient The ingredient to remove.
188  * @param rawdata The data value.
189  * @return The changed recipe.
190  * @deprecated Magic value
191  */
192  @Deprecated
193  public ShapelessRecipe removeIngredient(int count, Material ingredient, int rawdata) {
194  Iterator<ItemStack> iterator = ingredients.iterator();
195  while (count > 0 && iterator.hasNext()) {
196  ItemStack stack = iterator.next();
197  if (stack.getType() == ingredient && stack.getDurability() == rawdata) {
198  iterator.remove();
199  count--;
200  }
201  }
202  return this;
203  }
204 
205  /**
206  * Get the result of this recipe.
207  *
208  * @return The result stack.
209  */
210  public ItemStack getResult() {
211  return output.clone();
212  }
213 
214  /**
215  * Get the list of ingredients used for this recipe.
216  *
217  * @return The input list
218  */
219  public List<ItemStack> getIngredientList() {
220  ArrayList<ItemStack> result = new ArrayList<ItemStack>(ingredients.size());
221  for (ItemStack ingredient : ingredients) {
222  result.add(ingredient.clone());
223  }
224  return result;
225  }
226 }
ShapelessRecipe removeIngredient(Material ingredient, int rawdata)
ShapelessRecipe removeIngredient(int count, Material ingredient)
ShapelessRecipe addIngredient(Material ingredient, int rawdata)
ShapelessRecipe addIngredient(MaterialData ingredient)
ShapelessRecipe removeIngredient(Material ingredient)
ShapelessRecipe removeIngredient(int count, Material ingredient, int rawdata)
ShapelessRecipe addIngredient(int count, Material ingredient)
ShapelessRecipe removeIngredient(MaterialData ingredient)
ShapelessRecipe addIngredient(int count, Material ingredient, int rawdata)
ShapelessRecipe removeIngredient(int count, MaterialData ingredient)
ShapelessRecipe addIngredient(int count, MaterialData ingredient)
ShapelessRecipe addIngredient(Material ingredient)