Search in sources :

Example 1 with Wand

use of com.elmakers.mine.bukkit.wand.Wand in project MagicPlugin by elBukkit.

the class EnchantingController method onInventoryClick.

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
    if (event.isCancelled())
        return;
    InventoryType inventoryType = event.getInventory().getType();
    SlotType slotType = event.getSlotType();
    if (enchantingEnabled && inventoryType == InventoryType.ENCHANTING) {
        if (slotType == SlotType.CRAFTING) {
            HumanEntity clicker = event.getWhoClicked();
            Player player = clicker instanceof Player ? (Player) clicker : null;
            if (player == null || !controller.hasPermission(player, "Magic.wand.enchant")) {
                return;
            }
            ItemStack cursor = event.getCursor();
            ItemStack current = event.getCurrentItem();
            // Make wands into an enchantable item when placing
            if (Wand.isWand(cursor)) {
                Wand wand = controller.getWand(cursor);
                if (wand.isModifiable()) {
                    wand.makeEnchantable(true);
                }
            }
            // And turn them back when taking out
            if (Wand.isWand(current)) {
                Wand wand = controller.getWand(current);
                if (wand.isModifiable()) {
                    wand.makeEnchantable(false);
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) InventoryType(org.bukkit.event.inventory.InventoryType) HumanEntity(org.bukkit.entity.HumanEntity) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) SlotType(org.bukkit.event.inventory.InventoryType.SlotType) EventHandler(org.bukkit.event.EventHandler)

Example 2 with Wand

use of com.elmakers.mine.bukkit.wand.Wand in project MagicPlugin by elBukkit.

the class EntityController method onItemSpawn.

@EventHandler(priority = EventPriority.LOWEST)
public void onItemSpawn(ItemSpawnEvent event) {
    if (disableItemSpawn || com.elmakers.mine.bukkit.block.BlockData.undoing) {
        event.setCancelled(true);
        return;
    }
    Item itemEntity = event.getEntity();
    ItemStack spawnedItem = itemEntity.getItemStack();
    Block block = itemEntity.getLocation().getBlock();
    BlockData undoData = com.elmakers.mine.bukkit.block.UndoList.getBlockData(block.getLocation());
    boolean isBreaking = block.getType() != Material.AIR;
    if (!isBreaking) {
        MaterialSet doubleAttachables = controller.getMaterialSetManager().getMaterialSetEmpty("attachable_double");
        isBreaking = doubleAttachables.testItem(spawnedItem);
    }
    if (undoData != null && isBreaking) {
        // So we can catch this as a one-time event, for blocks we have recorded.
        if (undoData.getMaterial() != Material.AIR) {
            UndoList undoList = undoData.getUndoList();
            if (undoList != null) {
                undoList.add(block);
            } else {
                controller.getLogger().warning("Block broken into item under undo at " + block + ", but no undo list was assigned");
            }
            event.setCancelled(true);
            return;
        }
        // If this was a block we built magically, don't drop items if the item being dropped
        // matches the block type. This is messy, but avoid players losing all their items
        // when suffocating inside a Blob
        Collection<ItemStack> drops = block.getDrops();
        if (drops != null) {
            for (ItemStack drop : drops) {
                if (drop.getType() == spawnedItem.getType()) {
                    com.elmakers.mine.bukkit.block.UndoList.commit(undoData);
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }
    if (Wand.isSkill(spawnedItem)) {
        event.setCancelled(true);
        return;
    }
    if (Wand.isWand(spawnedItem)) {
        boolean invulnerable = controller.getWandProperty(spawnedItem, "invulnerable", false);
        if (invulnerable) {
            CompatibilityUtils.setInvulnerable(event.getEntity());
        }
        boolean trackWand = controller.getWandProperty(spawnedItem, "track", false);
        if (trackWand) {
            Wand wand = controller.getWand(spawnedItem);
            controller.addLostWand(wand, event.getEntity().getLocation());
        }
    } else {
        // registerEntityForUndo(event.getEntity());
        if (ageDroppedItems > 0) {
            int ticks = ageDroppedItems * 20 / 1000;
            Item item = event.getEntity();
            CompatibilityUtils.ageItem(item, ticks);
        }
    }
}
Also used : Item(org.bukkit.entity.Item) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) MaterialSet(com.elmakers.mine.bukkit.api.magic.MaterialSet) Block(org.bukkit.block.Block) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) BlockData(com.elmakers.mine.bukkit.api.block.BlockData) EventHandler(org.bukkit.event.EventHandler)

Example 3 with Wand

use of com.elmakers.mine.bukkit.wand.Wand 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 4 with Wand

use of com.elmakers.mine.bukkit.wand.Wand in project MagicPlugin by elBukkit.

the class PlayerController method onPlayerAnimate.

@EventHandler(ignoreCancelled = true)
public void onPlayerAnimate(PlayerAnimationEvent event) {
    Player player = event.getPlayer();
    if (event.getAnimationType() != PlayerAnimationType.ARM_SWING) {
        return;
    }
    Mage mage = controller.getMage(player);
    Wand wand = mage.checkWand();
    if (wand == null)
        return;
    Messages messages = controller.getMessages();
    if (!controller.hasWandPermission(player)) {
        return;
    }
    // Check for region or wand-specific permissions
    if (!controller.hasWandPermission(player, wand)) {
        wand.deactivate();
        mage.sendMessage(messages.get("wand.no_permission").replace("$wand", wand.getName()));
        return;
    }
    if (!mage.checkLastClick(clickCooldown)) {
        return;
    }
    if (wand.isUpgrade())
        return;
    wand.playEffects("swing");
    wand.performAction(wand.getLeftClickAction());
}
Also used : Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) EventHandler(org.bukkit.event.EventHandler)

Example 5 with Wand

use of com.elmakers.mine.bukkit.wand.Wand in project MagicPlugin by elBukkit.

the class PlayerController method onPlayerInteract.

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) {
    if (!controller.isLoaded())
        return;
    Action action = event.getAction();
    boolean isLeftClick = action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK;
    boolean isRightClick = (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK);
    // We only care about left click and right click.
    if (!isLeftClick && !isRightClick)
        return;
    // Note that an interact on air event will arrive pre-cancelled
    // So this is kind of useless. :\
    // if (event.isCancelled()) return;
    // Block block = event.getClickedBlock();
    // controller.getLogger().info("INTERACT: " + event.getAction() + " on " + (block == null ? "NOTHING" : block.getType()) + " cancelled: " + event.isCancelled());
    Player player = event.getPlayer();
    // Don't allow interacting while holding spells, brushes or upgrades
    ItemStack itemInHand = player.getInventory().getItemInMainHand();
    boolean isSkill = Wand.isSkill(itemInHand);
    boolean isSpell = !isSkill && Wand.isSpell(itemInHand);
    if (isSpell || Wand.isBrush(itemInHand) || Wand.isUpgrade(itemInHand)) {
        event.setCancelled(true);
        return;
    }
    boolean isOffhandSkill = false;
    ItemStack itemInOffhand = player.getInventory().getItemInOffHand();
    if (isRightClick) {
        isOffhandSkill = Wand.isSkill(itemInOffhand);
        boolean isOffhandSpell = !isOffhandSkill && Wand.isSpell(itemInOffhand);
        if (isOffhandSpell || Wand.isBrush(itemInOffhand) || Wand.isUpgrade(itemInOffhand)) {
            event.setCancelled(true);
            return;
        }
    }
    Mage mage = controller.getMage(player);
    Wand wand = mage.checkWand();
    if (action == Action.RIGHT_CLICK_BLOCK) {
        Material material = event.getClickedBlock().getType();
        isRightClick = !controller.isInteractable(event.getClickedBlock());
        // This is to prevent Essentials signs from giving you an item in your wand inventory.
        if (wand != null && (material == Material.SIGN_POST || material == Material.WALL_SIGN)) {
            wand.closeInventory();
        }
    }
    if (!isLeftClick && !mage.checkLastClick(clickCooldown)) {
        return;
    }
    // Prefer wand right-click if wand is active
    if (isOffhandSkill && wand != null) {
        if (wand.getRightClickAction() != WandAction.NONE) {
            isOffhandSkill = false;
        }
    }
    if (isRightClick && (isOffhandSkill || isSkill)) {
        if (isSkill) {
            mage.useSkill(itemInHand);
        } else {
            mage.useSkill(itemInOffhand);
        }
        event.setCancelled(true);
        return;
    }
    // Check for offhand casting
    if (isRightClick) {
        if (allowOffhandCasting && mage.offhandCast()) {
            // which in the offhand case are right-click actions.
            if (cancelInteractOnLeftClick) {
                event.setCancelled(true);
            }
            return;
        }
    }
    // Special-case here for skulls, which actually are not wearable via right-click.
    if (itemInHand != null && isRightClick && controller.isWearable(itemInHand) && itemInHand.getType() != Material.SKULL_ITEM) {
        if (wand != null) {
            wand.deactivate();
        }
        controller.onArmorUpdated(mage);
        return;
    }
    if (wand == null)
        return;
    Messages messages = controller.getMessages();
    if (!controller.hasWandPermission(player)) {
        return;
    }
    // Check for region or wand-specific permissions
    if (!controller.hasWandPermission(player, wand)) {
        wand.deactivate();
        mage.sendMessage(messages.get("wand.no_permission").replace("$wand", wand.getName()));
        return;
    }
    // Check for enchantment table click
    Block clickedBlock = event.getClickedBlock();
    if (clickedBlock != null && clickedBlock.getType() != Material.AIR && enchantBlockMaterial != null && enchantBlockMaterial.is(clickedBlock)) {
        Spell spell = null;
        if (player.isSneaking()) {
            spell = enchantSneakClickSpell != null ? mage.getSpell(enchantSneakClickSpell) : null;
        } else {
            spell = enchantClickSpell != null ? mage.getSpell(enchantClickSpell) : null;
        }
        if (spell != null) {
            spell.cast();
            event.setCancelled(true);
            return;
        }
    }
    if (isLeftClick && !wand.isUpgrade() && wand.getLeftClickAction() != WandAction.NONE && cancelInteractOnLeftClick) {
        event.setCancelled(true);
    }
    if (isRightClick && wand.performAction(wand.getRightClickAction()) && cancelInteractOnRightClick) {
        event.setCancelled(true);
    }
}
Also used : Action(org.bukkit.event.block.Action) WandAction(com.elmakers.mine.bukkit.api.wand.WandAction) Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) Block(org.bukkit.block.Block) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) Spell(com.elmakers.mine.bukkit.api.spell.Spell) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Wand (com.elmakers.mine.bukkit.wand.Wand)36 Player (org.bukkit.entity.Player)24 ItemStack (org.bukkit.inventory.ItemStack)23 EventHandler (org.bukkit.event.EventHandler)17 LostWand (com.elmakers.mine.bukkit.api.wand.LostWand)11 Mage (com.elmakers.mine.bukkit.magic.Mage)11 PlayerInventory (org.bukkit.inventory.PlayerInventory)6 Spell (com.elmakers.mine.bukkit.api.spell.Spell)5 Nullable (javax.annotation.Nullable)5 LostWand (com.elmakers.mine.bukkit.wand.LostWand)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Inventory (org.bukkit.inventory.Inventory)4 GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)3 Messages (com.elmakers.mine.bukkit.api.magic.Messages)3 Location (org.bukkit.Location)3 Block (org.bukkit.block.Block)3 Entity (org.bukkit.entity.Entity)3 Item (org.bukkit.entity.Item)3