Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ShapedRecipe.java
1 package org.bukkit.inventory;
2 
3 import java.util.HashMap;
4 import java.util.Map;
5 
6 import org.apache.commons.lang.Validate;
7 
8 import org.bukkit.Material;
10 
11 /**
12  * Represents a shaped (ie normal) crafting recipe.
13  */
14 public class ShapedRecipe implements Recipe {
15  private ItemStack output;
16  private String[] rows;
17  private Map<Character, ItemStack> ingredients = new HashMap<Character, ItemStack>();
18 
19  /**
20  * Create a shaped recipe to craft the specified ItemStack. The
21  * constructor merely determines the result and type; to set the actual
22  * recipe, you'll need to call the appropriate methods.
23  *
24  * @param result The item you want the recipe to create.
25  * @see ShapedRecipe#shape(String...)
26  * @see ShapedRecipe#setIngredient(char, Material)
27  * @see ShapedRecipe#setIngredient(char, Material, int)
28  * @see ShapedRecipe#setIngredient(char, MaterialData)
29  */
30  public ShapedRecipe(ItemStack result) {
31  this.output = new ItemStack(result);
32  }
33 
34  /**
35  * Set the shape of this recipe to the specified rows. Each character
36  * represents a different ingredient; exactly what each character
37  * represents is set separately. The first row supplied corresponds with
38  * the upper most part of the recipe on the workbench e.g. if all three
39  * rows are supplies the first string represents the top row on the
40  * workbench.
41  *
42  * @param shape The rows of the recipe (up to 3 rows).
43  * @return The changed recipe, so you can chain calls.
44  */
45  public ShapedRecipe shape(final String... shape) {
46  Validate.notNull(shape, "Must provide a shape");
47  Validate.isTrue(shape.length > 0 && shape.length < 4, "Crafting recipes should be 1, 2, 3 rows, not ", shape.length);
48 
49  for (String row : shape) {
50  Validate.notNull(row, "Shape cannot have null rows");
51  Validate.isTrue(row.length() > 0 && row.length() < 4, "Crafting rows should be 1, 2, or 3 characters, not ", row.length());
52  }
53  this.rows = new String[shape.length];
54  for (int i = 0; i < shape.length; i++) {
55  this.rows[i] = shape[i];
56  }
57 
58  // Remove character mappings for characters that no longer exist in the shape
59  HashMap<Character, ItemStack> newIngredients = new HashMap<Character, ItemStack>();
60  for (String row : shape) {
61  for (Character c : row.toCharArray()) {
62  newIngredients.put(c, ingredients.get(c));
63  }
64  }
65  this.ingredients = newIngredients;
66 
67  return this;
68  }
69 
70  /**
71  * Sets the material that a character in the recipe shape refers to.
72  *
73  * @param key The character that represents the ingredient in the shape.
74  * @param ingredient The ingredient.
75  * @return The changed recipe, so you can chain calls.
76  */
77  public ShapedRecipe setIngredient(char key, MaterialData ingredient) {
78  return setIngredient(key, ingredient.getItemType(), ingredient.getData());
79  }
80 
81  /**
82  * Sets the material that a character in the recipe shape refers to.
83  *
84  * @param key The character that represents the ingredient in the shape.
85  * @param ingredient The ingredient.
86  * @return The changed recipe, so you can chain calls.
87  */
88  public ShapedRecipe setIngredient(char key, Material ingredient) {
89  return setIngredient(key, ingredient, 0);
90  }
91 
92  /**
93  * Sets the material that a character in the recipe shape refers to.
94  *
95  * @param key The character that represents the ingredient in the shape.
96  * @param ingredient The ingredient.
97  * @param raw The raw material data as an integer.
98  * @return The changed recipe, so you can chain calls.
99  * @deprecated Magic value
100  */
101  @Deprecated
102  public ShapedRecipe setIngredient(char key, Material ingredient, int raw) {
103  Validate.isTrue(ingredients.containsKey(key), "Symbol does not appear in the shape:", key);
104 
105  // -1 is the old wildcard, map to Short.MAX_VALUE as the new one
106  if (raw == -1) {
107  raw = Short.MAX_VALUE;
108  }
109 
110  ingredients.put(key, new ItemStack(ingredient, 1, (short) raw));
111  return this;
112  }
113 
114  /**
115  * Get a copy of the ingredients map.
116  *
117  * @return The mapping of character to ingredients.
118  */
119  public Map<Character, ItemStack> getIngredientMap() {
120  HashMap<Character, ItemStack> result = new HashMap<Character, ItemStack>();
121  for (Map.Entry<Character, ItemStack> ingredient : ingredients.entrySet()) {
122  if (ingredient.getValue() == null) {
123  result.put(ingredient.getKey(), null);
124  } else {
125  result.put(ingredient.getKey(), ingredient.getValue().clone());
126  }
127  }
128  return result;
129  }
130 
131  /**
132  * Get the shape.
133  *
134  * @return The recipe's shape.
135  */
136  public String[] getShape() {
137  return rows.clone();
138  }
139 
140  /**
141  * Get the result.
142  *
143  * @return The result stack.
144  */
145  public ItemStack getResult() {
146  return output.clone();
147  }
148 }
ShapedRecipe setIngredient(char key, Material ingredient)
ShapedRecipe setIngredient(char key, Material ingredient, int raw)
ShapedRecipe shape(final String...shape)
ShapedRecipe setIngredient(char key, MaterialData ingredient)
Map< Character, ItemStack > getIngredientMap()