Search in sources :

Example 1 with PrerequisiteSpell

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

the class SpellShopAction method createShopItem.

@Nullable
private ShopItem createShopItem(String key, Double worth, CastContext context) {
    CasterProperties caster = getCaster(context);
    Mage mage = context.getMage();
    MageController controller = context.getController();
    ProgressionPath currentPath = caster.getPath();
    key = context.parameterize(key);
    String spellKey = key.split(" ", 2)[0];
    if (!castsSpells && caster.hasSpell(spellKey)) {
        mage.sendDebugMessage(ChatColor.GRAY + " Skipping " + spellKey + ", already have it", 3);
        return null;
    }
    SpellTemplate spell = controller.getSpellTemplate(spellKey);
    if (spell == null) {
        mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", invalid spell", 0);
        return null;
    }
    if (worth == null) {
        worth = spell.getWorth();
    }
    if (worth <= 0 && !showFree) {
        mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", is free (worth " + worth + ")", 3);
        return null;
    }
    if (!spell.hasCastPermission(mage.getCommandSender())) {
        mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", no permission", 3);
        return null;
    }
    ItemStack spellItem = controller.createSpellItem(key, castsSpells);
    if (!castsSpells) {
        Spell mageSpell = caster.getSpell(spellKey);
        String requiredPathKey = mageSpell != null ? mageSpell.getRequiredUpgradePath() : null;
        Set<String> requiredPathTags = mageSpell != null ? mageSpell.getRequiredUpgradeTags() : null;
        long requiredCastCount = mageSpell != null ? mageSpell.getRequiredUpgradeCasts() : 0;
        long castCount = mageSpell != null ? Math.min(mageSpell.getCastCount(), requiredCastCount) : 0;
        Collection<PrerequisiteSpell> missingSpells = PrerequisiteSpell.getMissingRequirements(caster, spell);
        ItemMeta meta = spellItem.getItemMeta();
        List<String> itemLore = meta.getLore();
        if (itemLore == null) {
            mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", spell item is invalid", 0);
            return null;
        }
        List<String> lore = new ArrayList<>();
        if (spell.getSpellKey().getLevel() > 1 && itemLore.size() > 0) {
            lore.add(itemLore.get(0));
        }
        String upgradeDescription = spell.getUpgradeDescription();
        if (showUpgrades && upgradeDescription != null && !upgradeDescription.isEmpty()) {
            upgradeDescription = controller.getMessages().get("spell.upgrade_description_prefix") + upgradeDescription;
            InventoryUtils.wrapText(upgradeDescription, lore);
        }
        String unpurchasableMessage = null;
        Collection<Requirement> requirements = spell.getRequirements();
        String requirementMissing = controller.checkRequirements(context, requirements);
        if (requirementMissing != null) {
            unpurchasableMessage = requirementMissing;
            InventoryUtils.wrapText(unpurchasableMessage, lore);
        }
        if ((requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) || (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) || (requiredPathTags != null && !currentPath.hasAllTags(requiredPathTags)) || !missingSpells.isEmpty()) {
            if (mageSpell != null && !spell.getName().equals(mageSpell.getName())) {
                lore.add(context.getMessage("upgrade_name_change", getDefaultMessage(context, "upgrade_name_change")).replace("$name", mageSpell.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) {
                    unpurchasableMessage = context.getMessage("level_requirement", getDefaultMessage(context, "level_requirement")).replace("$path", upgradePath.getName());
                    InventoryUtils.wrapText(unpurchasableMessage, lore);
                }
            } else if (requiredPathTags != null && !requiredPathTags.isEmpty() && !currentPath.hasAllTags(requiredPathTags)) {
                Set<String> tags = currentPath.getMissingTags(requiredPathTags);
                unpurchasableMessage = context.getMessage("tags_requirement", getDefaultMessage(context, "tags_requirement")).replace("$tags", controller.getMessages().formatList("tags", tags, "name"));
                InventoryUtils.wrapText(unpurchasableMessage, lore);
            }
            if (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) {
                unpurchasableMessage = ChatColor.RED + context.getMessage("cast_requirement", getDefaultMessage(context, "cast_requirement")).replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount));
                InventoryUtils.wrapText(unpurchasableMessage, lore);
            }
            if (!missingSpells.isEmpty()) {
                List<String> spells = new ArrayList<>(missingSpells.size());
                for (PrerequisiteSpell s : missingSpells) {
                    SpellTemplate template = context.getController().getSpellTemplate(s.getSpellKey().getKey());
                    String spellMessage = context.getMessage("prerequisite_spell_level", getDefaultMessage(context, "prerequisite_spell_level")).replace("$name", template.getName());
                    if (s.getProgressLevel() > 1) {
                        spellMessage += context.getMessage("prerequisite_spell_progress_level", getDefaultMessage(context, "prerequisite_spell_progress_level")).replace("$level", Long.toString(s.getProgressLevel())).replace("$max_level", Long.toString(Math.max(1, template.getMaxProgressLevel())));
                    }
                    spells.add(spellMessage);
                }
                unpurchasableMessage = ChatColor.RED + context.getMessage("required_spells", getDefaultMessage(context, "required_spells")).replace("$spells", StringUtils.join(spells, ", "));
                InventoryUtils.wrapText(ChatColor.GOLD + unpurchasableMessage, lore);
            }
        }
        for (int i = (spell.getSpellKey().getLevel() > 1 ? 1 : 0); i < itemLore.size(); i++) {
            lore.add(itemLore.get(i));
        }
        meta.setLore(lore);
        spellItem.setItemMeta(meta);
        if (unpurchasableMessage != null)
            InventoryUtils.setMeta(spellItem, "unpurchasable", unpurchasableMessage);
    }
    return new ShopItem(spellItem, worth);
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Set(java.util.Set) ArrayList(java.util.ArrayList) Spell(com.elmakers.mine.bukkit.api.spell.Spell) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Requirement(com.elmakers.mine.bukkit.api.requirements.Requirement) Mage(com.elmakers.mine.bukkit.api.magic.Mage) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) Nullable(javax.annotation.Nullable)

