Search in sources :

Example 26 with Wand

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

the class BaseShopAction method getCaster.

@Nonnull
protected CasterProperties getCaster(CastContext context) {
    Mage mage = context.getMage();
    Wand wand = mage.getActiveWand();
    CasterProperties caster = wand;
    if (caster == null) {
        caster = mage.getActiveClass();
    }
    if (caster == null) {
        caster = mage.getProperties();
    }
    return caster;
}
Also used : CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Nonnull(javax.annotation.Nonnull)

Example 27 with Wand

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

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

the class BaseShopAction method getInventoryTitle.

protected String getInventoryTitle(CastContext context) {
    Wand wand = context.getWand();
    WandUpgradePath path = (wand == null ? null : wand.getPath());
    String pathName = (path == null ? null : path.getName());
    if (pathName == null) {
        pathName = "";
    }
    String title = context.getMessage("title", getDefaultMessage(context, "title"));
    title = title.replace("$path", pathName);
    return title;
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 29 with Wand

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

the class AbsorbAction method perform.

@SuppressWarnings("deprecation")
@Override
public SpellResult perform(CastContext context) {
    Block target = context.getTargetBlock();
    Mage mage = context.getMage();
    Wand wand = context.getWand();
    if (wand == null) {
        return SpellResult.FAIL;
    }
    MageController controller = context.getController();
    Material material = target.getType();
    byte data = target.getData();
    MaterialSet buildingMaterials = controller.getBuildingMaterialSet();
    MaterialSet restrictedMaterials = mage.getRestrictedMaterialSet();
    if (material == null || material == Material.AIR) {
        return SpellResult.NO_TARGET;
    }
    if (!mage.getCommandSender().hasPermission("Magic.bypass_restricted") && (!buildingMaterials.testBlock(target) || restrictedMaterials.testBlock(target))) {
        return SpellResult.NO_TARGET;
    }
    // Add to the wand
    MaterialAndData mat = new MaterialAndData(material, data);
    if (!wand.addBrush(mat.getKey())) {
        // Still try and activate it
        wand.setActiveBrush(mat.getKey());
        return SpellResult.NO_TARGET;
    }
    // And activate it
    wand.setActiveBrush(mat.getKey());
    return SpellResult.CAST;
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) MaterialSet(com.elmakers.mine.bukkit.api.magic.MaterialSet) Block(org.bukkit.block.Block) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Material(org.bukkit.Material)

Example 30 with Wand

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

Wand (com.elmakers.mine.bukkit.api.wand.Wand)60 Mage (com.elmakers.mine.bukkit.api.magic.Mage)47 ItemStack (org.bukkit.inventory.ItemStack)20 Player (org.bukkit.entity.Player)18 MageController (com.elmakers.mine.bukkit.api.magic.MageController)14 Entity (org.bukkit.entity.Entity)10 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)7 WandUpgradePath (com.elmakers.mine.bukkit.api.wand.WandUpgradePath)7 ArrayList (java.util.ArrayList)7 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)6 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)5 Inventory (org.bukkit.inventory.Inventory)5 ItemMeta (org.bukkit.inventory.meta.ItemMeta)5 ProgressionPath (com.elmakers.mine.bukkit.api.magic.ProgressionPath)4 Spell (com.elmakers.mine.bukkit.api.spell.Spell)4 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)4 Nullable (javax.annotation.Nullable)4 Checker (ch.njol.util.Checker)3 IOException (java.io.IOException)3 Block (org.bukkit.block.Block)3