Search in sources :

Example 31 with MageController

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

the class VanishAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null) {
        return SpellResult.NO_TARGET;
    }
    MageController controller = context.getController();
    Mage mage = controller.getMage(entity);
    mage.setVanished(vanish);
    if (vanish) {
        context.registerForUndo(new UndoVanish(mage));
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Example 32 with MageController

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

the class SkillSelectorAction method clicked.

@Override
public void clicked(InventoryClickEvent event) {
    InventoryAction action = event.getAction();
    if (action == InventoryAction.NOTHING) {
        int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
        page = page + direction;
        openInventory();
        event.setCancelled(true);
        return;
    }
    ItemStack clickedItem = event.getCurrentItem();
    if (clickedItem != null && InventoryUtils.getMetaBoolean(clickedItem, "unavailable", false)) {
        event.setCancelled(true);
        return;
    }
    Mage mage = context.getMage();
    MageController controller = mage.getController();
    Inventory inventory = mage.getInventory();
    boolean isContainerSlot = event.getSlot() == event.getRawSlot();
    boolean isDrop = action == InventoryAction.DROP_ALL_CURSOR || action == InventoryAction.DROP_ALL_SLOT || action == InventoryAction.DROP_ONE_CURSOR || action == InventoryAction.DROP_ONE_SLOT;
    if (!isContainerSlot && isDrop && controller.isSkill(clickedItem) && !InventoryUtils.getMetaBoolean(clickedItem, "undroppable", false)) {
        inventory.setItem(event.getSlot(), null);
        return;
    }
    if (inventoryLimit > 0) {
        boolean isHotbar = event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
        if (isHotbar) {
            ItemStack destinationItem = inventory.getItem(event.getHotbarButton());
            if (controller.isSkill(destinationItem))
                return;
            isHotbar = controller.isSkill(clickedItem);
        }
        if (!isContainerSlot && !isHotbar)
            return;
        int skillCount = 0;
        for (int i = 0; i < inventory.getSize(); i++) {
            ItemStack item = inventory.getItem(i);
            if (controller.isSkill(item))
                skillCount++;
        }
        if (skillCount >= inventoryLimit) {
            event.setCancelled(true);
        }
        return;
    }
    // We don't allow quick-casting here if there is an inventory limit.
    if (action == InventoryAction.PICKUP_HALF) {
        Spell spell = mage.getSpell(Wand.getSpell(clickedItem));
        if (spell != null) {
            spell.cast();
        }
        mage.getPlayer().closeInventory();
        event.setCancelled(true);
    }
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) Spell(com.elmakers.mine.bukkit.api.spell.Spell) InventoryAction(org.bukkit.event.inventory.InventoryAction)

Example 33 with MageController

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

the class WandUpgradePath method upgrade.

@Override
public void upgrade(com.elmakers.mine.bukkit.api.wand.Wand wand, com.elmakers.mine.bukkit.api.magic.Mage mage) {
    WandUpgradePath newPath = getUpgrade();
    if (newPath == null) {
        if (mage != null)
            mage.sendMessage("Configuration issue, please check logs");
        wand.getController().getLogger().warning("Invalid upgrade path: " + this.getUpgrade());
        return;
    }
    if (mage != null) {
        MageController controller = mage.getController();
        mage.sendMessage(getMessage(controller.getMessages(), "level_up").replace("$wand", wand.getName()).replace("$path", newPath.getName()));
    }
    this.upgraded(wand, mage);
    if (this.icon != null && this.icon.equals(wand.getIcon())) {
        com.elmakers.mine.bukkit.api.block.MaterialAndData newIcon = newPath.getIcon();
        if (newIcon != null) {
            wand.setIcon(newIcon);
        }
    }
    newPath.upgradeTo(wand);
    // Don't do events without a mage
    if (mage == null) {
        return;
    }
    WandUpgradeEvent legacyEvent = new WandUpgradeEvent(mage, wand, this, newPath);
    Bukkit.getPluginManager().callEvent(legacyEvent);
    PathUpgradeEvent upgradeEvent = new PathUpgradeEvent(mage, wand, wand.getMageClass(), this, newPath);
    Bukkit.getPluginManager().callEvent(upgradeEvent);
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) WandUpgradeEvent(com.elmakers.mine.bukkit.api.event.WandUpgradeEvent) PathUpgradeEvent(com.elmakers.mine.bukkit.api.event.PathUpgradeEvent)

Example 34 with MageController

use of com.elmakers.mine.bukkit.api.magic.MageController 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 35 with MageController

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

the class BaseShopAction method takeCosts.

protected boolean takeCosts(CastContext context, ShopItem shopItem) {
    Mage mage = context.getMage();
    MageController controller = context.getController();
    double worth = shopItem.getWorth();
    if (isXP) {
        worth = Math.ceil(costScale * worth * controller.getWorthBase() / controller.getWorthXP());
        mage.removeExperience((int) worth);
    } else if (isItems) {
        worth = Math.ceil(costScale * worth * controller.getWorthBase() / controller.getWorthItemAmount());
        removeItems(controller, mage, (int) Math.ceil(worth));
    } else if (isSkillPoints) {
        worth = Math.ceil(costScale * worth * controller.getWorthBase() / controller.getWorthSkillPoints());
        mage.addSkillPoints(-(int) Math.ceil(worth));
    } else {
        worth = Math.ceil(costScale * worth * controller.getWorthBase());
        VaultController.getInstance().withdrawPlayer(mage.getPlayer(), worth);
    }
    return true;
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Aggregations

MageController (com.elmakers.mine.bukkit.api.magic.MageController)76 Mage (com.elmakers.mine.bukkit.api.magic.Mage)45 Entity (org.bukkit.entity.Entity)27 Player (org.bukkit.entity.Player)26 Location (org.bukkit.Location)16 ItemStack (org.bukkit.inventory.ItemStack)16 Wand (com.elmakers.mine.bukkit.api.wand.Wand)14 Block (org.bukkit.block.Block)11 LivingEntity (org.bukkit.entity.LivingEntity)11 ArrayList (java.util.ArrayList)10 Inventory (org.bukkit.inventory.Inventory)9 ItemMeta (org.bukkit.inventory.meta.ItemMeta)8 Spell (com.elmakers.mine.bukkit.api.spell.Spell)5 MagicController (com.elmakers.mine.bukkit.magic.MagicController)5 Nullable (javax.annotation.Nullable)5 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)5 ItemData (com.elmakers.mine.bukkit.api.item.ItemData)4 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)4 WandUpgradePath (com.elmakers.mine.bukkit.api.wand.WandUpgradePath)4 File (java.io.File)4