Search in sources :

Example 41 with Wand

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

the class StashWandAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null) {
        if (!context.getTargetsCaster())
            return SpellResult.NO_TARGET;
        entity = context.getEntity();
    }
    if (entity == null || !(entity instanceof Player)) {
        return SpellResult.NO_TARGET;
    }
    Player player = (Player) entity;
    MageController controller = context.getController();
    Mage mage = controller.getMage(player);
    Wand activeWand = mage.getActiveWand();
    Wand offhandWand = mage.getOffhandWand();
    // Check for trying to stash an item in the offhand slot
    ItemStack activeItem = null;
    if (offhandWand == context.getWand()) {
        isOffhand = true;
        activeWand = offhandWand;
        activeItem = player.getInventory().getItemInOffHand();
    } else if (activeWand != context.getWand()) {
        return SpellResult.NO_TARGET;
    } else {
        isOffhand = false;
        activeItem = player.getInventory().getItemInMainHand();
    }
    if (InventoryUtils.isEmpty(activeItem)) {
        return SpellResult.FAIL;
    }
    if (activeWand != null) {
        activeWand.deactivate();
    }
    slotNumber = player.getInventory().getHeldItemSlot();
    if (isOffhand) {
        stashedItem = player.getInventory().getItemInOffHand();
        player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
    } else {
        stashedItem = player.getInventory().getItemInMainHand();
        player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
    }
    targetMage = mage;
    context.registerForUndo(new StashWandUndoAction());
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack)

Example 42 with Wand

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

the class MagicRequirement method getRequirementDescription.

@Nullable
public String getRequirementDescription(@Nonnull CastContext context) {
    Mage mage = context.getMage();
    MageController controller = mage.getController();
    Player player = mage.getPlayer();
    if (permissionNode != null && (player == null || !player.hasPermission(permissionNode))) {
        return context.getMessage(SpellResult.INSUFFICIENT_PERMISSION.name().toLowerCase());
    }
    Wand wand = context.getWand();
    if (wand == null && requireWand) {
        return getMessage(context, "no_wand");
    }
    if (requiredTemplate != null) {
        String template = wand.getTemplateKey();
        if (template == null || !template.equals(requiredTemplate)) {
            return getMessage(context, "no_template").replace("$wand", wand.getName());
        }
    }
    if (requiredTemplates != null) {
        String template = wand.getTemplateKey();
        if (template == null || !requiredTemplates.contains(template)) {
            return getMessage(context, "no_template").replace("$wand", wand.getName());
        }
    }
    if (mageClass != null && !mageClass.isEmpty()) {
        if (mage.hasClassUnlocked(mageClass)) {
            return getMessage(context, "no_class").replace("$class", mageClass);
        }
    }
    CasterProperties checkProperties = context.getActiveProperties();
    ProgressionPath path = checkProperties.getPath();
    if (requiredPath != null || exactPath != null) {
        if (path == null) {
            return getMessage(context, "no_path");
        }
        if (requiredPath != null && !path.hasPath(requiredPath)) {
            WandUpgradePath requiresPath = controller.getPath(requiredPath);
            String pathName = requiredPath;
            if (requiresPath != null) {
                pathName = requiresPath.getName();
            } else {
                context.getLogger().warning("Invalid path specified in requirement " + requiredPath);
            }
            return getMessage(context, "no_required_path").replace("$path", pathName);
        }
        if (exactPath != null && !exactPath.equals(path.getKey())) {
            WandUpgradePath requiresPath = controller.getPath(exactPath);
            String pathName = exactPath;
            if (requiresPath != null) {
                pathName = requiresPath.getName();
            } else {
                context.getLogger().warning("Invalid path specified in requirement: " + exactPath);
            }
            return getMessage(context, "no_path_exact").replace("$path", pathName);
        }
        if (requiresCompletedPath != null) {
            boolean hasPathCompleted = false;
            if (path.hasPath(requiresCompletedPath)) {
                if (path.getKey().equals(requiresCompletedPath)) {
                    hasPathCompleted = !path.canProgress(checkProperties);
                } else {
                    hasPathCompleted = true;
                }
            }
            if (!hasPathCompleted) {
                WandUpgradePath requiresPath = controller.getPath(requiresCompletedPath);
                String pathName = requiresCompletedPath;
                if (requiresPath != null) {
                    pathName = requiresPath.getName();
                } else {
                    context.getLogger().warning("Invalid path specified in requirement: " + exactPath);
                }
                return getMessage(context, "no_path_end").replace("$path", pathName);
            }
        }
    }
    if (wandProperties != null) {
        String message = getRequiredProperty(context, wand, wandProperties);
        if (message != null) {
            return message;
        }
    }
    if (classProperties != null) {
        MageClass activeClass = mageClass == null ? mage.getActiveClass() : mage.getClass(mageClass);
        if (activeClass == null) {
            return getMessage(context, "no_path");
        }
        String message = getRequiredProperty(context, activeClass, classProperties);
        if (message != null) {
            return message;
        }
    }
    if (attributes != null) {
        for (PropertyRequirement requirement : attributes) {
            String key = requirement.key;
            Double value = mage.getAttribute(key);
            String message = checkRequiredProperty(context, requirement, key, value);
            if (message != null) {
                return message;
            }
        }
    }
    return null;
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) MageController(com.elmakers.mine.bukkit.api.magic.MageController) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Player(org.bukkit.entity.Player) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Nullable(javax.annotation.Nullable)

