Search in sources :

Example 6 with Wand

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

the class PlayerController method onPlayerPrePickupItem.

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerPrePickupItem(PlayerPickupItemEvent event) {
    Item item = event.getItem();
    ItemStack pickup = item.getItemStack();
    if (NMSUtils.isTemporary(pickup) || item.hasMetadata("temporary")) {
        item.remove();
        event.setCancelled(true);
        return;
    }
    boolean isWand = Wand.isWand(pickup);
    // Creative mode inventory hacky work-around :\
    if (event.getPlayer().getGameMode() == GameMode.CREATIVE && isWand && enableCreativeModeEjecting) {
        event.setCancelled(true);
        return;
    }
    // Check to see if this is an item we might undo, and remove it from undo
    UndoList undoList = controller.getEntityUndo(item);
    if (undoList != null) {
        undoList.remove(item);
    }
    Player player = event.getPlayer();
    Mage mage = controller.getMage(player);
    // Remove lost wands from records
    Messages messages = controller.getMessages();
    if (isWand) {
        Wand wand = controller.getWand(pickup);
        if (!wand.canUse(player)) {
            if (lastDropWarn == 0 || System.currentTimeMillis() - lastDropWarn > 10000) {
                mage.sendMessage(messages.get("wand.bound").replace("$name", wand.getOwner()));
            }
            lastDropWarn = System.currentTimeMillis();
            event.setCancelled(true);
            return;
        }
        controller.removeLostWand(wand.getId());
    }
    // Wands will absorb spells and upgrade items
    Wand activeWand = mage.getActiveWand();
    if (activeWand != null && activeWand.isModifiable() && (Wand.isSpell(pickup) || Wand.isBrush(pickup) || Wand.isUpgrade(pickup) || Wand.isSP(pickup)) && activeWand.addItem(pickup)) {
        event.getItem().remove();
        event.setCancelled(true);
        return;
    }
    if (!mage.hasStoredInventory() && isWand) {
        mage.checkWandNextTick();
    }
}
Also used : Item(org.bukkit.entity.Item) Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 7 with Wand

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

