Search in sources :

Example 11 with MageSpell

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

the class Mage method tick.

@Override
public void tick() {
    if (isNPC)
        return;
    long now = System.currentTimeMillis();
    if (entityData != null) {
        if (lastTick != 0) {
            long tickInterval = entityData.getTickInterval();
            if (tickInterval > 0 && now - lastTick > tickInterval) {
                updateVelocity();
                entityData.tick(this);
                lastTick = now;
            }
        } else {
            lastTick = now;
        }
    } else {
        updateVelocity();
        lastTick = now;
    }
    // We don't tick non-player or offline Mages, except
    // above where entityData is ticked if present.
    Player player = getPlayer();
    if (player != null && player.isOnline()) {
        checkWand();
        if (activeWand != null) {
            activeWand.tick();
        } else if (virtualExperience) {
            resetSentExperience();
        }
        if (offhandWand != null) {
            offhandWand.tick();
        }
        if (activeClass != null) {
            activeClass.tick();
        }
        properties.tick();
        if (Wand.LiveHotbarSkills && (activeWand == null || !activeWand.isInventoryOpen())) {
            updateHotbarStatus();
        }
        // it turns out that is not easy to check efficiently.
        if (JUMP_EFFECT_FLIGHT_EXEMPTION_DURATION > 0 && player.hasPotionEffect(PotionEffectType.JUMP)) {
            controller.addFlightExemption(player, JUMP_EFFECT_FLIGHT_EXEMPTION_DURATION);
        }
        for (Wand armorWand : activeArmor.values()) {
            armorWand.updateEffects(this);
        }
        // Copy this set since spells may get removed while iterating!
        List<MageSpell> active = new ArrayList<>(activeSpells);
        for (MageSpell spell : active) {
            spell.tick();
            if (!spell.isActive()) {
                deactivateSpell(spell);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.wand.Wand) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell)

Example 12 with MageSpell

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

the class Mage method loadSpells.

protected void loadSpells(Map<String, ConfigurationSection> spellConfiguration) {
    if (spellConfiguration == null)
        return;
    Collection<MageSpell> currentSpells = new ArrayList<>(spells.values());
    for (MageSpell spell : currentSpells) {
        String key = spell.getKey();
        if (spellConfiguration.containsKey(key)) {
            ConfigurationSection template = spellConfiguration.get(key);
            String className = template.getString("class");
            if (className == null) {
                className = ActionSpell.class.getName();
            }
            // TODO: Still unsure if this is right.
            if (!spell.getClass().getName().contains(className)) {
                // SpellData spellData = new SpellData(key);
                spell.save(null);
                spells.remove(key);
                this.spellData.put(key, spell.getSpellData());
            } else {
                spell.loadTemplate(key, template);
                spell.loadPrerequisites(template);
            }
        } else {
            spells.remove(key);
        }
    }
}
Also used : ActionSpell(com.elmakers.mine.bukkit.spell.ActionSpell) ArrayList(java.util.ArrayList) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 13 with MageSpell

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

the class SpellProgressAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage mage = context.getMage();
    CasterProperties casterProperties = context.getActiveProperties();
    this.context = context;
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    Collection<String> spells = casterProperties.getSpells();
    Collection<ItemStack> upgrades = new ArrayList<>();
    Messages messages = context.getController().getMessages();
    for (String spellKey : spells) {
        MageSpell spell = mage.getSpell(spellKey);
        SpellTemplate upgradeSpell = spell.getUpgrade();
        if (upgradeSpell != null) {
            ItemStack spellItem = MagicPlugin.getAPI().createSpellItem(upgradeSpell.getKey());
            if (spellItem != null) {
                long requiredCastCount = spell.getRequiredUpgradeCasts();
                String requiredPathKey = spell.getRequiredUpgradePath();
                Set<String> requiredPathTags = spell.getRequiredUpgradeTags();
                ItemMeta meta = spellItem.getItemMeta();
                List<String> lore = new ArrayList<>();
                String levelDescription = upgradeSpell.getLevelDescription();
                if (levelDescription == null || levelDescription.isEmpty()) {
                    levelDescription = upgradeSpell.getName();
                }
                lore.add(levelDescription);
                String upgradeDescription = upgradeSpell.getUpgradeDescription();
                if (upgradeDescription != null && !upgradeDescription.isEmpty()) {
                    upgradeDescription = context.getController().getMessages().get("spell.upgrade_description_prefix") + upgradeDescription;
                    InventoryUtils.wrapText(upgradeDescription, lore);
                }
                ProgressionPath currentPath = casterProperties.getPath();
                if (requiredPathKey != null && currentPath == null) {
                    continue;
                }
                if (!upgradeSpell.getName().equals(spell.getName())) {
                    lore.add(context.getMessage("upgrade_name_change", "&r&4Upgrades: &r$name").replace("$name", spell.getName()));
                }
                if (requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) {
                    requiredPathKey = currentPath.translatePath(requiredPathKey);
                    com.elmakers.mine.bukkit.wand.WandUpgradePath upgradePath = com.elmakers.mine.bukkit.wand.WandUpgradePath.getPath(requiredPathKey);
                    if (upgradePath == null)
                        continue;
                    lore.add(context.getMessage("level_requirement").replace("$path", upgradePath.getName()));
                }
                if (requiredPathTags != null && !requiredPathTags.isEmpty() && !currentPath.hasAllTags(requiredPathTags)) {
                    Set<String> tags = currentPath.getMissingTags(requiredPathTags);
                    lore.add(context.getMessage("tags_requirement").replace("$tags", messages.formatList("tags", tags, "name")));
                }
                long castCount = Math.min(spell.getCastCount(), requiredCastCount);
                if (castCount == requiredCastCount) {
                    lore.add(ChatColor.GREEN + context.getMessage("cast_requirement").replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount)));
                } else {
                    lore.add(ChatColor.RED + context.getMessage("cast_requirement").replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount)));
                }
                meta.setLore(lore);
                spellItem.setItemMeta(meta);
                upgrades.add(spellItem);
            }
        }
    }
    String inventoryTitle = context.getMessage("title", "Spell Upgrades");
    int invSize = ((upgrades.size() + 9) / 9) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    for (ItemStack item : upgrades) {
        displayInventory.addItem(item);
    }
    mage.activateGUI(this, displayInventory);
    return SpellResult.CAST;
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) ArrayList(java.util.ArrayList) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Inventory(org.bukkit.inventory.Inventory) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 14 with MageSpell

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

MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)14 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)6 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)3 Mage (com.elmakers.mine.bukkit.api.magic.Mage)3 ProgressionPath (com.elmakers.mine.bukkit.api.magic.ProgressionPath)3 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)3 ItemStack (org.bukkit.inventory.ItemStack)3 ItemMeta (org.bukkit.inventory.meta.ItemMeta)3 SpellData (com.elmakers.mine.bukkit.api.data.SpellData)2 Messages (com.elmakers.mine.bukkit.api.magic.Messages)2 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 Wand (com.elmakers.mine.bukkit.api.wand.Wand)2 ActionSpell (com.elmakers.mine.bukkit.spell.ActionSpell)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)2 Player (org.bukkit.entity.Player)2 CastEvent (com.elmakers.mine.bukkit.api.event.CastEvent)1 PreCastEvent (com.elmakers.mine.bukkit.api.event.PreCastEvent)1 MageController (com.elmakers.mine.bukkit.api.magic.MageController)1