Search in sources :

Example 6 with InventoryAction

use of org.bukkit.event.inventory.InventoryAction in project MassiveCore by MassiveCraft.

the class InventoryUtil method getChangesClick.

protected static List<Entry<ItemStack, Integer>> getChangesClick(InventoryClickEvent event) {
    // Create
    List<Entry<ItemStack, Integer>> ret = new MassiveList<>();
    // Fill
    final InventoryAlter alter = InventoryUtil.getAlter(event);
    final InventoryAction action = event.getAction();
    ItemStack item;
    int amount;
    // Give
    if (alter.isGiving()) {
        // Special > MOVE_TO_OTHER_INVENTORY
        if (action == InventoryAction.MOVE_TO_OTHER_INVENTORY) {
            item = event.getCurrentItem();
            ItemStack compare = item.clone();
            compare.setAmount(1);
            amount = InventoryUtil.roomLeft(event.getInventory(), compare, item.getAmount());
        } else // Special > HOTBAR_SWAP
        if (action == InventoryAction.HOTBAR_SWAP) {
            item = event.getView().getBottomInventory().getItem(event.getHotbarButton());
            amount = item.getAmount();
        } else // Normal
        {
            item = event.getCursor();
            amount = item.getAmount();
            if (action == InventoryAction.PLACE_ONE) {
                amount = 1;
            } else if (action == InventoryAction.PLACE_SOME) {
                int max = event.getCurrentItem().getType().getMaxStackSize();
                amount = max - event.getCurrentItem().getAmount();
            }
        }
        amount *= -1;
        ret.add(new SimpleEntry<>(item, amount));
    }
    // Take
    if (alter.isTaking()) {
        item = event.getCurrentItem();
        amount = item.getAmount();
        if (action == InventoryAction.PICKUP_ONE)
            amount = 1;
        if (action == InventoryAction.PICKUP_HALF)
            amount = (int) Math.ceil(amount / 2.0);
        ret.add(new SimpleEntry<>(item, amount));
    }
    // Return
    return ret;
}
Also used : SimpleEntry(java.util.AbstractMap.SimpleEntry) Entry(java.util.Map.Entry) MassiveList(com.massivecraft.massivecore.collections.MassiveList) ItemStack(org.bukkit.inventory.ItemStack) InventoryAction(org.bukkit.event.inventory.InventoryAction)

Aggregations

InventoryAction (org.bukkit.event.inventory.InventoryAction)6 ItemStack (org.bukkit.inventory.ItemStack)6 Inventory (org.bukkit.inventory.Inventory)4 Mage (com.elmakers.mine.bukkit.api.magic.Mage)3 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 Wand (com.elmakers.mine.bukkit.wand.Wand)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 InventoryType (org.bukkit.event.inventory.InventoryType)2 GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)1 MageController (com.elmakers.mine.bukkit.api.magic.MageController)1 Wand (com.elmakers.mine.bukkit.api.wand.Wand)1 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)1 Mage (com.elmakers.mine.bukkit.magic.Mage)1 WandMode (com.elmakers.mine.bukkit.wand.WandMode)1 MassiveList (com.massivecraft.massivecore.collections.MassiveList)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1