Advertisement
MSWS

ItemStack from Recipe

Feb 27th, 2020
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1.  
  2.     public ItemStack getResultingItem(Material[] mats) {
  3.         List<ItemStack> convert = new ArrayList<ItemStack>();
  4.         for (Material m : mats)
  5.             convert.add(new ItemStack(m));
  6.         return getResultingItem(convert);
  7.     }
  8.  
  9.     private Map<List<ItemStack>, ItemStack> cache = new HashMap<List<ItemStack>, ItemStack>();
  10.     private List<Recipe> recipes;
  11.  
  12.     public ItemStack getResultingItem(List<ItemStack> items) {
  13.         if (cache.containsKey(items))
  14.             return cache.get(items);
  15.         if (recipes == null) {
  16.             recipes = new ArrayList<Recipe>();
  17.             Iterator<Recipe> it = Bukkit.recipeIterator();
  18.             while (it.hasNext()) {
  19.                 Recipe rec = it.next();
  20.                 recipes.add(rec);
  21.             }
  22.         }
  23.  
  24.         for (Recipe r : recipes) {
  25.             if (r instanceof ShapedRecipe) {
  26.                 if (matchesShaped((ShapedRecipe) r, items))
  27.                     return r.getResult();
  28.  
  29.             } else if (r instanceof ShapelessRecipe) {
  30.                 ShapelessRecipe less = (ShapelessRecipe) r;
  31.                 if (matchesShapeless(less.getChoiceList(), items))
  32.                     return r.getResult();
  33.             }
  34.         }
  35.  
  36.         return null;
  37.     }
  38.  
  39.     private boolean matchesShaped(ShapedRecipe shape, List<ItemStack> items) {
  40.         String[] map = shape.getShape();
  41.         Map<Character, RecipeChoice> choices = shape.getChoiceMap();
  42.         for (Entry<Character, RecipeChoice> entry : choices.entrySet()) {
  43.             if (entry.getValue() == null)
  44.                 continue;
  45.         }
  46.         int index = 0;
  47.  
  48.         boolean test = true;
  49.  
  50.         for (String s : map) {
  51.             if (!test)
  52.                 break;
  53.             for (Character c : s.toCharArray()) {
  54.                 RecipeChoice currentChoice = choices.get(c);
  55.                 if (currentChoice == null) {
  56.                     if (index < items.size() && items.get(index) != null
  57.                             && items.get(index).getType() != Material.AIR) {
  58.                         test = false;
  59.                         break;
  60.                     }
  61.                     index++;
  62.                     continue;
  63.                 }
  64.                 if (index >= items.size()) {
  65.                     test = false;
  66.                     break;
  67.                 }
  68.                 if (!currentChoice.test(items.get(index))) {
  69.                     test = false;
  70.                     break;
  71.                 }
  72.                 index++;
  73.             }
  74.         }
  75.  
  76.         return test;
  77.     }
  78.  
  79.     private boolean matchesShapeless(List<RecipeChoice> choice, List<ItemStack> items) {
  80.         items = new ArrayList<ItemStack>(items);
  81.         for (RecipeChoice c : choice) {
  82.             boolean match = false;
  83.             for (int i = 0; i < items.size(); i++) {
  84.                 ItemStack item = items.get(i);
  85.                 if (item == null || item.getType() == Material.AIR)
  86.                     continue;
  87.                 if (c.test(item)) {
  88.                     match = true;
  89.                     items.remove(i);
  90.                     break;
  91.                 }
  92.             }
  93.             if (!match)
  94.                 return false;
  95.         }
  96.         return true;
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement