Search in sources :

Example 1 with DropActionTask

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

the class PlayerController method onPlayerDropItem.

@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent event) {
    final Player player = event.getPlayer();
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    // Catch lag-related glitches dropping items from GUIs
    if (mage.getActiveGUI() != null) {
        event.setCancelled(true);
        return;
    }
    final Wand activeWand = mage.getActiveWand();
    final ItemStack droppedItem = event.getItemDrop().getItemStack();
    boolean cancelEvent = false;
    ItemStack activeItem = activeWand == null ? null : activeWand.getItem();
    // It seems like Spigot sets the original item to air before dropping
    // We will be satisfied to only compare the metadata.
    ItemMeta activeMeta = activeItem == null ? null : activeItem.getItemMeta();
    ItemMeta droppedMeta = droppedItem.getItemMeta();
    boolean droppedWand = droppedMeta != null && activeMeta != null && activeItem.getItemMeta().equals(droppedItem.getItemMeta());
    if (droppedWand && activeWand.isUndroppable()) {
        // Postpone cycling until after this event unwinds
        Bukkit.getScheduler().scheduleSyncDelayedTask(controller.getPlugin(), new DropActionTask(activeWand));
        cancelEvent = true;
    } else if (activeWand != null) {
        if (droppedWand) {
            ItemStack mainhandItem = player.getInventory().getItemInMainHand();
            activeWand.deactivate();
            ItemStack restoredItem = player.getInventory().getItemInMainHand();
            ItemMeta restoredMeta = restoredItem == null ? null : restoredItem.getItemMeta();
            activeMeta = activeWand.getItem().getItemMeta();
            // Might have just saved some changes
            droppedItem.setItemMeta(activeMeta);
            // Clear after inventory restore (potentially with deactivate), since that will put the wand back
            if (Wand.hasActiveWand(player) && restoredItem.getType() != Material.AIR && restoredMeta != null && activeMeta.equals(restoredMeta)) {
                ItemStack newItem = player.getInventory().getItemInMainHand();
                if (mainhandItem.getAmount() > 0) {
                    newItem.setAmount(mainhandItem.getAmount());
                    player.getInventory().setItemInMainHand(newItem);
                } else {
                    player.getInventory().setItemInMainHand(new ItemStack(Material.AIR, 1));
                }
            }
        } else if (activeWand.isInventoryOpen()) {
            if (!controller.isSpellDroppingEnabled()) {
                cancelEvent = true;
                // This will happen if you graph an item, change pages, and then drop the item- it would disappear
                // from the inventory until reload.
                // Check for this state and prevent it.
                boolean isInventoryFull = true;
                PlayerInventory playerInventory = player.getInventory();
                for (int i = 0; i < Wand.PLAYER_INVENTORY_SIZE; i++) {
                    ItemStack item = playerInventory.getItem(i);
                    if (item == null || item.getType() == Material.AIR) {
                        isInventoryFull = false;
                        break;
                    }
                }
                if (isInventoryFull) {
                    activeWand.addToInventory(droppedItem);
                }
            } else {
                // The item is already removed from the wand's inventory, but that should be ok
                controller.removeItemFromWand(activeWand, droppedItem);
            }
        }
    }
    if (!cancelEvent) {
        cancelEvent = InventoryUtils.getMetaBoolean(droppedItem, "undroppable", false);
    }
    if (cancelEvent) {
        if (droppedWand) {
            activeWand.setItem(droppedItem);
        }
        event.setCancelled(true);
    }
}
Also used : DropActionTask(com.elmakers.mine.bukkit.magic.DropActionTask) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) EventHandler(org.bukkit.event.EventHandler)

Aggregations

DropActionTask (com.elmakers.mine.bukkit.magic.DropActionTask)1 Mage (com.elmakers.mine.bukkit.magic.Mage)1 Wand (com.elmakers.mine.bukkit.wand.Wand)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 ItemStack (org.bukkit.inventory.ItemStack)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1