package net.wayward_realms.waywardmonsters;

import net.wayward_realms.waywardlib.classes.ClassesPlugin;
import net.wayward_realms.waywardlib.economy.EconomyPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.RegisteredServiceProvider;

import java.util.Random;

public class EntityDeathListener implements Listener {

        private WaywardMonsters plugin;

    public EntityDeathListener(WaywardMonsters plugin) {
        this.plugin = plugin;
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onEntityDeath(EntityDeathEvent event) {
        if (event.getEntity().getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            Player player = null;
            if (((EntityDamageByEntityEvent) event.getEntity().getLastDamageCause()).getDamager() instanceof Player) {
                player = (Player) (((EntityDamageByEntityEvent) event.getEntity().getLastDamageCause()).getDamager());
            }
            if (((EntityDamageByEntityEvent) event.getEntity().getLastDamageCause()).getDamager() instanceof Projectile) {
                Projectile projectile = (Projectile) ((EntityDamageByEntityEvent) event.getEntity().getLastDamageCause()).getDamager();
                if (projectile.getShooter() instanceof LivingEntity) {
                    LivingEntity shooter = (LivingEntity) ((Projectile) ((EntityDamageByEntityEvent) event.getEntity().getLastDamageCause()).getDamager()).getShooter();
                    if (shooter != null) {
                        if (shooter instanceof Player) {
                            player = (Player) shooter;
                        }
                    }
                }
            }
            Random random = new Random();
            if (player != null) {
                int exp;
                int money;
                int expScale = plugin.getConfig().getInt("experience-multiplier." + event.getEntityType().toString(), 0);
                int entityLevel = plugin.getEntityLevelManager().getEntityLevel(event.getEntity());
                exp = (int) Math.ceil(((double) entityLevel * (double) expScale));
                money = random.nextInt(100) < 5 ? random.nextInt(5) : 0;
                if (exp > 0) {
                    RegisteredServiceProvider<ClassesPlugin> classesPluginProvider = Bukkit.getServer().getServicesManager().getRegistration(ClassesPlugin.class);
                    if (classesPluginProvider != null) {
                        ClassesPlugin classesPlugin = classesPluginProvider.getProvider();
                        classesPlugin.giveExperience(player, exp);
                    }
                }
                if (!(event.getEntity() instanceof Player)) {
                    event.getDrops().clear();
                    event.getDrops().addAll(plugin.getMobDropManager().getDrops(event.getEntity().getType(), entityLevel));
                }
                if (money > 0) {
                    RegisteredServiceProvider<EconomyPlugin> economyPluginProvider = Bukkit.getServer().getServicesManager().getRegistration(EconomyPlugin.class);
                    if (economyPluginProvider != null) {
                        EconomyPlugin economyPlugin = economyPluginProvider.getProvider();
                        ItemStack coins = new ItemStack(Material.GOLD_NUGGET, money);
                        ItemMeta coinMeta = coins.getItemMeta();
                        coinMeta.setDisplayName(economyPlugin.getPrimaryCurrency().getNameSingular());
                        coins.setItemMeta(coinMeta);
                        event.getDrops().add(coins);
                    }
                }
                event.setDroppedExp(0);
            } else {
                if (!(event.getEntity() instanceof Player)) {
                    event.getDrops().clear();
                    event.getDrops().addAll(plugin.getMobDropManager().getDrops(event.getEntity().getType(), plugin.getEntityLevel(event.getEntity())));
                }
            }
        } else {
            if (!(event.getEntity() instanceof Player)) {
                event.getDrops().clear();
                event.getDrops().addAll(plugin.getMobDropManager().getDrops(event.getEntity().getType(), plugin.getEntityLevel(event.getEntity())));
            }
        }
    }

}

@@ -38,16 +38,192 @@
 sters;%0A%0A
+import net.wayward_realms.waywardlib.character.Character;%0Aimport net.wayward_realms.waywardlib.character.CharacterPlugin;%0Aimport net.wayward_realms.waywardlib.character.Party;%0A
 import n
@@ -338,34 +338,8 @@
 in;%0A
-import org.bukkit.Bukkit;%0A
 impo
@@ -824,16 +824,45 @@
 vider;%0A%0A
+import java.util.Collection;%0A
 import j
@@ -2900,38 +2900,38 @@
 luginProvider = 
-Bukkit
+plugin
 .getServer().get
@@ -3126,32 +3126,1195 @@
 .getProvider();%0A
+                        RegisteredServiceProvider%3CCharacterPlugin%3E characterPluginProvider = plugin.getServer().getServicesManager().getRegistration(CharacterPlugin.class);%0A                        if (characterPluginProvider != null) %7B%0A                            CharacterPlugin characterPlugin = characterPluginProvider.getProvider();%0A                            Character character = characterPlugin.getActiveCharacter(player);%0A                            Party party = characterPlugin.getParty(character);%0A                            if (party != null) %7B%0A                                Collection%3C? extends Character%3E partyMembers = party.getMembers();%0A                                if (partyMembers.size() %3E 1) %7B%0A                                    exp = (int) Math.round((exp * 1.5D) / (double) partyMembers.size());%0A                                    for (Character partyMember : partyMembers) %7B%0A                                        if (partyMember.getPlayer().isOnline()) classesPlugin.giveExperience(partyMember, exp);%0A                                    %7D%0A                                %7D%0A                            %7D%0A                        %7D%0A
                 
@@ -4776,14 +4776,14 @@
 r = 
-Bukkit
+plugin
 .get

Split exp across parties, #25