Search in sources :

Example 36 with SpellTemplate

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

the class Wand method fill.

@Override
public boolean fill(Player player, int maxLevel) {
    closeInventory();
    Collection<String> currentSpells = new ArrayList<>(getSpells());
    for (String spellKey : currentSpells) {
        SpellTemplate spell = controller.getSpellTemplate(spellKey);
        if (!spell.hasCastPermission(player)) {
            removeSpell(spellKey);
        }
    }
    Collection<SpellTemplate> allSpells = controller.getPlugin().getSpellTemplates();
    // Hack to prevent messaging
    Mage mage = this.mage;
    this.mage = null;
    for (SpellTemplate spell : allSpells) {
        String key = spell.getKey();
        if (maxLevel > 0 && spell.getSpellKey().getLevel() > maxLevel) {
            continue;
        }
        if (key.startsWith("heroes*")) {
            continue;
        }
        if (spell.hasCastPermission(player) && spell.hasIcon() && !spell.isHidden()) {
            addSpell(key);
        }
    }
    this.mage = mage;
    if (autoFill)
        setProperty("fill", false);
    autoFill = false;
    saveState();
    return true;
}
Also used : Mage(com.elmakers.mine.bukkit.magic.Mage) ArrayList(java.util.ArrayList) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 37 with SpellTemplate

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

the class Wand method forceAddSpell.

@Override
public boolean forceAddSpell(String spellName) {
    if (!super.addSpell(spellName)) {
        return false;
    }
    saveInventory();
    SpellTemplate template = controller.getSpellTemplate(spellName);
    ItemStack spellItem = createSpellIcon(template);
    if (spellItem == null) {
        return false;
    }
    SpellKey spellKey = template.getSpellKey();
    int level = spellKey.getLevel();
    int inventoryCount = inventories.size();
    int spellCount = spells.size();
    // Look for existing spells for spell upgrades
    Integer inventorySlot = spellInventory.get(spellKey.getBaseKey());
    SpellTemplate currentSpell = getBaseSpell(spellKey);
    clearSlot(inventorySlot);
    setSpellLevel(spellKey.getBaseKey(), level);
    spells.add(spellKey.getBaseKey());
    if (activeSpell == null || activeSpell.isEmpty()) {
        setActiveSpell(spellKey.getBaseKey());
    }
    addToInventory(spellItem, inventorySlot);
    checkSpellLevelsAndInventory();
    updateInventory();
    updateHasInventory();
    saveState();
    updateLore();
    if (mage != null) {
        if (spells.size() != spellCount) {
            if (spellCount == 0) {
                if (leftClickAction == WandAction.CAST) {
                    String message = getMessage("spell_instructions", "").replace("$wand", getName());
                    mage.sendMessage(message.replace("$spell", template.getName()));
                }
            } else if (spellCount == 1) {
                String controlKey = getControlKey(WandAction.TOGGLE);
                String inventoryMessage = null;
                switch(getMode()) {
                    case INVENTORY:
                        inventoryMessage = "inventory_instructions";
                        break;
                    case CHEST:
                        inventoryMessage = "chest_instructions";
                        break;
                    case SKILLS:
                        inventoryMessage = "skills_instructions";
                        break;
                    case CYCLE:
                        inventoryMessage = "cycle_instructions";
                        if (controlKey == null) {
                            controlKey = getControlKey(WandAction.CYCLE);
                        }
                        break;
                    case CAST:
                    case NONE:
                        // Ignore
                        break;
                }
                if (controlKey != null && inventoryMessage != null) {
                    controlKey = controller.getMessages().get("controls." + controlKey);
                    mage.sendMessage(getMessage(inventoryMessage, "").replace("$wand", getName()).replace("$toggle", controlKey).replace("$cycle", controlKey));
                }
            }
            if (inventoryCount == 1 && inventories.size() > 1) {
                mage.sendMessage(getMessage("page_instructions", "").replace("$wand", getName()));
            }
        }
    }
    return true;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 38 with SpellTemplate

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

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

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

Aggregations

SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)48 ArrayList (java.util.ArrayList)16 Mage (com.elmakers.mine.bukkit.api.magic.Mage)14 Spell (com.elmakers.mine.bukkit.api.spell.Spell)10 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)9 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)8 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)8 ItemStack (org.bukkit.inventory.ItemStack)8 Wand (com.elmakers.mine.bukkit.api.wand.Wand)7 Player (org.bukkit.entity.Player)7 MageController (com.elmakers.mine.bukkit.api.magic.MageController)5 Messages (com.elmakers.mine.bukkit.api.magic.Messages)5 SpellCategory (com.elmakers.mine.bukkit.api.spell.SpellCategory)5 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Nullable (javax.annotation.Nullable)5 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)4 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)3 ProgressionPath (com.elmakers.mine.bukkit.api.magic.ProgressionPath)3