Search in sources :

Example 6 with Mage

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

the class PlayerController method onPlayerRespawn.

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerRespawn(PlayerRespawnEvent event) {
    Mage mage = controller.getRegisteredMage(event.getPlayer());
    if (mage == null)
        return;
    mage.restoreRespawnInventories();
}
Also used : Mage(com.elmakers.mine.bukkit.magic.Mage) EventHandler(org.bukkit.event.EventHandler)

Example 7 with Mage

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

Example 8 with Mage

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

the class PlayerController method onPlayerChangeWorld.

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerChangeWorld(PlayerChangedWorldEvent event) {
    Player player = event.getPlayer();
    if (controller.isNPC(player))
        return;
    Mage mage = controller.getMage(player);
    mage.onChangeWorld();
}
Also used : Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) EventHandler(org.bukkit.event.EventHandler)

Example 9 with Mage

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

the class PlayerController method onPlayerPrePickupItem.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerPrePickupItem(PlayerPickupItemEvent event) {
    Item item = event.getItem();
    ItemStack pickup = item.getItemStack();
    if (NMSUtils.isTemporary(pickup) || item.hasMetadata("temporary")) {
        item.remove();
        event.setCancelled(true);
        return;
    }
    boolean isWand = Wand.isWand(pickup);
    // Creative mode inventory hacky work-around :\
    if (event.getPlayer().getGameMode() == GameMode.CREATIVE && isWand && enableCreativeModeEjecting) {
        event.setCancelled(true);
        return;
    }
    // Check to see if this is an item we might undo, and remove it from undo
    UndoList undoList = controller.getEntityUndo(item);
    if (undoList != null) {
        undoList.remove(item);
    }
    Player player = event.getPlayer();
    Mage mage = controller.getMage(player);
    // Remove lost wands from records
    Messages messages = controller.getMessages();
    if (isWand) {
        Wand wand = controller.getWand(pickup);
        if (!wand.canUse(player)) {
            if (lastDropWarn == 0 || System.currentTimeMillis() - lastDropWarn > 10000) {
                mage.sendMessage(messages.get("wand.bound").replace("$name", wand.getOwner()));
            }
            lastDropWarn = System.currentTimeMillis();
            event.setCancelled(true);
            return;
        }
        controller.removeLostWand(wand.getId());
    }
    // Wands will absorb spells and upgrade items
    Wand activeWand = mage.getActiveWand();
    if (activeWand != null && activeWand.isModifiable() && (Wand.isSpell(pickup) || Wand.isBrush(pickup) || Wand.isUpgrade(pickup) || Wand.isSP(pickup)) && activeWand.addItem(pickup)) {
        event.getItem().remove();
        event.setCancelled(true);
        return;
    }
    if (!mage.hasStoredInventory() && isWand) {
        mage.checkWandNextTick();
    }
}
Also used : Item(org.bukkit.entity.Item) Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 10 with Mage

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

the class PlayerController method onPlayerInteractEntity.

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    Player player = event.getPlayer();
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    Wand wand = mage.checkWand();
    // Check for a player placing a wand in an item frame
    Entity clickedEntity = event.getRightClicked();
    // Don't think this ever fires for ArmorStand - see above
    boolean isPlaceable = clickedEntity instanceof ItemFrame || clickedEntity instanceof ArmorStand;
    if (isPlaceable) {
        if (wand != null) {
            if (wand.isUndroppable()) {
                event.setCancelled(true);
                return;
            } else {
                wand.deactivate();
            }
        } else {
            ItemStack mainHand = player.getInventory().getItemInMainHand();
            ItemStack offhand = player.getInventory().getItemInOffHand();
            if (InventoryUtils.getMetaBoolean(mainHand, "undroppable", false) || (InventoryUtils.isEmpty(mainHand) && InventoryUtils.getMetaBoolean(offhand, "undroppable", false))) {
                event.setCancelled(true);
                return;
            }
        }
    }
    // this hasn't been cancelled yet
    if (controller.isNPC(event.getRightClicked())) {
        if (wand != null) {
            wand.closeInventory();
        }
        // Don't let it re-open right away
        mage.checkLastClick(0);
    } else {
        // Don't allow interacting while holding spells, brushes or upgrades
        ItemStack itemInHand = player.getInventory().getItemInMainHand();
        if (Wand.isSpell(itemInHand) || Wand.isBrush(itemInHand) || Wand.isUpgrade(itemInHand)) {
            event.setCancelled(true);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArmorStand(org.bukkit.entity.ArmorStand) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemFrame(org.bukkit.entity.ItemFrame) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Mage (com.elmakers.mine.bukkit.magic.Mage)27 EventHandler (org.bukkit.event.EventHandler)22 Player (org.bukkit.entity.Player)20 Wand (com.elmakers.mine.bukkit.wand.Wand)11 ItemStack (org.bukkit.inventory.ItemStack)11 Spell (com.elmakers.mine.bukkit.api.spell.Spell)5 GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)4 Messages (com.elmakers.mine.bukkit.api.magic.Messages)3 Entity (org.bukkit.entity.Entity)3 Inventory (org.bukkit.inventory.Inventory)3 PlayerInventory (org.bukkit.inventory.PlayerInventory)3 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)2 Item (org.bukkit.entity.Item)2 CraftingInventory (org.bukkit.inventory.CraftingInventory)2 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)1 EntityData (com.elmakers.mine.bukkit.api.entity.EntityData)1 WandAction (com.elmakers.mine.bukkit.api.wand.WandAction)1 DropActionTask (com.elmakers.mine.bukkit.magic.DropActionTask)1 CompleteDragTask (com.elmakers.mine.bukkit.utility.CompleteDragTask)1 WandMode (com.elmakers.mine.bukkit.wand.WandMode)1