Example 43 with Wand

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

the class BaseSpell method updateCooldown.

protected void updateCooldown() {
    Wand wand = currentCast != null ? currentCast.getWand() : null;
    boolean isCooldownFree = wand != null ? wand.isCooldownFree() : mage.isCooldownFree();
    double cooldownReduction = wand != null ? wand.getCooldownReduction() : mage.getCooldownReduction();
    cooldownReduction += this.cooldownReduction;
    spellData.setLastCast(System.currentTimeMillis());
    if (!isCooldownFree && cooldown > 0) {
        if (cooldownReduction < 1) {
            int reducedCooldown = (int) Math.ceil((1.0f - cooldownReduction) * cooldown);
            spellData.setCooldownExpiration(Math.max(spellData.getCooldownExpiration(), System.currentTimeMillis() + reducedCooldown));
        }
    }
    if (!isCooldownFree && mageCooldown > 0) {
        if (cooldownReduction < 1) {
            int reducedCooldown = (int) Math.ceil((1.0f - cooldownReduction) * mageCooldown);
            mage.setRemainingCooldown(reducedCooldown);
        }
    }
}
Also used : Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 44 with Wand

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

the class BaseSpell method castMessage.

/*
     * Functions to send text to player- use these to respect "quiet" and "silent" modes.
     */
/**
 * Send a message to a player when a spell is cast.
 *
 * @param message The message to send
 */
@Override
public void castMessage(String message) {
    Wand activeWand = mage.getActiveWand();
    // First check wand
    if (!loud && activeWand != null && !activeWand.showCastMessages())
        return;
    if (!quiet && canSendMessage() && message != null && message.length() > 0) {
        mage.castMessage(message);
        lastMessageSent = System.currentTimeMillis();
    }
}
Also used : Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 45 with Wand

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

the class BaseSpell method finish.

@Override
public void finish(com.elmakers.mine.bukkit.api.action.CastContext context) {
    SpellResult result = context.getResult();
    // Notify other plugins of this spell cast
    CastEvent castEvent = new CastEvent(mage, this, result);
    Bukkit.getPluginManager().callEvent(castEvent);
    // Message targets
    if (result.isSuccess() && (loud || (!mage.isQuiet() && !quiet))) {
        messageTargets("cast_player_message");
    }
    // Clear cooldown on miss
    if (result.shouldRefundCooldown(castOnNoTarget)) {
        clearCooldown();
    }
    if (cancelEffects) {
        context.cancelEffects();
    }
    // Track cast counts
    if (result.isSuccess() && !passive) {
        spellData.addCast();
        if (template != null && template.spellData != null) {
            template.spellData.addCast();
            SpellCategory category = template.getCategory();
            if (category != null) {
                category.addCast();
            }
        }
        // Reward SP
        Wand wand = context.getWand();
        Wand activeWand = mage.getActiveWand();
        if (activeWand != null && wand != null && activeWand.getItem() != null && wand.getItem() != null && !InventoryUtils.isSameInstance(wand.getItem(), activeWand.getItem()) && activeWand.getItem().equals(wand.getItem())) {
            wand = activeWand;
        }
        Wand offhandWand = mage.getOffhandWand();
        if (offhandWand != null && wand != null && offhandWand.getItem() != null && wand.getItem() != null && !InventoryUtils.isSameInstance(wand.getItem(), offhandWand.getItem()) && offhandWand.getItem().equals(wand.getItem())) {
            wand = offhandWand;
        }
        WandUpgradePath path = wand == null ? null : wand.getPath();
        if (earns > 0 && wand != null && path != null && path.earnsSP() && controller.isSPEnabled() && controller.isSPEarnEnabled() && !mage.isAtMaxSkillPoints()) {
            long now = System.currentTimeMillis();
            int scaledEarn = earns;
            if (spellData.getLastEarn() > 0 && earnCooldown > 0 && now < spellData.getLastEarn() + earnCooldown) {
                scaledEarn = (int) Math.floor((double) earns * (now - spellData.getLastEarn()) / earnCooldown);
                if (scaledEarn > 0) {
                    context.playEffects("earn_scaled_sp");
                }
            } else {
                context.playEffects("earn_sp");
            }
            if (scaledEarn > 0) {
                mage.addSkillPoints((int) Math.floor(mage.getSPMultiplier() * scaledEarn));
                spellData.setLastEarn(now);
            }
        }
        // This currently only works on wands.
        if (wand != null && wand.upgradesAllowed() && wand.getSpellLevel(spellKey.getBaseKey()) == spellKey.getLevel()) {
            if (controller.isSpellUpgradingEnabled()) {
                SpellTemplate upgrade = getUpgrade();
                long requiredCasts = getRequiredUpgradeCasts();
                String upgradePath = getRequiredUpgradePath();
                WandUpgradePath currentPath = wand.getPath();
                Set<String> upgradeTags = getRequiredUpgradeTags();
                if ((upgrade != null && requiredCasts > 0 && getCastCount() >= requiredCasts) && (upgradePath == null || upgradePath.isEmpty() || (currentPath != null && currentPath.hasPath(upgradePath))) && (upgradeTags == null || upgradeTags.isEmpty() || (currentPath != null && currentPath.hasAllTags(upgradeTags)))) {
                    if (PrerequisiteSpell.hasPrerequisites(wand, upgrade)) {
                        MageSpell newSpell = mage.getSpell(upgrade.getKey());
                        if (isActive()) {
                            deactivate(true, true);
                            if (newSpell != null) {
                                newSpell.activate();
                            }
                        }
                        wand.forceAddSpell(upgrade.getKey());
                        playEffects("upgrade");
                        if (controller.isPathUpgradingEnabled()) {
                            wand.checkAndUpgrade(true);
                        }
                        // return so progress upgrade doesn't also happen
                        return;
                    }
                }
            }
            if (maxLevels > 0 && controller.isSpellProgressionEnabled()) {
                long previousLevel = getPreviousCastProgressLevel();
                long currentLevel = getProgressLevel();
                if (currentLevel != previousLevel) {
                    wand.addSpell(getKey());
                    if (currentLevel > previousLevel) {
                        Messages messages = controller.getMessages();
                        String progressDescription = getProgressDescription();
                        playEffects("progress");
                        if (progressDescription != null && !progressDescription.isEmpty()) {
                            mage.sendMessage(messages.get("wand.spell_progression").replace("$name", getName()).replace("$wand", getName()).replace("$level", Long.toString(getProgressLevel())).replace("$max_level", Long.toString(maxLevels)));
                        }
                    }
                    if (controller.isPathUpgradingEnabled()) {
                        wand.checkAndUpgrade(true);
                    }
                }
            }
        }
    }
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) SpellCategory(com.elmakers.mine.bukkit.api.spell.SpellCategory) Messages(com.elmakers.mine.bukkit.api.magic.Messages) CastEvent(com.elmakers.mine.bukkit.api.event.CastEvent) PreCastEvent(com.elmakers.mine.bukkit.api.event.PreCastEvent) Wand(com.elmakers.mine.bukkit.api.wand.Wand) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

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