package com.blazeloader.event.handlers;

import com.blazeloader.api.entity.EntityPropertyManager;
import com.blazeloader.api.world.UnpopulatedChunksQ;
import com.blazeloader.event.listeners.ChunkListener;
import com.blazeloader.event.listeners.EntityConstructingListener;
import com.blazeloader.event.listeners.ModEventListener;
import com.blazeloader.event.listeners.PlayerListener;
import com.blazeloader.event.listeners.TickListener;
import com.blazeloader.event.listeners.WorldListener;
import com.mumfrey.liteloader.core.event.HandlerList;
import com.mumfrey.liteloader.transformers.event.EventInfo;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.management.ServerConfigurationManager;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;

/**
 * Side-independent event handler
 */
public class EventHandler {
    public static final HandlerList<ModEventListener> modEventHandlers = new HandlerList<ModEventListener>(ModEventListener.class);
    public static final HandlerList<TickListener> tickEventHandlers = new HandlerList<TickListener>(TickListener.class);
    public static final HandlerList<WorldListener> worldEventHandlers = new HandlerList<WorldListener>(WorldListener.class);
    public static final HandlerList<PlayerListener> playerEventHandlers = new HandlerList<PlayerListener>(PlayerListener.class);
    public static final HandlerList<ChunkListener> chunkEventHandlers = new HandlerList<ChunkListener>(ChunkListener.class);
    public static final HandlerList<EntityConstructingListener> entityEventHandlers = new HandlerList<EntityConstructingListener>(EntityConstructingListener.class);

    public static void eventTick() {
        tickEventHandlers.all().eventTick();
    }

    public static void eventStart() {
        modEventHandlers.all().start();
    }

    public static void eventEnd() {
        modEventHandlers.all().stop();
    }

    public static void eventTickBlocksAndAmbiance(WorldServer server) {
        worldEventHandlers.all().eventTickBlocksAndAmbiance(server);
    }

    public static void eventTickServerWorld(WorldServer world) {
        worldEventHandlers.all().eventTickServerWorld(world);
    }

    public static void eventPlayerLoggedIn(EventInfo<ServerConfigurationManager> event, EntityPlayerMP player) {
        playerEventHandlers.all().eventMPPlayerLogin(event.getSource(), player);
    }

    public static void eventPlayerLoggedOut(EventInfo<ServerConfigurationManager> event, EntityPlayerMP player) {
        playerEventHandlers.all().eventMPPlayerLogout(event.getSource(), player);
    }

    public static void eventRespawnPlayer(EventInfo<ServerConfigurationManager> event, EntityPlayerMP oldPlayer, int dimension, boolean didWin) {
        playerEventHandlers.all().eventMPPlayerRespawn(event.getSource(), oldPlayer, dimension, !didWin);
    }

    public static boolean eventPlayerLoginAttempt(String username, boolean isAllowed) {
        boolean allow = isAllowed;
        for (PlayerListener mod : playerEventHandlers) {
            allow = mod.eventMPPlayerLoginAttempt(username, isAllowed);
        }
        return allow;
    }

    public static void eventOnChunkLoad(EventInfo<Chunk> event) {
        Chunk chunk = event.getSource();
        if (!chunk.isTerrainPopulated()) {
            UnpopulatedChunksQ.instance().push(chunk);
        }
        chunkEventHandlers.all().onChunkLoad(chunk);
    }

    public static void eventOnChunkUnload(EventInfo<Chunk> event) {
        Chunk chunk = event.getSource();
        UnpopulatedChunksQ.instance().pop(chunk);
        chunkEventHandlers.all().onChunkUnload(chunk);
    }
    
    public static void initEntity(EventInfo<Entity> event, World w) {
    	entityEventHandlers.all().eventEntityConstructed(event.getSource());
    	EntityPropertyManager.entityinit(event.getSource());
    }
}

@@ -187,307 +187,63 @@
 ers.
-ChunkListener;%0Aimport com.blazeloader.event.listeners.EntityConstructingListener;%0Aimport com.blazeloader.event
+*;%0Aimport com.mumfrey
 .li
-s
 te
-ners.ModEventListener;%0Aimport com.blazeloader.event.listeners.PlayerListener;%0Aimport com.blazeloader.event.listeners.TickListener;%0Aimport com.blazeloader.event.listeners.WorldListener
+loader.core.event.HandlerList
 ;%0Aim
@@ -270,38 +270,44 @@
 eloader.
-core.event.HandlerList
+transformers.event.EventInfo
 ;%0Aimport
@@ -341,32 +341,38 @@
 nsformers.event.
+Return
 EventInfo;%0A%0Aimpo
@@ -366,17 +366,16 @@
 ntInfo;%0A
-%0A
 import n
@@ -434,24 +434,29 @@
 ity.
-EntityLivingBase
+player.EntityPlayerMP
 ;%0Aim
@@ -478,36 +478,46 @@
 aft.
-entity.player.Entity
+server.integrated.Integrated
 Player
-MP
+List
 ;%0Aim
@@ -2528,68 +2528,100 @@
 tic 
-void eventRespawnPlayer(EventInfo%3CServerConfigurationManager
+%3CReturnType%3E void eventRecreatePlayerEntity(ReturnEventInfo%3CIntegratedPlayerList, ReturnType
 %3E ev

Fix crash on respawn