Search in sources :

Example 6 with CasterProperties

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

the class BaseShopAction method getCaster.

@Nonnull
protected CasterProperties getCaster(CastContext context) {
    Mage mage = context.getMage();
    Wand wand = mage.getActiveWand();
    CasterProperties caster = wand;
    if (caster == null) {
        caster = mage.getActiveClass();
    }
    if (caster == null) {
        caster = mage.getProperties();
    }
    return caster;
}
Also used : CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Nonnull(javax.annotation.Nonnull)

Example 7 with CasterProperties

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

the class BaseShopAction method clicked.

@Override
public void clicked(InventoryClickEvent event) {
    event.setCancelled(true);
    ItemStack item = event.getCurrentItem();
    Mage mage = context.getMage();
    if (item == null || !InventoryUtils.hasMeta(item, "shop")) {
        if (!autoClose) {
            mage.deactivateGUI();
        }
        return;
    }
    int slotIndex = Integer.parseInt(InventoryUtils.getMetaString(item, "shop"));
    MageController controller = context.getController();
    Wand wand = mage.getActiveWand();
    ShopItem shopItem = showingItems.get(slotIndex);
    if (shopItem == null) {
        return;
    }
    String unpurchasableMessage = InventoryUtils.getMetaString(shopItem.getItem(), "unpurchasable");
    if (unpurchasableMessage != null && !unpurchasableMessage.isEmpty()) {
        context.showMessage(unpurchasableMessage);
        mage.deactivateGUI();
        return;
    }
    boolean hasCosts = sell ? hasItem(controller, mage, shopItem.getItem()) : hasItemCosts(context, shopItem);
    if (!hasCosts) {
        String costString = context.getMessage("insufficient", getDefaultMessage(context, "insufficient"));
        if (sell) {
            costString = costString.replace("$cost", formatItemAmount(controller, item, shopItem.getItem().getAmount()));
        } else {
            costString = costString.replace("$cost", getItemCost(context, shopItem));
        }
        context.showMessage(costString);
    } else {
        String itemName = formatItemAmount(controller, item, item.getAmount());
        if (InventoryUtils.hasMeta(item, "confirm")) {
            String inventoryTitle = context.getMessage("confirm_title", getDefaultMessage(context, "confirm_title")).replace("$item", itemName);
            Inventory confirmInventory = CompatibilityUtils.createInventory(null, 9, inventoryTitle);
            InventoryUtils.removeMeta(item, "confirm");
            for (int i = 0; i < 9; i++) {
                if (i != 4) {
                    ItemStack filler = confirmFillMaterial.getItemStack(1);
                    ItemMeta meta = filler.getItemMeta();
                    if (meta != null) {
                        meta.setDisplayName(ChatColor.DARK_GRAY + (i < 4 ? "-->" : "<--"));
                        filler.setItemMeta(meta);
                    }
                    confirmInventory.setItem(i, filler);
                } else {
                    confirmInventory.setItem(i, item);
                }
            }
            mage.deactivateGUI();
            isActive = true;
            mage.activateGUI(this, confirmInventory);
            return;
        }
        String costString = context.getMessage("deducted", getDefaultMessage(context, "deducted"));
        if (sell) {
            costString = costString.replace("$cost", getItemCost(context, shopItem));
            removeItems(controller, mage, item, shopItem.getItem().getAmount());
            giveCosts(context, shopItem);
        } else {
            costString = costString.replace("$cost", getItemCost(context, shopItem));
            item = shopItem.getItem();
            if (requireWand) {
                if (wand == null) {
                    context.showMessage("no_wand", getDefaultMessage(context, "no_wand"));
                    mage.deactivateGUI();
                    return;
                }
                if (applyToWand && !wand.addItem(item)) {
                    String inapplicable = context.getMessage("not_applicable", getDefaultMessage(context, "not_applicable")).replace("$item", itemName);
                    context.showMessage(inapplicable);
                    mage.deactivateGUI();
                    return;
                }
            }
            CasterProperties caster = getCaster(context);
            if (applyToCaster && !caster.addItem(item)) {
                String inapplicable = context.getMessage("not_applicable", getDefaultMessage(context, "not_applicable")).replace("$item", itemName);
                context.showMessage(inapplicable);
                mage.deactivateGUI();
                return;
            }
            if (castsSpells) {
                Spell spell = null;
                String spellKey = controller.getSpell(item);
                String spellArgs = controller.getSpellArgs(item);
                spell = mage.getSpell(spellKey);
                if (spell != null && (spellArgs != null ? !spell.cast(StringUtils.split(spellArgs, ' ')) : !spell.cast())) {
                    context.showMessage("cast_fail", getDefaultMessage(context, "cast_fail"));
                    mage.deactivateGUI();
                    return;
                }
            }
            if (!takeCosts(context, shopItem)) {
                costString = context.getMessage("insufficient", getDefaultMessage(context, "insufficient"));
                costString = costString.replace("$cost", getItemCost(context, shopItem));
                context.showMessage(costString);
                return;
            }
            if (!castsSpells && !applyToWand && !applyToCaster) {
                ItemStack copy = InventoryUtils.getCopy(item);
                if (filterBound && com.elmakers.mine.bukkit.wand.Wand.isBound(copy)) {
                    Wand bindWand = controller.getWand(copy);
                    mage.tryToOwn(bindWand);
                }
                if (showActiveIcons && controller.getAPI().isWand(copy)) {
                    Wand newWand = controller.getWand(copy);
                    com.elmakers.mine.bukkit.api.block.MaterialAndData inactiveIcon = newWand.getInactiveIcon();
                    if (inactiveIcon != null) {
                        inactiveIcon.applyToItem(copy);
                    }
                }
                Player player = mage.getPlayer();
                if (putInHand) {
                    context.getController().giveItemToPlayer(player, copy);
                } else {
                    PlayerInventory inventory = player.getInventory();
                    ItemStack inHand = inventory.getItemInMainHand();
                    Integer freeSlot = null;
                    if (InventoryUtils.isEmpty(inHand)) {
                        for (int i = 0; i < inventory.getSize() && freeSlot == null; i++) {
                            if (i != inventory.getHeldItemSlot() && InventoryUtils.isEmpty(inventory.getItem(i))) {
                                freeSlot = i;
                            }
                        }
                    }
                    if (freeSlot == null) {
                        context.getController().giveItemToPlayer(player, copy);
                    } else {
                        inventory.setItem(freeSlot, copy);
                    }
                }
            }
        }
        costString = costString.replace("$item", itemName);
        context.showMessage(costString);
        if (!sell && wand != null && autoUpgrade) {
            if (upgradeLevels <= 0) {
                com.elmakers.mine.bukkit.api.wand.WandUpgradePath path = wand.getPath();
                WandUpgradePath nextPath = path != null ? path.getUpgrade() : null;
                if (nextPath != null && path.checkUpgradeRequirements(wand, null) && !path.canEnchant(wand)) {
                    path.upgrade(wand, mage);
                }
            } else {
                wand.enchant(upgradeLevels, mage, false);
            }
        }
        finalResult = SpellResult.CAST;
        onPurchase(context, item);
    }
    if (autoClose) {
        mage.deactivateGUI();
    } else {
        // update title
        mage.continueGUI(this, getInventory(context));
    }
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Player(org.bukkit.entity.Player) Wand(com.elmakers.mine.bukkit.api.wand.Wand) PlayerInventory(org.bukkit.inventory.PlayerInventory) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 8 with CasterProperties

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

the class PlaceholderAPIManager method onPlaceholderRequest.

@Override
public String onPlaceholderRequest(Player player, String placeholder) {
    Mage mage = controller.getMage(player);
    MageClass activeClass = mage.getActiveClass();
    Wand wand = mage.getActiveWand();
    Spell spell = wand == null ? null : wand.getActiveSpell();
    if (spell == null) {
        ItemStack item = player.getInventory().getItemInMainHand();
        String spellKey = controller.getSpell(item);
        if (spellKey != null) {
            spell = mage.getSpell(spellKey);
        }
    }
    if (spell == null) {
        ItemStack item = player.getInventory().getItemInOffHand();
        String spellKey = controller.getSpell(item);
        if (spellKey != null) {
            spell = mage.getSpell(spellKey);
        }
    }
    CasterProperties casterProperties = mage.getActiveProperties();
    switch(placeholder) {
        case "path":
            ProgressionPath path = casterProperties.getPath();
            return path == null ? "" : path.getName();
        case "class":
            return activeClass == null ? "" : activeClass.getName();
        case "wand":
            return wand == null ? "" : wand.getName();
        case "spell":
            return spell == null ? "" : spell.getName();
    }
    return "";
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) Spell(com.elmakers.mine.bukkit.api.spell.Spell)

Example 9 with CasterProperties

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

the class SpellProgressAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage mage = context.getMage();
    CasterProperties casterProperties = context.getActiveProperties();
    this.context = context;
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    Collection<String> spells = casterProperties.getSpells();
    Collection<ItemStack> upgrades = new ArrayList<>();
    Messages messages = context.getController().getMessages();
    for (String spellKey : spells) {
        MageSpell spell = mage.getSpell(spellKey);
        SpellTemplate upgradeSpell = spell.getUpgrade();
        if (upgradeSpell != null) {
            ItemStack spellItem = MagicPlugin.getAPI().createSpellItem(upgradeSpell.getKey());
            if (spellItem != null) {
                long requiredCastCount = spell.getRequiredUpgradeCasts();
                String requiredPathKey = spell.getRequiredUpgradePath();
                Set<String> requiredPathTags = spell.getRequiredUpgradeTags();
                ItemMeta meta = spellItem.getItemMeta();
                List<String> lore = new ArrayList<>();
                String levelDescription = upgradeSpell.getLevelDescription();
                if (levelDescription == null || levelDescription.isEmpty()) {
                    levelDescription = upgradeSpell.getName();
                }
                lore.add(levelDescription);
                String upgradeDescription = upgradeSpell.getUpgradeDescription();
                if (upgradeDescription != null && !upgradeDescription.isEmpty()) {
                    upgradeDescription = context.getController().getMessages().get("spell.upgrade_description_prefix") + upgradeDescription;
                    InventoryUtils.wrapText(upgradeDescription, lore);
                }
                ProgressionPath currentPath = casterProperties.getPath();
                if (requiredPathKey != null && currentPath == null) {
                    continue;
                }
                if (!upgradeSpell.getName().equals(spell.getName())) {
                    lore.add(context.getMessage("upgrade_name_change", "&r&4Upgrades: &r$name").replace("$name", spell.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)
                        continue;
                    lore.add(context.getMessage("level_requirement").replace("$path", upgradePath.getName()));
                }
                if (requiredPathTags != null && !requiredPathTags.isEmpty() && !currentPath.hasAllTags(requiredPathTags)) {
                    Set<String> tags = currentPath.getMissingTags(requiredPathTags);
                    lore.add(context.getMessage("tags_requirement").replace("$tags", messages.formatList("tags", tags, "name")));
                }
                long castCount = Math.min(spell.getCastCount(), requiredCastCount);
                if (castCount == requiredCastCount) {
                    lore.add(ChatColor.GREEN + context.getMessage("cast_requirement").replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount)));
                } else {
                    lore.add(ChatColor.RED + context.getMessage("cast_requirement").replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount)));
                }
                meta.setLore(lore);
                spellItem.setItemMeta(meta);
                upgrades.add(spellItem);
            }
        }
    }
    String inventoryTitle = context.getMessage("title", "Spell Upgrades");
    int invSize = ((upgrades.size() + 9) / 9) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    for (ItemStack item : upgrades) {
        displayInventory.addItem(item);
    }
    mage.activateGUI(this, displayInventory);
    return SpellResult.CAST;
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) ArrayList(java.util.ArrayList) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Inventory(org.bukkit.inventory.Inventory) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 10 with CasterProperties

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

