package thor12022.hardcorewither;

/*
 * Class for most of your events to be registered in.
 * Remember that there are two different registries for Events. This one will not work for everything.
 */

import java.util.List;

import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.player.PlayerUseItemEvent;
import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import thor12022.hardcorewither.config.ConfigManager;
import thor12022.hardcorewither.HardcoreWither;
import thor12022.hardcorewither.ModInformation;
import thor12022.hardcorewither.handlers.PlayerHandler;
import thor12022.hardcorewither.powerUps.PowerUpManager;
import thor12022.hardcorewither.util.TextHelper;

public class EventHandler
{
   private PlayerHandler playerHandler;
   private PowerUpManager powerUpManager;
   
   public EventHandler(PlayerHandler playerHandler, PowerUpManager powerUpManager)
   {
      this.playerHandler = playerHandler;
      this.powerUpManager = powerUpManager;
      MinecraftForge.EVENT_BUS.register(this);
   }

   @SubscribeEvent
   public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent eventArgs)
   {
      //! @todo pretty sure this event is invalid if there are multiple config files
      if(eventArgs.modID.equals(ModInformation.ID))
      {
         ConfigManager.getInstance().syncConfig();
         HardcoreWither.logger.info(TextHelper.localize("info." + ModInformation.ID + ".console.config.refresh"));
      }
   }
   
   @SubscribeEvent
   public void onSpawnMob(EntityJoinWorldEvent event)
   {
      if (event.entity != null && event.entity.getClass() == EntityWither.class)
      {
         EntityWither theWither = (EntityWither)event.entity;
         if( !powerUpManager.isWitherPoweredUp(theWither) )
         {
            List nearbyPlayers = theWither.worldObj.getEntitiesWithinAABB(EntityPlayer.class, theWither.boundingBox.expand(64.0D, 64.0D, 64.0D));
            double powerUpSize = 0.0;
            for (int index = 0; index < nearbyPlayers.size(); ++index)
            {
               EntityPlayer player = (EntityPlayer)nearbyPlayers.get(index);
               powerUpSize += playerHandler.wasAtWitherSpawn(player);
            }
            powerUpManager.powerUpWither( theWither, (int)Math.round(powerUpSize));
         }
      }
   }
   
   @SubscribeEvent
   public void onLivingUpdate(LivingUpdateEvent event)
   {
      if (event.entityLiving != null && event.entityLiving.getClass() == EntityWither.class)
      {
         powerUpManager.update((EntityWither) event.entityLiving);
      }
   }
   
   @SubscribeEvent
   public void onEntityDieing(LivingDeathEvent event)
   {
      if (event.entityLiving != null && event.entityLiving.getClass() == EntityWither.class)
      {
         List nearbyPlayers = event.entity.worldObj.getEntitiesWithinAABB(EntityPlayer.class, event.entity.boundingBox.expand(64.0D, 64.0D, 64.0D));
         double powerUpSize = 0.0;
         for (int index = 0; index < nearbyPlayers.size(); ++index)
         {
            EntityPlayer player = (EntityPlayer)nearbyPlayers.get(index);
            powerUpSize += playerHandler.wasAtWitherSpawn(player);
            player.addChatMessage(new ChatComponentText(TextHelper.localize("debug." + ModInformation.ID + ".chat.wither-experience")));
         }
         powerUpManager.witherDied((EntityWither) event.entityLiving);
      }
   }
}

@@ -2067,32 +2067,75 @@
 ent event)%0A   %7B%0A
+      if(!event.world.isRemote)%0A      %7B%0A   
       if (event.
@@ -2197,32 +2197,35 @@
 er.class)%0A      
+   
 %7B%0A         Entit
@@ -2215,24 +2215,27 @@
  %7B%0A         
+ 
+  
 EntityWither
@@ -2275,16 +2275,19 @@
 entity;%0A
+   
         
@@ -2339,34 +2339,40 @@
 her) )%0A         
-%7B%0A
+   %7B%0A   
             List
@@ -2505,32 +2505,35 @@
 ));%0A            
+ 
+  
 double powerUpSi
@@ -2534,32 +2534,35 @@
 erUpSize = 0.0;%0A
+   
             for 
@@ -2620,34 +2620,40 @@
 ex)%0A            
-%7B%0A
+   %7B%0A   
                E
@@ -2720,32 +2720,35 @@
 %0A               
+   
 powerUpSize += p
@@ -2790,34 +2790,40 @@
 r);%0A            
-%7D%0A
+   %7D%0A   
             powe
@@ -2886,24 +2886,38 @@
 erUpSize));%0A
+            %7D%0A
          %7D%0A 

Correct issue with Wither Spawning attempting to powerup client-side Wither