Search in sources :

Example 1 with Cost

use of com.elmakers.mine.bukkit.item.Cost in project MagicPlugin by elBukkit.

the class SelectorAction 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, "slot")) {
        if (!autoClose) {
            mage.deactivateGUI();
        }
        return;
    }
    int slotIndex = Integer.parseInt(InventoryUtils.getMetaString(item, "slot"));
    MageController controller = context.getController();
    SelectorOption option = showingItems.get(slotIndex);
    if (option == null || option.isPlaceholder()) {
        return;
    }
    String unpurchasableMessage = InventoryUtils.getMetaString(item, "unpurchasable");
    if (unpurchasableMessage != null && !unpurchasableMessage.isEmpty()) {
        context.showMessage(unpurchasableMessage);
        mage.deactivateGUI();
        return;
    }
    Cost required = option.getRequiredCost(this, context);
    if (required != null) {
        String baseMessage = getMessage("insufficient");
        String costDescription = required.getFullDescription(controller.getMessages(), this);
        costDescription = baseMessage.replace("$cost", costDescription);
        context.showMessage(costDescription);
    } else {
        String itemName = option.getName();
        if (InventoryUtils.hasMeta(item, "confirm")) {
            String inventoryTitle = getConfirmTitle(option).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;
        }
        finalResult = option.give(this, context);
        if (finalResult.isSuccess() && finalResult != SpellResult.NO_TARGET) {
            context.showMessage(option.getSelectedMessage(this));
        }
    }
    if (autoClose || finalResult != SpellResult.CAST) {
        if (isActive) {
            mage.deactivateGUI();
        }
    } else {
        // update title
        mage.continueGUI(this, getInventory(context));
    }
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) Cost(com.elmakers.mine.bukkit.item.Cost) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

Mage (com.elmakers.mine.bukkit.api.magic.Mage)1 MageController (com.elmakers.mine.bukkit.api.magic.MageController)1 Cost (com.elmakers.mine.bukkit.item.Cost)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1