<commit_before>package uk.co.eluinhost.UltraHardcore.features.core;

import java.util.Collection;
import java.util.Iterator;

import org.bukkit.Bukkit;
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 uk.co.eluinhost.UltraHardcore.config.PermissionNodes;
import uk.co.eluinhost.UltraHardcore.features.UHCFeature;


/**
 * RecipeHandler
 * <p/>
 * Handles the alternative recipies for the regen items
 *
 * @author ghowden
 */
public class RecipeFeature extends UHCFeature {

    public RecipeFeature(boolean enabled) {
        super("HardRecipes", enabled);
        setDescription("Handles changed recipes");
    }

    @EventHandler
    public void onPrepareCraftItemEvent(PrepareItemCraftEvent e) {
        if (isEnabled()) {
            Recipe r = e.getRecipe();
            if (r.getResult().getType().equals(Material.GOLDEN_APPLE)) {
                if (recipeContainsMaterial(r, Material.GOLD_BLOCK)) {
                    if (!e.getView().getPlayer().hasPermission(PermissionNodes.ALLOW_NOTCH_APPLE)) {
                        e.getInventory().setResult(new ItemStack(Material.AIR));
                    }
                }
            } else if (r.getResult().getType().equals(Material.SPECKLED_MELON)) {
                if (recipeContainsMaterial(r, Material.GOLD_NUGGET)) {
                    if (e.getView().getPlayer().hasPermission(PermissionNodes.DISALLOW_OLD_GMELON)) {
                        e.getInventory().setResult(new ItemStack(Material.AIR));
                    }
                } else if (recipeContainsMaterial(r, Material.GOLD_BLOCK)) {
                    if (!e.getView().getPlayer().hasPermission(PermissionNodes.ALLOW_NEW_GMELON)) {
                        e.getInventory().setResult(new ItemStack(Material.AIR));
                    }
                }
            } else if (r.getResult().getType().equals(Material.GOLDEN_CARROT)) {
                if (recipeContainsMaterial(r, Material.GOLD_NUGGET)) {
                    if (e.getView().getPlayer().hasPermission(PermissionNodes.DISALLOW_OLD_GCARROT)) {
                        e.getInventory().setResult(new ItemStack(Material.AIR));
                    }
                } else if (recipeContainsMaterial(r, Material.GOLD_INGOT)) {
                    if (!e.getView().getPlayer().hasPermission(PermissionNodes.ALLOW_NEW_GCARROT)) {
                        e.getInventory().setResult(new ItemStack(Material.AIR));
                    }
                }
            }
        }
    }

    private boolean recipeContainsMaterial(Recipe r, Material mat) {
        Collection<ItemStack> ingredients = null;
        if (r instanceof ShapedRecipe) {
            ingredients = ((ShapedRecipe) r).getIngredientMap().values();
        }
        if (r instanceof ShapelessRecipe) {
            ingredients = ((ShapelessRecipe) r).getIngredientList();
        }
        if (ingredients == null) {
            return false;
        }
        for (ItemStack i : ingredients) {
            if (i.getType().equals(mat)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Add our recipes
     */
    @Override
    public void enableFeature() {
        //Make a recipe that will return a golden carrot when the right shape is made
        ShapedRecipe newGoldenCarrot = new ShapedRecipe(new ItemStack(Material.GOLDEN_CARROT, 1));
        //8 gold ingots surrounding an apple
        newGoldenCarrot.shape("AAA", "ABA", "AAA");
        newGoldenCarrot.setIngredient('A', Material.GOLD_INGOT);
        newGoldenCarrot.setIngredient('B', Material.CARROT_ITEM);
        Bukkit.addRecipe(newGoldenCarrot);

        //Make the recipe for glistering melons minus a set shape (glistering melon is speckled melon in code
        ShapelessRecipe newGlisteringMelon = new ShapelessRecipe(new ItemStack(Material.SPECKLED_MELON, 1));
        //1 gold ingot with a melon
        newGlisteringMelon.addIngredient(Material.GOLD_BLOCK);
        newGlisteringMelon.addIngredient(Material.MELON);
        Bukkit.addRecipe(newGlisteringMelon);
    }

    /**
     * Remove the recipes we added
     */
    @Override
    public void disableFeature() {
        Iterator<Recipe> i = Bukkit.recipeIterator();
        while (i.hasNext()) {
            Recipe r = i.next();
            if (r.getResult().getType().equals(Material.SPECKLED_MELON)) {
                if (recipeContainsMaterial(r, Material.GOLD_BLOCK)) {
                    i.remove();
                }
            }else if (r.getResult().getType().equals(Material.GOLDEN_CARROT)) {
                if (recipeContainsMaterial(r, Material.GOLD_INGOT)) {
                    i.remove();
                }
            }
        }
    }
}<commit_msg>refactor<commit_after>package uk.co.eluinhost.UltraHardcore.features.core;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.bukkit.Bukkit;
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 uk.co.eluinhost.UltraHardcore.config.PermissionNodes;
import uk.co.eluinhost.UltraHardcore.features.UHCFeature;


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

    public RecipeFeature(boolean enabled) {
        super("HardRecipes", enabled);
        setDescription("Handles changed recipes");
    }

    @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
     * TODO abstract this into a config style instead of hardcode and loop
     */
    private static boolean isRecipeAllowedForPermissible(Permissible permissible, Recipe recipe){
        Material result = recipe.getResult().getType();

        if(result == Material.GOLDEN_APPLE && hasRecipeGotMaterial(recipe, Material.GOLD_BLOCK) && !permissible.hasPermission(PermissionNodes.ALLOW_NOTCH_APPLE)){
            return false;
        }
        if(result == Material.SPECKLED_MELON && hasRecipeGotMaterial(recipe, Material.GOLD_NUGGET) && permissible.hasPermission(PermissionNodes.DISALLOW_OLD_GMELON)){
            return false;
        }
        if(result == Material.SPECKLED_MELON && hasRecipeGotMaterial(recipe, Material.GOLD_BLOCK) && permissible.hasPermission(PermissionNodes.ALLOW_NEW_GMELON)) {
            return false;
        }
        if(result == Material.GOLDEN_CARROT && hasRecipeGotMaterial(recipe, Material.GOLD_NUGGET) && permissible.hasPermission(PermissionNodes.DISALLOW_OLD_GCARROT)) {
            return false;
        }
        if(result == Material.GOLDEN_CARROT && hasRecipeGotMaterial(recipe, Material.GOLD_INGOT) && permissible.hasPermission(PermissionNodes.ALLOW_NEW_GCARROT)) {
            return false;
        }
        //passed all checks
        return true;
    }

    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);
    }

    private static boolean isMaterialInList(Iterable<ItemStack> itemStackList, Material mat){
        for (ItemStack itemStack : itemStackList) {
            if (itemStack.getType() == mat) {
                return true;
            }
        }
        return false;
    }

    /**
     * Add our recipes
     */
    @Override
    public void enableFeature() {
        //Make a recipe that will return a golden carrot when the right shape is made
        ShapedRecipe newGoldenCarrot = new ShapedRecipe(new ItemStack(Material.GOLDEN_CARROT, 1));
        //8 gold ingots surrounding an apple
        newGoldenCarrot.shape("AAA", "ABA", "AAA");
        newGoldenCarrot.setIngredient('A', Material.GOLD_INGOT);
        newGoldenCarrot.setIngredient('B', Material.CARROT_ITEM);
        Bukkit.addRecipe(newGoldenCarrot);

        //Make the recipe for glistering melons minus a set shape (glistering melon is speckled melon in code
        ShapelessRecipe newGlisteringMelon = new ShapelessRecipe(new ItemStack(Material.SPECKLED_MELON, 1));
        //1 gold ingot with a melon
        newGlisteringMelon.addIngredient(Material.GOLD_BLOCK);
        newGlisteringMelon.addIngredient(Material.MELON);
        Bukkit.addRecipe(newGlisteringMelon);
    }

    /**
     * Remove the recipes we added
     */
    @Override
    public void disableFeature() {
        Iterator<Recipe> recipeIterator = Bukkit.recipeIterator();
        while (recipeIterator.hasNext()) {
            Recipe recipe = recipeIterator.next();
            boolean removeRecipe = false;
            if (recipe.getResult().getType() == Material.SPECKLED_MELON && hasRecipeGotMaterial(recipe, Material.GOLD_BLOCK)) {
                removeRecipe = true;
            }
            if (recipe.getResult().getType() == Material.GOLDEN_CARROT && hasRecipeGotMaterial(recipe, Material.GOLD_INGOT)) {
                removeRecipe = true;
            }
            if(removeRecipe){
                recipeIterator.remove();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.10080527796643059, 'fraction_numerical': 0.0007761715339089939, 'mean_word_length': 2.5253077975376197, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6889508', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2989, 'n_words': 576}