Search in sources :

Example 11 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class SkillSelectorAction method clicked.

@Override
public void clicked(InventoryClickEvent event) {
    InventoryAction action = event.getAction();
    if (action == InventoryAction.NOTHING) {
        int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
        page = page + direction;
        openInventory();
        event.setCancelled(true);
        return;
    }
    ItemStack clickedItem = event.getCurrentItem();
    if (clickedItem != null && InventoryUtils.getMetaBoolean(clickedItem, "unavailable", false)) {
        event.setCancelled(true);
        return;
    }
    Mage mage = context.getMage();
    MageController controller = mage.getController();
    Inventory inventory = mage.getInventory();
    boolean isContainerSlot = event.getSlot() == event.getRawSlot();
    boolean isDrop = action == InventoryAction.DROP_ALL_CURSOR || action == InventoryAction.DROP_ALL_SLOT || action == InventoryAction.DROP_ONE_CURSOR || action == InventoryAction.DROP_ONE_SLOT;
    if (!isContainerSlot && isDrop && controller.isSkill(clickedItem) && !InventoryUtils.getMetaBoolean(clickedItem, "undroppable", false)) {
        inventory.setItem(event.getSlot(), null);
        return;
    }
    if (inventoryLimit > 0) {
        boolean isHotbar = event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
        if (isHotbar) {
            ItemStack destinationItem = inventory.getItem(event.getHotbarButton());
            if (controller.isSkill(destinationItem))
                return;
            isHotbar = controller.isSkill(clickedItem);
        }
        if (!isContainerSlot && !isHotbar)
            return;
        int skillCount = 0;
        for (int i = 0; i < inventory.getSize(); i++) {
            ItemStack item = inventory.getItem(i);
            if (controller.isSkill(item))
                skillCount++;
        }
        if (skillCount >= inventoryLimit) {
            event.setCancelled(true);
        }
        return;
    }
    // We don't allow quick-casting here if there is an inventory limit.
    if (action == InventoryAction.PICKUP_HALF) {
        Spell spell = mage.getSpell(Wand.getSpell(clickedItem));
        if (spell != null) {
            spell.cast();
        }
        mage.getPlayer().closeInventory();
        event.setCancelled(true);
    }
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) Spell(com.elmakers.mine.bukkit.api.spell.Spell) InventoryAction(org.bukkit.event.inventory.InventoryAction)

Example 12 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class Wand method updateBrushName.

public static void updateBrushName(Messages messages, ItemStack itemStack, MaterialBrush brush, Wand wand) {
    String displayName;
    if (wand != null) {
        Spell activeSpell = wand.getActiveSpell();
        if (activeSpell != null && activeSpell.usesBrush()) {
            displayName = wand.getActiveWandName(brush);
        } else {
            displayName = ChatColor.RED + brush.getName(messages);
        }
    } else {
        displayName = brush.getName(messages);
    }
    CompatibilityUtils.setDisplayName(itemStack, displayName);
}
Also used : Spell(com.elmakers.mine.bukkit.api.spell.Spell)

Example 13 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class Wand method toggleInventory.

public void toggleInventory() {
    if (mage != null && mage.cancelSelection()) {
        mage.playSoundEffect(noActionSound);
        return;
    }
    Player player = mage == null ? null : mage.getPlayer();
    boolean isSneaking = player != null && player.isSneaking();
    Spell currentSpell = getActiveSpell();
    if (getBrushMode() == WandMode.CHEST && brushSelectSpell != null && !brushSelectSpell.isEmpty() && isSneaking && currentSpell != null && currentSpell.usesBrushSelection()) {
        Spell brushSelect = mage.getSpell(brushSelectSpell);
        if (brushSelect != null) {
            brushSelect.cast();
            return;
        }
    }
    if (!hasInventory) {
        if (activeSpell == null || activeSpell.length() == 0) {
            // Sanity check, so it'll switch to inventory next time
            updateHasInventory();
            if (spells.size() > 0) {
                setActiveSpell(spells.iterator().next());
            }
        }
        updateName();
        return;
    }
    if (!isInventoryOpen()) {
        openInventory();
    } else {
        closeInventory();
    }
}
Also used : Player(org.bukkit.entity.Player) Spell(com.elmakers.mine.bukkit.api.spell.Spell)

Example 14 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class BaseShopAction method clicked.

