Search in sources :

Example 1 with WandUpgradePath

use of com.elmakers.mine.bukkit.api.wand.WandUpgradePath 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 2 with WandUpgradePath

use of com.elmakers.mine.bukkit.api.wand.WandUpgradePath in project MagicPlugin by elBukkit.

the class BaseShopAction method getInventoryTitle.

protected String getInventoryTitle(CastContext context) {
    Wand wand = context.getWand();
    WandUpgradePath path = (wand == null ? null : wand.getPath());
    String pathName = (path == null ? null : path.getName());
    if (pathName == null) {
        pathName = "";
    }
    String title = context.getMessage("title", getDefaultMessage(context, "title"));
    title = title.replace("$path", pathName);
    return title;
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 3 with WandUpgradePath

use of com.elmakers.mine.bukkit.api.wand.WandUpgradePath in project MagicPlugin by elBukkit.

the class BaseShopAction method checkContext.

public SpellResult checkContext(CastContext context) {
    Mage mage = context.getMage();
    MageController controller = mage.getController();
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (permissionNode != null && !player.hasPermission(permissionNode)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    if (!requireWand) {
        return SpellResult.CAST;
    }
    Wand wand = mage.getActiveWand();
    if (wand == null) {
        context.showMessage("no_wand", getDefaultMessage(context, "no_wand"));
        return SpellResult.FAIL;
    }
    WandUpgradePath path = wand.getPath();
    if (requiredTemplate != null) {
        String template = wand.getTemplateKey();
        if (template == null || !template.equals(requiredTemplate)) {
            context.showMessage(context.getMessage("no_template", getDefaultMessage(context, "no_template")).replace("$wand", wand.getName()));
            return SpellResult.FAIL;
        }
    }
    // Check path requirements
    if (requiredPath != null || exactPath != null) {
        if (path == null) {
            context.showMessage(context.getMessage("no_path", getDefaultMessage(context, "no_path")).replace("$wand", wand.getName()));
            return SpellResult.FAIL;
        }
        if (requiredPath != null && !path.hasPath(requiredPath)) {
            WandUpgradePath requiresPath = controller.getPath(requiredPath);
            if (requiresPath != null) {
                context.showMessage(context.getMessage("no_required_path", getDefaultMessage(context, "no_required_path")).replace("$path", requiresPath.getName()));
            } else {
                context.getLogger().warning("Invalid path specified in Shop action: " + requiredPath);
            }
            return SpellResult.FAIL;
        }
        if (exactPath != null && !exactPath.equals(path.getKey())) {
            WandUpgradePath requiresPath = controller.getPath(exactPath);
            if (requiresPath != null) {
                context.showMessage(context.getMessage("no_path_exact", getDefaultMessage(context, "no_path_exact")).replace("$path", requiresPath.getName()));
            } else {
                context.getLogger().warning("Invalid path specified in Shop action: " + exactPath);
            }
            return SpellResult.FAIL;
        }
        if (requiresCompletedPath != null) {
            if (path.canEnchant(wand)) {
                context.showMessage(context.getMessage("no_path_end", getDefaultMessage(context, "no_path_end")).replace("$path", path.getName()));
                return SpellResult.FAIL;
            }
        }
    }
    return SpellResult.CAST;
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 4 with WandUpgradePath

use of com.elmakers.mine.bukkit.api.wand.WandUpgradePath in project MagicPlugin by elBukkit.

the class AddSpellAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage mage = context.getMage();
    Wand wand = context.getWand();
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (permissionNode != null && !player.hasPermission(permissionNode)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    if (wand == null || spellKey == null || spellKey.isEmpty()) {
        context.showMessage("no_wand", "You must be holding a wand!");
        return SpellResult.FAIL;
    }
    if (wand.hasSpell(spellKey)) {
        return SpellResult.NO_TARGET;
    }
    if (requiredPath != null || exactPath != null) {
        WandUpgradePath path = wand.getPath();
        if (path == null) {
            context.showMessage(context.getMessage("no_upgrade", "You may not learn with that $wand.").replace("$wand", wand.getName()));
            return SpellResult.FAIL;
        }
        MageController controller = context.getController();
        if (requiredPath != null && !path.hasPath(requiredPath)) {
            WandUpgradePath requiresPath = controller.getPath(requiredPath);
            if (requiresPath != null) {
                context.showMessage(context.getMessage("no_required_path", "You must be at least $path!").replace("$path", requiresPath.getName()));
            } else {
                context.getLogger().warning("Invalid path specified in AddSpell action: " + requiredPath);
            }
            return SpellResult.FAIL;
        }
        if (exactPath != null && !exactPath.equals(path.getKey())) {
            WandUpgradePath requiresPath = controller.getPath(exactPath);
            if (requiresPath != null) {
                context.showMessage(context.getMessage("no_path_exact", "You must be at $path!").replace("$path", requiresPath.getName()));
            } else {
                context.getLogger().warning("Invalid path specified in AddSpell action: " + exactPath);
            }
            return SpellResult.FAIL;
        }
        if (requiresCompletedPath != null) {
            WandUpgradePath pathUpgrade = path.getUpgrade();
            if (pathUpgrade == null) {
                context.showMessage(context.getMessage("no_upgrade", "There is nothing more for you here.").replace("$wand", wand.getName()));
                return SpellResult.FAIL;
            }
            if (path.canEnchant(wand)) {
                context.showMessage(context.getMessage("no_path_end", "You must be ready to advance to $path!").replace("$path", pathUpgrade.getName()));
                return SpellResult.FAIL;
            }
        }
    }
    if (!wand.addSpell(spellKey)) {
        return SpellResult.NO_TARGET;
    }
    wand.setActiveSpell(spellKey);
    if (autoUpgrade) {
        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);
        }
    }
    return SpellResult.CAST;
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 5 with WandUpgradePath

use of com.elmakers.mine.bukkit.api.wand.WandUpgradePath in project MagicPlugin by elBukkit.

the class EnchantWandAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage mage = context.getMage();
    Wand wand = context.getWand();
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (levels <= 0) {
        return SpellResult.FAIL;
    }
    if (wand == null) {
        context.showMessage("no_wand", "You must be holding a wand!");
        return SpellResult.FAIL;
    }
    if (requiredPath != null || exactPath != null) {
        WandUpgradePath path = wand.getPath();
        if (path == null) {
            context.showMessage(context.getMessage("no_upgrade", "You may not learn with that $wand.").replace("$wand", wand.getName()));
            return SpellResult.FAIL;
        }
        if ((requiredPath != null && !path.hasPath(requiredPath)) || (exactPath != null && !exactPath.equals(path.getKey()))) {
            WandUpgradePath requiresPath = com.elmakers.mine.bukkit.wand.WandUpgradePath.getPath(requiredPath);
            if (requiresPath != null) {
                context.showMessage(context.getMessage("no_path", "You may not learn with that $wand.").replace("$path", requiresPath.getName()));
            } else {
                context.getLogger().warning("Invalid path specified in EnchantWand action: " + requiredPath);
            }
            return SpellResult.FAIL;
        }
    }
    if (requireItem != null) {
        MageController controller = context.getController();
        boolean foundItem = false;
        ItemStack[] contents = player.getInventory().getContents();
        for (int i = 0; i < contents.length; i++) {
            ItemStack item = contents[i];
            if (controller.itemsAreEqual(item, requireItem)) {
                player.getInventory().setItem(i, null);
                foundItem = true;
                break;
            }
        }
        if (!foundItem) {
            context.showMessage("insufficient_resources", "You must have a $requires");
            return SpellResult.INSUFFICIENT_RESOURCES;
        }
    }
    int xpLevels = levels;
    if (useXp) {
        xpLevels = mage.getLevel();
    }
    int levelsUsed = wand.enchant(xpLevels, mage);
    if (levelsUsed == 0) {
        return SpellResult.FAIL;
    }
    if (useXp) {
        mage.setLevel(Math.max(0, mage.getLevel() - levelsUsed));
    }
    return SpellResult.CAST;
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.api.wand.WandUpgradePath) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

Wand (com.elmakers.mine.bukkit.api.wand.Wand)7 WandUpgradePath (com.elmakers.mine.bukkit.api.wand.WandUpgradePath)7 Mage (com.elmakers.mine.bukkit.api.magic.Mage)5 MageController (com.elmakers.mine.bukkit.api.magic.MageController)5 Player (org.bukkit.entity.Player)5 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)2 ItemStack (org.bukkit.inventory.ItemStack)2 CastEvent (com.elmakers.mine.bukkit.api.event.CastEvent)1 PreCastEvent (com.elmakers.mine.bukkit.api.event.PreCastEvent)1 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)1 Messages (com.elmakers.mine.bukkit.api.magic.Messages)1 ProgressionPath (com.elmakers.mine.bukkit.api.magic.ProgressionPath)1 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)1 Spell (com.elmakers.mine.bukkit.api.spell.Spell)1 SpellCategory (com.elmakers.mine.bukkit.api.spell.SpellCategory)1 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)1 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)1 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)1 Nullable (javax.annotation.Nullable)1 Inventory (org.bukkit.inventory.Inventory)1