11package com .sk89q .craftbook .util ;
22
33import com .sk89q .craftbook .bukkit .CraftBookPlugin ;
4+
5+ import org .bukkit .Bukkit ;
46import org .bukkit .ChatColor ;
57import org .bukkit .DyeColor ;
68import org .bukkit .Material ;
7- import org .bukkit .Tag ;
89import org .bukkit .TreeSpecies ;
910import org .bukkit .block .Block ;
1011import org .bukkit .enchantments .Enchantment ;
1112import org .bukkit .entity .Entity ;
1213import org .bukkit .entity .Item ;
1314import org .bukkit .entity .Player ;
15+ import org .bukkit .inventory .BlastingRecipe ;
16+ import org .bukkit .inventory .CookingRecipe ;
17+ import org .bukkit .inventory .FurnaceRecipe ;
1418import org .bukkit .inventory .Inventory ;
1519import org .bukkit .inventory .ItemStack ;
1620import org .bukkit .inventory .Recipe ;
2226import org .bukkit .inventory .meta .ItemMeta ;
2327
2428import java .util .ArrayList ;
29+ import java .util .HashMap ;
2530import java .util .HashSet ;
31+ import java .util .Iterator ;
2632import java .util .List ;
2733import java .util .Map ;
2834import java .util .Map .Entry ;
2935import java .util .Set ;
3036import java .util .regex .Pattern ;
3137
3238public final class ItemUtil {
39+ private static Map <Material , ItemStack > smeltableItems ;
40+ private static Map <Material , ItemStack > cookableItems ;
41+ private static Map <Material , ItemStack > blastSmeltableItems ;
42+
43+ /**
44+ * Precompute lists of all recipes available for furnace, blast_furnace and smoker/campfire
45+ *
46+ */
47+ public static void setupSmeltableItemLists () {
48+
49+ smeltableItems = new HashMap <>();
50+ cookableItems = new HashMap <>();
51+ blastSmeltableItems = new HashMap <>();
52+
53+ Iterator <Recipe > it = Bukkit .recipeIterator ();
54+ while (it .hasNext ()) {
55+ Recipe recipe = it .next ();
56+
57+ if (recipe instanceof FurnaceRecipe ) {
58+ smeltableItems .put (((FurnaceRecipe )recipe ).getInput ().getType (), ((FurnaceRecipe )recipe ).getResult ());
59+ }
60+ else if (recipe instanceof BlastingRecipe ) {
61+ blastSmeltableItems .put (((BlastingRecipe )recipe ).getInput ().getType (), ((BlastingRecipe )recipe ).getResult ());
62+ }
63+ else if (recipe instanceof CookingRecipe ) {
64+ cookableItems .put (((CookingRecipe )recipe ).getInput ().getType (), ((CookingRecipe )recipe ).getResult ());
65+ }
66+ }
67+ }
3368
3469 /**
3570 * Add an itemstack to an existing itemstack.
@@ -466,29 +501,11 @@ public static boolean isCookable(ItemStack item) {
466501
467502 public static ItemStack getCookedResult (ItemStack item ) {
468503
469- switch (item .getType ()) {
470- case BEEF :
471- return new ItemStack (Material .COOKED_BEEF );
472- case CHICKEN :
473- return new ItemStack (Material .COOKED_CHICKEN );
474- case COD :
475- return new ItemStack (Material .COOKED_COD );
476- case SALMON :
477- return new ItemStack (Material .COOKED_SALMON );
478- case PORKCHOP :
479- return new ItemStack (Material .COOKED_PORKCHOP );
480- case POTATO :
481- return new ItemStack (Material .BAKED_POTATO );
482- case MUTTON :
483- return new ItemStack (Material .COOKED_MUTTON );
484- case RABBIT :
485- return new ItemStack (Material .COOKED_RABBIT );
486- case CHORUS_FRUIT :
487- return new ItemStack (Material .POPPED_CHORUS_FRUIT );
488- case KELP :
489- return new ItemStack (Material .DRIED_KELP );
490- default :
491- return null ;
504+ if (cookableItems .containsKey (item .getType ())) {
505+ return new ItemStack (cookableItems .get (item .getType ()));
506+ }
507+ else {
508+ return null ;
492509 }
493510 }
494511
@@ -499,142 +516,26 @@ public static boolean isSmeltable(ItemStack item) {
499516
500517 public static ItemStack getSmeltedResult (ItemStack item ) {
501518
502- switch (item .getType ()) {
503- case COBBLESTONE :
504- return new ItemStack (Material .STONE );
505- case COBBLED_DEEPSLATE :
506- return new ItemStack (Material .DEEPSLATE );
507- case DEEPSLATE_BRICKS :
508- return new ItemStack (Material .CRACKED_DEEPSLATE_BRICKS );
509- case DEEPSLATE_TILES :
510- return new ItemStack (Material .CRACKED_DEEPSLATE_TILES );
511- case POLISHED_BLACKSTONE_BRICKS :
512- return new ItemStack (Material .CRACKED_POLISHED_BLACKSTONE_BRICKS );
513- case BASALT :
514- return new ItemStack (Material .SMOOTH_BASALT );
515- case CACTUS :
516- return new ItemStack (Material .GREEN_DYE );
517- case IRON_ORE :
518- case RAW_IRON :
519- case DEEPSLATE_IRON_ORE :
520- return new ItemStack (Material .IRON_INGOT );
521- case COAL_ORE :
522- return new ItemStack (Material .COAL );
523- case LAPIS_ORE :
524- case DEEPSLATE_LAPIS_ORE :
525- return new ItemStack (Material .LAPIS_LAZULI );
526- case REDSTONE_ORE :
527- case DEEPSLATE_REDSTONE_ORE :
528- return new ItemStack (Material .REDSTONE , 4 );
529- case EMERALD_ORE :
530- case DEEPSLATE_EMERALD_ORE :
531- return new ItemStack (Material .EMERALD );
532- case GOLD_ORE :
533- case NETHER_GOLD_ORE :
534- case RAW_GOLD :
535- case DEEPSLATE_GOLD_ORE :
536- return new ItemStack (Material .GOLD_INGOT );
537- case ANCIENT_DEBRIS :
538- return new ItemStack (Material .NETHERITE_SCRAP );
539- case COPPER_ORE :
540- case RAW_COPPER :
541- case DEEPSLATE_COPPER_ORE :
542- return new ItemStack (Material .COPPER_INGOT );
543- case DIAMOND_ORE :
544- case DEEPSLATE_DIAMOND_ORE :
545- return new ItemStack (Material .DIAMOND );
546- case SAND :
547- case RED_SAND :
548- return new ItemStack (Material .GLASS );
549- case CLAY_BALL :
550- return new ItemStack (Material .BRICK );
551- case NETHERRACK :
552- return new ItemStack (Material .NETHER_BRICK );
553- case CLAY :
554- return new ItemStack (Material .TERRACOTTA );
555- case NETHER_QUARTZ_ORE :
556- return new ItemStack (Material .QUARTZ );
557- case STONE_BRICKS :
558- return new ItemStack (Material .CRACKED_STONE_BRICKS );
559- case NETHER_BRICKS :
560- return new ItemStack (Material .CRACKED_NETHER_BRICKS );
561- case WET_SPONGE :
562- return new ItemStack (Material .SPONGE );
563- case WHITE_TERRACOTTA :
564- return new ItemStack (Material .WHITE_GLAZED_TERRACOTTA );
565- case ORANGE_TERRACOTTA :
566- return new ItemStack (Material .ORANGE_GLAZED_TERRACOTTA );
567- case MAGENTA_TERRACOTTA :
568- return new ItemStack (Material .MAGENTA_GLAZED_TERRACOTTA );
569- case LIGHT_BLUE_TERRACOTTA :
570- return new ItemStack (Material .LIGHT_BLUE_GLAZED_TERRACOTTA );
571- case YELLOW_TERRACOTTA :
572- return new ItemStack (Material .YELLOW_GLAZED_TERRACOTTA );
573- case LIME_TERRACOTTA :
574- return new ItemStack (Material .LIME_GLAZED_TERRACOTTA );
575- case PINK_TERRACOTTA :
576- return new ItemStack (Material .PINK_GLAZED_TERRACOTTA );
577- case GRAY_TERRACOTTA :
578- return new ItemStack (Material .GRAY_GLAZED_TERRACOTTA );
579- case LIGHT_GRAY_TERRACOTTA :
580- return new ItemStack (Material .LIGHT_GRAY_GLAZED_TERRACOTTA );
581- case CYAN_TERRACOTTA :
582- return new ItemStack (Material .CYAN_GLAZED_TERRACOTTA );
583- case PURPLE_TERRACOTTA :
584- return new ItemStack (Material .PURPLE_GLAZED_TERRACOTTA );
585- case BLUE_TERRACOTTA :
586- return new ItemStack (Material .BLUE_GLAZED_TERRACOTTA );
587- case BROWN_TERRACOTTA :
588- return new ItemStack (Material .BROWN_GLAZED_TERRACOTTA );
589- case GREEN_TERRACOTTA :
590- return new ItemStack (Material .GREEN_GLAZED_TERRACOTTA );
591- case RED_TERRACOTTA :
592- return new ItemStack (Material .RED_GLAZED_TERRACOTTA );
593- case BLACK_TERRACOTTA :
594- return new ItemStack (Material .BLACK_GLAZED_TERRACOTTA );
595- case IRON_SWORD :
596- case IRON_PICKAXE :
597- case IRON_AXE :
598- case IRON_SHOVEL :
599- case IRON_HOE :
600- case CHAINMAIL_HELMET :
601- case CHAINMAIL_CHESTPLATE :
602- case CHAINMAIL_LEGGINGS :
603- case CHAINMAIL_BOOTS :
604- case IRON_HELMET :
605- case IRON_CHESTPLATE :
606- case IRON_LEGGINGS :
607- case IRON_BOOTS :
608- case IRON_HORSE_ARMOR :
609- return new ItemStack (Material .IRON_NUGGET );
610- case GOLDEN_SWORD :
611- case GOLDEN_PICKAXE :
612- case GOLDEN_AXE :
613- case GOLDEN_SHOVEL :
614- case GOLDEN_HOE :
615- case GOLDEN_HELMET :
616- case GOLDEN_CHESTPLATE :
617- case GOLDEN_LEGGINGS :
618- case GOLDEN_BOOTS :
619- case GOLDEN_HORSE_ARMOR :
620- return new ItemStack (Material .GOLD_NUGGET );
621- case STONE :
622- return new ItemStack (Material .SMOOTH_STONE );
623- case QUARTZ_BLOCK :
624- return new ItemStack (Material .SMOOTH_QUARTZ );
625- case SANDSTONE :
626- return new ItemStack (Material .SMOOTH_SANDSTONE );
627- case RED_SANDSTONE :
628- return new ItemStack (Material .SMOOTH_RED_SANDSTONE );
629- case CHORUS_FRUIT :
630- return new ItemStack (Material .POPPED_CHORUS_FRUIT );
631- case SEA_PICKLE :
632- return new ItemStack (Material .LIME_DYE );
633- default :
634- if (Tag .LOGS .isTagged (item .getType ())) {
635- return new ItemStack (Material .CHARCOAL );
636- }
637- return null ;
519+ if (smeltableItems .containsKey (item .getType ())) {
520+ return new ItemStack (smeltableItems .get (item .getType ()));
521+ }
522+ else {
523+ return null ;
524+ }
525+ }
526+
527+ public static boolean isBlastSmeltable (ItemStack item ) {
528+
529+ return getBlastSmeltedResult (item ) != null ;
530+ }
531+
532+ public static ItemStack getBlastSmeltedResult (ItemStack item ) {
533+
534+ if (blastSmeltableItems .containsKey (item .getType ())) {
535+ return new ItemStack (blastSmeltableItems .get (item .getType ()));
536+ }
537+ else {
538+ return null ;
638539 }
639540 }
640541
@@ -700,69 +601,7 @@ public static Material getBoatFromTree(TreeSpecies treeSpecies) {
700601 */
701602 public static boolean isAFuel (ItemStack item ) {
702603
703- switch (item .getType ()) {
704- case COAL :
705- case CHARCOAL :
706- case COAL_BLOCK :
707- case WOODEN_AXE :
708- case WOODEN_HOE :
709- case WOODEN_PICKAXE :
710- case WOODEN_SHOVEL :
711- case WOODEN_SWORD :
712- case STICK :
713- case OAK_FENCE :
714- case OAK_FENCE_GATE :
715- case OAK_TRAPDOOR :
716- case ACACIA_TRAPDOOR :
717- case BIRCH_TRAPDOOR :
718- case DARK_OAK_TRAPDOOR :
719- case JUNGLE_TRAPDOOR :
720- case SPRUCE_TRAPDOOR :
721- case CRAFTING_TABLE :
722- case CHEST :
723- case TRAPPED_CHEST :
724- case JUKEBOX :
725- case NOTE_BLOCK :
726- case BROWN_MUSHROOM_BLOCK :
727- case RED_MUSHROOM_BLOCK :
728- case BLAZE_ROD :
729- case LAVA_BUCKET :
730- case BOOKSHELF :
731- case ACACIA_STAIRS :
732- case DARK_OAK_STAIRS :
733- case SPRUCE_FENCE :
734- case JUNGLE_FENCE :
735- case BIRCH_FENCE :
736- case ACACIA_FENCE :
737- case DARK_OAK_FENCE :
738- case SPRUCE_FENCE_GATE :
739- case JUNGLE_FENCE_GATE :
740- case BIRCH_FENCE_GATE :
741- case ACACIA_FENCE_GATE :
742- case DARK_OAK_FENCE_GATE :
743- case FISHING_ROD :
744- case BOW :
745- case LADDER :
746- case SCAFFOLDING :
747- case DRIED_KELP_BLOCK :
748- case BAMBOO :
749- return true ;
750- default :
751- return Tag .ITEMS_BOATS .isTagged (item .getType ())
752- || Tag .WOODEN_DOORS .isTagged (item .getType ())
753- || Tag .WOOL_CARPETS .isTagged (item .getType ())
754- || Tag .WOODEN_BUTTONS .isTagged (item .getType ())
755- || Tag .ITEMS_BANNERS .isTagged (item .getType ())
756- || Tag .LOGS .isTagged (item .getType ())
757- || Tag .LEAVES .isTagged (item .getType ())
758- || Tag .PLANKS .isTagged (item .getType ())
759- || Tag .WOODEN_STAIRS .isTagged (item .getType ())
760- || Tag .WOODEN_SLABS .isTagged (item .getType ())
761- || Tag .SAPLINGS .isTagged (item .getType ())
762- || Tag .WOOL .isTagged (item .getType ())
763- || Tag .WOODEN_PRESSURE_PLATES .isTagged (item .getType ())
764- || Tag .SIGNS .isTagged (item .getType ());
765- }
604+ return item .getType ().isFuel ();
766605 }
767606
768607 /**
@@ -856,7 +695,7 @@ public static List<ItemStack> getRawMinerals(Inventory inv) {
856695 public static boolean containsRawMaterials (Inventory inv ) {
857696
858697 for (ItemStack it : inv .getContents ()) {
859- if (isStackValid (it ) && isFurnacable (it ))
698+ if (isStackValid (it ) && isFurnaceable (it ))
860699 return true ;
861700 }
862701 return false ;
@@ -867,15 +706,15 @@ public static List<ItemStack> getRawMaterials(Inventory inv) {
867706 List <ItemStack > ret = new ArrayList <>();
868707
869708 for (ItemStack it : inv .getContents ()) {
870- if (isStackValid (it ) && isFurnacable (it ))
709+ if (isStackValid (it ) && isFurnaceable (it ))
871710 ret .add (it );
872711 }
873712 return ret ;
874713 }
875714
876- public static boolean isFurnacable (ItemStack item ) {
715+ public static boolean isFurnaceable (ItemStack item ) {
877716
878- return isCookable (item ) || isSmeltable (item );
717+ return isSmeltable ( item ) || isCookable (item ) || isBlastSmeltable (item );
879718 }
880719
881720 public static boolean isItemEdible (ItemStack item ) {
0 commit comments