Search in sources :

Example 1 with PlayerCraftsItemScriptEvent

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

the class ItemScriptHelper method specialRecipeClick.

// When special Denizen recipes that have itemscripts as ingredients
// are being used, check crafting matrix for recipe matches whenever
// clicks are made in CRAFTING or RESULT slots
@EventHandler
public void specialRecipeClick(InventoryClickEvent event) {
    // Proceed only if at least one special recipe has been stored
    if (ItemScriptContainer.specialrecipesMap.isEmpty() && ItemScriptContainer.shapelessRecipesMap.isEmpty()) {
        return;
    }
    // Proceed only if this is a CraftingInventory
    if (!(event.getInventory() instanceof CraftingInventory)) {
        return;
    }
    // Store the slot type that was clicked
    SlotType slotType = event.getSlotType();
    // Proceed only if a CRAFTING or RESULT slot was clicked
    if (slotType.equals(InventoryType.SlotType.CRAFTING) || slotType.equals(InventoryType.SlotType.RESULT)) {
        CraftingInventory inventory = (CraftingInventory) event.getInventory();
        Player player = (Player) event.getWhoClicked();
        if (slotType == SlotType.RESULT && inventory.getResult() != null && inventory.getResult().getData().getItemType() != Material.AIR) {
            PlayerCraftsItemScriptEvent scriptEvent = PlayerCraftsItemScriptEvent.instance;
            scriptEvent.inventory = inventory;
            scriptEvent.result = new dItem(inventory.getResult());
            dList recipeList = new dList();
            for (ItemStack item : inventory.getMatrix()) {
                if (item != null) {
                    recipeList.add(new dItem(item.clone()).identify());
                } else {
                    recipeList.add(new dItem(Material.AIR).identify());
                }
            }
            scriptEvent.recipe = recipeList;
            scriptEvent.player = dPlayer.mirrorBukkitPlayer(player);
            scriptEvent.resultChanged = false;
            scriptEvent.fire();
            if (scriptEvent.cancelled) {
                event.setCancelled(true);
                return;
            } else if (scriptEvent.resultChanged) {
                event.setCurrentItem(scriptEvent.result.getItemStack());
            }
        }
        // If the RESULT slot was shift-clicked, emulate
        // shift click behavior for it
        boolean clicked;
        if (slotType == SlotType.RESULT && event.isShiftClick()) {
            clicked = emulateSpecialRecipeResultShiftClick(inventory, player);
        } else // Otherwise check for special recipe matches
        {
            clicked = processSpecialRecipes(inventory, player);
        }
        if (clicked && slotType.equals(SlotType.RESULT)) {
            removeOneFromEachSlot(inventory, player);
        }
    }
}
Also used : CraftingInventory(org.bukkit.inventory.CraftingInventory) Player(org.bukkit.entity.Player) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) PlayerCraftsItemScriptEvent(net.aufdemrand.denizen.events.player.PlayerCraftsItemScriptEvent) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) ItemStack(org.bukkit.inventory.ItemStack) SlotType(org.bukkit.event.inventory.InventoryType.SlotType) EventHandler(org.bukkit.event.EventHandler)

Aggregations

PlayerCraftsItemScriptEvent (net.aufdemrand.denizen.events.player.PlayerCraftsItemScriptEvent)1 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)1 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 SlotType (org.bukkit.event.inventory.InventoryType.SlotType)1 CraftingInventory (org.bukkit.inventory.CraftingInventory)1 ItemStack (org.bukkit.inventory.ItemStack)1