Search in sources :

Example 81 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.

the class Wand method randomize.

protected void randomize() {
    if (template != null && template.length() > 0) {
        ConfigurationSection wandConfig = controller.getWandTemplateConfiguration(template);
        if (wandConfig != null && wandConfig.contains("icon")) {
            String iconKey = wandConfig.getString("icon");
            if (iconKey.contains(",")) {
                Random r = new Random();
                String[] keys = StringUtils.split(iconKey, ',');
                iconKey = keys[r.nextInt(keys.length)];
            }
            setIcon(ConfigurationUtils.toMaterialAndData(iconKey));
            updateIcon();
            playEffects("randomize");
        }
    }
}
Also used : Random(java.util.Random) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 82 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection 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 83 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.

the class Wand method createSpellItem.

@Nullable
public static ItemStack createSpellItem(SpellTemplate spell, String args, MagicController controller, com.elmakers.mine.bukkit.api.magic.Mage mage, Wand wand, boolean isItem) {
    if (spell == null)
        return null;
    String iconURL = spell.getIconURL();
    ItemStack itemStack = null;
    if (iconURL != null && (controller.isUrlIconsEnabled() || spell.getIcon() == null || !spell.getIcon().isValid() || spell.getIcon().getMaterial() == Material.AIR)) {
        itemStack = InventoryUtils.getURLSkull(iconURL);
    }
    if (itemStack == null) {
        ItemStack originalItemStack = null;
        com.elmakers.mine.bukkit.api.block.MaterialAndData icon = spell.getIcon();
        if (icon == null) {
            controller.getPlugin().getLogger().warning("Unable to create spell icon for " + spell.getName() + ", missing material");
            return null;
        }
        try {
            originalItemStack = new ItemStack(icon.getMaterial(), 1, icon.getData());
            itemStack = InventoryUtils.makeReal(originalItemStack);
        } catch (Exception ex) {
            itemStack = null;
        }
        if (itemStack == null) {
            if (icon.getMaterial() != Material.AIR) {
                String iconName = icon.getName();
                controller.getPlugin().getLogger().warning("Unable to create spell icon for " + spell.getKey() + " with material " + iconName);
            }
            return originalItemStack;
        }
    }
    InventoryUtils.makeUnbreakable(itemStack);
    InventoryUtils.hideFlags(itemStack, (byte) 63);
    updateSpellItem(controller.getMessages(), itemStack, spell, args, mage, wand, wand == null ? null : wand.activeBrush, isItem);
    if (wand != null && wand.getMode() == WandMode.SKILLS && !isItem) {
        String mageClassKey = wand.getMageClassKey();
        ConfigurationSection skillsConfig = wand.getConfigurationSection("skills");
        InventoryUtils.configureSkillItem(itemStack, mageClassKey, skillsConfig);
    }
    return itemStack;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) Nullable(javax.annotation.Nullable)

Example 84 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection 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 85 with ConfigurationSection

use of org.bukkit.configuration.ConfigurationSection in project MagicPlugin by elBukkit.

the class WandProperties method getEffectiveConfiguration.

public ConfigurationSection getEffectiveConfiguration() {
    ConfigurationSection effectiveConfiguration = ConfigurationUtils.cloneConfiguration(getConfiguration());
    if (wandTemplate != null) {
        ConfigurationSection parentConfiguration = wandTemplate.getConfiguration();
        ConfigurationUtils.overlayConfigurations(effectiveConfiguration, parentConfiguration);
    }
    if (mageClass != null) {
        ConfigurationSection classConfiguration = mageClass.getConfiguration();
        ConfigurationUtils.overlayConfigurations(effectiveConfiguration, classConfiguration);
    }
    return effectiveConfiguration;
}
Also used : ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

ConfigurationSection (org.bukkit.configuration.ConfigurationSection)263 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)29 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)27 ItemStack (org.bukkit.inventory.ItemStack)26 File (java.io.File)22 YamlConfiguration (org.bukkit.configuration.file.YamlConfiguration)22 IOException (java.io.IOException)21 Map (java.util.Map)18 Mage (com.elmakers.mine.bukkit.api.magic.Mage)17 Material (org.bukkit.Material)16 Nullable (javax.annotation.Nullable)14 Location (org.bukkit.Location)13 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)11 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)10 Player (org.bukkit.entity.Player)10 EntityType (org.bukkit.entity.EntityType)9 List (java.util.List)8 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)7 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)7