Search in sources :

Example 26 with SpellKey

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

the class Wand method getSpell.

@Nullable
@Override
public Spell getSpell(String spellKey, com.elmakers.mine.bukkit.api.magic.Mage mage) {
    if (mage == null) {
        return null;
    }
    SpellKey key = new SpellKey(spellKey);
    spellKey = key.getBaseKey();
    if (!spells.contains(spellKey))
        return null;
    Integer level = spellLevels.get(spellKey);
    if (level != null) {
        spellKey = new SpellKey(spellKey, level).getKey();
    }
    return mage.getSpell(spellKey);
}
Also used : SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) Nullable(javax.annotation.Nullable)

Example 27 with SpellKey

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

the class WandOrganizer method organize.

public void organize() {
    Map<String, Integer> spells = wand.getSpellInventory();
    Map<String, Integer> brushes = wand.getBrushInventory();
    removeHotbar(spells, brushes);
    // Collect favorite spells
    MageController controller = wand.getController();
    TreeMap<Long, List<String>> favoriteSpells = new TreeMap<>();
    Map<String, Collection<String>> groupedSpells = new TreeMap<>();
    for (String spellName : spells.keySet()) {
        Spell mageSpell = mage == null ? null : mage.getSpell(spellName);
        SpellTemplate spell = mageSpell == null ? controller.getSpellTemplate(spellName) : mageSpell;
        if (spell != null) {
            // Sum up all levels of this spell:
            long castCount = 0;
            int spellLevel = 1;
            while (mageSpell != null) {
                castCount += mageSpell.getCastCount();
                spellLevel++;
                SpellKey spellKey = new SpellKey(spellName, spellLevel);
                String key = spellKey.getKey();
                mageSpell = mage.hasSpell(key) ? mage.getSpell(key) : null;
            }
            spellName = spell.getSpellKey().getBaseKey();
            if (castCount > favoriteCastCountThreshold) {
                List<String> favorites = null;
                if (!favoriteSpells.containsKey(castCount)) {
                    favorites = new ArrayList<>();
                    favoriteSpells.put(castCount, favorites);
                } else {
                    favorites = favoriteSpells.get(castCount);
                }
                favorites.add(spellName);
            }
            SpellCategory spellCategory = spell.getCategory();
            String category = spellCategory == null ? null : spellCategory.getKey();
            if (category == null || category.length() == 0) {
                category = "default";
            }
            Collection<String> spellList = groupedSpells.get(category);
            if (spellList == null) {
                spellList = new TreeSet<>();
                groupedSpells.put(category, spellList);
            }
            spellList.add(spellName);
        }
    }
    Map<String, String> materials = new TreeMap<>();
    if (wand.getBrushMode() == WandMode.INVENTORY) {
        for (String materialKey : brushes.keySet()) {
            if (MaterialBrush.isSpecialMaterialKey(materialKey)) {
                materials.put(" " + materialKey, materialKey);
            } else {
                materials.put(materialKey, materialKey);
            }
        }
    }
    currentInventoryIndex = 0;
    currentInventoryCount = 0;
    // Organize favorites
    WandMode mode = wand.getMode();
    Set<String> addedFavorites = new HashSet<>();
    List<String> favoriteList = new ArrayList<>();
    int favoritePageSize = wand.getInventorySize() - favoritePageBuffer;
    for (List<String> favorites : favoriteSpells.descendingMap().values()) {
        if (addedFavorites.size() >= favoritePageSize)
            break;
        for (String spellName : favorites) {
            addedFavorites.add(spellName);
            favoriteList.add(spellName);
            if (addedFavorites.size() >= favoritePageSize)
                break;
        }
    }
    if (addedFavorites.size() > 0) {
        for (String favorite : favoriteList) {
            int slot = getNextSlot();
            spells.put(favorite, slot);
        }
        if (mode != WandMode.CHEST && addedFavorites.size() > wand.getInventorySize() - favoriteCountBuffer) {
            nextPage();
        }
    } else {
        addedFavorites.clear();
    }
    // Add unused spells by category
    int inventoryOrganizeNewGroupSize = wand.getInventorySize() - inventoryOrganizeNewGroupBuffer;
    for (Collection<String> spellGroup : groupedSpells.values()) {
        // Start a new inventory for a new group if the previous inventory is over 2/3 full
        if (mode != WandMode.CHEST && currentInventoryCount > inventoryOrganizeNewGroupSize) {
            nextPage();
        }
        for (String spellName : spellGroup) {
            if (!addedFavorites.contains(spellName)) {
                int slot = getNextSlot();
                spells.put(spellName, slot);
            }
        }
    }
    if (materials.size() > 0) {
        nextPage();
        for (String materialName : materials.values()) {
            brushes.put(materialName, getNextSlot());
        }
    }
    wand.updateSpellInventory(spells);
    if (materials.size() > 0) {
        wand.updateBrushInventory(brushes);
    }
}
Also used : SpellCategory(com.elmakers.mine.bukkit.api.spell.SpellCategory) ArrayList(java.util.ArrayList) Spell(com.elmakers.mine.bukkit.api.spell.Spell) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) MageController(com.elmakers.mine.bukkit.api.magic.MageController) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) TreeMap(java.util.TreeMap) Collection(java.util.Collection) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 28 with SpellKey

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

