Search in sources :

Example 21 with SpellTemplate

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

the class Wand method buildInventory.

protected void buildInventory() {
    // Force an update of the display inventory since chest mode is a different size
    displayInventory = null;
    updateHotbarCount();
    for (Inventory hotbar : hotbars) {
        hotbar.clear();
    }
    inventories.clear();
    List<ItemStack> unsorted = new ArrayList<>();
    for (String key : spells) {
        int spellLevel = getSpellLevel(key);
        SpellKey spellKey = new SpellKey(key, spellLevel);
        SpellTemplate spell = controller.getSpellTemplate(spellKey.getKey());
        ItemStack itemStack = createSpellItem(spell, "", controller, getActiveMage(), this, false);
        if (itemStack != null) {
            Integer slot = spellInventory.get(spell.getSpellKey().getBaseKey());
            if (slot == null) {
                unsorted.add(itemStack);
            } else {
                addToInventory(itemStack, slot);
            }
        }
    }
    WandMode brushMode = getBrushMode();
    for (String brushKey : brushes) {
        boolean addToInventory = brushMode == WandMode.INVENTORY || (MaterialBrush.isSpecialMaterialKey(brushKey) && !MaterialBrush.isSchematic(brushKey));
        if (addToInventory) {
            ItemStack itemStack = createBrushIcon(brushKey);
            if (itemStack == null) {
                controller.getPlugin().getLogger().warning("Unable to create brush icon for key " + brushKey);
                continue;
            }
            Integer slot = brushInventory.get(brushKey);
            if (activeBrush == null || activeBrush.length() == 0)
                activeBrush = brushKey;
            addToInventory(itemStack, slot);
        }
    }
    for (ItemStack unsortedItem : unsorted) {
        addToInventory(unsortedItem);
    }
    updateHasInventory();
    if (openInventoryPage >= inventories.size() && openInventoryPage != 0 && hasInventory) {
        setOpenInventoryPage(0);
    }
}
Also used : ArrayList(java.util.ArrayList) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) PlayerInventory(org.bukkit.inventory.PlayerInventory) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 22 with SpellTemplate

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

the class Wand method addPropertyLore.

