Search in sources :

Example 1 with MageClass

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

the class Wand method activate.

public boolean activate(Mage mage, boolean offhand) {
    if (mage == null)
        return false;
    Player player = mage.getPlayer();
    if (player == null)
        return false;
    if (!controller.hasWandPermission(player, this))
        return false;
    InventoryView openInventory = player.getOpenInventory();
    InventoryType inventoryType = openInventory.getType();
    if (inventoryType == InventoryType.ENCHANTING || inventoryType == InventoryType.ANVIL)
        return false;
    if (hasUses && uses <= 0) {
        if (offhand) {
            player.getInventory().setItemInOffHand(new ItemStack(Material.AIR, 1));
        } else {
            player.getInventory().setItemInMainHand(new ItemStack(Material.AIR, 1));
        }
        return false;
    }
    if (!canUse(player)) {
        mage.sendMessage(getMessage("bound").replace("$name", getOwner()));
        return false;
    }
    if (this.isUpgrade) {
        controller.getLogger().warning("Activated an upgrade item- this shouldn't happen");
        return false;
    }
    WandPreActivateEvent preActivateEvent = new WandPreActivateEvent(mage, this);
    Bukkit.getPluginManager().callEvent(preActivateEvent);
    if (preActivateEvent.isCancelled()) {
        return false;
    }
    boolean needsSave = false;
    if (hasId) {
        needsSave = this.checkId() || needsSave;
    } else {
        setProperty("id", null);
    }
    this.mage = mage;
    this.isInOffhand = offhand;
    this.heldSlot = offhand ? OFFHAND_SLOT : player.getInventory().getHeldItemSlot();
    if (mageClassKeys != null && !mageClassKeys.isEmpty()) {
        MageClass mageClass = null;
        for (String mageClassKey : mageClassKeys) {
            mageClass = mage.getClass(mageClassKey);
            if (mageClass != null)
                break;
        }
        if (mageClass == null) {
            Integer lastSlot = mage.getLastActivatedSlot();
            if (!offhand && (lastSlot == null || lastSlot != player.getInventory().getHeldItemSlot())) {
                mage.setLastActivatedSlot(player.getInventory().getHeldItemSlot());
                mage.sendMessage(controller.getMessages().get("mage.no_class").replace("$name", getName()));
            }
            return false;
        }
        setMageClass(mageClass);
        if (!offhand) {
            mage.setActiveClass(mageClass.getKey());
        }
        // This double-load here is not really ideal.
        // Seems hard to prevent without merging Wand construction and activation, though.
        loadProperties();
    }
    mage.setLastActivatedSlot(player.getInventory().getHeldItemSlot());
    // Check for replacement template
    String replacementTemplate = getString("replace_on_activate", "");
    if (!replacementTemplate.isEmpty() && !replacementTemplate.equals(template)) {
        playEffects("replace");
        setTemplate(replacementTemplate);
        loadProperties();
        saveState();
        return activate(mage, offhand);
    }
    // We have to delay this 1 tick so it happens after the Mage has accepted the Wand
    if (getMode() != WandMode.INVENTORY || offhand) {
        Plugin plugin = controller.getPlugin();
        plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

            @Override
            public void run() {
                showActiveIcon(true);
                playPassiveEffects("open");
            }
        }, 1);
    }
    // Check for an empty wand and auto-fill
    if (!isUpgrade && (controller.fillWands() || autoFill)) {
        fill(mage.getPlayer(), controller.getMaxWandFillLevel());
        needsSave = true;
    }
    if (isHeroes) {
        HeroesManager heroes = controller.getHeroes();
        if (heroes != null) {
            Set<String> skills = heroes.getSkills(player);
            Collection<String> currentSpells = new ArrayList<>(getSpells());
            for (String spellKey : currentSpells) {
                if (spellKey.startsWith("heroes*") && !skills.contains(spellKey.substring(7))) {
                    removeSpell(spellKey);
                }
            }
            // Hack to prevent messaging
            this.mage = null;
            for (String skillKey : skills) {
                String heroesKey = "heroes*" + skillKey;
                if (!spells.contains(heroesKey)) {
                    addSpell(heroesKey);
                }
            }
            this.mage = mage;
        }
    }
    // Check for auto-organize
    if (autoOrganize && !isUpgrade) {
        organizeInventory(mage);
        needsSave = true;
    }
    // Check for auto-alphabetize
    if (autoAlphabetize && !isUpgrade) {
        alphabetizeInventory();
        needsSave = true;
    }
    boolean forceUpdate = false;
    if (checkInventoryForUpgrades()) {
        forceUpdate = true;
        needsSave = true;
    }
    // Check for auto-bind
    if (bound) {
        String mageName = ChatColor.stripColor(mage.getPlayer().getDisplayName());
        String mageId = mage.getId();
        boolean ownerRenamed = owner != null && ownerId != null && ownerId.equals(mageId) && !owner.equals(mageName);
        if (ownerId == null || ownerId.length() == 0 || owner == null || ownerRenamed) {
            takeOwnership(mage.getPlayer());
            needsSave = true;
        }
    }
    // Check for randomized wands
    if (randomizeOnActivate) {
        randomize();
        randomizeOnActivate = false;
        forceUpdate = true;
        needsSave = true;
    }
    // Don't build the inventory until activated so we can take Mage boosts into account
    if (offhand) {
        mage.setOffhandWand(this);
    } else {
        mage.setActiveWand(this);
    }
    buildInventory();
    updateMaxMana(false);
    tick();
    if (!isInOffhand) {
        updateMana();
    }
    checkActiveMaterial();
    if (needsSave) {
        saveState();
    }
    updateActiveMaterial();
    updateName();
    updateLore();
    // Play activate FX
    playPassiveEffects("activate");
    lastSoundEffect = 0;
    lastParticleEffect = 0;
    lastSpellCast = 0;
    if (forceUpdate) {
        DeprecatedUtils.updateInventory(player);
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MageClass(com.elmakers.mine.bukkit.magic.MageClass) InventoryType(org.bukkit.event.inventory.InventoryType) ArrayList(java.util.ArrayList) WandPreActivateEvent(com.elmakers.mine.bukkit.api.event.WandPreActivateEvent) HeroesManager(com.elmakers.mine.bukkit.heroes.HeroesManager) InventoryView(org.bukkit.inventory.InventoryView) ItemStack(org.bukkit.inventory.ItemStack) Plugin(org.bukkit.plugin.Plugin)

Aggregations

WandPreActivateEvent (com.elmakers.mine.bukkit.api.event.WandPreActivateEvent)1 HeroesManager (com.elmakers.mine.bukkit.heroes.HeroesManager)1 MageClass (com.elmakers.mine.bukkit.magic.MageClass)1 ArrayList (java.util.ArrayList)1 Player (org.bukkit.entity.Player)1 InventoryType (org.bukkit.event.inventory.InventoryType)1 InventoryView (org.bukkit.inventory.InventoryView)1 ItemStack (org.bukkit.inventory.ItemStack)1 Plugin (org.bukkit.plugin.Plugin)1