Example 29 with SpellKey

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

the class SkillSelectorAction method perform.

@Override
public SpellResult perform(CastContext context) {
    this.context = context;
    MageController apiController = context.getController();
    Player player = context.getMage().getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (!(apiController instanceof MagicController))
        return SpellResult.FAIL;
    MagicController controller = (MagicController) apiController;
    Messages messages = controller.getMessages();
    HeroesManager heroes = controller.getHeroes();
    allSkills.clear();
    if (controller.useHeroesSkills() && heroes != null) {
        @Nonnull String classString = heroes.getClassName(player);
        @Nonnull String class2String = heroes.getSecondaryClassName(player);
        String messageKey = !class2String.isEmpty() ? "skills.inventory_title_secondary" : "skills.inventory_title";
        inventoryTitle = controller.getMessages().get(messageKey, "Skills ($page/$pages)");
        inventoryTitle = inventoryTitle.replace("$class2", class2String).replace("$class", classString);
        List<String> heroesSkills = heroes.getSkillList(player, true, true);
        for (String heroesSkill : heroesSkills) {
            allSkills.add(new SkillDescription(heroes, controller, player, heroesSkill));
        }
    } else {
        inventoryTitle = messages.get("skills.inventory_title");
    }
    if (controller.usePermissionSkills()) {
        boolean bypassHidden = player.hasPermission("Magic.bypass_hidden");
        Collection<SpellTemplate> spells = controller.getSpellTemplates(bypassHidden);
        for (SpellTemplate spell : spells) {
            SpellKey key = spell.getSpellKey();
            if (key.isVariant())
                continue;
            if (key.getBaseKey().startsWith("heroes*"))
                continue;
            if (!spell.hasCastPermission(player))
                continue;
            allSkills.add(new SkillDescription(spell));
        }
    } else {
        Mage mage = controller.getMage(player);
        MageClass activeClass = mage.getActiveClass();
        if (activeClass != null) {
            // gather spells in player's inventory to avoid showing
            Set<String> hasSpells = new HashSet<>();
            for (ItemStack item : player.getInventory().getContents()) {
                String spellKey = controller.getSpell(item);
                if (spellKey != null) {
                    hasSpells.add(spellKey);
                }
            }
            classKey = activeClass.getKey();
            skillsConfig = activeClass.getProperty("skills", ConfigurationSection.class);
            inventoryLimit = activeClass.getProperty("skills.skill_limit", 0);
            Collection<String> spells = activeClass.getSpells();
            for (String spellKey : spells) {
                if (hasSpells.contains(spellKey)) {
                    extraSlots++;
                    continue;
                }
                SpellTemplate spell = controller.getSpellTemplate(spellKey);
                if (spell != null) {
                    allSkills.add(new SkillDescription(spell));
                }
            }
        }
    }
    if (allSkills.size() == 0) {
        player.sendMessage(messages.get("skills.none"));
        return SpellResult.NO_ACTION;
    }
    Collections.sort(allSkills);
    openInventory();
    return SpellResult.CAST;
}
Also used : Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Nonnull(javax.annotation.Nonnull) HeroesManager(com.elmakers.mine.bukkit.heroes.HeroesManager) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MagicController(com.elmakers.mine.bukkit.magic.MagicController) ItemStack(org.bukkit.inventory.ItemStack) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) HashSet(java.util.HashSet) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 30 with SpellKey

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

the class BaseSpell method loadTemplate.

