Skip to content

Commit 456ac01

Browse files
authored
Update CraftingRecipes to use NamespacedKey, and RecipeChoice instead of MaterialData (#65)
* Update CraftingRecipes to use NamespacedKey and RecipeChoice instead of MaterialData * Added moar tests, made functions return ShapedRecipe instead of Recipe when applicable * Fix Changelog and some docs * Refactor shaped() material overload * Fix changelog
1 parent 4f41842 commit 456ac01

File tree

3 files changed

+331
-84
lines changed

3 files changed

+331
-84
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ We follow semantic versioning: you can tell if a version contains breaking chang
66

77
Changes marked with :warning: are **breaking changes**.
88

9+
## QuartzLib 0.0.3
910

11+
_Published one day_
12+
13+
### Changed
14+
15+
#### `CraftingRecipes`
16+
- :warning: All recipes are now required to provide names, therefore all helper methods to generate recipes take a
17+
mandatory name argument. The name is automatically namespaced with the plugin's name.
18+
- :warning: All helpers that were consuming the now-deprecated `MaterialData` now consume the more flexible `RecipeChoice` instead.
19+
- All helpers that generate shaped recipes (2x2, 2x2 diagonal, etc.) now return `ShapedRecipe`s explicitely, since there
20+
is no way those recipes can be anything other than shaped, and hiding this detail is not useful at all.
1021

1122
## QuartzLib 0.0.2
1223

src/main/java/fr/zcraft/quartzlib/tools/items/CraftingRecipes.java

Lines changed: 105 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
import fr.zcraft.quartzlib.core.QuartzComponent;
3434
import fr.zcraft.quartzlib.core.QuartzLib;
3535
import java.util.ArrayList;
36+
import java.util.Arrays;
3637
import java.util.List;
3738
import org.bukkit.Material;
39+
import org.bukkit.NamespacedKey;
3840
import org.bukkit.inventory.ItemStack;
3941
import org.bukkit.inventory.Recipe;
42+
import org.bukkit.inventory.RecipeChoice;
4043
import org.bukkit.inventory.ShapedRecipe;
41-
import org.bukkit.material.MaterialData;
44+
import org.jetbrains.annotations.NotNull;
4245

4346
/**
4447
* This class provides various utilities for Crafting Recipes management.
@@ -115,78 +118,74 @@ public static void add(Iterable<Recipe> recipes) {
115118
* @param str The recipe string
116119
* @return The recipe string array
117120
*/
118-
public static String[] getRecipeShape(String str) {
121+
public static @NotNull String[] getRecipeShape(@NotNull String str) {
119122
if (str.length() > 9) {
120123
throw new IllegalArgumentException("Invalid recipe shape string");
121124
}
122125

123-
while (str.length() < 9) {
124-
str += " ";
126+
StringBuilder strBuilder = new StringBuilder(str);
127+
while (strBuilder.length() < 9) {
128+
strBuilder.append(" ");
125129
}
130+
str = strBuilder.toString();
126131

127132
return new String[] {
128-
str.substring(0, 2),
129-
str.substring(3, 5),
130-
str.substring(6, 8)
133+
str.substring(0, 3),
134+
str.substring(3, 6),
135+
str.substring(6, 9)
131136
};
132137
}
133138

134-
private static String[] generateRecipeShape(int count) {
135-
String shape = "";
139+
private static @NotNull String[] generateRecipeShape(int count) {
140+
StringBuilder shape = new StringBuilder();
136141
char key = 'A';
137-
for (; count-- > 0; ) {
138-
shape += key;
142+
while (count-- > 0) {
143+
shape.append(key);
139144
key = (char) (key + 1);
140145
}
141-
return getRecipeShape(shape);
146+
return getRecipeShape(shape.toString());
142147
}
143148

144149
/**
145150
* Generates a dummy shaped recipe, without any specified shape.
146151
* Ingredients are automatically indexed, using uppercase letters
147152
* (A = first ingredient, B = second ingredient, etc.)
148153
*
154+
* @param recipeName The name of the new recipe.
149155
* @param result The resulting item of the recipe.
150156
* @param materials The ingredients for the recipe
151157
* @return The dummy recipe.
152158
*/
153-
public static ShapedRecipe shaped(ItemStack result, Material... materials) {
154-
ShapedRecipe recipe = new ShapedRecipe(result)
155-
.shape(generateRecipeShape(materials.length));
156-
157-
if (materials.length > 9) {
158-
throw new IllegalArgumentException("Too many materials for a recipe.");
159-
}
160-
161-
char key = 'A';
162-
for (int i = 0; i < materials.length; ++i) {
163-
recipe.setIngredient(key, materials[i]);
164-
key = (char) (key + 1);
165-
}
159+
public static @NotNull ShapedRecipe shaped(String recipeName, ItemStack result, Material... materials) {
160+
RecipeChoice[] choices = Arrays.stream(materials)
161+
.map(RecipeChoice.MaterialChoice::new)
162+
.toArray(RecipeChoice[]::new);
166163

167-
return recipe;
164+
return shaped(recipeName, result, choices);
168165
}
169166

170167
/**
171168
* Generates a dummy shaped recipe, without any specified shape.
172169
* Ingredients are automatically indexed, using uppercase letters
173170
* (A = first ingredient, B = second ingredient, etc.)
174171
*
172+
* @param recipeName The name of the new recipe.
175173
* @param result The resulting item of the recipe.
176174
* @param materials The ingredients for the recipe
177175
* @return The dummy recipe.
178176
*/
179-
public static ShapedRecipe shaped(ItemStack result, MaterialData... materials) {
180-
ShapedRecipe recipe = new ShapedRecipe(result)
181-
.shape(generateRecipeShape(materials.length));
182-
177+
public static @NotNull ShapedRecipe shaped(String recipeName, ItemStack result, RecipeChoice... materials) {
183178
if (materials.length > 9) {
184-
throw new IllegalArgumentException("Too many materials for a recipe.");
179+
throw new IllegalArgumentException("Too many materials for a recipe (a maximum of 9 is expected).");
185180
}
186181

182+
NamespacedKey namespacedKey = new NamespacedKey(QuartzLib.getPlugin(), recipeName);
183+
ShapedRecipe recipe = new ShapedRecipe(namespacedKey, result)
184+
.shape(generateRecipeShape(materials.length));
185+
187186
char key = 'A';
188-
for (int i = 0; i < materials.length; ++i) {
189-
recipe.setIngredient(key, materials[i]);
187+
for (RecipeChoice material : materials) {
188+
recipe.setIngredient(key, material);
190189
key = (char) (key + 1);
191190
}
192191

@@ -198,16 +197,17 @@ public static ShapedRecipe shaped(ItemStack result, MaterialData... materials) {
198197
* Ingredients are automatically indexed, using uppercase letters
199198
* (A = first ingredient, B = second ingredient, etc.)
200199
*
200+
* @param recipeName The name of the new recipe.
201201
* @param result The resulting item of the recipe.
202202
* @param line1 The first line of the recipe
203203
* @param line2 The second line of the recipe
204204
* @param line3 The third line of the recipe
205205
* @param materials The ingredients for the recipe
206206
* @return The shaped recipe
207207
*/
208-
public static ShapedRecipe shaped(ItemStack result, String line1, String line2, String line3,
208+
public static ShapedRecipe shaped(String recipeName, ItemStack result, String line1, String line2, String line3,
209209
Material... materials) {
210-
ShapedRecipe recipe = shaped(result, materials);
210+
ShapedRecipe recipe = shaped(recipeName, result, materials);
211211
recipe.shape(line1, line2, line3);
212212
return recipe;
213213
}
@@ -217,31 +217,33 @@ public static ShapedRecipe shaped(ItemStack result, String line1, String line2,
217217
* Ingredients are automatically indexed, using uppercase letters
218218
* (A = first ingredient, B = second ingredient, etc.)
219219
*
220+
* @param recipeName The name of the new recipe.
220221
* @param result The resulting item of the recipe.
221222
* @param line1 The first line of the recipe
222223
* @param line2 The second line of the recipe
223224
* @param line3 The third line of the recipe
224225
* @return The shaped recipe
225226
*/
226-
public static ShapedRecipe shaped(ItemStack result, String line1, String line2, String line3) {
227-
return shaped(result, line1, line2, line3, new Material[] {});
227+
public static ShapedRecipe shaped(String recipeName, ItemStack result, String line1, String line2, String line3) {
228+
return shaped(recipeName, result, line1, line2, line3, new Material[] {});
228229
}
229230

230231
/**
231232
* Generates a new shaped recipe.
232233
* Ingredients are automatically indexed, using uppercase letters
233234
* (A = first ingredient, B = second ingredient, etc.)
234235
*
236+
* @param recipeName The name of the new recipe.
235237
* @param result The resulting item of the recipe.
236238
* @param line1 The first line of the recipe
237239
* @param line2 The second line of the recipe
238240
* @param line3 The third line of the recipe
239241
* @param materials The ingredients for the recipe
240242
* @return The shaped recipe
241243
*/
242-
public static ShapedRecipe shaped(ItemStack result, String line1, String line2, String line3,
243-
MaterialData... materials) {
244-
ShapedRecipe recipe = shaped(result, materials);
244+
public static ShapedRecipe shaped(String recipeName, ItemStack result, String line1, String line2, String line3,
245+
RecipeChoice... materials) {
246+
ShapedRecipe recipe = shaped(recipeName, result, materials);
245247
recipe.shape(line1, line2, line3);
246248
return recipe;
247249
}
@@ -252,21 +254,16 @@ public static ShapedRecipe shaped(ItemStack result, String line1, String line2,
252254
* Ingredients are automatically indexed, using uppercase letters
253255
* (A = first ingredient, B = second ingredient, etc.)
254256
*
255-
* @param other The recipe to retreive the ingredients and result from.
257+
* @param recipeName The name of the new recipe.
258+
* @param other The recipe to retrieve the ingredients and result from.
256259
* @param line1 The first line of the recipe
257260
* @param line2 The second line of the recipe
258261
* @param line3 The third line of the recipe
259262
* @return The shaped recipe
260263
*/
261-
public static ShapedRecipe shaped(ShapedRecipe other, String line1, String line2, String line3) {
262-
ShapedRecipe newRecipe = shaped(other.getResult(), line1, line2, line3);
263-
264-
for (char key : other.getIngredientMap().keySet()) {
265-
ItemStack ingredient = other.getIngredientMap().get(key);
266-
if (ingredient != null && ingredient.getType() != Material.AIR) {
267-
newRecipe.setIngredient(key, ingredient.getData());
268-
}
269-
}
264+
public static ShapedRecipe shaped(String recipeName, ShapedRecipe other, String line1, String line2, String line3) {
265+
ShapedRecipe newRecipe = shaped(recipeName, other.getResult(), line1, line2, line3);
266+
other.getChoiceMap().forEach(newRecipe::setIngredient);
270267

271268
return newRecipe;
272269
}
@@ -279,13 +276,23 @@ public static ShapedRecipe shaped(ShapedRecipe other, String line1, String line2
279276
* B A -
280277
* - - -
281278
*
279+
* @param baseRecipeName The base name of the new recipe.
280+
* Individual IDs will be appended to this name for each recipe.
282281
* @param a The first material of the recipe.
283282
* @param b The second material of the recipe.
284283
* @param result The resulting item of the recipe.
285284
* @return All the possible recipes.
286285
*/
287-
public static List<Recipe> get2x2DiagonalRecipes(Material a, Material b, ItemStack result) {
288-
return get2x2DiagonalRecipes(new MaterialData(a), new MaterialData(b), result);
286+
public static List<ShapedRecipe> get2x2DiagonalRecipes(
287+
String baseRecipeName,
288+
Material a,
289+
Material b,
290+
ItemStack result) {
291+
return get2x2DiagonalRecipes(
292+
baseRecipeName,
293+
new RecipeChoice.MaterialChoice(a),
294+
new RecipeChoice.MaterialChoice(b),
295+
result);
289296
}
290297

291298
/**
@@ -296,22 +303,28 @@ public static List<Recipe> get2x2DiagonalRecipes(Material a, Material b, ItemSta
296303
* B A -
297304
* - - -
298305
*
306+
* @param baseRecipeName The base name of the new recipe.
307+
* Individual IDs will be appended to this name for each recipe.
299308
* @param a The first material of the recipe.
300309
* @param b The second material of the recipe.
301310
* @param result The resulting item of the recipe.
302311
* @return All the possible recipes.
303312
*/
304-
public static List<Recipe> get2x2DiagonalRecipes(MaterialData a, MaterialData b, ItemStack result) {
305-
ArrayList<Recipe> recipes = new ArrayList<>();
306-
307-
recipes.add(shaped(result, "AB ", "BA ", " ", a, b));
308-
recipes.add(shaped(result, "BA ", "AB ", " ", a, b));
309-
recipes.add(shaped(result, " AB", " BA", " ", a, b));
310-
recipes.add(shaped(result, " BA", " AB", " ", a, b));
311-
recipes.add(shaped(result, " ", "AB ", "BA ", a, b));
312-
recipes.add(shaped(result, " ", "BA ", "AB ", a, b));
313-
recipes.add(shaped(result, " ", " AB", " BA", a, b));
314-
recipes.add(shaped(result, " ", " BA", " AB", a, b));
313+
public static List<ShapedRecipe> get2x2DiagonalRecipes(
314+
String baseRecipeName,
315+
RecipeChoice a,
316+
RecipeChoice b,
317+
ItemStack result) {
318+
ArrayList<ShapedRecipe> recipes = new ArrayList<>();
319+
320+
recipes.add(shaped(baseRecipeName + '1', result, "AB ", "BA ", " ", a, b));
321+
recipes.add(shaped(baseRecipeName + '2', result, "BA ", "AB ", " ", a, b));
322+
recipes.add(shaped(baseRecipeName + '3', result, " AB", " BA", " ", a, b));
323+
recipes.add(shaped(baseRecipeName + '4', result, " BA", " AB", " ", a, b));
324+
recipes.add(shaped(baseRecipeName + '5', result, " ", "AB ", "BA ", a, b));
325+
recipes.add(shaped(baseRecipeName + '6', result, " ", "BA ", "AB ", a, b));
326+
recipes.add(shaped(baseRecipeName + '7', result, " ", " AB", " BA", a, b));
327+
recipes.add(shaped(baseRecipeName + '8', result, " ", " BA", " AB", a, b));
315328

316329
return recipes;
317330
}
@@ -324,12 +337,14 @@ public static List<Recipe> get2x2DiagonalRecipes(MaterialData a, MaterialData b,
324337
* - A -
325338
* - - -
326339
*
340+
* @param baseRecipeName The base name of the new recipe.
341+
* Individual IDs will be appended to this name for each recipe.
327342
* @param a The material of the recipe.
328343
* @param result The resulting item of the recipe.
329344
* @return All the possible recipes.
330345
*/
331-
public static List<Recipe> get2x2DiagonalRecipes(Material a, ItemStack result) {
332-
return get2x2DiagonalRecipes(new MaterialData(a), result);
346+
public static List<ShapedRecipe> get2x2DiagonalRecipes(String baseRecipeName, Material a, ItemStack result) {
347+
return get2x2DiagonalRecipes(baseRecipeName, new RecipeChoice.MaterialChoice(a), result);
333348
}
334349

335350
/**
@@ -340,21 +355,23 @@ public static List<Recipe> get2x2DiagonalRecipes(Material a, ItemStack result) {
340355
* - A -
341356
* - - -
342357
*
358+
* @param baseRecipeName The base name of the new recipe.
359+
* Individual IDs will be appended to this name for each recipe.
343360
* @param a The material of the recipe.
344361
* @param result The resulting item of the recipe.
345362
* @return All the possible recipes.
346363
*/
347-
public static List<Recipe> get2x2DiagonalRecipes(MaterialData a, ItemStack result) {
348-
ArrayList<Recipe> recipes = new ArrayList<>();
349-
350-
recipes.add(shaped(result, "A ", " A ", " ", a));
351-
recipes.add(shaped(result, " A ", "A ", " ", a));
352-
recipes.add(shaped(result, " A ", " A", " ", a));
353-
recipes.add(shaped(result, " A", " A ", " ", a));
354-
recipes.add(shaped(result, " ", "A ", " A ", a));
355-
recipes.add(shaped(result, " ", " A ", "A ", a));
356-
recipes.add(shaped(result, " ", " A ", " A", a));
357-
recipes.add(shaped(result, " ", " A", " A ", a));
364+
public static List<ShapedRecipe> get2x2DiagonalRecipes(String baseRecipeName, RecipeChoice a, ItemStack result) {
365+
ArrayList<ShapedRecipe> recipes = new ArrayList<>();
366+
367+
recipes.add(shaped(baseRecipeName + '1', result, "A ", " A ", " ", a));
368+
recipes.add(shaped(baseRecipeName + '2', result, " A ", "A ", " ", a));
369+
recipes.add(shaped(baseRecipeName + '3', result, " A ", " A", " ", a));
370+
recipes.add(shaped(baseRecipeName + '4', result, " A", " A ", " ", a));
371+
recipes.add(shaped(baseRecipeName + '5', result, " ", "A ", " A ", a));
372+
recipes.add(shaped(baseRecipeName + '6', result, " ", " A ", "A ", a));
373+
recipes.add(shaped(baseRecipeName + '7', result, " ", " A ", " A", a));
374+
recipes.add(shaped(baseRecipeName + '8', result, " ", " A", " A ", a));
358375

359376
return recipes;
360377
}
@@ -367,12 +384,14 @@ public static List<Recipe> get2x2DiagonalRecipes(MaterialData a, ItemStack resul
367384
* A A -
368385
* - - -
369386
*
387+
* @param baseRecipeName The base name of the new recipe.
388+
* Individual IDs will be appended to this name for each recipe.
370389
* @param a The material of the recipe.
371390
* @param result The resulting item of the recipe.
372391
* @return All the possible recipes.
373392
*/
374-
public static List<Recipe> get2x2Recipes(Material a, ItemStack result) {
375-
return get2x2Recipes(new MaterialData(a), result);
393+
public static List<ShapedRecipe> get2x2Recipes(String baseRecipeName, Material a, ItemStack result) {
394+
return get2x2Recipes(baseRecipeName, new RecipeChoice.MaterialChoice(a), result);
376395
}
377396

378397
/**
@@ -383,17 +402,19 @@ public static List<Recipe> get2x2Recipes(Material a, ItemStack result) {
383402
* A A -
384403
* - - -
385404
*
405+
* @param baseRecipeName The base name of the new recipe.
406+
* Individual IDs will be appended to this name for each recipe.
386407
* @param a The material of the recipe.
387408
* @param result The resulting item of the recipe.
388409
* @return All the possible recipes.
389410
*/
390-
public static List<Recipe> get2x2Recipes(MaterialData a, ItemStack result) {
391-
ArrayList<Recipe> recipes = new ArrayList<>();
411+
public static List<ShapedRecipe> get2x2Recipes(String baseRecipeName, RecipeChoice a, ItemStack result) {
412+
ArrayList<ShapedRecipe> recipes = new ArrayList<>();
392413

393-
recipes.add(shaped(result, "AA ", "AA ", " ", a));
394-
recipes.add(shaped(result, " AA", " AA", " ", a));
395-
recipes.add(shaped(result, " ", "AA ", "AA ", a));
396-
recipes.add(shaped(result, " ", " AA", " AA", a));
414+
recipes.add(shaped(baseRecipeName + '1', result, "AA ", "AA ", " ", a));
415+
recipes.add(shaped(baseRecipeName + '2', result, " AA", " AA", " ", a));
416+
recipes.add(shaped(baseRecipeName + '3', result, " ", "AA ", "AA ", a));
417+
recipes.add(shaped(baseRecipeName + '4', result, " ", " AA", " AA", a));
397418

398419
return recipes;
399420
}

0 commit comments

Comments
 (0)