package net.craftminecraft.bungee.bungeeban.listener;

import com.google.common.eventbus.Subscribe;

import net.craftminecraft.bungee.bungeeban.BanManager;
import net.craftminecraft.bungee.bungeeban.BungeeBan;
import net.craftminecraft.bungee.bungeeban.banstore.BanEntry;
import net.craftminecraft.bungee.bungeeban.util.Utils;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener;

public class ProxiedPlayerListener implements Listener {
	BungeeBan plugin;
	public ProxiedPlayerListener(BungeeBan plugin) {
		this.plugin = plugin;
	}
	
	@Subscribe
	public void onPlayerJoin(PostLoginEvent e) {
		BanEntry ban = BanManager.getBan(e.getPlayer().getName(), "(GLOBAL)");
		if (ban != null) {
			e.getPlayer().disconnect(Utils.formatMessage(ban.getReason(), ban));
			return;
		}
		ban = BanManager.getBan(e.getPlayer().getAddress().getAddress().getHostAddress(), "(GLOBAL)");
		if (ban != null) {
            e.getPlayer().disconnect(Utils.formatMessage(ban.getReason(), ban));
            return;
		}
	}
	
	@Subscribe
	public void onServerConnect(ServerConnectEvent e) {
		BanEntry ban = BanManager.getBan(e.getPlayer().getName(), e.getTarget().getName());
		if (ban != null) {
			// Ugly workaround the player joined... player left messages
			Server srv = e.getPlayer().getServer();
			if (srv != null)
				e.setTarget(srv.getInfo());
			e.getPlayer().disconnect(Utils.formatMessage(ban.getReason(), ban));
			return;
		} 
		ban = BanManager.getBan(e.getPlayer().getAddress().getAddress().getHostAddress(), e.getTarget().getName());
		if (ban != null) {
			// Ugly workaround the player joined... player left messages
			Server srv = e.getPlayer().getServer();
			if (srv != null)
				e.setTarget(srv.getInfo());
			e.getPlayer().disconnect(Utils.formatMessage(ban.getReason(), ban));
		}
		return;
	}
}

@@ -408,20 +408,16 @@
 i.event.
-Post
 LoginEve
@@ -413,32 +413,32 @@
 nt.LoginEvent;%0D%0A
+
 import net.md_5.
@@ -581,16 +581,24 @@
 ner %7B%0D%0A%09
+private 
 BungeeBa
@@ -734,12 +734,14 @@
 oin(
-Post
+final 
 Logi
@@ -745,32 +745,171 @@
 oginEvent e) %7B%0D%0A
+%09%09e.registerIntent(plugin);%0D%0A%09%09plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() %7B%0D%0A%09%09%09@Override%0D%0A%09%09%09public void run() %7B%0D%0A%09%09
 %09%09BanEntry ban =
@@ -924,38 +924,42 @@
 ger.getBan(e.get
-Player
+Connection
 ().getName(), %22(
@@ -962,32 +962,34 @@
 , %22(GLOBAL)%22);%0D%0A
+%09%09
 %09%09if (ban != nul
@@ -1001,32 +1001,87 @@
 %0A%09%09%09
-e.getPlayer().disconnect
+%09%09e.completeIntent(plugin);%0D%0A%09%09%09%09%09e.setCancelled(true);%0D%0A%09%09%09%09%09e.setCancelReason
 (Uti
@@ -1128,23 +1128,15 @@
 %0D%0A%09%09
-%09return;%0D%0A
 %09%09%7D%0D%0A
+%09%09
 %09%09ba
@@ -1154,38 +1154,42 @@
 ger.getBan(e.get
-Player
+Connection
 ().getAddress().
@@ -1229,24 +1229,26 @@
 GLOBAL)%22);%0D%0A
+%09%09
 %09%09if (ban !=
@@ -1257,16 +1257,18 @@
 ull) %7B%0D%0A
+%09%09
         
@@ -1277,100 +1277,155 @@
   e.
-getPlayer().disconnect(Utils.formatMessage(ban.getReason(), ban));%0D%0A            return;%0D%0A%09%09%7D
+setCancelled(true);%0D%0A%09%09            e.setCancelReason(Utils.formatMessage(ban.getReason(), ban));%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09e.completeIntent(plugin);%0D%0A%09%09%09%7D%0D%0A%09%09%7D);
 %0D%0A%09%7D

Use LoginEvent with intents. \nShould be a big performance improvement. All hail AsyncEvent.