/*
 * RecipeFeature.java
 *
 * Copyright (c) 2014 Graham Howden <graham_howden1 at yahoo.co.uk>.
 *
 * This file is part of UltraHardcore.
 *
 * UltraHardcore is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * UltraHardcore is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with UltraHardcore.  If not, see <http ://www.gnu.org/licenses/>.
 */

package com.publicuhc.ultrahardcore.pluginfeatures.uberapples;

import com.publicuhc.pluginframework.configuration.Configurator;
import com.publicuhc.pluginframework.shaded.inject.Inject;
import com.publicuhc.pluginframework.shaded.inject.Singleton;
import com.publicuhc.pluginframework.translate.Translate;
import com.publicuhc.ultrahardcore.pluginfeatures.UHCFeature;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.permissions.Permissible;
import org.bukkit.plugin.Plugin;

import java.util.Collection;


/**
 * RecipeHandler
 * Handles the alternative recipies for the regen items
 *
 * @author ghowden
 */
@Singleton
public class UberApples extends UHCFeature {

    public static final String RECIPE_BASE = BASE_PERMISSION + "recipies.";
    public static final String ALLOW_NOTCH_APPLE = RECIPE_BASE + "allowNotchApple";

    /**
     * Disables uber apples, normal when disabled
     * @param plugin the plugin
     * @param configManager the config manager
     * @param translate the translator
     */
    @Inject
    private UberApples(Plugin plugin, Configurator configManager, Translate translate) {
        super(plugin, configManager, translate);
    }

    /**
     * Whenever an item is about to pop up on the crafting table
     * @param e the prepareitemcraftevent
     */
    @EventHandler
    public void onPrepareCraftItemEvent(PrepareItemCraftEvent e) {
        //if we are enabled
        if (isEnabled()) {
            //check if no permission to create
            if(!isRecipeAllowedForPermissible(e.getView().getPlayer(),e.getRecipe())){
                //set to air if not allowed
                e.getInventory().setResult(new ItemStack(Material.AIR));
            }
        }
    }

    /**
     * Is the recipe allowed?
     * @param permissible Permissible the permissible to check against
     * @param recipe the recipe to check
     * @return boolean true if allowed, false if not
     */
    private static boolean isRecipeAllowedForPermissible(Permissible permissible, Recipe recipe){
        Material result = recipe.getResult().getType();
        return result != Material.GOLDEN_APPLE || !hasRecipeGotMaterial(recipe, Material.GOLD_BLOCK) || permissible.hasPermission(ALLOW_NOTCH_APPLE);
    }

    /**
     * Check if the recipe has the given material in it
     * @param r the recipe to check
     * @param mat the material to look for
     * @return true if found, false if not
     */
    private static boolean hasRecipeGotMaterial(Recipe r, Material mat) {
        Collection<ItemStack> ingredients = null;
        //noinspection ChainOfInstanceofChecks
        if (r instanceof ShapedRecipe) {
            ingredients = ((ShapedRecipe) r).getIngredientMap().values();
        }else if (r instanceof ShapelessRecipe) {
            ingredients = ((ShapelessRecipe) r).getIngredientList();
        }
        return null != ingredients && isMaterialInList(ingredients, mat);
    }

    /**
     * Checks if the material is in the lsit
     * @param itemStackList the list to check
     * @param mat the material to look for
     * @return true if found, false if not
     */
    private static boolean isMaterialInList(Iterable<ItemStack> itemStackList, Material mat){
        for (ItemStack itemStack : itemStackList) {
            if (itemStack.getType() == mat) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String getFeatureID() {
        return "DisableUberApples";
    }

    @Override
    public String getDescription() {
        return "Disables crafting uber apples when enabled";
    }
}

@@ -895,213 +895,31 @@
 ork.
-configuration.Configurator;%0Aimport com.publicuhc.pluginframework.shaded.inject.Inject;%0Aimport com.publicuhc.pluginframework.shaded.inject.Singleton;%0Aimport com.publicuhc.pluginframework.translate.Translate
+shaded.inject.Singleton
 ;%0Aim
@@ -943,38 +943,32 @@
 c.ultrahardcore.
-plugin
 features.UHCFeat
@@ -1303,41 +1303,8 @@
 ble;
-%0Aimport org.bukkit.plugin.Plugin;
 %0A%0Aim
@@ -1343,99 +1343,83 @@
 %0A * 
-RecipeHandler%0A * Handles the alternative recipies for the regen items%0A *%0A * @author ghowden
+UberApples%0A *%0A * Enabled: Disables uber apple crafting%0A * Disabled: Nothing
 %0A */
@@ -1511,601 +1511,60 @@
 ing 
-RECIPE_BASE = BASE_PERMISSION + %22recipies.%22;%0A    public static final String ALLOW_NOTCH_APPLE = RECIPE_BASE + %22allowNotchApple%22;%0A%0A    /**%0A     * Disables uber apples, normal when disabled%0A     * @param plugin the plugin%0A     * @param configManager the config manager%0A     * @param translate the translator%0A     */%0A    @Inject%0A    private UberApples(Plugin plugin, Configurator configManager, Translate translate) %7B%0A        super(plugin, configManager, translate);%0A    %7D%0A%0A    /**%0A     * Whenever an item is about to pop up on the crafting table%0A     * @param e the prepareitemcraftevent%0A     */
+ALLOW_NOTCH_APPLE = %22UHC.recipies.allowNotchApple%22;%0A
 %0A   
@@ -1700,74 +1700,12 @@
 ed()
-) %7B%0A            //check if no permission to create%0A            if(
+ && 
 !isR
@@ -1784,20 +1784,16 @@
         
-    
 //set to
@@ -1824,20 +1824,16 @@
         
-    
 e.getInv
@@ -1881,30 +1881,16 @@
 .AIR));%0A
-            %7D%0A
         
@@ -1936,16 +1936,23 @@
 llowed?%0A
+     *%0A
      * @
@@ -2491,16 +2491,23 @@
 l in it%0A
+     *%0A
      * @

update uber apples feature