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.HashMap;
7
8/**
9 * Represents a shaped (ie normal) crafting recipe.
10 */
11public class ShapedRecipe implements Recipe {
12 private ItemStack output;
13 private String[] rows;
14 private HashMap<Character, MaterialData> ingredients = new HashMap<Character, MaterialData>();
15
16 /**
17 * Create a shaped 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 ShapedRecipe#shape(String...)
22 * @see ShapedRecipe#setIngredient(char, Material)
23 * @see ShapedRecipe#setIngredient(char, Material, int)
24 * @see ShapedRecipe#setIngredient(char, MaterialData)
25 */
26 public ShapedRecipe(ItemStack result) {
27 this.output = result;
28 }
29
30 /**
31 * Set the shape of this recipe to the specified rows. Each character represents a different
32 * ingredient; exactly what each character represents is set separately.
33 *
34 * @param shape The rows of the recipe (up to 3 rows).
35 * @return The changed recipe, so you can chain calls.
36 */
37 public ShapedRecipe shape(String... shape) {
38 if (shape == null || shape.length > 3 || shape.length < 1) {
39 throw new IllegalArgumentException("Crafting recipes should be 1, 2, or 3 rows.");
40 }
41 for (String row : shape) {
42 if (row == null || row.length() > 3 || row.length() < 1) {
43 throw new IllegalArgumentException("Crafting rows should be 1, 2, or 3 characters.");
44 }
45 }
46 this.rows = shape;
47
48 // Remove character mappings for characters that no longer exist in the shape
49 HashMap<Character, MaterialData> ingredientsTemp = this.ingredients;
50
51 this.ingredients = new HashMap<Character, MaterialData>();
52 for (char key : ingredientsTemp.keySet()) {
53 try {
54 setIngredient(key, ingredientsTemp.get(key));
55 } catch (IllegalArgumentException e) {
56 }
57 }
58 return this;
59 }
60
61 /**
62 * Sets the material that a character in the recipe shape refers to.
63 *
64 * @param key The character that represents the ingredient in the shape.
65 * @param ingredient The ingredient.
66 * @return The changed recipe, so you can chain calls.
67 */
68 public ShapedRecipe setIngredient(char key, MaterialData ingredient) {
69 if (!hasKey(key)) {
70 throw new IllegalArgumentException("Symbol " + key + " does not appear in the shape.");
71 }
72 ingredients.put(key, ingredient);
73 return this;
74 }
75
76 /**
77 * Sets the material that a character in the recipe shape refers to.
78 *
79 * @param key The character that represents the ingredient in the shape.
80 * @param ingredient The ingredient.
81 * @return The changed recipe, so you can chain calls.
82 */
83 public ShapedRecipe setIngredient(char key, Material ingredient) {
84 return setIngredient(key, ingredient, 0);
85 }
86
87 /**
88 * Sets the material that a character in the recipe shape refers to.
89 *
90 * @param key The character that represents the ingredient in the shape.
91 * @param ingredient The ingredient.
92 * @param raw The raw material data as an integer.
93 * @return The changed recipe, so you can chain calls.
94 */
95 public ShapedRecipe setIngredient(char key, Material ingredient, int raw) {
96 MaterialData data = ingredient.getNewData((byte) raw);
97
98 if (data == null) {
99 data = new MaterialData(ingredient, (byte) raw);
100 }
101 return setIngredient(key, data);
102 }
103
104 private boolean hasKey(char c) {
105 String key = Character.toString(c);
106
107 for (String row : rows) {
108 if (row.contains(key)) {
109 return true;
110 }
111 }
112 return false;
113 }
114
115 /**
116 * Get the ingredients map.
117 *
118 * @return The mapping of character to ingredients.
119 */
120 public HashMap<Character, MaterialData> getIngredientMap() {
121 return ingredients;
122 }
123
124 /**
125 * Get the shape.
126 *
127 * @return The recipe's shape.
128 */
129 public String[] getShape() {
130 return rows;
131 }
132
133 /**
134 * Get the result.
135 *
136 * @return The result stack.
137 */
138 public ItemStack getResult() {
139 return output;
140 }
141}