the class MagicRequirement method getRequirementDescription.

@Nullable
public String getRequirementDescription(@Nonnull CastContext context) {
    Mage mage = context.getMage();
    MageController controller = mage.getController();
    Player player = mage.getPlayer();
    if (permissionNode != null && (player == null || !player.hasPermission(permissionNode))) {
        return context.getMessage(SpellResult.INSUFFICIENT_PERMISSION.name().toLowerCase());
    }
    Wand wand = context.getWand();
    if (wand == null && requireWand) {
        return getMessage(context, "no_wand");
    }
    if (requiredTemplate != null) {
        String template = wand.getTemplateKey();
        if (template == null || !template.equals(requiredTemplate)) {
            return getMessage(context, "no_template").replace("$wand", wand.getName());
        }
    }
    if (requiredTemplates != null) {
        String template = wand.getTemplateKey();
        if (template == null || !requiredTemplates.contains(template)) {
            return getMessage(context, "no_template").replace("$wand", wand.getName());
        }
    }
    if (mageClass != null && !mageClass.isEmpty()) {
        if (mage.hasClassUnlocked(mageClass)) {
            return getMessage(context, "no_class").replace("$class", mageClass);
        }
    }
    CasterProperties checkProperties = context.getActiveProperties();
    ProgressionPath path = checkProperties.getPath();
    if (requiredPath != null || exactPath != null) {
        if (path == null) {
            return getMessage(context, "no_path");
        }
        if (requiredPath != null && !path.hasPath(requiredPath)) {
            WandUpgradePath requiresPath = controller.getPath(requiredPath);
            String pathName = requiredPath;
            if (requiresPath != null) {
                pathName = requiresPath.getName();
            } else {
                context.getLogger().warning("Invalid path specified in requirement " + requiredPath);
            }
            return getMessage(context, "no_required_path").replace("$path", pathName);
        }
        if (exactPath != null && !exactPath.equals(path.getKey())) {
            WandUpgradePath requiresPath = controller.getPath(exactPath);
            String pathName = exactPath;
            if (requiresPath != null) {
                pathName = requiresPath.getName();
            } else {
                context.getLogger().warning("Invalid path specified in requirement: " + exactPath);
            }
            return getMessage(context, "no_path_exact").replace("$path", pathName);
        }
        if (requiresCompletedPath != null) {
            boolean hasPathCompleted = false;
            if (path.hasPath(requiresCompletedPath)) {
                if (path.getKey().equals(requiresCompletedPath)) {
                    hasPathCompleted = !path.canProgress(checkProperties);
                } else {
                    hasPathCompleted = true;
                }
            }
            if (!hasPathCompleted) {
                WandUpgradePath requiresPath = controller.getPath(requiresCompletedPath);
                String pathName = requiresCompletedPath;
                if (requiresPath != null) {
                    pathName = requiresPath.getName();
                } else {
                    context.getLogger().warning("Invalid path specified in requirement: " + exactPath);
                }
                return getMessage(context, "no_path_end").replace("$path", pathName);
            }
        }
    }
    if (wandProperties != null) {
        String message = getRequiredProperty(context, wand, wandProperties);
        if (message != null) {
            return message;
        }
    }
    if (classProperties != null) {
        MageClass activeClass = mageClass == null ? mage.getActiveClass() : mage.getClass(mageClass);
        if (activeClass == null) {
            return getMessage(context, "no_path");
        }
        String message = getRequiredProperty(context, activeClass, classProperties);
        if (message != null) {
            return message;
        }
    }
    if (attributes != null) {
        for (PropertyRequirement requirement : attributes) {
            String key = requirement.key;
            Double value = mage.getAttribute(key);
            String message = checkRequiredProperty(context, requirement, key, value);
            if (message != null) {
                return message;
            }
        }
    }
    return null;
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) MageController(com.elmakers.mine.bukkit.api.magic.MageController) 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) 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