Example 2 with PrerequisiteSpell

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

the class WandUpgradePath method load.

protected void load(MageController controller, String key, ConfigurationSection template) {
    // Cache spells, mainly used for spellbooks
    Collection<PrerequisiteSpell> pathSpells = ConfigurationUtils.getPrerequisiteSpells(controller, template, "spells", "path " + key, true);
    for (PrerequisiteSpell prereq : pathSpells) {
        spells.add(prereq.getSpellKey().getKey());
    }
    allSpells.addAll(spells);
    Collection<PrerequisiteSpell> pathExtraSpells = ConfigurationUtils.getPrerequisiteSpells(controller, template, "extra_spells", "path " + key, true);
    for (PrerequisiteSpell prereq : pathExtraSpells) {
        extraSpells.add(prereq.getSpellKey().getKey());
    }
    allExtraSpells.addAll(extraSpells);
    // Get brush info
    brushes.addAll(ConfigurationUtils.getKeysOrList(template, "brushes"));
    allBrushes.addAll(brushes);
    // Upgrade information
    followsPath = template.getString("follows");
    upgradeKey = template.getString("upgrade");
    upgradeItemKey = template.getString("upgrade_item");
    Collection<PrerequisiteSpell> prerequisiteSpells = ConfigurationUtils.getPrerequisiteSpells(controller, template, "required_spells", "path " + key, false);
    this.requiredSpells = new ArrayList<>(pathSpells.size() + prerequisiteSpells.size());
    requiredSpells.addAll(pathSpells);
    requiredSpells.addAll(prerequisiteSpells);
    requiredSpellKeys = new HashSet<>(prerequisiteSpells.size());
    for (PrerequisiteSpell prereq : prerequisiteSpells) {
        requiredSpellKeys.add(prereq.getSpellKey().getKey());
        allRequiredSpells.add(prereq.getSpellKey().getKey());
    }
    // Icon information for upgrading/migrating wands
    icon = ConfigurationUtils.getMaterialAndData(template, "icon");
    migrateIcon = ConfigurationUtils.getMaterialAndData(template, "migrate_icon");
    // Validate requirements - disabling a required spell disables the upgrade.
    for (PrerequisiteSpell requiredSpell : requiredSpells) {
        SpellTemplate spell = controller.getSpellTemplate(requiredSpell.getSpellKey().getKey());
        if (spell == null) {
            controller.getLogger().warning("Invalid spell required for upgrade: " + requiredSpell.getSpellKey().getKey() + ", upgrade path " + key + " will disable upgrades");
            upgradeKey = null;
        }
    }
    matchSpellMana = template.getBoolean("match_spell_mana", matchSpellMana);
    hidden = template.getBoolean("hidden", false);
    earnsSP = template.getBoolean("earns_sp", earnsSP);
    // Description information
    Messages messages = controller.getMessages();
    name = template.getString("name", name);
    name = messages.get("paths." + key + ".name", name);
    description = template.getString("description", description);
    description = messages.get("paths." + key + ".description", description);
    // Upgrade commands
    upgradeCommands = template.getStringList("upgrade_commands");
    // Effects
    if (template.contains("effects")) {
        effects.clear();
        ConfigurationSection effectsNode = template.getConfigurationSection("effects");
        Collection<String> effectKeys = effectsNode.getKeys(false);
        for (String effectKey : effectKeys) {
            if (effectsNode.isString(effectKey)) {
                String referenceKey = effectsNode.getString(effectKey);
                if (effects.containsKey(referenceKey)) {
                    effects.put(effectKey, new ArrayList<>(effects.get(referenceKey)));
                }
            } else {
                effects.put(effectKey, EffectPlayer.loadEffects(controller.getPlugin(), effectsNode, effectKey));
            }
        }
    }
    // Fetch overall limits
    maxUses = template.getInt("max_uses", maxUses);
    maxMaxMana = template.getInt("max_mana", maxMaxMana);
    maxManaRegeneration = template.getInt("max_mana_regeneration", maxManaRegeneration);
    maxMana = template.getInt("mana_max", maxMana);
    manaRegeneration = template.getInt("mana_regeneration", manaRegeneration);
    minLevel = template.getInt("min_enchant_level", minLevel);
    maxLevel = template.getInt("max_enchant_level", maxLevel);
    ConfigurationSection maxConfig = template.getConfigurationSection("max_properties");
    if (maxConfig != null) {
        for (String maxKey : maxConfig.getKeys(false)) {
            double value = maxConfig.getDouble(maxKey);
            maxProperties.put(maxKey.replace("|", "."), value);
        }
    }
    Collection<String> tagList = ConfigurationUtils.getStringList(template, "tags");
    if (tagList != null && !tagList.isEmpty()) {
        if (tags == null) {
            tags = new HashSet<>(tagList);
        } else {
            tags.addAll(tagList);
        }
    }
    // Parse defined levels
    if (levelMap == null) {
        levelMap = new TreeMap<>();
    }
    if (template.contains("levels")) {
        String[] levelStrings = StringUtils.split(template.getString("levels"), ',');
        levels = new int[levelStrings.length];
        for (int i = 0; i < levels.length; i++) {
            levels[i] = Integer.parseInt(levelStrings[i]);
        }
    }
    if (levels == null) {
        levels = new int[1];
        levels[0] = 1;
    }
    for (int level = 1; level <= levels[levels.length - 1]; level++) {
        // TODO: Could this be optimized?
        int levelIndex;
        int nextLevelIndex = 0;
        float distance = 1;
        for (levelIndex = 0; levelIndex < levels.length; levelIndex++) {
            if (level == levels[levelIndex] || levelIndex == levels.length - 1) {
                nextLevelIndex = levelIndex;
                distance = 0;
                break;
            }
            if (level < levels[levelIndex + 1]) {
                nextLevelIndex = levelIndex + 1;
                int previousLevel = levels[levelIndex];
                int nextLevel = levels[nextLevelIndex];
                distance = (float) (level - previousLevel) / (float) (nextLevel - previousLevel);
                break;
            }
        }
        WandLevel wandLevel = levelMap.get(level);
        WandLevel newLevel = new WandLevel(this, controller, template, levelIndex, nextLevelIndex, distance);
        if (wandLevel == null) {
            wandLevel = newLevel;
        } else {
            newLevel.add(wandLevel);
            wandLevel = newLevel;
        }
        levelMap.put(level, wandLevel);
    }
}
Also used : Messages(com.elmakers.mine.bukkit.api.magic.Messages) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 3 with PrerequisiteSpell

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

