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);
}
Aggregations