the class PlayerController method onPlayerInteractEntity.

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    Player player = event.getPlayer();
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    Wand wand = mage.checkWand();
    // Check for a player placing a wand in an item frame
    Entity clickedEntity = event.getRightClicked();
    // Don't think this ever fires for ArmorStand - see above
    boolean isPlaceable = clickedEntity instanceof ItemFrame || clickedEntity instanceof ArmorStand;
    if (isPlaceable) {
        if (wand != null) {
            if (wand.isUndroppable()) {
                event.setCancelled(true);
                return;
            } else {
                wand.deactivate();
            }
        } else {
            ItemStack mainHand = player.getInventory().getItemInMainHand();
            ItemStack offhand = player.getInventory().getItemInOffHand();
            if (InventoryUtils.getMetaBoolean(mainHand, "undroppable", false) || (InventoryUtils.isEmpty(mainHand) && InventoryUtils.getMetaBoolean(offhand, "undroppable", false))) {
                event.setCancelled(true);
                return;
            }
        }
    }
    // this hasn't been cancelled yet
    if (controller.isNPC(event.getRightClicked())) {
        if (wand != null) {
            wand.closeInventory();
        }
        // Don't let it re-open right away
        mage.checkLastClick(0);
    } else {
        // Don't allow interacting while holding spells, brushes or upgrades
        ItemStack itemInHand = player.getInventory().getItemInMainHand();
        if (Wand.isSpell(itemInHand) || Wand.isBrush(itemInHand) || Wand.isUpgrade(itemInHand)) {
            event.setCancelled(true);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArmorStand(org.bukkit.entity.ArmorStand) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) ItemFrame(org.bukkit.entity.ItemFrame) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 8 with Wand

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

the class MagicController method createItem.

@Nullable
@Override
public ItemStack createItem(String magicItemKey, boolean brief) {
    ItemStack itemStack = null;
    if (magicItemKey == null) {
        return null;
    }
    // Check for amounts
    int amount = 1;
    if (magicItemKey.contains("@")) {
        String[] pieces = StringUtils.split(magicItemKey, '@');
        magicItemKey = pieces[0];
        try {
            amount = Integer.parseInt(pieces[1]);
        } catch (Exception ignored) {
        }
    }
    // Handle : or | as delimiter
    magicItemKey = magicItemKey.replace("|", ":");
    try {
        if (magicItemKey.contains("skull:") || magicItemKey.contains("skull_item:")) {
            magicItemKey = magicItemKey.replace("skull:", "skull_item:");
            MaterialAndData skullData = new MaterialAndData(magicItemKey);
            itemStack = skullData.getItemStack(amount);
        } else if (magicItemKey.contains("book:")) {
            String bookCategory = magicItemKey.substring(5);
            com.elmakers.mine.bukkit.api.spell.SpellCategory category = null;
            if (!bookCategory.isEmpty() && !bookCategory.equalsIgnoreCase("all")) {
                category = getCategory(bookCategory);
                if (category == null) {
                    return null;
                }
            }
            itemStack = getSpellBook(category, amount);
        } else if (skillPointItemsEnabled && magicItemKey.contains("sp:")) {
            String spAmount = magicItemKey.substring(3);
            itemStack = InventoryUtils.getURLSkull(skillPointIcon);
            ItemMeta meta = itemStack.getItemMeta();
            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', messages.get("sp.name")).replace("$amount", spAmount));
            String spDescription = messages.get("sp.description");
            if (spDescription.length() > 0) {
                List<String> lore = new ArrayList<>();
                lore.add(ChatColor.translateAlternateColorCodes('&', spDescription));
                meta.setLore(lore);
            }
            itemStack.setItemMeta(meta);
            InventoryUtils.setMeta(itemStack, "sp", spAmount);
        } else if (magicItemKey.contains("spell:")) {
            String spellKey = magicItemKey.substring(6);
            itemStack = createSpellItem(spellKey, brief);
        } else if (magicItemKey.contains("wand:")) {
            String wandKey = magicItemKey.substring(5);
            com.elmakers.mine.bukkit.api.wand.Wand wand = createWand(wandKey);
            if (wand != null) {
                itemStack = wand.getItem();
            }
        } else if (magicItemKey.contains("upgrade:")) {
            String wandKey = magicItemKey.substring(8);
            com.elmakers.mine.bukkit.api.wand.Wand wand = createWand(wandKey);
            if (wand != null) {
                wand.makeUpgrade();
                itemStack = wand.getItem();
            }
        } else if (magicItemKey.contains("brush:")) {
            String brushKey = magicItemKey.substring(6);
            itemStack = createBrushItem(brushKey);
        } else if (magicItemKey.contains("item:")) {
            String itemKey = magicItemKey.substring(5);
            itemStack = createGenericItem(itemKey);
        } else if (items != null) {
            ItemData itemData = items.get(magicItemKey);
            if (itemData != null) {
                return itemData.getItemStack(amount);
            }
            MaterialAndData item = new MaterialAndData(magicItemKey);
            if (item.isValid()) {
                return item.getItemStack(amount);
            }
            com.elmakers.mine.bukkit.api.wand.Wand wand = createWand(magicItemKey);
            if (wand != null) {
                ItemStack wandItem = wand.getItem();
                if (wandItem != null) {
                    wandItem.setAmount(amount);
                }
                return wandItem;
            }
            // Spells may be using the | delimiter for levels
            // I am regretting overloading this delimiter!
            String spellKey = magicItemKey.replace(":", "|");
            itemStack = createSpellItem(spellKey, brief);
            if (itemStack != null) {
                itemStack.setAmount(amount);
                return itemStack;
            }
            itemStack = createBrushItem(magicItemKey);
            if (itemStack != null) {
                itemStack.setAmount(amount);
            }
        }
    } catch (Exception ex) {
        getLogger().log(Level.WARNING, "Error creating item: " + magicItemKey, ex);
    }
    return itemStack;
}
Also used : SpellCategory(com.elmakers.mine.bukkit.spell.SpellCategory) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.wand.Wand) LostWand(com.elmakers.mine.bukkit.wand.LostWand) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException) IOException(java.io.IOException) ParseException(java.text.ParseException) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) ItemData(com.elmakers.mine.bukkit.api.item.ItemData) Nullable(javax.annotation.Nullable)

Example 9 with Wand

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

the class Mage method onLoad.