the class WandUpgradePath method checkUpgradeRequirements.

@Override
public boolean checkUpgradeRequirements(com.elmakers.mine.bukkit.api.wand.Wand wand, com.elmakers.mine.bukkit.api.magic.Mage mage) {
    if (requiredSpells == null || requiredSpells.isEmpty())
        return true;
    // Then check for spell requirements to advance
    for (PrerequisiteSpell prereq : requiredSpells) {
        if (!wand.hasSpell(prereq.getSpellKey())) {
            SpellTemplate spell = wand.getController().getSpellTemplate(prereq.getSpellKey().getKey());
            if (spell == null) {
                wand.getController().getLogger().warning("Invalid spell required for upgrade: " + prereq.getSpellKey().getKey());
                return false;
            }
            if (mage != null) {
                String requiredSpellMessage = getMessage(wand.getController().getMessages(), "required_spell");
                String message = requiredSpellMessage.replace("$spell", spell.getName());
                com.elmakers.mine.bukkit.api.wand.WandUpgradePath upgradePath = getUpgrade();
                if (upgradePath != null) {
                    message = message.replace("$path", upgradePath.getName());
                }
                mage.sendMessage(message);
            }
            return false;
        } else if (mage != null) {
            Spell spell = wand.getSpell(prereq.getSpellKey().getKey(), mage);
            if (!PrerequisiteSpell.isSpellSatisfyingPrerequisite(spell, prereq)) {
                if (spell != null) {
                    String message = getMessage(wand.getController().getMessages(), "spell.prerequisite_spell_level").replace("$name", spell.getName()).replace("$level", Integer.toString(prereq.getSpellKey().getLevel()));
                    if (prereq.getProgressLevel() > 1) {
                        message += getMessage(wand.getController().getMessages(), "spell.prerequisite_spell_progress_level").replace("$level", Long.toString(prereq.getProgressLevel())).replace("$max_level", Long.toString(Math.max(1, spell.getMaxProgressLevel())));
                    }
                    mage.sendMessage(message);
                }
                return false;
            }
        }
    }
    return true;
}
Also used : PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 4 with PrerequisiteSpell

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

