Search in sources :

Example 6 with CastingCost

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

the class Mage method updateHotbarStatus.

public void updateHotbarStatus() {
    Player player = getPlayer();
    if (player != null) {
        Location location = getLocation();
        for (int i = 0; i < Wand.HOTBAR_SIZE; i++) {
            ItemStack spellItem = player.getInventory().getItem(i);
            String spellKey = Wand.getSpell(spellItem);
            String classKey = Wand.getSpellClass(spellItem);
            if (spellKey != null) {
                Spell spell = getSpell(spellKey);
                if (spell != null) {
                    int targetAmount = 1;
                    long remainingCooldown = 0;
                    CastingCost requiredCost = null;
                    boolean canCastSpell = false;
                    if (classKey != null && !classKey.isEmpty()) {
                        MageClass mageClass = getClass(classKey);
                        if (mageClass != null && spell instanceof BaseSpell) {
                            BaseSpell baseSpell = (BaseSpell) spell;
                            baseSpell.setMageClass(mageClass);
                            remainingCooldown = spell.getRemainingCooldown();
                            requiredCost = spell.getRequiredCost();
                            canCastSpell = spell.canCast(location);
                            baseSpell.setMageClass(null);
                        }
                    } else {
                        remainingCooldown = spell.getRemainingCooldown();
                        requiredCost = spell.getRequiredCost();
                        canCastSpell = spell.canCast(location);
                    }
                    boolean canCast = canCastSpell;
                    if (canCastSpell && remainingCooldown == 0 && requiredCost == null) {
                        targetAmount = 1;
                    } else if (canCastSpell) {
                        canCast = remainingCooldown == 0;
                        targetAmount = Wand.LiveHotbarCooldown ? (int) Math.min(Math.ceil((double) remainingCooldown / 1000), 99) : 99;
                        if (Wand.LiveHotbarCooldown && requiredCost != null) {
                            int mana = requiredCost.getMana();
                            if (mana > 0) {
                                if (mana <= getEffectiveManaMax() && getEffectiveManaRegeneration() > 0) {
                                    float remainingMana = mana - getMana();
                                    canCast = canCast && remainingMana <= 0;
                                    int targetManaTime = (int) Math.min(Math.ceil(remainingMana / getEffectiveManaRegeneration()), 99);
                                    targetAmount = Math.max(targetManaTime, targetAmount);
                                } else {
                                    canCastSpell = false;
                                    canCast = false;
                                }
                            }
                        }
                    }
                    if (targetAmount == 0)
                        targetAmount = 1;
                    boolean setAmount = false;
                    MaterialAndData disabledIcon = spell.getDisabledIcon();
                    MaterialAndData spellIcon = spell.getIcon();
                    String urlIcon = spell.getIconURL();
                    String disabledUrlIcon = spell.getDisabledIconURL();
                    boolean usingURLIcon = (controller.isUrlIconsEnabled() || spellIcon == null || spellIcon.getMaterial() == Material.AIR) && urlIcon != null && !urlIcon.isEmpty();
                    if (disabledIcon != null && spellIcon != null && !usingURLIcon) {
                        if (!canCast) {
                            if (disabledIcon.isValid() && (disabledIcon.getMaterial() != spellItem.getType() || disabledIcon.getData() != spellItem.getDurability())) {
                                disabledIcon.applyToItem(spellItem);
                            }
                            if (!canCastSpell) {
                                if (spellItem.getAmount() != 1) {
                                    spellItem.setAmount(1);
                                }
                                setAmount = true;
                            }
                        } else {
                            if (spellIcon.isValid() && (spellIcon.getMaterial() != spellItem.getType() || spellIcon.getData() != spellItem.getDurability())) {
                                spellIcon.applyToItem(spellItem);
                            }
                        }
                    } else if (usingURLIcon && disabledUrlIcon != null && !disabledUrlIcon.isEmpty() && spellItem.getType() == Material.SKULL_ITEM) {
                        String currentURL = InventoryUtils.getSkullURL(spellItem);
                        if (!canCast) {
                            if (!disabledUrlIcon.equals(currentURL)) {
                                InventoryUtils.setNewSkullURL(spellItem, disabledUrlIcon);
                                player.getInventory().setItem(i, spellItem);
                            }
                            if (!canCastSpell) {
                                if (spellItem.getAmount() != 1) {
                                    spellItem.setAmount(1);
                                }
                                setAmount = true;
                            }
                        } else {
                            if (!urlIcon.equals(currentURL)) {
                                InventoryUtils.setNewSkullURL(spellItem, urlIcon);
                                player.getInventory().setItem(i, spellItem);
                            }
                        }
                    }
                    if (!setAmount && spellItem.getAmount() != targetAmount) {
                        spellItem.setAmount(targetAmount);
                    }
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) ItemStack(org.bukkit.inventory.ItemStack) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ActionSpell(com.elmakers.mine.bukkit.spell.ActionSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) Location(org.bukkit.Location) CastSourceLocation(com.elmakers.mine.bukkit.api.magic.CastSourceLocation)

Example 7 with CastingCost

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

the class BaseSpell method checkActiveCosts.

public void checkActiveCosts() {
    if (activeCosts == null)
        return;
    long now = System.currentTimeMillis();
    activeCostScale = (float) ((double) (now - lastActiveCost) / 1000);
    lastActiveCost = now;
    CasterProperties caster = null;
    if (currentCast != null) {
        caster = currentCast.getWand();
        if (caster == null) {
            caster = currentCast.getMageClass();
        }
    }
    for (CastingCost cost : activeCosts) {
        if (!cost.has(mage, caster, this)) {
            deactivate();
            break;
        }
        cost.deduct(mage, caster, this);
    }
    activeCostScale = 1;
}
Also used : CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost)

Example 8 with CastingCost

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

the class BaseSpell method finalizeCast.

protected boolean finalizeCast(ConfigurationSection parameters) {
    SpellResult result = null;
    // Global parameters
    controller.disablePhysics(parameters.getInt("disable_physics", 0));
    if (!mage.isSuperPowered()) {
        if (backfireChance > 0 && random.nextDouble() < backfireChance) {
            backfire();
        } else if (fizzleChance > 0 && random.nextDouble() < fizzleChance) {
            result = SpellResult.FIZZLE;
        }
    }
    if (result == null) {
        result = onCast(parameters);
    }
    if (backfired) {
        result = SpellResult.BACKFIRE;
    }
    if (result == SpellResult.CAST) {
        LivingEntity sourceEntity = mage.getLivingEntity();
        Entity targetEntity = getTargetEntity();
        if (sourceEntity == targetEntity) {
            result = SpellResult.CAST_SELF;
        }
    }
    processResult(result, parameters);
    boolean success = result.isSuccess();
    boolean free = result.isFree(castOnNoTarget);
    if (!free) {
        if (costs != null && !mage.isCostFree()) {
            UndoList undoList = currentCast.getUndoList();
            for (CastingCost cost : costs) {
                if (undoList != null && cost.isItem() && currentCast != null) {
                    undoList.setConsumed(true);
                }
                cost.use(this);
            }
        }
        updateCooldown();
    }
    if (success && toggle != ToggleType.NONE) {
        activate();
    }
    sendCastMessage(result, " (" + success + ")");
    return success;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Aggregations

CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)8 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)3 MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)2 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)2 CostReducer (com.elmakers.mine.bukkit.api.spell.CostReducer)2 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)2 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)2 ArrayList (java.util.ArrayList)2 Location (org.bukkit.Location)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)2 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)2 LivingEntity (org.bukkit.entity.LivingEntity)2 ItemStack (org.bukkit.inventory.ItemStack)2 CastContext (com.elmakers.mine.bukkit.action.CastContext)1 Batch (com.elmakers.mine.bukkit.api.batch.Batch)1 SpellBatch (com.elmakers.mine.bukkit.api.batch.SpellBatch)1 PreCastEvent (com.elmakers.mine.bukkit.api.event.PreCastEvent)1 CastSourceLocation (com.elmakers.mine.bukkit.api.magic.CastSourceLocation)1 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)1