Search in sources :

Example 1 with GUIAction

use of com.elmakers.mine.bukkit.api.action.GUIAction in project MagicPlugin by elBukkit.

the class InventoryController method onInventoryOpen.

@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
    Player player = (Player) event.getPlayer();
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    mage.setOpenCooldown(openCooldown);
    Wand wand = mage.getActiveWand();
    GUIAction gui = mage.getActiveGUI();
    if (wand != null && gui == null) {
        // Anyway, check for opening another inventory and close the wand.
        if (event.getView().getType() != InventoryType.CRAFTING) {
            if (wand.getMode() == WandMode.INVENTORY || !wand.isInventoryOpen()) {
                wand.deactivate();
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) GUIAction(com.elmakers.mine.bukkit.api.action.GUIAction) Wand(com.elmakers.mine.bukkit.wand.Wand) EventHandler(org.bukkit.event.EventHandler)

Example 2 with GUIAction

use of com.elmakers.mine.bukkit.api.action.GUIAction in project MagicPlugin by elBukkit.

the class Mage method onGUIDeactivate.

public void onGUIDeactivate() {
    GUIAction previousGUI = gui;
    gui = null;
    Player player = getPlayer();
    if (player != null) {
        DeprecatedUtils.updateInventory(player);
    }
    if (previousGUI != null) {
        previousGUI.deactivated();
    }
}
Also used : Player(org.bukkit.entity.Player) GUIAction(com.elmakers.mine.bukkit.api.action.GUIAction)

Example 3 with GUIAction

use of com.elmakers.mine.bukkit.api.action.GUIAction 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)

Example 4 with GUIAction

use of com.elmakers.mine.bukkit.api.action.GUIAction in project MagicPlugin by elBukkit.

the class InventoryController method onInventoryClick.

@SuppressWarnings("deprecation")
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    if (event.isCancelled())
        return;
    if (!(event.getWhoClicked() instanceof Player))
        return;
    Player player = (Player) event.getWhoClicked();
    final Mage mage = controller.getMage(player);
    GUIAction gui = mage.getActiveGUI();
    if (gui != null) {
        gui.clicked(event);
        return;
    }
    // Check for temporary items and skill items
    InventoryAction action = event.getAction();
    InventoryType inventoryType = event.getInventory().getType();
    boolean isPlayerInventory = inventoryType == InventoryType.CRAFTING || inventoryType == InventoryType.PLAYER;
    ItemStack clickedItem = event.getCurrentItem();
    boolean isDrop = event.getClick() == ClickType.DROP || event.getClick() == ClickType.CONTROL_DROP;
    boolean isSkill = clickedItem != null && Wand.isSkill(clickedItem);
    // Check for right-click-to-use
    boolean isRightClick = action == InventoryAction.PICKUP_HALF;
    if (isSkill && isRightClick) {
        mage.useSkill(clickedItem);
        player.closeInventory();
        event.setCancelled(true);
        return;
    }
    if (clickedItem != null && NMSUtils.isTemporary(clickedItem)) {
        String message = NMSUtils.getTemporaryMessage(clickedItem);
        if (message != null && message.length() > 1) {
            mage.sendMessage(message);
        }
        ItemStack replacement = NMSUtils.getReplacement(clickedItem);
        event.setCurrentItem(replacement);
        event.setCancelled(true);
        return;
    }
    // Check for wearing spells
    ItemStack heldItem = event.getCursor();
    boolean heldSpell = Wand.isSpell(heldItem);
    if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
        if (heldSpell) {
            event.setCancelled(true);
            return;
        }
        if (Wand.isWand(clickedItem) || Wand.isWand(heldItem)) {
            controller.onArmorUpdated(mage);
        }
    }
    boolean isHotbar = event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
    if (isHotbar && event.getSlotType() == InventoryType.SlotType.ARMOR) {
        int slot = event.getHotbarButton();
        ItemStack item = mage.getPlayer().getInventory().getItem(slot);
        if (item != null && Wand.isSpell(item)) {
            event.setCancelled(true);
            return;
        }
        controller.onArmorUpdated(mage);
    }
    Wand activeWand = mage.getActiveWand();
    boolean isWandInventoryOpen = activeWand != null && activeWand.isInventoryOpen();
    // Preventing putting skills in containers
    if (isSkill && !isPlayerInventory && !isWandInventoryOpen) {
        if (!isDrop) {
            event.setCancelled(true);
        }
        return;
    }
    boolean isFurnace = inventoryType == InventoryType.FURNACE;
    boolean isChest = inventoryType == InventoryType.CHEST || inventoryType == InventoryType.HOPPER || inventoryType == InventoryType.DISPENSER || inventoryType == InventoryType.DROPPER;
    // TODO: use enum when dropping backwards compat
    if (!isChest)
        isChest = inventoryType.name().equals("SHULKER_BOX");
    boolean clickedWand = Wand.isWand(clickedItem);
    boolean isContainerSlot = event.getSlot() == event.getRawSlot();
    if (isWandInventoryOpen) {
        // Don't allow the offhand slot to be messed with while the spell inventory is open
        if (event.getRawSlot() == 45) {
            event.setCancelled(true);
            return;
        }
        // Don't allow putting spells in a crafting slot
        if (event.getSlotType() == InventoryType.SlotType.CRAFTING && heldSpell) {
            event.setCancelled(true);
            return;
        }
        if (Wand.isSpell(clickedItem) && clickedItem.getAmount() != 1) {
            clickedItem.setAmount(1);
        }
        if (clickedWand) {
            event.setCancelled(true);
            if (dropChangesPages || isRightClick) {
                activeWand.cycleInventory();
            } else {
                activeWand.cycleHotbar(1);
                // There doesn't seem to be any other way to allow cycling in creative
                if (inventoryType == InventoryType.PLAYER) {
                    activeWand.cycleInventory();
                }
            }
            return;
        }
        // So many ways to try and move the wand around, that we have to watch for!
        if (isHotbar && Wand.isWand(player.getInventory().getItem(event.getHotbarButton()))) {
            event.setCancelled(true);
            return;
        }
        // Can't wear spells
        if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
            if (controller.isWearable(clickedItem)) {
                event.setCancelled(true);
                return;
            }
        }
        // but perhaps happens with lag?
        if (Wand.isWand(event.getCursor())) {
            activeWand.closeInventory();
            event.setCursor(null);
            event.setCancelled(true);
            return;
        }
    } else if (activeWand != null) {
        // Check for changes that could have been made to the active wand
        int activeSlot = player.getInventory().getHeldItemSlot();
        if (event.getSlot() == activeSlot || (isHotbar && event.getHotbarButton() == activeSlot)) {
            mage.checkWand();
            activeWand = mage.getActiveWand();
        }
    }
    // Don't allow smelting wands
    if (isFurnace && clickedWand) {
        event.setCancelled(true);
        return;
    }
    if (isFurnace && isHotbar) {
        ItemStack destinationItem = player.getInventory().getItem(event.getHotbarButton());
        if (Wand.isWand(destinationItem)) {
            event.setCancelled(true);
            return;
        }
    }
    if (InventoryUtils.getMetaBoolean(clickedItem, "unmoveable", false)) {
        event.setCancelled(true);
        return;
    }
    if (isHotbar) {
        ItemStack destinationItem = player.getInventory().getItem(event.getHotbarButton());
        if (InventoryUtils.getMetaBoolean(destinationItem, "unmoveable", false)) {
            event.setCancelled(true);
            return;
        }
        if (isChest && InventoryUtils.getMetaBoolean(destinationItem, "unstashable", false) && !player.hasPermission("Magic.wand.override_stash")) {
            event.setCancelled(true);
            return;
        }
    }
    // Check for unstashable wands
    if (isChest && !isContainerSlot && !player.hasPermission("Magic.wand.override_stash")) {
        if (InventoryUtils.getMetaBoolean(clickedItem, "unstashable", false)) {
            event.setCancelled(true);
            return;
        }
    }
    // Check for taking bound wands out of chests
    if (isChest && isContainerSlot && Wand.isBound(clickedItem)) {
        Wand testWand = controller.getWand(clickedItem);
        if (!testWand.canUse(player)) {
            event.setCancelled(true);
            return;
        }
    }
    // Check for armor changing
    if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
        if (controller.isWearable(clickedItem)) {
            controller.onArmorUpdated(mage);
        }
    }
    // or dropping undroppable wands
    if (isDrop) {
        if (InventoryUtils.getMetaBoolean(clickedItem, "undroppable", false)) {
            event.setCancelled(true);
            if (activeWand != null) {
                if (activeWand.getHotbarCount() > 1) {
                    activeWand.cycleHotbar(1);
                } else {
                    activeWand.closeInventory();
                }
            }
            return;
        }
        if (activeWand != null && activeWand.isInventoryOpen()) {
            ItemStack droppedItem = clickedItem;
            if (!Wand.isSpell(droppedItem)) {
                mage.giveItem(droppedItem);
                event.setCurrentItem(null);
                event.setCancelled(true);
                return;
            }
            // This is a hack to deal with spells on cooldown disappearing,
            // Since the event handler doesn't match the zero-count itemstacks
            Integer slot = event.getSlot();
            int heldSlot = player.getInventory().getHeldItemSlot();
            Inventory hotbar = activeWand.getHotbar();
            if (hotbar != null && slot >= 0 && slot <= hotbar.getSize() && slot != heldSlot && activeWand.getMode() == WandMode.INVENTORY) {
                if (slot > heldSlot)
                    slot--;
                if (slot < hotbar.getSize()) {
                    droppedItem = hotbar.getItem(slot);
                } else {
                    slot = null;
                }
            } else {
                slot = null;
            }
            if (!controller.isSpellDroppingEnabled()) {
                player.closeInventory();
                String spellName = Wand.getSpell(droppedItem);
                if (spellName != null && !activeWand.isManualQuickCastDisabled() && enableInventoryCasting) {
                    Spell spell = mage.getSpell(spellName);
                    if (spell != null) {
                        activeWand.cast(spell);
                        // Just in case a spell has levelled up... jeez!
                        if (hotbar != null && slot != null) {
                            droppedItem = hotbar.getItem(slot);
                        }
                    }
                }
                event.setCancelled(true);
                // This is needed to avoid spells on cooldown disappearing from the hotbar
                if (hotbar != null && slot != null && mage.getActiveGUI() == null) {
                    player.getInventory().setItem(event.getSlot(), droppedItem);
                    DeprecatedUtils.updateInventory(player);
                }
                return;
            }
            ItemStack newDrop = controller.removeItemFromWand(activeWand, droppedItem);
            if (newDrop != null) {
                Location location = player.getLocation();
                Item item = location.getWorld().dropItem(location, newDrop);
                SafetyUtils.setVelocity(item, location.getDirection().normalize());
            } else {
                event.setCancelled(true);
            }
        }
        return;
    }
    // Check for wand cycling with active inventory
    if (activeWand != null) {
        WandMode wandMode = activeWand.getMode();
        boolean isChestInventory = wandMode == WandMode.CHEST || wandMode == WandMode.SKILLS;
        if ((wandMode == WandMode.INVENTORY && isPlayerInventory) || (isChestInventory && inventoryType == InventoryType.CHEST)) {
            if (activeWand.isInventoryOpen()) {
                if (event.getAction() == InventoryAction.NOTHING) {
                    int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
                    activeWand.cycleInventory(direction);
                    event.setCancelled(true);
                    return;
                }
                if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
                    event.setCancelled(true);
                    return;
                }
                // Chest mode falls back to selection from here.
                boolean isInventoryQuickSelect = isRightClick && wandMode == WandMode.INVENTORY && enableInventorySelection;
                if (isInventoryQuickSelect || wandMode == WandMode.CHEST) {
                    player.closeInventory();
                    mage.activateIcon(activeWand, clickedItem);
                    event.setCancelled(true);
                }
                // Prevent clicking any non-skill item when a skill inventory is open
                if (wandMode == WandMode.SKILLS && clickedItem != null && clickedItem.getType() != Material.AIR && !Wand.isSkill(clickedItem)) {
                    event.setCancelled(true);
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryType(org.bukkit.event.inventory.InventoryType) GUIAction(com.elmakers.mine.bukkit.api.action.GUIAction) Wand(com.elmakers.mine.bukkit.wand.Wand) Spell(com.elmakers.mine.bukkit.api.spell.Spell) InventoryAction(org.bukkit.event.inventory.InventoryAction) Item(org.bukkit.entity.Item) Mage(com.elmakers.mine.bukkit.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) CraftingInventory(org.bukkit.inventory.CraftingInventory) Inventory(org.bukkit.inventory.Inventory) WandMode(com.elmakers.mine.bukkit.wand.WandMode) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 5 with GUIAction

use of com.elmakers.mine.bukkit.api.action.GUIAction in project MagicPlugin by elBukkit.

the class InventoryController method onInventoryClosed.

@EventHandler
public void onInventoryClosed(InventoryCloseEvent event) {
    if (!(event.getPlayer() instanceof Player))
        return;
    // Update the active wand, it may have changed around
    Player player = (Player) event.getPlayer();
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    Wand previousWand = mage.getActiveWand();
    // Prevent spells getting smuggled out via crafting slots
    Inventory inventory = event.getInventory();
    if (inventory instanceof CraftingInventory && previousWand != null && previousWand.wasInventoryOpen()) {
        CraftingInventory craftingInventory = (CraftingInventory) inventory;
        ItemStack[] matrix = craftingInventory.getMatrix();
        for (int i = 0; i < matrix.length; i++) {
            matrix[i] = new ItemStack(Material.AIR);
        }
        craftingInventory.setMatrix(matrix);
    }
    GUIAction gui = mage.getActiveGUI();
    if (gui != null) {
        mage.onGUIDeactivate();
    }
    // This is just to make sure we don't lose changes made to the inventory
    if (previousWand != null && previousWand.isInventoryOpen()) {
        if (previousWand.getMode() == WandMode.INVENTORY) {
            previousWand.saveInventory();
            // Update hotbar names
            previousWand.updateHotbar();
        } else if (previousWand.getMode() == WandMode.CHEST || previousWand.getMode() == WandMode.SKILLS) {
            // Close and save an open chest mode inventory
            previousWand.closeInventory();
        }
    } else {
        if (previousWand != null && !previousWand.wasInventoryOpen()) {
            previousWand.checkInventoryForUpgrades();
        }
        mage.checkWand();
    }
}
Also used : CraftingInventory(org.bukkit.inventory.CraftingInventory) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) GUIAction(com.elmakers.mine.bukkit.api.action.GUIAction) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) CraftingInventory(org.bukkit.inventory.CraftingInventory) Inventory(org.bukkit.inventory.Inventory) EventHandler(org.bukkit.event.EventHandler)

Aggregations

GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)5 Player (org.bukkit.entity.Player)5 Mage (com.elmakers.mine.bukkit.magic.Mage)4 EventHandler (org.bukkit.event.EventHandler)4 Wand (com.elmakers.mine.bukkit.wand.Wand)3 ItemStack (org.bukkit.inventory.ItemStack)3 CraftingInventory (org.bukkit.inventory.CraftingInventory)2 Inventory (org.bukkit.inventory.Inventory)2 Spell (com.elmakers.mine.bukkit.api.spell.Spell)1 CompleteDragTask (com.elmakers.mine.bukkit.utility.CompleteDragTask)1 WandMode (com.elmakers.mine.bukkit.wand.WandMode)1 Location (org.bukkit.Location)1 HumanEntity (org.bukkit.entity.HumanEntity)1 Item (org.bukkit.entity.Item)1 InventoryAction (org.bukkit.event.inventory.InventoryAction)1 InventoryType (org.bukkit.event.inventory.InventoryType)1 InventoryView (org.bukkit.inventory.InventoryView)1