Advertisement
Guest User

CustomRecipeHandler.java

a guest
Jul 5th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.List;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.event.EventHandler;
  5. import org.bukkit.event.Listener;
  6. import org.bukkit.event.inventory.PrepareItemCraftEvent;
  7. import org.bukkit.inventory.CraftingInventory;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.bukkit.inventory.ShapedRecipe;
  10. import org.bukkit.inventory.ShapelessRecipe;
  11. import org.ourcraft.daskrr.smp.SMP;
  12. import org.ourcraft.daskrr.smp.inventory.InventoryUtils;
  13.  
  14. import com.google.common.collect.Lists;
  15.  
  16. public class CustomRecipeHandler
  17. {
  18.     public static List<CustomRecipe> RECIPES = Lists.newArrayList();
  19.    
  20.     public CustomRecipeHandler ()
  21.     {
  22.         register();
  23.     }
  24.    
  25.     private void register()
  26.     {
  27.         Listener l = new Listener ()
  28.         {
  29.             @EventHandler
  30.             public void craft (PrepareItemCraftEvent e)
  31.             {
  32.                 for (CustomRecipe recipe : RECIPES)
  33.                 {
  34.                     if (recipe instanceof CustomShapedRecipe)
  35.                     {
  36.                         CustomShapedRecipe shapedRecipe = (CustomShapedRecipe) recipe;
  37.                        
  38.                         CraftingInventory inv = e.getInventory();
  39.                         if (inv.getResult() == null)
  40.                             continue;
  41.                            
  42.                         if (!inv.getResult().equals(recipe.result))
  43.                             continue;
  44.                        
  45.                         matrixLoop: for (int i = 0; i < 9; i++)
  46.                             if (inv.getMatrix()[i] == null)
  47.                             {
  48.                                 if (shapedRecipe.getMatrix()[i] != null)
  49.                                 {
  50.                                     inv.setResult(null);
  51.                                     break matrixLoop;
  52.                                 }
  53.                             }
  54.                             else
  55.                             {
  56.                                 if (shapedRecipe.getMatrix() == null)
  57.                                 {
  58.                                     inv.setResult(null);
  59.                                     break matrixLoop;
  60.                                 }
  61.                                    
  62.                                 if (!InventoryUtils.compareItems(inv.getMatrix()[i], shapedRecipe.getMatrix()[i]))
  63.                                 {                                  
  64.                                     inv.setResult(null);
  65.                                     break matrixLoop;
  66.                                 }
  67.                             }
  68.                     }
  69.                     else if (recipe instanceof CustomShapelessRecipe)
  70.                     {
  71.                         CustomShapelessRecipe shapelessRecipe = (CustomShapelessRecipe) recipe;
  72.                        
  73.                         CraftingInventory inv = e.getInventory();
  74.                         if (inv.getResult() == null)
  75.                             continue;
  76.                            
  77.                         if (!inv.getResult().equals(recipe.result))
  78.                             continue;
  79.                        
  80.                         matrixLoop: for (ItemStack ing : shapelessRecipe.getIngredients())
  81.                         {
  82.                             boolean found = false;
  83.                            
  84.                             for (ItemStack in : inv.getContents())
  85.                                 if (InventoryUtils.compareItems(ing, in))
  86.                                     found = true;
  87.                                    
  88.                             if (!found)
  89.                             {
  90.                                 inv.setResult(null);
  91.                                 break matrixLoop;
  92.                             }
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         };
  98.        
  99.         Bukkit.getPluginManager().registerEvents(l, SMP.PLUGIN);
  100.     }
  101.    
  102.     public void addRecipe (CustomShapedRecipe recipe)
  103.     {
  104.         RECIPES.add(recipe);
  105.        
  106.         Bukkit.getServer().addRecipe(recipe.getBukkitRecipe());
  107.     }
  108.    
  109.     public void addRecipe (ShapedRecipe recipe)
  110.     {
  111.         RECIPES.add(new CustomShapedRecipe(recipe));
  112.        
  113.         Bukkit.getServer().addRecipe(recipe);
  114.     }
  115.    
  116.     public void addRecipe (ShapelessRecipe recipe)
  117.     {
  118.         RECIPES.add(new CustomShapelessRecipe(recipe));
  119.        
  120.         Bukkit.getServer().addRecipe(recipe);
  121.     }
  122.    
  123.     public void addRecipe (CustomShapelessRecipe recipe)
  124.     {
  125.         RECIPES.add(recipe);
  126.        
  127.         Bukkit.getServer().addRecipe(recipe.getBukkitRecipe());
  128.     }
  129.    
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement