Bukkit  1.4.7-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
ShapedRecipe.java
Go to the documentation of this file.
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;
9 import org.bukkit.material.MaterialData;
10 
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 
29  public ShapedRecipe(ItemStack result) {
30  this.output = new ItemStack(result);
31  }
32 
42  public ShapedRecipe shape(final String... shape) {
43  Validate.notNull(shape, "Must provide a shape");
44  Validate.isTrue(shape.length > 0 && shape.length < 4, "Crafting recipes should be 1, 2, 3 rows, not ", shape.length);
45 
46  for (String row : shape) {
47  Validate.notNull(row, "Shape cannot have null rows");
48  Validate.isTrue(row.length() > 0 && row.length() < 4, "Crafting rows should be 1, 2, or 3 characters, not ", row.length());
49  }
50  this.rows = new String[shape.length];
51  for (int i = 0; i < shape.length; i++) {
52  this.rows[i] = shape[i];
53  }
54 
55  // Remove character mappings for characters that no longer exist in the shape
56  HashMap<Character, ItemStack> newIngredients = new HashMap<Character, ItemStack>();
57  for (String row : shape) {
58  for (Character c : row.toCharArray()) {
59  newIngredients.put(c, ingredients.get(c));
60  }
61  }
62  this.ingredients = newIngredients;
63 
64  return this;
65  }
66 
74  public ShapedRecipe setIngredient(char key, MaterialData ingredient) {
75  return setIngredient(key, ingredient.getItemType(), ingredient.getData());
76  }
77 
85  public ShapedRecipe setIngredient(char key, Material ingredient) {
86  return setIngredient(key, ingredient, 0);
87  }
88 
97  public ShapedRecipe setIngredient(char key, Material ingredient, int raw) {
98  Validate.isTrue(ingredients.containsKey(key), "Symbol does not appear in the shape:", key);
99  ingredients.put(key, new ItemStack(ingredient, 1, (short) raw));
100  return this;
101  }
102 
108  public Map<Character, ItemStack> getIngredientMap() {
109  HashMap<Character, ItemStack> result = new HashMap<Character, ItemStack>();
110  for (Map.Entry<Character, ItemStack> ingredient : ingredients.entrySet()) {
111  if (ingredient.getValue() == null) {
112  result.put(ingredient.getKey(), null);
113  } else {
114  result.put(ingredient.getKey(), ingredient.getValue().clone());
115  }
116  }
117  return result;
118  }
119 
125  public String[] getShape() {
126  return rows.clone();
127  }
128 
134  public ItemStack getResult() {
135  return output.clone();
136  }
137 }