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.LoginEvent;
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(LoginEvent e) {
		BanEntry ban = BanManager.getBan(e.getConnection().getName(), "(GLOBAL)");
		if (ban != null) {
			e.setCancelled(true);
			e.setCancelReason(Utils.formatMessage(ban.getReason(), ban));
			return;
		}
		ban = BanManager.getBan(e.getConnection().getAddress().getAddress().getHostAddress(), "(GLOBAL)");
		if (ban != null) {
			e.setCancelled(true);
			e.setCancelReason(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,16 +408,20 @@
 i.event.
+Post
 LoginEve
@@ -726,16 +726,20 @@
 yerJoin(
+Post
 LoginEve
@@ -783,34 +783,30 @@
 getBan(e.get
-Connection
+Player
 ().getName()
@@ -852,49 +852,30 @@
 %09%09e.
-setCancelled(true);%0D%0A%09%09%09e.setCancelReason
+getPlayer().disconnect
 (Uti
@@ -968,18 +968,14 @@
 .get
-Connection
+Player
 ().g
@@ -1057,54 +1057,44 @@
  %7B%0D%0A
-%09%09%09e.setCancelled(true);%0D%0A%09%09%09e.setCancelReason
+            e.getPlayer().disconnect
 (Uti
@@ -1127,35 +1127,44 @@
 ason(), ban));%0D%0A
-%09%09%09
+            
 return;%0D%0A%09%09%7D%0D%0A%09%7D

Use PostLoginEvent instead of LoginEvent - Temporary fix until @md_5 explains how AsyncEvent works.