Search in sources :

Example 1 with CompleteDragTask

use of com.elmakers.mine.bukkit.utility.CompleteDragTask in project MagicPlugin by elBukkit.

the class InventoryController method onInventoryDrag.

@EventHandler(ignoreCancelled = true)
public void onInventoryDrag(InventoryDragEvent event) {
    Mage mage = controller.getMage(event.getWhoClicked());
    GUIAction activeGUI = mage.getActiveGUI();
    if (activeGUI != null) {
        activeGUI.dragged(event);
        return;
    }
    // Unfortunately this event gives us a shallow copy of the item so we need to dig a little bit.
    // This could possibly be narrowed down to events only effecting held item slots and armor
    ItemStack oldCursor = event.getOldCursor();
    if (oldCursor.hasItemMeta() && Wand.isWand(InventoryUtils.makeReal(oldCursor))) {
        controller.onArmorUpdated(mage);
    }
    if (!enableItemHacks)
        return;
    // this is a huge hack! :\
    // I apologize for any weird behavior this causes.
    // Bukkit, unfortunately, will blow away NBT data for anything you drag
    // Which will nuke a wand or spell.
    // To make matters worse, Bukkit passes a copy of the item in the event, so we can't
    // even check for metadata and only cancel the event if it involves one of our special items.
    // The best I can do is look for metadata at all, since Bukkit will retain the name and lore.
    // I have now decided to copy over the CB default handler for this, and cancel the event.
    // The only change I have made is that *real* ItemStack copies are made, instead of shallow Bukkit ones.
    ItemStack oldStack = event.getOldCursor();
    HumanEntity entity = event.getWhoClicked();
    if (oldStack != null && oldStack.hasItemMeta() && entity instanceof Player) {
        // Only do this if we're only dragging one item, since I don't
        // really know what happens or how you would drag more than one.
        Map<Integer, ItemStack> draggedSlots = event.getNewItems();
        if (draggedSlots.size() != 1)
            return;
        event.setCancelled(true);
        // Cancelling the event will put the item back on the cursor,
        // and skip updating the inventory.
        // So we will wait one tick and then fix this up using the original item.
        InventoryView view = event.getView();
        for (Integer dslot : draggedSlots.keySet()) {
            CompleteDragTask completeDrag = new CompleteDragTask((Player) entity, view, dslot);
            completeDrag.runTaskLater(controller.getPlugin(), 1);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryView(org.bukkit.inventory.InventoryView) Mage(com.elmakers.mine.bukkit.magic.Mage) CompleteDragTask(com.elmakers.mine.bukkit.utility.CompleteDragTask) GUIAction(com.elmakers.mine.bukkit.api.action.GUIAction) HumanEntity(org.bukkit.entity.HumanEntity) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)1 Mage (com.elmakers.mine.bukkit.magic.Mage)1 CompleteDragTask (com.elmakers.mine.bukkit.utility.CompleteDragTask)1 HumanEntity (org.bukkit.entity.HumanEntity)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 InventoryView (org.bukkit.inventory.InventoryView)1 ItemStack (org.bukkit.inventory.ItemStack)1