66import java .util .HashMap ;
77import java .util .List ;
88import java .util .Map ;
9+ import java .util .logging .Logger ;
910import java .util .stream .Collectors ;
1011
12+ import org .bukkit .Bukkit ;
1113import org .bukkit .Material ;
1214import org .bukkit .NamespacedKey ;
1315import org .bukkit .configuration .ConfigurationSection ;
1719import org .bukkit .entity .Player ;
1820import org .bukkit .inventory .ItemFlag ;
1921import org .bukkit .inventory .ItemStack ;
22+ import org .bukkit .inventory .Recipe ;
23+ import org .bukkit .inventory .ShapedRecipe ;
24+ import org .bukkit .inventory .ShapelessRecipe ;
2025import org .bukkit .inventory .meta .Damageable ;
2126import org .bukkit .inventory .meta .ItemMeta ;
2227import org .bukkit .persistence .PersistentDataType ;
@@ -37,6 +42,7 @@ private ItemManager() {}
3742 private static final Map <String , BendingItem > NAME_CACHE = new HashMap <>();
3843 private static final Map <Integer , BendingItem > ID_CACHE = new HashMap <>();
3944 private static final Map <Player , BendingItem > EQUIPPED = new HashMap <>();
45+ private static final char [] CHARS = {'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' };
4046
4147 private static final String DISPLAY_PATH = "Display" ;
4248 private static final String LORE_PATH = "Lore" ;
@@ -48,6 +54,7 @@ private ItemManager() {}
4854 private static final String ENCHANTS_PATH = "Enchants" ;
4955 private static final String FLAGS_PATH = "Flags" ;
5056 private static final String USES_PATH = "Uses" ;
57+ private static final String RECIPE_PATH = "Recipe" ;
5158
5259 public static void equip (Player player , BendingItem item ) {
5360 EQUIPPED .put (player , item );
@@ -282,12 +289,83 @@ public static BendingItem register(File file) throws IllegalArgumentException {
282289 }
283290 }
284291
292+ if (config .contains (RECIPE_PATH )) {
293+ Recipe recipe = null ;
294+ try {
295+ recipe = loadRecipe (name , item , config );
296+ } catch (Exception e ) {
297+ e .printStackTrace ();
298+ recipe = null ;
299+ }
300+ Bukkit .addRecipe (recipe );
301+ }
302+
285303 BendingItem bItem = new BendingItem (name , item , usage , element , mods );
286304 NAME_CACHE .put (name , bItem );
287305 ID_CACHE .put (id , bItem );
288306 return bItem ;
289307 }
290308
309+ private static Recipe loadRecipe (String name , ItemStack item , FileConfiguration config ) {
310+ Logger logger = JavaPlugin .getPlugin (ItemsPlugin .class ).getLogger ();
311+ if (!config .contains (RECIPE_PATH + ".Ingredients" )) {
312+ logger .warning ("Recipe for '" + name + "' requires a list of ingredients under the config path 'Recipe.Ingredients'" );
313+ return null ;
314+ } else if (!config .contains (RECIPE_PATH + ".Shaped" )) {
315+ logger .warning ("Recipe for '" + name + "' requires a boolean (true / false) under the config path 'Recipe.Shaped'" );
316+ return null ;
317+ }
318+
319+ boolean shaped = config .getBoolean (RECIPE_PATH + ".Shaped" );
320+ List <String > ingredients = config .getStringList (RECIPE_PATH + ".Ingredients" );
321+
322+ if (ingredients == null ) {
323+ logger .warning ("Ingredients list for '" + name + "' recipe not found!" );
324+ return null ;
325+ } else if (ingredients .isEmpty ()) {
326+ logger .warning ("Ingredients list for '" + name + "' recipe cannot be empty!" );
327+ return null ;
328+ } else if (ingredients .size () > 9 ) {
329+ logger .warning ("Ingredients list for '" + name + "' recipe cannot be longer than 9 items!" );
330+ return null ;
331+ }
332+
333+ List <Material > mats = new ArrayList <>();
334+ for (String mat : ingredients ) {
335+ try {
336+ mats .add (Material .valueOf (mat .toUpperCase ()));
337+ } catch (Exception e ) {
338+ logger .warning ("Unable to parse material from '" + mat + "' in '" + name + "' recipe!" );
339+ return null ;
340+ }
341+ }
342+
343+ if (shaped ) {
344+ if (!config .contains (RECIPE_PATH + ".Shape" )) {
345+ logger .warning ("Recipe for '" + name + "' requires a shape under the config path 'Recipe.Shape'" );
346+ return null ;
347+ }
348+
349+ ShapedRecipe recipe = new ShapedRecipe (new NamespacedKey (JavaPlugin .getPlugin (ItemsPlugin .class ), name ), item );
350+
351+ recipe .shape (config .getStringList (RECIPE_PATH + ".Shape" ).toArray (new String [0 ]));
352+
353+ for (int i = 0 ; i < ingredients .size (); ++i ) {
354+ recipe .setIngredient (CHARS [i ], mats .get (i ));
355+ }
356+
357+ return recipe ;
358+ } else {
359+ ShapelessRecipe recipe = new ShapelessRecipe (new NamespacedKey (JavaPlugin .getPlugin (ItemsPlugin .class ), name ), item );
360+
361+ for (Material ingredient : mats ) {
362+ recipe .addIngredient (ingredient );
363+ }
364+
365+ return recipe ;
366+ }
367+ }
368+
291369 private static List <BendingModifier > loadMods (ConfigurationSection section ) {
292370 List <BendingModifier > mods = new ArrayList <>();
293371
0 commit comments