Search in sources :

Example 36 with Wand

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

the class EntityController method onEntityDeath.

@EventHandler(priority = EventPriority.LOWEST)
public void onEntityDeath(EntityDeathEvent event) {
    Entity entity = event.getEntity();
    if (entity.hasMetadata("nodrops")) {
        event.setDroppedExp(0);
        event.getDrops().clear();
        entity.removeMetadata("nodrops", controller.getPlugin());
    }
    com.elmakers.mine.bukkit.magic.Mage mage = controller.getRegisteredMage(entity);
    if (mage == null)
        return;
    mage.deactivateAllSpells();
    mage.onDeath(event);
    if (!(entity instanceof Player)) {
        return;
    }
    final Player player = (Player) entity;
    String rule = entity.getWorld().getGameRuleValue("keepInventory");
    if (rule.equals("true"))
        return;
    List<ItemStack> drops = event.getDrops();
    Wand wand = mage.getActiveWand();
    if (wand != null) {
        // Retrieve stored inventory before deactivating the wand
        if (mage.hasStoredInventory()) {
            // Remove the wand inventory from drops
            drops.removeAll(Arrays.asList(player.getInventory().getContents()));
            // Deactivate the wand.
            wand.deactivate();
            // Add restored inventory back to drops
            ItemStack[] stored = player.getInventory().getContents();
            for (ItemStack stack : stored) {
                if (stack != null) {
                    drops.add(stack);
                }
            }
        } else {
            wand.deactivate();
        }
    }
    List<ItemStack> removeDrops = new ArrayList<>();
    PlayerInventory inventory = player.getInventory();
    ItemStack[] contents = inventory.getContents();
    for (int index = 0; index < contents.length; index++) {
        ItemStack itemStack = contents[index];
        if (itemStack == null || itemStack.getType() == Material.AIR)
            continue;
        if (NMSUtils.isTemporary(itemStack)) {
            removeDrops.add(itemStack);
            continue;
        }
        boolean keepItem = InventoryUtils.getMetaBoolean(itemStack, "keep", false);
        if (!keepItem && keepWandsOnDeath && Wand.isWand(itemStack))
            keepItem = true;
        if (keepItem) {
            mage.addToRespawnInventory(index, itemStack);
            removeDrops.add(itemStack);
        } else if (Wand.isSkill(itemStack)) {
            removeDrops.add(itemStack);
        }
    }
    ItemStack[] armor = player.getInventory().getArmorContents();
    for (int index = 0; index < armor.length; index++) {
        ItemStack itemStack = armor[index];
        if (itemStack == null || itemStack.getType() == Material.AIR)
            continue;
        if (NMSUtils.isTemporary(itemStack) || Wand.isSkill(itemStack)) {
            removeDrops.add(itemStack);
            continue;
        }
        boolean keepItem = InventoryUtils.getMetaBoolean(itemStack, "keep", false);
        if (!keepItem && keepWandsOnDeath && Wand.isWand(itemStack))
            keepItem = true;
        if (keepItem) {
            mage.addToRespawnArmor(index, itemStack);
            removeDrops.add(itemStack);
        }
    }
    drops.removeAll(removeDrops);
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.wand.Wand) PlayerInventory(org.bukkit.inventory.PlayerInventory) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

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