Search in sources :

Example 6 with MaterialAndData

use of com.elmakers.mine.bukkit.api.block.MaterialAndData in project MagicPlugin by elBukkit.

the class Mage method updateHotbarStatus.

public void updateHotbarStatus() {
    Player player = getPlayer();
    if (player != null) {
        Location location = getLocation();
        for (int i = 0; i < Wand.HOTBAR_SIZE; i++) {
            ItemStack spellItem = player.getInventory().getItem(i);
            String spellKey = Wand.getSpell(spellItem);
            String classKey = Wand.getSpellClass(spellItem);
            if (spellKey != null) {
                Spell spell = getSpell(spellKey);
                if (spell != null) {
                    int targetAmount = 1;
                    long remainingCooldown = 0;
                    CastingCost requiredCost = null;
                    boolean canCastSpell = false;
                    if (classKey != null && !classKey.isEmpty()) {
                        MageClass mageClass = getClass(classKey);
                        if (mageClass != null && spell instanceof BaseSpell) {
                            BaseSpell baseSpell = (BaseSpell) spell;
                            baseSpell.setMageClass(mageClass);
                            remainingCooldown = spell.getRemainingCooldown();
                            requiredCost = spell.getRequiredCost();
                            canCastSpell = spell.canCast(location);
                            baseSpell.setMageClass(null);
                        }
                    } else {
                        remainingCooldown = spell.getRemainingCooldown();
                        requiredCost = spell.getRequiredCost();
                        canCastSpell = spell.canCast(location);
                    }
                    boolean canCast = canCastSpell;
                    if (canCastSpell && remainingCooldown == 0 && requiredCost == null) {
                        targetAmount = 1;
                    } else if (canCastSpell) {
                        canCast = remainingCooldown == 0;
                        targetAmount = Wand.LiveHotbarCooldown ? (int) Math.min(Math.ceil((double) remainingCooldown / 1000), 99) : 99;
                        if (Wand.LiveHotbarCooldown && requiredCost != null) {
                            int mana = requiredCost.getMana();
                            if (mana > 0) {
                                if (mana <= getEffectiveManaMax() && getEffectiveManaRegeneration() > 0) {
                                    float remainingMana = mana - getMana();
                                    canCast = canCast && remainingMana <= 0;
                                    int targetManaTime = (int) Math.min(Math.ceil(remainingMana / getEffectiveManaRegeneration()), 99);
                                    targetAmount = Math.max(targetManaTime, targetAmount);
                                } else {
                                    canCastSpell = false;
                                    canCast = false;
                                }
                            }
                        }
                    }
                    if (targetAmount == 0)
                        targetAmount = 1;
                    boolean setAmount = false;
                    MaterialAndData disabledIcon = spell.getDisabledIcon();
                    MaterialAndData spellIcon = spell.getIcon();
                    String urlIcon = spell.getIconURL();
                    String disabledUrlIcon = spell.getDisabledIconURL();
                    boolean usingURLIcon = (controller.isUrlIconsEnabled() || spellIcon == null || spellIcon.getMaterial() == Material.AIR) && urlIcon != null && !urlIcon.isEmpty();
                    if (disabledIcon != null && spellIcon != null && !usingURLIcon) {
                        if (!canCast) {
                            if (disabledIcon.isValid() && (disabledIcon.getMaterial() != spellItem.getType() || disabledIcon.getData() != spellItem.getDurability())) {
                                disabledIcon.applyToItem(spellItem);
                            }
                            if (!canCastSpell) {
                                if (spellItem.getAmount() != 1) {
                                    spellItem.setAmount(1);
                                }
                                setAmount = true;
                            }
                        } else {
                            if (spellIcon.isValid() && (spellIcon.getMaterial() != spellItem.getType() || spellIcon.getData() != spellItem.getDurability())) {
                                spellIcon.applyToItem(spellItem);
                            }
                        }
                    } else if (usingURLIcon && disabledUrlIcon != null && !disabledUrlIcon.isEmpty() && spellItem.getType() == Material.SKULL_ITEM) {
                        String currentURL = InventoryUtils.getSkullURL(spellItem);
                        if (!canCast) {
                            if (!disabledUrlIcon.equals(currentURL)) {
                                InventoryUtils.setNewSkullURL(spellItem, disabledUrlIcon);
                                player.getInventory().setItem(i, spellItem);
                            }
                            if (!canCastSpell) {
                                if (spellItem.getAmount() != 1) {
                                    spellItem.setAmount(1);
                                }
                                setAmount = true;
                            }
                        } else {
                            if (!urlIcon.equals(currentURL)) {
                                InventoryUtils.setNewSkullURL(spellItem, urlIcon);
                                player.getInventory().setItem(i, spellItem);
                            }
                        }
                    }
                    if (!setAmount && spellItem.getAmount() != targetAmount) {
                        spellItem.setAmount(targetAmount);
                    }
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) ItemStack(org.bukkit.inventory.ItemStack) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ActionSpell(com.elmakers.mine.bukkit.spell.ActionSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) Location(org.bukkit.Location) CastSourceLocation(com.elmakers.mine.bukkit.api.magic.CastSourceLocation)

Example 7 with MaterialAndData

use of com.elmakers.mine.bukkit.api.block.MaterialAndData 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

MaterialAndData (com.elmakers.mine.bukkit.api.block.MaterialAndData)7 ItemStack (org.bukkit.inventory.ItemStack)5 Player (org.bukkit.entity.Player)4 ArrayList (java.util.ArrayList)3 Material (org.bukkit.Material)3 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 MageController (com.elmakers.mine.bukkit.api.magic.MageController)2 CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)2 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)2 Location (org.bukkit.Location)2 Vector (org.bukkit.util.Vector)2 EntityData (com.elmakers.mine.bukkit.api.entity.EntityData)1 CastSourceLocation (com.elmakers.mine.bukkit.api.magic.CastSourceLocation)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 SpellKey (com.elmakers.mine.bukkit.api.spell.SpellKey)1 Wand (com.elmakers.mine.bukkit.api.wand.Wand)1 HeroesManager (com.elmakers.mine.bukkit.heroes.HeroesManager)1 MagicController (com.elmakers.mine.bukkit.magic.MagicController)1