Commits

Parker Hawke authored and md_5 committed ccbf091556d
SPIGOT-7770: Reserve spaces in shaped recipes for blank ingredients
No tags

src/main/java/org/bukkit/inventory/ShapedRecipe.java

Modified
47 47 * @see ShapedRecipe#setIngredient(char, Material, int)
48 48 * @see ShapedRecipe#setIngredient(char, MaterialData)
49 49 * @see ShapedRecipe#setIngredient(char, RecipeChoice)
50 50 */
51 51 public ShapedRecipe(@NotNull NamespacedKey key, @NotNull ItemStack result) {
52 52 super(key, result);
53 53 }
54 54
55 55 /**
56 56 * Set the shape of this recipe to the specified rows. Each character
57 - * represents a different ingredient; exactly what each character
58 - * represents is set separately. The first row supplied corresponds with
59 - * the upper most part of the recipe on the workbench e.g. if all three
60 - * rows are supplies the first string represents the top row on the
61 - * workbench.
57 + * represents a different ingredient; excluding space characters, which
58 + * must be empty, exactly what each character represents is set separately.
59 + * The first row supplied corresponds with the upper most part of the recipe
60 + * on the workbench e.g. if all three rows are supplies the first string
61 + * represents the top row on the workbench.
62 62 *
63 63 * @param shape The rows of the recipe (up to 3 rows).
64 64 * @return The changed recipe, so you can chain calls.
65 65 */
66 66 @NotNull
67 67 public ShapedRecipe shape(@NotNull final String... shape) {
68 68 Preconditions.checkArgument(shape != null, "Must provide a shape");
69 69 Preconditions.checkArgument(shape.length > 0 && shape.length < 4, "Crafting recipes should be 1, 2 or 3 rows, not ", shape.length);
70 70
71 71 int lastLen = -1;
77 77 lastLen = row.length();
78 78 }
79 79 this.rows = new String[shape.length];
80 80 for (int i = 0; i < shape.length; i++) {
81 81 this.rows[i] = shape[i];
82 82 }
83 83
84 84 // Remove character mappings for characters that no longer exist in the shape
85 85 HashMap<Character, RecipeChoice> newIngredients = new HashMap<>();
86 86 for (String row : shape) {
87 - for (Character c : row.toCharArray()) {
87 + for (char c : row.toCharArray()) {
88 + // SPIGOT-7770: Space in recipe shape must represent no ingredient
89 + if (c == ' ') {
90 + continue;
91 + }
92 +
88 93 newIngredients.put(c, ingredients.get(c));
89 94 }
90 95 }
91 96 this.ingredients = newIngredients;
92 97
93 98 return this;
94 99 }
95 100
96 101 /**
97 102 * Sets the material that a character in the recipe shape refers to.
98 103 * <p>
99 104 * Note that before an ingredient can be set, the recipe's shape must be defined
100 105 * with {@link #shape(String...)}.
101 106 *
102 107 * @param key The character that represents the ingredient in the shape.
103 108 * @param ingredient The ingredient.
104 109 * @return The changed recipe, so you can chain calls.
110 + * @throws IllegalArgumentException if the {@code key} is a space character
105 111 * @throws IllegalArgumentException if the {@code key} does not appear in the shape.
106 112 */
107 113 @NotNull
108 114 public ShapedRecipe setIngredient(char key, @NotNull MaterialData ingredient) {
109 115 return setIngredient(key, ingredient.getItemType(), ingredient.getData());
110 116 }
111 117
112 118 /**
113 119 * Sets the material that a character in the recipe shape refers to.
114 120 * <p>
115 121 * Note that before an ingredient can be set, the recipe's shape must be defined
116 122 * with {@link #shape(String...)}.
117 123 *
118 124 * @param key The character that represents the ingredient in the shape.
119 125 * @param ingredient The ingredient.
120 126 * @return The changed recipe, so you can chain calls.
127 + * @throws IllegalArgumentException if the {@code key} is a space character
121 128 * @throws IllegalArgumentException if the {@code key} does not appear in the shape.
122 129 */
123 130 @NotNull
124 131 public ShapedRecipe setIngredient(char key, @NotNull Material ingredient) {
125 132 return setIngredient(key, ingredient, 0);
126 133 }
127 134
128 135 /**
129 136 * Sets the material that a character in the recipe shape refers to.
130 137 * <p>
131 138 * Note that before an ingredient can be set, the recipe's shape must be defined
132 139 * with {@link #shape(String...)}.
133 140 *
134 141 * @param key The character that represents the ingredient in the shape.
135 142 * @param ingredient The ingredient.
136 143 * @param raw The raw material data as an integer.
137 144 * @return The changed recipe, so you can chain calls.
145 + * @throws IllegalArgumentException if the {@code key} is a space character
138 146 * @throws IllegalArgumentException if the {@code key} does not appear in the shape.
139 147 * @deprecated Magic value
140 148 */
141 149 @Deprecated
142 150 @NotNull
143 151 public ShapedRecipe setIngredient(char key, @NotNull Material ingredient, int raw) {
152 + Preconditions.checkArgument(key != ' ', "Space in recipe shape must represent no ingredient");
144 153 Preconditions.checkArgument(ingredients.containsKey(key), "Symbol does not appear in the shape:", key);
145 154
146 155 // -1 is the old wildcard, map to Short.MAX_VALUE as the new one
147 156 if (raw == -1) {
148 157 raw = Short.MAX_VALUE;
149 158 }
150 159
151 160 ingredients.put(key, new RecipeChoice.MaterialChoice(Collections.singletonList(ingredient)));
152 161 return this;
153 162 }
154 163
155 164 /**
156 165 * Sets the {@link RecipeChoice} that a character in the recipe shape refers to.
157 166 * <p>
158 167 * Note that before an ingredient can be set, the recipe's shape must be defined
159 168 * with {@link #shape(String...)}.
160 169 *
161 170 * @param key The character that represents the ingredient in the shape.
162 171 * @param ingredient The ingredient.
163 172 * @return The changed recipe, so you can chain calls.
173 + * @throws IllegalArgumentException if the {@code key} is a space character
164 174 * @throws IllegalArgumentException if the {@code key} does not appear in the shape.
165 175 */
166 176 @NotNull
167 177 public ShapedRecipe setIngredient(char key, @NotNull RecipeChoice ingredient) {
178 + Preconditions.checkArgument(key != ' ', "Space in recipe shape must represent no ingredient");
168 179 Preconditions.checkArgument(ingredients.containsKey(key), "Symbol does not appear in the shape:", key);
169 180
170 181 ingredients.put(key, ingredient);
171 182 return this;
172 183 }
173 184
174 185 /**
175 186 * Get a copy of the ingredients map.
176 187 *
177 188 * @return The mapping of character to ingredients.

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut