Search in sources :

Example 1 with WandUpgradePath

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

the class MagicController method getSpellBook.

public ItemStack getSpellBook(com.elmakers.mine.bukkit.api.spell.SpellCategory category, int count) {
    Map<String, List<SpellTemplate>> categories = new HashMap<>();
    Collection<SpellTemplate> spellVariants = spells.values();
    String categoryKey = category == null ? null : category.getKey();
    for (SpellTemplate spell : spellVariants) {
        if (spell.isHidden() || spell.getSpellKey().isVariant())
            continue;
        com.elmakers.mine.bukkit.api.spell.SpellCategory spellCategory = spell.getCategory();
        if (spellCategory == null)
            continue;
        String spellCategoryKey = spellCategory.getKey();
        if (categoryKey == null || spellCategoryKey.equalsIgnoreCase(categoryKey)) {
            List<SpellTemplate> categorySpells = categories.get(spellCategoryKey);
            if (categorySpells == null) {
                categorySpells = new ArrayList<>();
                categories.put(spellCategoryKey, categorySpells);
            }
            categorySpells.add(spell);
        }
    }
    List<String> categoryKeys = new ArrayList<>(categories.keySet());
    Collections.sort(categoryKeys);
    // Hrm? So much Copy+paste! :(
    CostReducer reducer = null;
    ItemStack bookItem = new ItemStack(Material.WRITTEN_BOOK, count);
    BookMeta book = (BookMeta) bookItem.getItemMeta();
    book.setAuthor(messages.get("books.default.author"));
    String title = null;
    if (category != null) {
        title = messages.get("books.default.title").replace("$category", category.getName());
    } else {
        title = messages.get("books.all.title");
    }
    book.setTitle(title);
    List<String> pages = new ArrayList<>();
    Set<String> paths = WandUpgradePath.getPathKeys();
    for (String key : categoryKeys) {
        category = getCategory(key);
        title = messages.get("books.default.title").replace("$category", category.getName());
        String description = "" + ChatColor.BOLD + ChatColor.BLUE + title + "\n\n";
        description += "" + ChatColor.RESET + ChatColor.DARK_BLUE + category.getDescription();
        pages.add(description);
        List<SpellTemplate> categorySpells = categories.get(key);
        Collections.sort(categorySpells);
        for (SpellTemplate spell : categorySpells) {
            List<String> lines = new ArrayList<>();
            lines.add("" + ChatColor.GOLD + ChatColor.BOLD + spell.getName());
            lines.add("" + ChatColor.RESET);
            String spellDescription = spell.getDescription();
            if (spellDescription != null && spellDescription.length() > 0) {
                lines.add("" + ChatColor.BLACK + spellDescription);
                lines.add("");
            }
            String spellCooldownDescription = spell.getCooldownDescription();
            if (spellCooldownDescription != null && spellCooldownDescription.length() > 0) {
                spellCooldownDescription = messages.get("cooldown.description").replace("$time", spellCooldownDescription);
                lines.add("" + ChatColor.DARK_PURPLE + spellCooldownDescription);
            }
            String spellMageCooldownDescription = spell.getMageCooldownDescription();
            if (spellMageCooldownDescription != null && spellMageCooldownDescription.length() > 0) {
                spellMageCooldownDescription = messages.get("cooldown.mage_description").replace("$time", spellMageCooldownDescription);
                lines.add("" + ChatColor.RED + spellMageCooldownDescription);
            }
            Collection<CastingCost> costs = spell.getCosts();
            if (costs != null) {
                for (CastingCost cost : costs) {
                    if (!cost.isEmpty(reducer)) {
                        lines.add(ChatColor.DARK_PURPLE + messages.get("wand.costs_description").replace("$description", cost.getFullDescription(messages, reducer)));
                    }
                }
            }
            Collection<CastingCost> activeCosts = spell.getActiveCosts();
            if (activeCosts != null) {
                for (CastingCost cost : activeCosts) {
                    if (!cost.isEmpty(reducer)) {
                        lines.add(ChatColor.DARK_PURPLE + messages.get("wand.active_costs_description").replace("$description", cost.getFullDescription(messages, reducer)));
                    }
                }
            }
            for (String pathKey : paths) {
                WandUpgradePath checkPath = WandUpgradePath.getPath(pathKey);
                if (!checkPath.isHidden() && (checkPath.hasSpell(spell.getKey()) || checkPath.hasExtraSpell(spell.getKey()))) {
                    lines.add(ChatColor.DARK_BLUE + messages.get("spell.available_path").replace("$path", checkPath.getName()));
                    break;
                }
            }
            for (String pathKey : paths) {
                WandUpgradePath checkPath = WandUpgradePath.getPath(pathKey);
                if (checkPath.requiresSpell(spell.getKey())) {
                    lines.add(ChatColor.DARK_RED + messages.get("spell.required_path").replace("$path", checkPath.getName()));
                    break;
                }
            }
            String duration = spell.getDurationDescription(messages);
            if (duration != null) {
                lines.add(ChatColor.DARK_GREEN + duration);
            } else if (spell.showUndoable()) {
                if (spell.isUndoable()) {
                    String undoable = messages.get("spell.undoable", "");
                    if (undoable != null && !undoable.isEmpty()) {
                        lines.add(undoable);
                    }
                } else {
                    String notUndoable = messages.get("spell.not_undoable", "");
                    if (notUndoable != null && !notUndoable.isEmpty()) {
                        lines.add(notUndoable);
                    }
                }
            }
            if (spell.usesBrush()) {
                lines.add(ChatColor.DARK_GRAY + messages.get("spell.brush"));
            }
            SpellKey baseKey = spell.getSpellKey();
            SpellKey upgradeKey = new SpellKey(baseKey.getBaseKey(), baseKey.getLevel() + 1);
            SpellTemplate upgradeSpell = getSpellTemplate(upgradeKey.getKey());
            int spellLevels = 0;
            while (upgradeSpell != null) {
                spellLevels++;
                upgradeKey = new SpellKey(upgradeKey.getBaseKey(), upgradeKey.getLevel() + 1);
                upgradeSpell = getSpellTemplate(upgradeKey.getKey());
            }
            if (spellLevels > 0) {
                spellLevels++;
                lines.add(ChatColor.DARK_AQUA + messages.get("spell.levels_available").replace("$levels", Integer.toString(spellLevels)));
            }
            String usage = spell.getUsage();
            if (usage != null && usage.length() > 0) {
                lines.add("" + ChatColor.GRAY + ChatColor.ITALIC + usage + ChatColor.RESET);
                lines.add("");
            }
            String spellExtendedDescription = spell.getExtendedDescription();
            if (spellExtendedDescription != null && spellExtendedDescription.length() > 0) {
                lines.add("" + ChatColor.BLACK + spellExtendedDescription);
                lines.add("");
            }
            pages.add(StringUtils.join(lines, "\n"));
        }
    }
    book.setPages(pages);
    bookItem.setItemMeta(book);
    return bookItem;
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.wand.WandUpgradePath) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) CostReducer(com.elmakers.mine.bukkit.api.spell.CostReducer) ArrayList(java.util.ArrayList) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) BookMeta(org.bukkit.inventory.meta.BookMeta) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 2 with WandUpgradePath

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

