package net.craftminecraft.bungee.bungeeban.listener;

import com.google.common.eventbus.Subscribe;

import net.craftminecraft.bungee.bungeeban.BanManager;
import net.craftminecraft.bungee.bungeeban.banstore.BanEntry;
import net.craftminecraft.bungee.bungeeban.util.Utils;
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 {
	public ProxiedPlayerListener() {
	}
	
	@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));
		}
	}
	
	@Subscribe
	public void onServerConnect(ServerConnectEvent e) {
		BanEntry ban = BanManager.getBan(e.getPlayer().getName(), e.getTarget().getName());
		if (ban != null) {
			e.getPlayer().disconnect(Utils.formatMessage(ban.getReason(), ban));
			return;
		} 
		ban = BanManager.getBan(e.getPlayer().getAddress().getAddress().getHostAddress(), e.getTarget().getName());
		if (ban != null) {
			e.getPlayer().disconnect(Utils.formatMessage(ban.getReason(), ban));
		}
		return;
	}
}

@@ -311,32 +311,82 @@
 nt.LoginEvent;%0D%0A
+import net.md_5.bungee.api.event.PostLoginEvent;%0D%0A
 import net.md_5.
@@ -609,16 +609,20 @@
 yerJoin(
+Post
 LoginEve
@@ -666,34 +666,30 @@
 getBan(e.get
-Connection
+Player
 ().getName()
@@ -735,49 +735,30 @@
 %09%09e.
-setCancelled(true);%0D%0A%09%09%09e.setCancelReason
+getPlayer().disconnect
 (Uti
@@ -851,18 +851,14 @@
 .get
-Connection
+Player
 ().g
@@ -945,49 +945,30 @@
 %09%09e.
-setCancelled(true);%0D%0A%09%09%09e.setCancelReason
+getPlayer().disconnect
 (Uti
@@ -1005,24 +1005,36 @@
 (), ban));%0D%0A
+%09%09%09return;%0D%0A
 %09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A

Use PostLoginEvent so offline-mode bungee servers can still global ban (although I really dislike supporting offline-mode, I need it for testing...)