protected void loadTemplate(ConfigurationSection node) {
    // Get localizations
    String baseKey = spellKey.getBaseKey();
    // Message defaults come from the messages.yml file
    name = controller.getMessages().get("spells." + baseKey + ".name", baseKey);
    description = controller.getMessages().get("spells." + baseKey + ".description", "");
    extendedDescription = controller.getMessages().get("spells." + baseKey + ".extended_description", "");
    usage = controller.getMessages().get("spells." + baseKey + ".usage", "");
    // Upgrade path information
    // The actual upgrade spell will be set externally.
    requiredUpgradePath = node.getString("upgrade_required_path");
    if (requiredUpgradePath != null && requiredUpgradePath.isEmpty()) {
        requiredUpgradePath = null;
    }
    requiredUpgradeCasts = node.getLong("upgrade_required_casts");
    List<String> pathTags = ConfigurationUtils.getStringList(node, "upgrade_required_path_tags");
    if (pathTags == null || pathTags.isEmpty()) {
        requiredUpgradeTags = null;
    } else {
        requiredUpgradeTags = new HashSet<>(pathTags);
    }
    requiredSpells = new ArrayList<>();
    List<String> removesSpellKeys = ConfigurationUtils.getStringList(node, "removes_spells");
    if (removesSpellKeys != null) {
        removesSpells = new ArrayList<>(removesSpellKeys.size());
        for (String key : removesSpellKeys) {
            removesSpells.add(new SpellKey(key));
        }
    } else {
        removesSpells = new ArrayList<>(0);
    }
    // Inheritance, currently only used to look up messages, and only goes one level deep
    inheritKey = node.getString("inherit");
    progressDescription = controller.getMessages().get("spell.progress_description", progressDescription);
    // Can be overridden by the base spell, or the variant spell
    levelDescription = controller.getMessages().get("spells." + baseKey + ".level_description", levelDescription);
    progressDescription = controller.getMessages().get("spells." + baseKey + ".progress_description", progressDescription);
    upgradeDescription = controller.getMessages().get("spells." + baseKey + ".upgrade_description", upgradeDescription);
    // Spell level variants can override
    if (spellKey.isVariant()) {
        // Level description defaults to pre-formatted text
        levelDescription = controller.getMessages().get("spell.level_description", levelDescription);
        String variantKey = spellKey.getKey();
        name = controller.getMessages().get("spells." + variantKey + ".name", name);
        description = controller.getMessages().get("spells." + variantKey + ".description", description);
        extendedDescription = controller.getMessages().get("spells." + variantKey + ".extended_description", extendedDescription);
        usage = controller.getMessages().get("spells." + variantKey + ".usage", usage);
        // Any spell may have a level description, including base spells if chosen.
        // Base spells must specify their own level in each spell config though,
        // they don't get an auto-generated one.
        levelDescription = controller.getMessages().get("spells." + variantKey + ".level_description", levelDescription);
        progressDescription = controller.getMessages().get("spells." + variantKey + ".progress_description", progressDescription);
        upgradeDescription = controller.getMessages().get("spells." + variantKey + ".upgrade_description", upgradeDescription);
    }
    // Individual spell configuration overrides all
    name = node.getString("name", name);
    alias = node.getString("alias", "");
    extendedDescription = node.getString("extended_description", extendedDescription);
    description = node.getString("description", description);
    levelDescription = node.getString("level_description", levelDescription);
    progressDescription = node.getString("progress_description", progressDescription);
    // Parameterize level description
    if (levelDescription != null && !levelDescription.isEmpty()) {
        levelDescription = levelDescription.replace("$level", Integer.toString(spellKey.getLevel()));
    }
    // Load basic properties
    icon = ConfigurationUtils.getMaterialAndData(node, "icon", icon);
    disabledIcon = ConfigurationUtils.getMaterialAndData(node, "icon_disabled", null);
    iconURL = node.getString("icon_url");
    iconDisabledURL = node.getString("icon_disabled_url");
    color = ConfigurationUtils.getColor(node, "color", null);
    worth = node.getDouble("worth", 0);
    if (node.contains("worth_sp")) {
        worth = node.getDouble("worth_sp", 0) * controller.getWorthSkillPoints();
    }
    earns = node.getInt("earns_sp", 0);
    earnCooldown = node.getInt("earns_cooldown", 0);
    category = controller.getCategory(node.getString("category"));
    Collection<String> tagList = ConfigurationUtils.getStringList(node, "tags");
    if (tagList != null) {
        tags = new HashSet<>(tagList);
    } else {
        tags = null;
    }
    requiredHealth = node.getDouble("require_health_percentage", 0);
    costs = parseCosts(node.getConfigurationSection("costs"));
    activeCosts = parseCosts(node.getConfigurationSection("active_costs"));
    pvpRestricted = node.getBoolean("pvp_restricted", false);
    quickCast = node.getBoolean("quick_cast", false);
    disguiseRestricted = node.getBoolean("disguise_restricted", false);
    glideRestricted = node.getBoolean("glide_restricted", false);
    glideExclusive = node.getBoolean("glide_exclusive", false);
    worldBorderRestricted = node.getBoolean("world_border_restricted", false);
    usesBrushSelection = node.getBoolean("brush_selection", false);
    castOnNoTarget = node.getBoolean("cast_on_no_target", true);
    hidden = node.getBoolean("hidden", false);
    showUndoable = node.getBoolean("show_undoable", true);
    cancellable = node.getBoolean("cancellable", true);
    cancelEffects = node.getBoolean("cancel_effects", false);
    disableManaRegeneration = node.getBoolean("disable_mana_regeneration", false);
    String toggleString = node.getString("toggle", "NONE");
    try {
        toggle = ToggleType.valueOf(toggleString.toUpperCase());
    } catch (Exception ex) {
        controller.getLogger().warning("Invalid toggle type: " + toggleString);
    }
    Collection<ConfigurationSection> requirementConfigurations = ConfigurationUtils.getNodeList(node, "requirements");
    if (requirementConfigurations != null) {
        requirements = new ArrayList<>();
        for (ConfigurationSection requirementConfiguration : requirementConfigurations) {
            requirements.add(new Requirement(requirementConfiguration));
        }
    }
    progressLevels = node.getConfigurationSection("progress_levels");
    if (progressLevels != null) {
        requiredCastsPerLevel = progressLevels.getLong("required_casts_per_level");
        maxLevels = progressLevels.getLong("max_levels");
        if (requiredCastsPerLevel <= 0 && maxLevels > 0) {
            if (requiredUpgradeCasts <= 0) {
                maxLevels = 0;
            } else {
                // auto determine casts per level
                requiredCastsPerLevel = requiredUpgradeCasts / maxLevels;
            }
        }
        progressLevelParameters = progressLevels.getConfigurationSection("parameters");
        if (progressLevelParameters != null) {
            Set<String> keys = progressLevelParameters.getKeys(true);
            progressLevelEquations = new HashMap<>(keys.size());
            for (String key : keys) {
                if (progressLevelParameters.isString(key)) {
                    String value = progressLevelParameters.getString(key, "");
                    progressLevelEquations.put(key, EquationStore.getInstance().getTransform(value));
                }
            }
        }
    }
    // Preload some parameters
    parameters.wrap(node.getConfigurationSection("parameters"));
    bypassMageCooldown = parameters.getBoolean("bypass_mage_cooldown", false);
    warmup = parameters.getInt("warmup", 0);
    cooldown = parameters.getInt("cooldown", 0);
    cooldown = parameters.getInt("cool", cooldown);
    mageCooldown = parameters.getInt("cooldown_mage", 0);
    displayCooldown = parameters.getInt("display_cooldown", -1);
    bypassPvpRestriction = parameters.getBoolean("bypass_pvp", false);
    bypassPvpRestriction = parameters.getBoolean("bp", bypassPvpRestriction);
    bypassPermissions = parameters.getBoolean("bypass_permissions", false);
    bypassBuildRestriction = parameters.getBoolean("bypass_build", false);
    bypassBuildRestriction = parameters.getBoolean("bb", bypassBuildRestriction);
    bypassBreakRestriction = parameters.getBoolean("bypass_break", false);
    bypassProtection = parameters.getBoolean("bypass_protection", false);
    bypassProtection = parameters.getBoolean("bp", bypassProtection);
    bypassAll = parameters.getBoolean("bypass", false);
    duration = parameters.getInt("duration", 0);
    totalDuration = parameters.getInt("total_duration", -1);
    effects.clear();
    if (node.contains("effects")) {
        ConfigurationSection effectsNode = node.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));
            }
        }
    }
}
Also used : Requirement(com.elmakers.mine.bukkit.api.requirements.Requirement) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)30 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)9 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)9 ArrayList (java.util.ArrayList)6 ItemStack (org.bukkit.inventory.ItemStack)6 Nullable (javax.annotation.Nullable)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Mage (com.elmakers.mine.bukkit.api.magic.Mage)3 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 Map (java.util.Map)3 Set (java.util.Set)3 CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)2 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)2 Inventory (org.bukkit.inventory.Inventory)2 MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)1 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)1 SpellData (com.elmakers.mine.bukkit.api.data.SpellData)1