the class ConfigurationUtils method getPrerequisiteSpells.

public static Collection<PrerequisiteSpell> getPrerequisiteSpells(MageController controller, ConfigurationSection node, String key, String loadContext, boolean removeMissing) {
    if (node == null || key == null) {
        return new ArrayList<>(0);
    }
    Collection<?> spells = null;
    if (node.isString(key)) {
        spells = ConfigurationUtils.getStringList(node, key);
    } else if (node.isConfigurationSection(key)) {
        ConfigurationSection spellSection = node.getConfigurationSection(key);
        if (spellSection != null) {
            spells = spellSection.getKeys(false);
        }
    } else {
        spells = node.getList(key);
    }
    if (spells == null) {
        spells = new ArrayList<>(0);
    }
    List<PrerequisiteSpell> requiredSpells = new ArrayList<>(spells.size());
    for (Object o : spells) {
        PrerequisiteSpell prerequisiteSpell = null;
        if (o instanceof String) {
            prerequisiteSpell = new PrerequisiteSpell(new SpellKey((String) o), 0);
        } else if (o instanceof ConfigurationSection) {
            ConfigurationSection section = (ConfigurationSection) o;
            String spell = section.getString("spell");
            long progressLevel = section.getLong("progress_level");
            prerequisiteSpell = new PrerequisiteSpell(new SpellKey(spell), progressLevel);
        } else if (o instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) o;
            String spell = map.get("spell").toString();
            String progressLevelString = map.containsKey("progress_level") ? map.get("progress_level").toString() : "0";
            if (spell != null && StringUtils.isNumeric(progressLevelString)) {
                long progressLevel = 0;
                try {
                    progressLevel = Long.parseLong(progressLevelString);
                } catch (NumberFormatException ignore) {
                }
                prerequisiteSpell = new PrerequisiteSpell(new SpellKey(spell), progressLevel);
            }
        }
        if (prerequisiteSpell != null) {
            if (controller.getSpellTemplate(prerequisiteSpell.getSpellKey().getKey()) != null) {
                requiredSpells.add(prerequisiteSpell);
            } else {
                if (!removeMissing) {
                    requiredSpells.add(prerequisiteSpell);
                    controller.getLogger().warning("Unknown or disabled spell requirement " + prerequisiteSpell.getSpellKey().getKey() + " in " + loadContext + ", upgrade will be disabled");
                } else {
                    controller.getLogger().warning("Unknown or disabled spell prerequisite " + prerequisiteSpell.getSpellKey().getKey() + " in " + loadContext + ", ignoring");
                }
            }
        }
    }
    return requiredSpells;
}
Also used : ArrayList(java.util.ArrayList) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) HashMap(java.util.HashMap) Map(java.util.Map) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

PrerequisiteSpell (com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell)4 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)3 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 ArrayList (java.util.ArrayList)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)2 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)1 Mage (com.elmakers.mine.bukkit.api.magic.Mage)1 MageController (com.elmakers.mine.bukkit.api.magic.MageController)1 Messages (com.elmakers.mine.bukkit.api.magic.Messages)1 ProgressionPath (com.elmakers.mine.bukkit.api.magic.ProgressionPath)1 Requirement (com.elmakers.mine.bukkit.api.requirements.Requirement)1 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)1 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)1 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1 ItemStack (org.bukkit.inventory.ItemStack)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1