Search in sources :

Example 6 with MagicController

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

the class SkillSelectorAction method perform.

@Override
public SpellResult perform(CastContext context) {
    this.context = context;
    MageController apiController = context.getController();
    Player player = context.getMage().getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (!(apiController instanceof MagicController))
        return SpellResult.FAIL;
    MagicController controller = (MagicController) apiController;
    Messages messages = controller.getMessages();
    HeroesManager heroes = controller.getHeroes();
    allSkills.clear();
    if (controller.useHeroesSkills() && heroes != null) {
        @Nonnull String classString = heroes.getClassName(player);
        @Nonnull String class2String = heroes.getSecondaryClassName(player);
        String messageKey = !class2String.isEmpty() ? "skills.inventory_title_secondary" : "skills.inventory_title";
        inventoryTitle = controller.getMessages().get(messageKey, "Skills ($page/$pages)");
        inventoryTitle = inventoryTitle.replace("$class2", class2String).replace("$class", classString);
        List<String> heroesSkills = heroes.getSkillList(player, true, true);
        for (String heroesSkill : heroesSkills) {
            allSkills.add(new SkillDescription(heroes, controller, player, heroesSkill));
        }
    } else {
        inventoryTitle = messages.get("skills.inventory_title");
    }
    if (controller.usePermissionSkills()) {
        boolean bypassHidden = player.hasPermission("Magic.bypass_hidden");
        Collection<SpellTemplate> spells = controller.getSpellTemplates(bypassHidden);
        for (SpellTemplate spell : spells) {
            SpellKey key = spell.getSpellKey();
            if (key.isVariant())
                continue;
            if (key.getBaseKey().startsWith("heroes*"))
                continue;
            if (!spell.hasCastPermission(player))
                continue;
            allSkills.add(new SkillDescription(spell));
        }
    } else {
        Mage mage = controller.getMage(player);
        MageClass activeClass = mage.getActiveClass();
        if (activeClass != null) {
            // gather spells in player's inventory to avoid showing
            Set<String> hasSpells = new HashSet<>();
            for (ItemStack item : player.getInventory().getContents()) {
                String spellKey = controller.getSpell(item);
                if (spellKey != null) {
                    hasSpells.add(spellKey);
                }
            }
            classKey = activeClass.getKey();
            skillsConfig = activeClass.getProperty("skills", ConfigurationSection.class);
            inventoryLimit = activeClass.getProperty("skills.skill_limit", 0);
            Collection<String> spells = activeClass.getSpells();
            for (String spellKey : spells) {
                if (hasSpells.contains(spellKey)) {
                    extraSlots++;
                    continue;
                }
                SpellTemplate spell = controller.getSpellTemplate(spellKey);
                if (spell != null) {
                    allSkills.add(new SkillDescription(spell));
                }
            }
        }
    }
    if (allSkills.size() == 0) {
        player.sendMessage(messages.get("skills.none"));
        return SpellResult.NO_ACTION;
    }
    Collections.sort(allSkills);
    openInventory();
    return SpellResult.CAST;
}
Also used : Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Nonnull(javax.annotation.Nonnull) HeroesManager(com.elmakers.mine.bukkit.heroes.HeroesManager) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MagicController(com.elmakers.mine.bukkit.magic.MagicController) ItemStack(org.bukkit.inventory.ItemStack) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) HashSet(java.util.HashSet) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 7 with MagicController

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

the class SkillSelectorAction method openInventory.

protected void openInventory() {
    MageController apiController = context.getController();
    if (!(apiController instanceof MagicController))
        return;
    MagicController controller = (MagicController) apiController;
    HeroesManager heroes = controller.getHeroes();
    int inventorySize = 9 * controller.getSkillInventoryRows();
    float numSlots = extraSlots + allSkills.size();
    int numPages = (int) Math.ceil(numSlots / inventorySize);
    if (page < 1)
        page = numPages;
    else if (page > numPages)
        page = 1;
    Mage mage = context.getMage();
    Player player = mage.getPlayer();
    int pageIndex = page - 1;
    int startIndex = pageIndex * inventorySize;
    int maxIndex = (pageIndex + 1) * inventorySize - 1;
    List<SkillDescription> skills = new ArrayList<>();
    for (int i = startIndex; i <= maxIndex && i < allSkills.size(); i++) {
        skills.add(allSkills.get(i));
    }
    if (skills.size() == 0) {
        String messageTemplate = controller.getMessages().get("skills.none_on_page", "$page");
        player.sendMessage(messageTemplate.replace("$page", Integer.toString(page)));
        return;
    }
    int invSize = (int) Math.ceil(numSlots / 9.0f) * 9;
    String title = inventoryTitle;
    title = title.replace("$pages", Integer.toString(numPages)).replace("$page", Integer.toString(page));
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, title);
    for (SkillDescription skill : skills) {
        ItemStack skillItem = controller.getAPI().createItem(skill.getSkillKey(), mage);
        if (skillItem == null)
            continue;
        InventoryUtils.configureSkillItem(skillItem, classKey, skillsConfig);
        if (skill.isHeroes() && heroes != null && !heroes.canUseSkill(player, skill.heroesSkill)) {
            String nameTemplate = controller.getMessages().get("skills.item_name_unavailable", "$skill");
            String spellName = skill.spell != null ? skill.spell.getName() : skill.heroesSkill;
            CompatibilityUtils.setDisplayName(skillItem, nameTemplate.replace("$skill", spellName));
            InventoryUtils.setMetaBoolean(skillItem, "unavailable", true);
            if (skill.spell != null) {
                MaterialAndData disabledIcon = skill.spell.getDisabledIcon();
                if (disabledIcon != null) {
                    disabledIcon.applyToItem(skillItem);
                } else {
                    String disabledIconURL = skill.spell.getDisabledIconURL();
                    if (disabledIconURL != null && !disabledIconURL.isEmpty()) {
                        InventoryUtils.setNewSkullURL(skillItem, disabledIconURL);
                    }
                }
            }
        }
        displayInventory.addItem(skillItem);
    }
    mage.deactivateGUI();
    mage.activateGUI(this, displayInventory);
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) HeroesManager(com.elmakers.mine.bukkit.heroes.HeroesManager) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MagicController(com.elmakers.mine.bukkit.magic.MagicController) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory)

Aggregations

MagicController (com.elmakers.mine.bukkit.magic.MagicController)7 MageController (com.elmakers.mine.bukkit.api.magic.MageController)5 Mage (com.elmakers.mine.bukkit.api.magic.Mage)4 Player (org.bukkit.entity.Player)4 HeroesManager (com.elmakers.mine.bukkit.heroes.HeroesManager)2 ArrayList (java.util.ArrayList)2 ItemStack (org.bukkit.inventory.ItemStack)2 Batch (com.elmakers.mine.bukkit.api.batch.Batch)1 BlockData (com.elmakers.mine.bukkit.api.block.BlockData)1 MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)1 MageData (com.elmakers.mine.bukkit.api.data.MageData)1 MageDataCallback (com.elmakers.mine.bukkit.api.data.MageDataCallback)1 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)1 MagicAPI (com.elmakers.mine.bukkit.api.magic.MagicAPI)1 Messages (com.elmakers.mine.bukkit.api.magic.Messages)1 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)1 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)1 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)1 LostWand (com.elmakers.mine.bukkit.api.wand.LostWand)1 UndoList (com.elmakers.mine.bukkit.block.UndoList)1