protected void onLoad(MageData data) {
    try {
        Collection<SpellData> spellDataList = data == null ? null : data.getSpellData();
        if (spellDataList != null) {
            for (SpellData spellData : spellDataList) {
                this.spellData.put(spellData.getKey().getKey(), spellData);
            }
        }
        // Load player-specific data
        Player player = getPlayer();
        if (player != null) {
            if (controller.isInventoryBackupEnabled()) {
                if (restoreInventory != null) {
                    controller.getLogger().info("Restoring saved inventory for player " + player.getName() + " - did the server not shut down properly?");
                    if (activeWand != null) {
                        activeWand.deactivate();
                    }
                    Inventory inventory = player.getInventory();
                    for (int slot = 0; slot < restoreInventory.size(); slot++) {
                        Object item = restoreInventory.get(slot);
                        if (item instanceof ItemStack) {
                            inventory.setItem(slot, (ItemStack) item);
                        } else {
                            inventory.setItem(slot, null);
                        }
                    }
                    restoreInventory = null;
                }
                if (restoreExperience != null) {
                    player.setExp(restoreExperience);
                    restoreExperience = null;
                }
                if (restoreLevel != null) {
                    player.setLevel(restoreLevel);
                    restoreLevel = null;
                }
            }
            if (activeWand == null) {
                String welcomeWand = controller.getWelcomeWand();
                if (!gaveWelcomeWand && welcomeWand.length() > 0) {
                    gaveWelcomeWand = true;
                    Wand wand = Wand.createWand(controller, welcomeWand);
                    if (wand != null) {
                        wand.takeOwnership(player);
                        giveItem(wand.getItem());
                        controller.getLogger().info("Gave welcome wand " + wand.getName() + " to " + player.getName());
                    } else {
                        controller.getLogger().warning("Unable to give welcome wand '" + welcomeWand + "' to " + player.getName());
                    }
                }
            }
            if (activeWand != null && restoreOpenWand && !activeWand.isInventoryOpen()) {
                activeWand.openInventory();
            }
            restoreOpenWand = false;
        }
        armorUpdated();
        loading = false;
    } catch (Exception ex) {
        controller.getLogger().warning("Error finalizing player data for " + playerName + ": " + ex.getMessage());
    }
}
Also used : Player(org.bukkit.entity.Player) SpellData(com.elmakers.mine.bukkit.api.data.SpellData) Wand(com.elmakers.mine.bukkit.wand.Wand) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) PlayerInventory(org.bukkit.inventory.PlayerInventory)

Example 10 with Wand

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

the class Mage method useSkill.

public boolean useSkill(ItemStack skillItem) {
    Spell spell = getSpell(Wand.getSpell(skillItem));
    if (spell == null)
        return false;
    boolean canUse = true;
    String skillClass = Wand.getSpellClass(skillItem);
    if (skillClass != null && !skillClass.isEmpty()) {
        if (!setActiveClass(skillClass)) {
            canUse = false;
            sendMessage(controller.getMessages().get("mage.no_class").replace("$name", spell.getName()));
        }
    }
    if (canUse) {
        // TODO: Maybe find a better way to handle this.
        // There is also an issue of an active wand taking over the hotbar display.
        Wand activeWand = this.activeWand;
        this.activeWand = null;
        spell.cast();
        this.activeWand = activeWand;
    }
    return canUse;
}
Also used : Wand(com.elmakers.mine.bukkit.wand.Wand) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) 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)

Aggregations

Wand (com.elmakers.mine.bukkit.wand.Wand)36 Player (org.bukkit.entity.Player)24 ItemStack (org.bukkit.inventory.ItemStack)23 EventHandler (org.bukkit.event.EventHandler)17 LostWand (com.elmakers.mine.bukkit.api.wand.LostWand)11 Mage (com.elmakers.mine.bukkit.magic.Mage)11 PlayerInventory (org.bukkit.inventory.PlayerInventory)6 Spell (com.elmakers.mine.bukkit.api.spell.Spell)5 Nullable (javax.annotation.Nullable)5 LostWand (com.elmakers.mine.bukkit.wand.LostWand)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Inventory (org.bukkit.inventory.Inventory)4 GUIAction (com.elmakers.mine.bukkit.api.action.GUIAction)3 Messages (com.elmakers.mine.bukkit.api.magic.Messages)3 Location (org.bukkit.Location)3 Block (org.bukkit.block.Block)3 Entity (org.bukkit.entity.Entity)3 Item (org.bukkit.entity.Item)3