Search in sources :

Example 1 with CasterProperties

use of com.elmakers.mine.bukkit.api.magic.CasterProperties in project MagicPlugin by elBukkit.

the class MagicRequirement method checkRequirement.

public boolean checkRequirement(@Nonnull CastContext context) {
    Mage mage = context.getMage();
    Player player = mage.getPlayer();
    if (permissionNode != null && (player == null || !player.hasPermission(permissionNode))) {
        return false;
    }
    Wand wand = context.getWand();
    if (wand == null && requireWand) {
        return false;
    }
    if (requiredTemplate != null) {
        String template = wand.getTemplateKey();
        if (template == null || !template.equals(requiredTemplate)) {
            return false;
        }
    }
    if (requiredTemplates != null) {
        String template = wand.getTemplateKey();
        if (template == null || !requiredTemplates.contains(template)) {
            return false;
        }
    }
    CasterProperties checkProperties = context.getActiveProperties();
    ProgressionPath path = checkProperties.getPath();
    if (mageClass != null && !mageClass.isEmpty()) {
        if (!mage.hasClassUnlocked(mageClass)) {
            return false;
        }
    }
    if (requiredPath != null || exactPath != null) {
        if (path == null) {
            return false;
        }
        if (requiredPath != null && !path.hasPath(requiredPath)) {
            return false;
        }
        if (exactPath != null && !exactPath.equals(path.getKey())) {
            return false;
        }
        if (requiresCompletedPath != null) {
            boolean hasPathCompleted = false;
            if (path.hasPath(requiresCompletedPath)) {
                if (path.getKey().equals(requiresCompletedPath)) {
                    hasPathCompleted = !path.canProgress(checkProperties);
                } else {
                    hasPathCompleted = true;
                }
            }
            if (!hasPathCompleted) {
                return false;
            }
        }
    }
    if (wandProperties != null) {
        if (!checkProperties(wand, wandProperties)) {
            return false;
        }
    }
    if (classProperties != null) {
        MageClass activeClass = mageClass == null ? mage.getActiveClass() : mage.getClass(mageClass);
        if (activeClass == null) {
            return false;
        }
        if (!checkProperties(activeClass, classProperties)) {
            return false;
        }
    }
    if (attributes != null) {
        for (PropertyRequirement requirement : attributes) {
            String key = requirement.key;
            Double value = mage.getAttribute(key);
            if (!checkProperty(requirement, value)) {
                return false;
            }
        }
    }
    return true;
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Player(org.bukkit.entity.Player) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 2 with CasterProperties

use of com.elmakers.mine.bukkit.api.magic.CasterProperties in project MagicPlugin by elBukkit.

the class MageCommandExecutor method onMageAttribute.

public boolean onMageAttribute(CommandSender sender, Player player, String[] args) {
    Mage mage = controller.getMage(player);
    Set<String> attributes = api.getController().getAttributes();
    if (attributes.isEmpty()) {
        sender.sendMessage(ChatColor.RED + "No attributes configured, see attributes.yml");
        return true;
    }
    if (args.length == 0) {
        sender.sendMessage(ChatColor.GOLD + "Attributes for: " + ChatColor.AQUA + player.getName());
        for (String key : attributes) {
            Double value = mage.getAttribute(key);
            String valueDescription = value == null ? ChatColor.RED + "(not set)" : ChatColor.AQUA + Double.toString(value);
            sender.sendMessage(ChatColor.DARK_AQUA + key + ChatColor.BLUE + " = " + valueDescription);
        }
        return true;
    }
    String key = args[0];
    if (args.length == 1) {
        if (!attributes.contains(key)) {
            sender.sendMessage(ChatColor.RED + "Unknown attribute: " + ChatColor.YELLOW + key);
            return true;
        }
        Double value = mage.getAttribute(key);
        String valueDescription = value == null ? ChatColor.RED + "(not set)" : ChatColor.AQUA + Double.toString(value);
        sender.sendMessage(ChatColor.AQUA + player.getName() + " has " + ChatColor.DARK_AQUA + key + ChatColor.BLUE + " of " + valueDescription);
        return true;
    }
    MageClass activeClass = mage.getActiveClass();
    CasterProperties attributeProperties = activeClass == null ? mage.getProperties() : activeClass;
    String value = args[1];
    for (int i = 2; i < args.length; i++) {
        value = value + " " + args[i];
    }
    if (value.equals("-")) {
        Double oldValue = attributeProperties.getAttribute(key);
        attributeProperties.setAttribute(key, null);
        String valueDescription = oldValue == null ? ChatColor.RED + "(not set)" : ChatColor.AQUA + Double.toString(oldValue);
        sender.sendMessage(ChatColor.BLUE + "Removed attribute " + ChatColor.DARK_AQUA + key + ChatColor.BLUE + ", was " + valueDescription);
        return true;
    }
    double transformed = Double.NaN;
    try {
        transformed = Double.parseDouble(value);
    } catch (Exception ex) {
        EquationTransform transform = EquationStore.getInstance().getTransform(value);
        if (transform.getException() == null) {
            Double property = attributeProperties.getAttribute(key);
            if (property == null || Double.isNaN(property)) {
                property = 0.0;
            }
            transform.setVariable("x", property);
            transformed = transform.get();
        }
    }
    if (Double.isNaN(transformed)) {
        sender.sendMessage(ChatColor.RED + "Could not set " + ChatColor.YELLOW + key + ChatColor.RED + " to " + ChatColor.YELLOW + value);
        return true;
    }
    attributeProperties.setAttribute(key, transformed);
    sender.sendMessage(ChatColor.GOLD + "Set " + ChatColor.DARK_AQUA + key + ChatColor.GOLD + " to " + ChatColor.AQUA + transformed + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
    return true;
}
Also used : CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) EquationTransform(de.slikey.effectlib.math.EquationTransform) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Example 3 with CasterProperties

use of com.elmakers.mine.bukkit.api.magic.CasterProperties in project MagicPlugin by elBukkit.

the class ModifyPropertiesAction method perform.

@Override
public SpellResult perform(CastContext context) {
    if (modify == null) {
        return SpellResult.FAIL;
    }
    Entity entity = context.getTargetEntity();
    Mage mage = entity == null ? null : context.getController().getRegisteredMage(entity);
    if (mage == null) {
        return SpellResult.NO_TARGET;
    }
    CasterProperties properties = null;
    if (modifyTarget.equals("wand")) {
        properties = mage.getActiveWand();
    } else if (modifyTarget.equals("player")) {
        properties = mage.getProperties();
    } else {
        properties = mage.getClass(modifyTarget);
    }
    // I am now wishing I hadn't made a base class called "mage" :(
    if (properties == null && modifyTarget.equals("mage")) {
        properties = mage.getProperties();
    }
    if (properties == null) {
        return SpellResult.NO_TARGET;
    }
    ConfigurationSection original = new MemoryConfiguration();
    ConfigurationSection changed = new MemoryConfiguration();
    for (ModifyProperty property : modify) {
        Object originalValue = properties.getProperty(property.path);
        Object newValue = property.value;
        if ((originalValue == null || originalValue instanceof Number) && property.value instanceof String) {
            EquationTransform transform = EquationStore.getInstance().getTransform((String) property.value);
            originalValue = originalValue == null ? null : NumberConversions.toDouble(originalValue);
            double defaultValue = property.defaultValue == null ? 0 : property.defaultValue;
            if (transform.isValid()) {
                if (originalValue == null) {
                    originalValue = defaultValue;
                } else if (property.max != null && (Double) originalValue >= property.max) {
                    continue;
                } else if (property.min != null && (Double) originalValue <= property.min) {
                    continue;
                }
                transform.setVariable("x", (Double) originalValue);
                double transformedValue = transform.get();
                if (!Double.isNaN(transformedValue)) {
                    if (property.max != null) {
                        transformedValue = Math.min(transformedValue, property.max);
                    }
                    if (property.min != null) {
                        transformedValue = Math.max(transformedValue, property.min);
                    }
                    newValue = transformedValue;
                }
            }
        }
        changed.set(property.path, newValue);
        original.set(property.path, originalValue);
    }
    if (changed.getKeys(false).isEmpty())
        return SpellResult.NO_TARGET;
    if (upgrade)
        properties.upgrade(changed);
    else
        properties.configure(changed);
    context.registerForUndo(new ModifyPropertyUndoAction(original, properties));
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) EquationTransform(de.slikey.effectlib.math.EquationTransform) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 4 with CasterProperties

use of com.elmakers.mine.bukkit.api.magic.CasterProperties in project MagicPlugin by elBukkit.

the class SpellShopAction method getItems.

@Nullable
@Override
public List<ShopItem> getItems(CastContext context) {
    Mage mage = context.getMage();
    Wand wand = mage.getActiveWand();
    CasterProperties caster = getCaster(context);
    // Check for auto upgrade, if the player can no longer progress on their current path and is
    // eligible for an upgrade, we will upgrade them and skip showing the spell shop so the player
    // can see their upgrade rewards.
    boolean canProgress = false;
    // TODO: Make this work on CasterProperties
    if (autoUpgrade && wand != null) {
        canProgress = caster.canProgress();
        if (!canProgress && wand.checkUpgrade(true) && wand.upgrade(false)) {
            return null;
        }
    }
    ProgressionPath currentPath = caster.getPath();
    if (!castsSpells && !allowLocked && wand != null && wand.isLocked()) {
        context.showMessage(context.getMessage("no_path", getDefaultMessage(context, "no_path")).replace("$wand", wand.getName()));
        return null;
    }
    // Load spells
    Map<String, Double> spellPrices = new HashMap<>();
    if (spells.size() > 0) {
        spellPrices.putAll(spells);
    } else {
        if (currentPath == null) {
            String name = wand == null ? "" : wand.getName();
            context.showMessage(context.getMessage("no_path", getDefaultMessage(context, "no_path")).replace("$wand", name));
            return null;
        }
        if (showPath) {
            Collection<String> pathSpells = currentPath.getSpells();
            for (String pathSpell : pathSpells) {
                spellPrices.put(pathSpell, null);
            }
        }
        if (showRequired) {
            Collection<String> requiredSpells = currentPath.getRequiredSpells();
            for (String requiredSpell : requiredSpells) {
                spellPrices.put(requiredSpell, null);
            }
        }
        if (showUpgrades) {
            Collection<String> spells = caster.getSpells();
            for (String spellKey : spells) {
                MageSpell spell = mage.getSpell(spellKey);
                SpellTemplate upgradeSpell = spell.getUpgrade();
                if (upgradeSpell != null) {
                    spellPrices.put(upgradeSpell.getKey(), null);
                }
            }
        }
    }
    List<ShopItem> shopItems = new ArrayList<>();
    for (Map.Entry<String, Double> spellValue : spellPrices.entrySet()) {
        ShopItem shopItem = createShopItem(spellValue.getKey(), spellValue.getValue(), context);
        if (shopItem != null) {
            shopItems.add(shopItem);
        }
    }
    Collections.sort(shopItems);
    if (spells.size() == 0 && showExtra && !castsSpells && currentPath != null) {
        Collection<String> extraSpells = currentPath.getExtraSpells();
        List<ShopItem> extraItems = new ArrayList<>();
        for (String spellKey : extraSpells) {
            ShopItem shopItem = createShopItem(spellKey, null, context);
            if (shopItem != null) {
                ItemStack spellItem = shopItem.getItem();
                ItemMeta meta = spellItem.getItemMeta();
                List<String> itemLore = meta.getLore();
                itemLore.add(context.getMessage("extra_spell", getDefaultMessage(context, "extra_spell")));
                meta.setLore(itemLore);
                spellItem.setItemMeta(meta);
                extraItems.add(shopItem);
            }
        }
        Collections.sort(extraItems);
        shopItems.addAll(extraItems);
    }
    // If there is nothing here for us to do, check for upgrades being blocked
    // we will upgrade the wand here, but in theory that should never happen since we
    // checked for upgrades above.
    // TODO: Make this work for caster...
    mage.sendDebugMessage(ChatColor.GOLD + "Spells to buy: " + shopItems.size(), 2);
    if (wand != null && shopItems.size() == 0) {
        boolean canUpgrade = autoUpgrade && wand.checkUpgrade(false);
        boolean hasUpgrade = autoUpgrade && wand.hasUpgrade();
        if (!canProgress && !hasUpgrade) {
            context.showMessage(context.getMessage("no_upgrade", getDefaultMessage(context, "no_upgrade")).replace("$wand", wand.getName()));
            return null;
        } else if (canUpgrade) {
            wand.upgrade(false);
            return null;
        } else {
            return null;
        }
    }
    return shopItems;
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ItemMeta(org.bukkit.inventory.meta.ItemMeta) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) Nullable(javax.annotation.Nullable)

Example 5 with CasterProperties

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

Aggregations

CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)11 Mage (com.elmakers.mine.bukkit.api.magic.Mage)10 ProgressionPath (com.elmakers.mine.bukkit.api.magic.ProgressionPath)6 Wand (com.elmakers.mine.bukkit.api.wand.Wand)6 ItemStack (org.bukkit.inventory.ItemStack)5 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)4 Player (org.bukkit.entity.Player)4 ItemMeta (org.bukkit.inventory.meta.ItemMeta)4 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)3 Spell (com.elmakers.mine.bukkit.api.spell.Spell)3 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)3 ArrayList (java.util.ArrayList)3 Nullable (javax.annotation.Nullable)3 WandUpgradePath (com.elmakers.mine.bukkit.api.wand.WandUpgradePath)2 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)2 EquationTransform (de.slikey.effectlib.math.EquationTransform)2 Inventory (org.bukkit.inventory.Inventory)2 Messages (com.elmakers.mine.bukkit.api.magic.Messages)1 Requirement (com.elmakers.mine.bukkit.api.requirements.Requirement)1