Advertisement
Guest User

CustomShapedRecipe.java

a guest
Jul 5th, 2019
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.Map.Entry;
  3.  
  4. import org.bukkit.Material;
  5. import org.bukkit.inventory.ItemStack;
  6. import org.bukkit.inventory.Recipe;
  7. import org.bukkit.inventory.ShapedRecipe;
  8. import org.ourcraft.daskrr.smp.inventory.items.handler.ItemModifier.MArmor;
  9. import org.ourcraft.daskrr.smp.inventory.items.handler.ItemModifier.MWeapon;
  10.  
  11. import com.google.common.collect.Maps;
  12.  
  13. public class CustomShapedRecipe extends CustomRecipe
  14. {
  15.     protected ItemStack result;
  16.     protected String[] shape = new String[3];
  17.     protected ItemStack[] matrix = new ItemStack[9];
  18.    
  19.     public CustomShapedRecipe (ItemStack result)
  20.     {
  21.         super(result);
  22.        
  23.         this.recipe = new ShapedRecipe (key(), result);
  24.     }
  25.    
  26.     public CustomShapedRecipe (ShapedRecipe recipe)
  27.     {
  28.         super(recipe.getResult());
  29.        
  30.         this.recipe = recipe;
  31.        
  32.         if (recipe.getShape().length > 0)
  33.             shape[0] = recipe.getShape()[0];
  34.         else
  35.             shape[0] = "   ";
  36.        
  37.         if (recipe.getShape().length > 1)
  38.             shape[1] = recipe.getShape()[1];
  39.         else
  40.             shape[1] = "   ";
  41.        
  42.         if (recipe.getShape().length > 2)
  43.             shape[2] = recipe.getShape()[2];
  44.         else
  45.             shape[2] = "   ";
  46.        
  47.         Map<Character, ItemStack> finalMap = Maps.newHashMap();
  48.        
  49.         for (Entry<Character, ItemStack> entry : recipe.getIngredientMap().entrySet())
  50.         {
  51.             if (entry.getValue() == null)
  52.                 continue;
  53.            
  54.             ItemStack ingredientFinal = new ItemStack(entry.getValue());
  55.             if (!entry.getValue().hasItemMeta())
  56.                 if (MWeapon.contains(entry.getValue().getType().toString()))
  57.                     ingredientFinal = MWeapon.convertDefaultItem(new ItemStack(entry.getValue()));
  58.                 else if (MArmor.contains(entry.getValue().getType().toString()))
  59.                     ingredientFinal = MArmor.convertDefaultItem(new ItemStack(entry.getValue()));
  60.                
  61.             finalMap.put(entry.getKey(), ingredientFinal);
  62.         }
  63.        
  64.         int row = 0;
  65.         for (String line : recipe.getShape())
  66.         {
  67.             int index = 0;
  68.             for (String ing : line.split("(?!^)"))
  69.             {
  70.                 if (!ing.equals(" "))
  71.                     matrix[row + index] = finalMap.get(ing.charAt(0));
  72.                
  73.                 index++;
  74.             }
  75.            
  76.             row += 3;
  77.         }
  78.     }
  79.  
  80.     public CustomShapedRecipe shape(String m0, String m1, String m2)
  81.     {
  82.         shape[0] = m0;
  83.         shape[1] = m1;
  84.         shape[2] = m2;
  85.        
  86.         ((ShapedRecipe) recipe).shape(m0, m1, m2);
  87.        
  88.         return this;
  89.     }
  90.    
  91.     public CustomShapedRecipe setIngredient (char key, ItemStack ingredient)
  92.     {
  93.         ((ShapedRecipe) recipe).setIngredient(key, ingredient.getType());
  94.        
  95.         int row = 0;
  96.         for (String line : shape)
  97.         {
  98.             int index = 0;
  99.             for (String item : line.split("(?!^)"))
  100.             {
  101.                 if (item.equals(String.valueOf(key)))
  102.                     matrix[row + index] = ingredient;
  103.                 index++;
  104.             }
  105.                
  106.             row += 3;
  107.         }
  108.        
  109.         return this;
  110.     }
  111.    
  112.     public CustomShapedRecipe setIngredient (char key, Material ingredient)
  113.     {
  114.         ((ShapedRecipe) recipe).setIngredient(key, ingredient);
  115.        
  116.         ItemStack ingredientFinal;
  117.         if (MWeapon.contains(ingredient.toString()))
  118.             ingredientFinal = MWeapon.convertDefaultItem(new ItemStack(ingredient));
  119.         else if (MArmor.contains(ingredient.toString()))
  120.             ingredientFinal = MArmor.convertDefaultItem(new ItemStack(ingredient));
  121.         else
  122.             ingredientFinal = new ItemStack(ingredient);
  123.        
  124.         int row = 0;
  125.         for (String line : shape)
  126.         {
  127.             int index = 0;
  128.             for (String item : line.split("(?!^)"))
  129.             {
  130.                 if (item.equals(String.valueOf(key)))
  131.                     matrix[row + index] = ingredientFinal;
  132.                 index++;
  133.             }
  134.                
  135.             row += 3;
  136.         }
  137.        
  138.         return this;
  139.     }
  140.    
  141.     public ItemStack[] getMatrix()
  142.     {
  143.         return this.matrix;
  144.     }
  145.    
  146.     @Override
  147.     public Recipe getBukkitRecipe ()
  148.     {
  149.         return recipe;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement