Search in sources :

Example 1 with ItemRecipeFormedScriptEvent

use of net.aufdemrand.denizen.events.player.ItemRecipeFormedScriptEvent in project Denizen-For-Bukkit by DenizenScript.

the class ItemScriptHelper method processSpecialRecipes.

// Compare a crafting matrix with all stored special recipes right
// after a click or drag has been made in it
public boolean processSpecialRecipes(final CraftingInventory inventory, final Player player) {
    // Store the current matrix
    ItemStack[] matrix1 = inventory.getMatrix();
    for (int i = 0; i < matrix1.length; i++) {
        matrix1[i] = matrix1[i] == null ? new ItemStack(Material.AIR) : matrix1[i].clone();
    }
    // Get the result of the special recipe that this matrix matches,
    // if any
    dItem result1 = getSpecialRecipeResult(matrix1, player);
    boolean returnme = result1 != null;
    // Run a task 1 tick later than the event from which this method
    // was called, to check the new state of the CraftingInventory's matrix
    Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {

        @Override
        public void run() {
            // Store the current matrix
            ItemStack[] matrix = inventory.getMatrix();
            for (int i = 0; i < matrix.length; i++) {
                matrix[i] = matrix[i] == null ? new ItemStack(Material.AIR) : matrix[i].clone();
            }
            // Get the result of the special recipe that this matrix matches,
            // if any
            dItem result = getSpecialRecipeResult(matrix, player);
            // Proceed only if the result was not null
            if (result != null) {
                dList recipeList = new dList();
                for (ItemStack item : matrix) {
                    if (item != null) {
                        recipeList.add(new dItem(item).identify());
                    } else {
                        recipeList.add(new dItem(Material.AIR).identify());
                    }
                }
                ItemRecipeFormedScriptEvent event = ItemRecipeFormedScriptEvent.instance;
                event.result = result;
                event.recipe = recipeList;
                event.inventory = inventory;
                event.player = dPlayer.mirrorBukkitPlayer(player);
                event.cancelled = false;
                event.resultChanged = false;
                event.fire();
                if (event.cancelled) {
                    inventory.setResult(null);
                } else {
                    // If this was a valid match, set the crafting's result
                    inventory.setResult(event.result.getItemStack());
                }
                // Update the player's inventory
                player.updateInventory();
            }
        }
    }, 2);
    return returnme;
}
Also used : net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) ItemRecipeFormedScriptEvent(net.aufdemrand.denizen.events.player.ItemRecipeFormedScriptEvent) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

ItemRecipeFormedScriptEvent (net.aufdemrand.denizen.events.player.ItemRecipeFormedScriptEvent)1 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 ItemStack (org.bukkit.inventory.ItemStack)1