Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.inventory.PrepareItemCraftEvent;
- import org.bukkit.inventory.CraftingInventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.ShapedRecipe;
- import org.bukkit.inventory.ShapelessRecipe;
- import org.ourcraft.daskrr.smp.SMP;
- import org.ourcraft.daskrr.smp.inventory.InventoryUtils;
- import com.google.common.collect.Lists;
- public class CustomRecipeHandler
- {
- public static List<CustomRecipe> RECIPES = Lists.newArrayList();
- public CustomRecipeHandler ()
- {
- register();
- }
- private void register()
- {
- Listener l = new Listener ()
- {
- @EventHandler
- public void craft (PrepareItemCraftEvent e)
- {
- for (CustomRecipe recipe : RECIPES)
- {
- if (recipe instanceof CustomShapedRecipe)
- {
- CustomShapedRecipe shapedRecipe = (CustomShapedRecipe) recipe;
- CraftingInventory inv = e.getInventory();
- if (inv.getResult() == null)
- continue;
- if (!inv.getResult().equals(recipe.result))
- continue;
- matrixLoop: for (int i = 0; i < 9; i++)
- if (inv.getMatrix()[i] == null)
- {
- if (shapedRecipe.getMatrix()[i] != null)
- {
- inv.setResult(null);
- break matrixLoop;
- }
- }
- else
- {
- if (shapedRecipe.getMatrix() == null)
- {
- inv.setResult(null);
- break matrixLoop;
- }
- if (!InventoryUtils.compareItems(inv.getMatrix()[i], shapedRecipe.getMatrix()[i]))
- {
- inv.setResult(null);
- break matrixLoop;
- }
- }
- }
- else if (recipe instanceof CustomShapelessRecipe)
- {
- CustomShapelessRecipe shapelessRecipe = (CustomShapelessRecipe) recipe;
- CraftingInventory inv = e.getInventory();
- if (inv.getResult() == null)
- continue;
- if (!inv.getResult().equals(recipe.result))
- continue;
- matrixLoop: for (ItemStack ing : shapelessRecipe.getIngredients())
- {
- boolean found = false;
- for (ItemStack in : inv.getContents())
- if (InventoryUtils.compareItems(ing, in))
- found = true;
- if (!found)
- {
- inv.setResult(null);
- break matrixLoop;
- }
- }
- }
- }
- }
- };
- Bukkit.getPluginManager().registerEvents(l, SMP.PLUGIN);
- }
- public void addRecipe (CustomShapedRecipe recipe)
- {
- RECIPES.add(recipe);
- Bukkit.getServer().addRecipe(recipe.getBukkitRecipe());
- }
- public void addRecipe (ShapedRecipe recipe)
- {
- RECIPES.add(new CustomShapedRecipe(recipe));
- Bukkit.getServer().addRecipe(recipe);
- }
- public void addRecipe (ShapelessRecipe recipe)
- {
- RECIPES.add(new CustomShapelessRecipe(recipe));
- Bukkit.getServer().addRecipe(recipe);
- }
- public void addRecipe (CustomShapelessRecipe recipe)
- {
- RECIPES.add(recipe);
- Bukkit.getServer().addRecipe(recipe.getBukkitRecipe());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement