Search in sources :

Example 1 with MagicRecipe

use of com.elmakers.mine.bukkit.magic.MagicRecipe in project MagicPlugin by elBukkit.

the class CraftingController method onPrepareCraftItem.

@EventHandler
public void onPrepareCraftItem(PrepareItemCraftEvent event) {
    CraftingInventory inventory = event.getInventory();
    ItemStack[] contents = inventory.getMatrix();
    // Check for wands glitched into the crafting inventory
    for (int i = 0; i < 9 && i < contents.length; i++) {
        ItemStack item = contents[i];
        if (Wand.isSpecial(item)) {
            inventory.setResult(new ItemStack(Material.AIR));
            return;
        }
    }
    if (!craftingEnabled)
        return;
    Recipe recipe = event.getRecipe();
    if (recipe == null)
        return;
    ItemStack result = recipe.getResult();
    if (result == null)
        return;
    Material resultType = result.getType();
    List<MagicRecipe> candidates = recipes.get(resultType);
    if (candidates == null || candidates.size() == 0)
        return;
    for (MagicRecipe candidate : candidates) {
        MagicRecipe.MatchType matchType = candidate.getMatchType(contents);
        Material substitute = candidate.getSubstitute();
        if (matchType != MagicRecipe.MatchType.NONE) {
            for (HumanEntity human : event.getViewers()) {
                if (human instanceof Player && (matchType == MagicRecipe.MatchType.PARTIAL || !hasCraftPermission((Player) human, candidate))) {
                    inventory.setResult(new ItemStack(Material.AIR));
                    return;
                }
            }
            if (matchType == MagicRecipe.MatchType.MATCH) {
                ItemStack crafted = candidate.craft();
                inventory.setResult(crafted);
            }
            break;
        } else if (substitute != null) {
            inventory.setResult(new ItemStack(substitute, 1));
        }
    }
}
Also used : CraftingInventory(org.bukkit.inventory.CraftingInventory) Player(org.bukkit.entity.Player) Recipe(org.bukkit.inventory.Recipe) MagicRecipe(com.elmakers.mine.bukkit.magic.MagicRecipe) HumanEntity(org.bukkit.entity.HumanEntity) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) MagicRecipe(com.elmakers.mine.bukkit.magic.MagicRecipe) EventHandler(org.bukkit.event.EventHandler)

Example 2 with MagicRecipe

use of com.elmakers.mine.bukkit.magic.MagicRecipe in project MagicPlugin by elBukkit.

the class CraftingController method load.

public void load(ConfigurationSection configuration) {
    recipes.clear();
    recipeCount = 0;
    if (!craftingEnabled) {
        return;
    }
    Set<String> recipeKeys = configuration.getKeys(false);
    for (String key : recipeKeys) {
        ConfigurationSection parameters = configuration.getConfigurationSection(key);
        if (!parameters.getBoolean("enabled", true))
            continue;
        MagicRecipe recipe = new MagicRecipe(key, controller);
        if (!recipe.load(parameters)) {
            controller.getLogger().warning("Failed to create crafting recipe: " + key);
            continue;
        }
        Material outputType = recipe.getOutputType();
        List<MagicRecipe> similar = recipes.get(outputType);
        if (similar == null) {
            similar = new ArrayList<>();
            recipes.put(outputType, similar);
        }
        similar.add(recipe);
        recipeCount++;
    }
}
Also used : Material(org.bukkit.Material) MagicRecipe(com.elmakers.mine.bukkit.magic.MagicRecipe) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

MagicRecipe (com.elmakers.mine.bukkit.magic.MagicRecipe)2 Material (org.bukkit.Material)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 HumanEntity (org.bukkit.entity.HumanEntity)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 CraftingInventory (org.bukkit.inventory.CraftingInventory)1 ItemStack (org.bukkit.inventory.ItemStack)1 Recipe (org.bukkit.inventory.Recipe)1