protected void addPropertyLore(List<String> lore, boolean isSingleSpell) {
    if (usesMana() && effectiveManaMax > 0) {
        int manaMax = getManaMax();
        if (effectiveManaMax != manaMax) {
            String fullMessage = getLevelString("mana_amount_boosted", manaMax, controller.getMaxMana());
            ConfigurationUtils.addIfNotEmpty(fullMessage.replace("$mana", Integer.toString(effectiveManaMax)), lore);
        } else {
            ConfigurationUtils.addIfNotEmpty(getLevelString("mana_amount", manaMax, controller.getMaxMana()), lore);
        }
        int manaRegeneration = getManaRegeneration();
        if (manaRegeneration > 0 && effectiveManaRegeneration > 0) {
            if (effectiveManaRegeneration != manaRegeneration) {
                String fullMessage = getLevelString("mana_regeneration_boosted", manaRegeneration, controller.getMaxManaRegeneration());
                ConfigurationUtils.addIfNotEmpty(fullMessage.replace("$mana", Integer.toString(effectiveManaRegeneration)), lore);
            } else {
                ConfigurationUtils.addIfNotEmpty(getLevelString("mana_regeneration", manaRegeneration, controller.getMaxManaRegeneration()), lore);
            }
        }
        if (manaPerDamage > 0) {
            ConfigurationUtils.addIfNotEmpty(getLevelString("mana_per_damage", manaPerDamage, controller.getMaxManaRegeneration()), lore);
        }
    }
    if (superPowered) {
        ConfigurationUtils.addIfNotEmpty(getMessage("super_powered"), lore);
    }
    if (blockReflectChance > 0) {
        ConfigurationUtils.addIfNotEmpty(getLevelString("reflect_chance", blockReflectChance), lore);
    } else if (blockChance != 0) {
        ConfigurationUtils.addIfNotEmpty(getLevelString("block_chance", blockChance), lore);
    }
    float manaMaxBoost = getManaMaxBoost();
    if (manaMaxBoost != 0) {
        ConfigurationUtils.addIfNotEmpty(getPropertyString("mana_boost", manaMaxBoost), lore);
    }
    float manaRegenerationBoost = getManaRegenerationBoost();
    if (manaRegenerationBoost != 0) {
        ConfigurationUtils.addIfNotEmpty(getPropertyString("mana_regeneration_boost", manaRegenerationBoost), lore);
    }
    if (castSpell != null) {
        SpellTemplate spell = controller.getSpellTemplate(castSpell);
        if (spell != null) {
            ConfigurationUtils.addIfNotEmpty(getMessage("spell_aura").replace("$spell", spell.getName()), lore);
        }
    }
    for (Map.Entry<PotionEffectType, Integer> effect : potionEffects.entrySet()) {
        ConfigurationUtils.addIfNotEmpty(describePotionEffect(effect.getKey(), effect.getValue()), lore);
    }
    // In this case we should show it as such in the lore.
    if (passive)
        isSingleSpell = false;
    if (consumeReduction != 0 && !isSingleSpell)
        ConfigurationUtils.addIfNotEmpty(getPropertyString("consume_reduction", consumeReduction), lore);
    if (costReduction != 0 && !isSingleSpell)
        ConfigurationUtils.addIfNotEmpty(getPropertyString("cost_reduction", costReduction), lore);
    if (cooldownReduction != 0 && !isSingleSpell)
        ConfigurationUtils.addIfNotEmpty(getPropertyString("cooldown_reduction", cooldownReduction), lore);
    if (power > 0)
        ConfigurationUtils.addIfNotEmpty(getLevelString("power", power), lore);
    if (superProtected) {
        ConfigurationUtils.addIfNotEmpty(getMessage("super_protected"), lore);
    } else if (protection != null) {
        for (Map.Entry<String, Double> entry : protection.entrySet()) {
            String protectionType = entry.getKey();
            double amount = entry.getValue();
            addDamageTypeLore("protection", protectionType, amount, lore);
        }
    }
    ConfigurationSection weaknessConfig = getConfigurationSection("weakness");
    if (weaknessConfig != null) {
        Set<String> keys = weaknessConfig.getKeys(false);
        for (String key : keys) {
            addDamageTypeLore("weakness", key, weaknessConfig.getDouble(key), lore);
        }
    }
    ConfigurationSection strengthConfig = getConfigurationSection("strength");
    if (strengthConfig != null) {
        Set<String> keys = strengthConfig.getKeys(false);
        for (String key : keys) {
            addDamageTypeLore("strength", key, strengthConfig.getDouble(key), lore);
        }
    }
    if (spMultiplier > 1) {
        ConfigurationUtils.addIfNotEmpty(getPropertyString("sp_multiplier", spMultiplier - 1), lore);
    }
    ConfigurationSection attributes = getConfigurationSection("attributes");
    if (attributes != null) {
        // Don't bother with the lore at all if the template has been blanked out
        String template = getMessage("attributes");
        if (!template.isEmpty()) {
            Set<String> keys = attributes.getKeys(false);
            for (String key : keys) {
                String label = controller.getMessages().get("attributes." + key + ".name", key);
                // We are only display attributes as integers for now
                int value = attributes.getInt(key);
                if (value == 0)
                    continue;
                float max = 1;
                MagicAttribute attribute = controller.getAttribute(key);
                if (attribute != null) {
                    Double maxValue = attribute.getMax();
                    if (maxValue != null) {
                        max = (float) (double) maxValue;
                    }
                }
                label = getPropertyString("attributes", value, max).replace("$attribute", label);
                lore.add(label);
            }
        }
    }
}
Also used : PotionEffectType(org.bukkit.potion.PotionEffectType) MagicAttribute(com.elmakers.mine.bukkit.magic.MagicAttribute) Map(java.util.Map) HashMap(java.util.HashMap) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 23 with SpellTemplate

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

the class Wand method loadSpells.

protected void loadSpells(Collection<String> spellKeys) {
    clearSpells();
    WandUpgradePath path = getPath();
    for (String spellName : spellKeys) {
        String[] pieces = StringUtils.split(spellName, '@');
        Integer slot = parseSlot(pieces);
        // Handle aliases and upgrades smoothly
        String loadedKey = pieces[0].trim();
        SpellKey spellKey = new SpellKey(loadedKey);
        SpellTemplate spell = controller.getSpellTemplate(loadedKey);
        if (limitSpellsToPath && path != null && !path.containsSpell(spellKey.getBaseKey()))
            continue;
        // Downgrade spells if higher levels have gone missing
        while (spell == null && spellKey.getLevel() > 0) {
            spellKey = new SpellKey(spellKey.getBaseKey(), spellKey.getLevel() - 1);
            spell = controller.getSpellTemplate(spellKey.getKey());
        }
        if (spell != null) {
            spellKey = spell.getSpellKey();
            Integer currentLevel = spellLevels.get(spellKey.getBaseKey());
            if (spellKey.getLevel() > 1 && (currentLevel == null || currentLevel < spellKey.getLevel())) {
                setSpellLevel(spellKey.getBaseKey(), spellKey.getLevel());
            }
            if (slot != null) {
                spellInventory.put(spellKey.getBaseKey(), slot);
            }
            spells.add(spellKey.getBaseKey());
            if (activeSpell == null || activeSpell.length() == 0) {
                activeSpell = spellKey.getBaseKey();
            }
        }
    }
}
Also used : SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 24 with SpellTemplate

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