@Override
public void clicked(InventoryClickEvent event) {
    event.setCancelled(true);
    ItemStack item = event.getCurrentItem();
    Mage mage = context.getMage();
    if (item == null || !InventoryUtils.hasMeta(item, "shop")) {
        if (!autoClose) {
            mage.deactivateGUI();
        }
        return;
    }
    int slotIndex = Integer.parseInt(InventoryUtils.getMetaString(item, "shop"));
    MageController controller = context.getController();
    Wand wand = mage.getActiveWand();
    ShopItem shopItem = showingItems.get(slotIndex);
    if (shopItem == null) {
        return;
    }
    String unpurchasableMessage = InventoryUtils.getMetaString(shopItem.getItem(), "unpurchasable");
    if (unpurchasableMessage != null && !unpurchasableMessage.isEmpty()) {
        context.showMessage(unpurchasableMessage);
        mage.deactivateGUI();
        return;
    }
    boolean hasCosts = sell ? hasItem(controller, mage, shopItem.getItem()) : hasItemCosts(context, shopItem);
    if (!hasCosts) {
        String costString = context.getMessage("insufficient", getDefaultMessage(context, "insufficient"));
        if (sell) {
            costString = costString.replace("$cost", formatItemAmount(controller, item, shopItem.getItem().getAmount()));
        } else {
            costString = costString.replace("$cost", getItemCost(context, shopItem));
        }
        context.showMessage(costString);
    } else {
        String itemName = formatItemAmount(controller, item, item.getAmount());
        if (InventoryUtils.hasMeta(item, "confirm")) {
            String inventoryTitle = context.getMessage("confirm_title", getDefaultMessage(context, "confirm_title")).replace("$item", itemName);
            Inventory confirmInventory = CompatibilityUtils.createInventory(null, 9, inventoryTitle);
            InventoryUtils.removeMeta(item, "confirm");
            for (int i = 0; i < 9; i++) {
                if (i != 4) {
                    ItemStack filler = confirmFillMaterial.getItemStack(1);
                    ItemMeta meta = filler.getItemMeta();
                    if (meta != null) {
                        meta.setDisplayName(ChatColor.DARK_GRAY + (i < 4 ? "-->" : "<--"));
                        filler.setItemMeta(meta);
                    }
                    confirmInventory.setItem(i, filler);
                } else {
                    confirmInventory.setItem(i, item);
                }
            }
            mage.deactivateGUI();
            isActive = true;
            mage.activateGUI(this, confirmInventory);
            return;
        }
        String costString = context.getMessage("deducted", getDefaultMessage(context, "deducted"));
        if (sell) {
            costString = costString.replace("$cost", getItemCost(context, shopItem));
            removeItems(controller, mage, item, shopItem.getItem().getAmount());
            giveCosts(context, shopItem);
        } else {
            costString = costString.replace("$cost", getItemCost(context, shopItem));
            item = shopItem.getItem();
            if (requireWand) {
                if (wand == null) {
                    context.showMessage("no_wand", getDefaultMessage(context, "no_wand"));
                    mage.deactivateGUI();
                    return;
                }
                if (applyToWand && !wand.addItem(item)) {
                    String inapplicable = context.getMessage("not_applicable", getDefaultMessage(context, "not_applicable")).replace("$item", itemName);
                    context.showMessage(inapplicable);
                    mage.deactivateGUI();
                    return;
                }
            }
            CasterProperties caster = getCaster(context);
            if (applyToCaster && !caster.addItem(item)) {
                String inapplicable = context.getMessage("not_applicable", getDefaultMessage(context, "not_applicable")).replace("$item", itemName);
                context.showMessage(inapplicable);
                mage.deactivateGUI();
                return;
            }
            if (castsSpells) {
                Spell spell = null;
                String spellKey = controller.getSpell(item);
                String spellArgs = controller.getSpellArgs(item);
                spell = mage.getSpell(spellKey);
                if (spell != null && (spellArgs != null ? !spell.cast(StringUtils.split(spellArgs, ' ')) : !spell.cast())) {
                    context.showMessage("cast_fail", getDefaultMessage(context, "cast_fail"));
                    mage.deactivateGUI();
                    return;
                }
            }
            if (!takeCosts(context, shopItem)) {
                costString = context.getMessage("insufficient", getDefaultMessage(context, "insufficient"));
                costString = costString.replace("$cost", getItemCost(context, shopItem));
                context.showMessage(costString);
                return;
            }
            if (!castsSpells && !applyToWand && !applyToCaster) {
                ItemStack copy = InventoryUtils.getCopy(item);
                if (filterBound && com.elmakers.mine.bukkit.wand.Wand.isBound(copy)) {
                    Wand bindWand = controller.getWand(copy);
                    mage.tryToOwn(bindWand);
                }
                if (showActiveIcons && controller.getAPI().isWand(copy)) {
                    Wand newWand = controller.getWand(copy);
                    com.elmakers.mine.bukkit.api.block.MaterialAndData inactiveIcon = newWand.getInactiveIcon();
                    if (inactiveIcon != null) {
                        inactiveIcon.applyToItem(copy);
                    }
                }
                Player player = mage.getPlayer();
                if (putInHand) {
                    context.getController().giveItemToPlayer(player, copy);
                } else {
                    PlayerInventory inventory = player.getInventory();
                    ItemStack inHand = inventory.getItemInMainHand();
                    Integer freeSlot = null;
                    if (InventoryUtils.isEmpty(inHand)) {
                        for (int i = 0; i < inventory.getSize() && freeSlot == null; i++) {
                            if (i != inventory.getHeldItemSlot() && InventoryUtils.isEmpty(inventory.getItem(i))) {
                                freeSlot = i;
                            }
                        }
                    }
                    if (freeSlot == null) {
                        context.getController().giveItemToPlayer(player, copy);
                    } else {
                        inventory.setItem(freeSlot, copy);
                    }
                }
            }
        }
        costString = costString.replace("$item", itemName);
        context.showMessage(costString);
        if (!sell && wand != null && autoUpgrade) {
            if (upgradeLevels <= 0) {
                com.elmakers.mine.bukkit.api.wand.WandUpgradePath path = wand.getPath();
                WandUpgradePath nextPath = path != null ? path.getUpgrade() : null;
                if (nextPath != null && path.checkUpgradeRequirements(wand, null) && !path.canEnchant(wand)) {
                    path.upgrade(wand, mage);
                }
            } else {
                wand.enchant(upgradeLevels, mage, false);
            }
        }
        finalResult = SpellResult.CAST;
        onPurchase(context, item);
    }
    if (autoClose) {
        mage.deactivateGUI();
    } else {
        // update title
        mage.continueGUI(this, getInventory(context));
    }
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Player(org.bukkit.entity.Player) Wand(com.elmakers.mine.bukkit.api.wand.Wand) PlayerInventory(org.bukkit.inventory.PlayerInventory) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 15 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class ApplyCooldownAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage targetMage = context.getMage();
    if (!targetCaster) {
        Entity entity = context.getTargetEntity();
        MageController controller = context.getController();
        if (entity == null || !controller.isMage(entity)) {
            return SpellResult.NO_TARGET;
        }
        targetMage = controller.getMage(entity);
    }
    int amount = cooldownAmount;
    if (!bypassReduction && cooldownAmount > 0) {
        double cooldownReduction = targetMage.getCooldownReduction();
        if (cooldownReduction < 1) {
            amount = (int) Math.ceil((1.0f - cooldownReduction) * amount);
        } else {
            amount = 0;
        }
    }
    if (spells != null) {
        Wand wand = targetMage.getActiveWand();
        for (String spellName : spells) {
            Spell spell = null;
            if (wand != null) {
                spell = wand.getSpell(spellName);
            }
            if (spell == null) {
                spell = targetMage.getSpell(spellName);
            }
            if (spell != null) {
                if (clear) {
                    spell.clearCooldown();
                }
                if (amount > 0) {
                    spell.setRemainingCooldown(amount);
                }
            }
        }
    } else if (excludeSpells != null) {
        Collection<Spell> allSpells = targetMage.getSpells();
        for (Spell spell : allSpells) {
            if (!excludeSpells.contains(spell.getSpellKey().getBaseKey())) {
                if (clear) {
                    spell.clearCooldown();
                }
                if (amount > 0) {
                    spell.setRemainingCooldown(amount);
                }
            }
        }
    } else {
        if (clear) {
            targetMage.clearCooldown();
        }
        if (amount > 0) {
            targetMage.setRemainingCooldown(amount);
        }
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Collection(java.util.Collection) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell)

Aggregations

Spell (com.elmakers.mine.bukkit.api.spell.Spell)39 Mage (com.elmakers.mine.bukkit.api.magic.Mage)14 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)12 Player (org.bukkit.entity.Player)11 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)10 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)9 ItemStack (org.bukkit.inventory.ItemStack)9 MageController (com.elmakers.mine.bukkit.api.magic.MageController)6 Mage (com.elmakers.mine.bukkit.magic.Mage)5 ActionSpell (com.elmakers.mine.bukkit.spell.ActionSpell)5 Wand (com.elmakers.mine.bukkit.wand.Wand)5 ArrayList (java.util.ArrayList)5 Location (org.bukkit.Location)5 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)5 Wand (com.elmakers.mine.bukkit.api.wand.Wand)4 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)4 EventHandler (org.bukkit.event.EventHandler)4 Batch (com.elmakers.mine.bukkit.api.batch.Batch)3 SpellBatch (com.elmakers.mine.bukkit.api.batch.SpellBatch)3 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)3