the class EnchantingController method onPrepareEnchantItem.

@EventHandler
public void onPrepareEnchantItem(PrepareItemEnchantEvent event) {
    if (Wand.isWand(event.getItem())) {
        if (!enchantingEnabled) {
            event.setCancelled(true);
            return;
        }
        Player player = event.getEnchanter();
        if (player == null || !controller.hasPermission(player, "Magic.wand.enchant")) {
            event.setCancelled(true);
            return;
        }
        Wand wandItem = controller.getWand(event.getItem());
        if (!wandItem.isModifiable() && wandItem.getPath() == null) {
            event.setCancelled(true);
            return;
        }
        if (controller.isSPEnabled() && wandItem.hasSpellProgression()) {
            event.setCancelled(true);
            return;
        }
        if (!wandItem.canUse(player)) {
            event.setCancelled(true);
            return;
        }
        wandItem.makeEnchantable(true);
        WandUpgradePath path = wandItem.getPath();
        if (path == null) {
            event.setCancelled(true);
            return;
        }
        int minLevel = path.getMinLevel();
        int maxLevel = path.getMaxLevel();
        int levelRange = maxLevel - minLevel;
        int[] offered = event.getExpLevelCostsOffered();
        float bonusLevelMultiplier = path.getBonusLevelMultiplier();
        int bonusLevels = event.getEnchantmentBonus();
        for (int i = 0; i < offered.length; i++) {
            int level = minLevel + (int) ((float) i * levelRange / offered.length);
            if (bonusLevels > 0 && bonusLevelMultiplier > 0) {
                level = (int) (level + bonusLevels * bonusLevelMultiplier);
            }
            offered[i] = level;
        }
        event.setCancelled(false);
    }
}
Also used : WandUpgradePath(com.elmakers.mine.bukkit.wand.WandUpgradePath) Player(org.bukkit.entity.Player) Wand(com.elmakers.mine.bukkit.wand.Wand) EventHandler(org.bukkit.event.EventHandler)

Aggregations

WandUpgradePath (com.elmakers.mine.bukkit.wand.WandUpgradePath)2 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)1 CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)1 CostReducer (com.elmakers.mine.bukkit.api.spell.CostReducer)1 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)1 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)1 Wand (com.elmakers.mine.bukkit.wand.Wand)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 ItemStack (org.bukkit.inventory.ItemStack)1 BookMeta (org.bukkit.inventory.meta.BookMeta)1