the class WandLevel method randomizeWand.

public boolean randomizeWand(Mage mage, Wand wand, boolean additive, boolean hasUpgrade, boolean addSpells) {
    // Add random spells to the wand
    Mage activeMage = wand.getActiveMage();
    if (mage == null) {
        mage = activeMage;
    }
    wand.setActiveMage(mage);
    boolean addedSpells = false;
    Deque<WeightedPair<String>> remainingSpells = getRemainingSpells(wand);
    if (addSpells) {
        if (remainingSpells.size() > 0) {
            Integer spellCount = RandomUtils.weightedRandom(spellCountProbability);
            for (int i = 0; spellCount != null && i < spellCount; i++) {
                String spellKey = RandomUtils.weightedRandom(remainingSpells);
                boolean added = wand.addSpell(spellKey);
                mage.sendDebugMessage("Trying to add spell: " + spellKey + " ? " + added);
                if (added) {
                    addedSpells = true;
                }
            }
        }
    }
    // Look through all spells for the max mana casting cost
    // Also look for any material-using spells
    boolean needsMaterials = false;
    int maxManaCost = 0;
    Set<String> spells = wand.getSpells();
    for (String spellName : spells) {
        SpellTemplate spell = wand.getController().getSpellTemplate(spellName);
        if (spell != null) {
            needsMaterials = needsMaterials || spell.usesBrush();
            Collection<CastingCost> costs = spell.getCosts();
            if (costs != null) {
                for (CastingCost cost : costs) {
                    maxManaCost = Math.max(maxManaCost, cost.getMana());
                }
            }
        }
    }
    // Add random materials
    boolean addedMaterials = false;
    Deque<WeightedPair<String>> remainingMaterials = getRemainingMaterials(wand);
    if (needsMaterials && remainingMaterials.size() > 0) {
        int currentMaterialCount = wand.getBrushes().size();
        Integer materialCount = RandomUtils.weightedRandom(materialCountProbability);
        // Make sure the wand has at least one material.
        if (materialCount == null) {
            materialCount = 0;
        }
        if (currentMaterialCount == 0) {
            materialCount = Math.max(1, materialCount);
        }
        int retries = 100;
        for (int i = 0; i < materialCount; i++) {
            String materialKey = RandomUtils.weightedRandom(remainingMaterials);
            materialKey = materialKey.replace("|", ":");
            if (!wand.addBrush(materialKey)) {
                // Try again up to a certain number if we picked one the wand already had.
                if (retries-- > 0)
                    i--;
            } else {
                addedMaterials = true;
            }
        }
    }
    // Let them upgrade if they aren't getting any new spells or brushes
    if (hasUpgrade && addSpells && !(addedMaterials && needsMaterials) && !addedSpells && ((getSpellCount() > 0 && spellProbability.size() > 0) || (getMaterialCount() > 0 && materialProbability.size() > 0))) {
        if (mage != null && mage.getDebugLevel() > 0) {
            mage.sendDebugMessage("Has upgrade: " + hasUpgrade);
            mage.sendDebugMessage("Added spells: " + addedSpells + ", should: " + addSpells);
            mage.sendDebugMessage("Spells per enchant: " + getSpellCount());
            mage.sendDebugMessage("Spells in list: " + spellProbability.size());
            mage.sendDebugMessage("Added brushes: " + addedMaterials + ", needed: " + needsMaterials);
        }
        wand.setActiveMage(activeMage);
        return false;
    }
    // Add random wand properties
    boolean addedProperties = false;
    Integer propertyCount = propertyCountProbability.size() == 0 ? Integer.valueOf(0) : RandomUtils.weightedRandom(propertyCountProbability);
    ConfigurationSection wandProperties = new MemoryConfiguration();
    List<String> propertyKeys = new ArrayList<>(propertiesProbability.keySet());
    List<String> propertiesAvailable = new ArrayList<>();
    for (String propertyKey : propertyKeys) {
        double currentValue = wand.getDouble(propertyKey);
        double maxValue = path.getMaxProperty(propertyKey);
        if (currentValue < maxValue) {
            propertiesAvailable.add(propertyKey);
        }
    }
    // Make sure we give them *something* if something is available
    if (propertiesAvailable.size() > 0 && !addedMaterials && !addedSpells && propertyCount == 0) {
        propertyCount = 1;
    }
    while (propertyCount != null && propertyCount-- > 0 && propertiesAvailable.size() > 0) {
        int randomPropertyIndex = (int) (Math.random() * propertiesAvailable.size());
        String randomProperty = propertiesAvailable.get(randomPropertyIndex);
        Deque<WeightedPair<Float>> probabilities = propertiesProbability.get(randomProperty);
        double current = wand.getDouble(randomProperty);
        double maxValue = path.getMaxProperty(randomProperty);
        if (probabilities.size() > 0 && current < maxValue) {
            addedProperties = true;
            current = Math.min(maxValue, current + RandomUtils.weightedRandom(probabilities));
            wandProperties.set(randomProperty, current);
        }
    }
    if (wand.isCostFree()) {
        // Cost-Free wands don't need mana.
        wandProperties.set("mana_regeneration", 0);
        wandProperties.set("mana_max", 0);
        wandProperties.set("mana", 0);
    } else {
        int manaRegeneration = wand.getManaRegeneration();
        if (manaRegenerationProbability.size() > 0 && manaRegeneration < path.getMaxManaRegeneration()) {
            addedProperties = true;
            manaRegeneration = Math.min(path.getMaxManaRegeneration(), manaRegeneration + RandomUtils.weightedRandom(manaRegenerationProbability));
            wandProperties.set("mana_regeneration", manaRegeneration);
        }
        int manaMax = wand.getManaMax();
        if (manaMaxProbability.size() > 0 && manaMax < path.getMaxMaxMana()) {
            manaMax = Math.min(path.getMaxMaxMana(), manaMax + RandomUtils.weightedRandom(manaMaxProbability));
            if (path.getMatchSpellMana()) {
                // Make sure the wand has at least enough mana to cast the highest costing spell it has.
                manaMax = Math.max(maxManaCost, manaMax);
            }
            wandProperties.set("mana_max", manaMax);
            addedProperties = true;
        }
        // Refill the wand's mana, why not
        wandProperties.set("mana", manaMax);
    }
    // Add or set uses to the wand
    if (additive) {
        // Only add uses to a wand if it already has some.
        int wandUses = wand.getRemainingUses();
        if (wandUses > 0 && wandUses < path.getMaxUses() && addUseProbability.size() > 0) {
            wandProperties.set("uses", Math.min(path.getMaxUses(), wandUses + RandomUtils.weightedRandom(addUseProbability)));
            addedProperties = true;
        }
    } else if (useProbability.size() > 0) {
        wandProperties.set("uses", Math.min(path.getMaxUses(), RandomUtils.weightedRandom(useProbability)));
    }
    // Set properties.
    wand.upgrade(wandProperties);
    wand.setActiveMage(activeMage);
    return addedMaterials || addedSpells || addedProperties;
}
Also used : ArrayList(java.util.ArrayList) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) Mage(com.elmakers.mine.bukkit.api.magic.Mage) WeightedPair(com.elmakers.mine.bukkit.utility.WeightedPair) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 25 with SpellTemplate

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

the class WandOrganizer method alphabetize.

public void alphabetize() {
    Map<String, Integer> spells = wand.getSpellInventory();
    Map<String, Integer> brushes = wand.getBrushInventory();
    removeHotbar(spells, brushes);
    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);
            }
        }
    }
    Map<String, String> alphabetized = new TreeMap<>();
    for (String spellKey : spells.keySet()) {
        SpellTemplate spell = wand.getController().getSpellTemplate(spellKey);
        if (spell != null) {
            alphabetized.put(spell.getName(), spell.getSpellKey().getBaseKey());
        }
    }
    currentInventoryIndex = 0;
    currentInventoryCount = 0;
    for (String spellName : alphabetized.values()) {
        spells.put(spellName, getNextSlot(wand.getInventorySize()));
    }
    if (materials.size() > 0) {
        nextPage();
        for (String materialName : materials.values()) {
            brushes.put(materialName, getNextSlot(wand.getInventorySize()));
        }
    }
    wand.updateSpellInventory(spells);
    if (materials.size() > 0) {
        wand.updateBrushInventory(brushes);
    }
}
Also used : TreeMap(java.util.TreeMap) 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