package info.tregmine.listeners;

import info.tregmine.Tregmine;
import info.tregmine.api.TregminePlayer;
import info.tregmine.database.DAOException;
import info.tregmine.database.IContext;
import info.tregmine.database.ILogDAO;

import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class PlayerLookupListener implements Listener
{
    private Tregmine plugin;

    public PlayerLookupListener(Tregmine instance)
    {
        plugin = instance;
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event)
    {
        TregminePlayer player = plugin.getPlayer(event.getPlayer());
        if (player == null) {
            event.getPlayer().kickPlayer("Something went wrong");
            Tregmine.LOGGER.info(event.getPlayer().getName() + " was not found " +
                    "in players map.");
            return;
        }

        if (player.hasFlag(TregminePlayer.Flags.INVISIBLE)) {

            for (TregminePlayer to : plugin.getOnlinePlayers()) {
                if (to.getRank().canSeeHiddenInfo()) {
                    if (player.getCountry() != null) {
                        to.sendMessage(
                                ChatColor.DARK_AQUA + "Welcome " + player.getChatName() +
                                ChatColor.DARK_AQUA + " from " + player.getCountry() + "!");
                        to.sendMessage(
                                player.getChatName() + ChatColor.DARK_AQUA + " is invisible!");
                    } else {
                        to.sendMessage(
                                ChatColor.DARK_AQUA + "Welcome " + player.getChatName());
                        to.sendMessage(
                                player.getChatName() + ChatColor.DARK_AQUA + " is invisible!");
                    }
                }
            }

        } else {

            if (player.getCountry() != null && !player.hasFlag(TregminePlayer.Flags.HIDDEN_LOCATION)) {
                plugin.getServer().broadcastMessage(
                    ChatColor.DARK_AQUA + "Welcome " + player.getChatName() +
                    ChatColor.DARK_AQUA + " from " + player.getCountry() + "!");
            } else {
                plugin.getServer().broadcastMessage(
                    ChatColor.DARK_AQUA + "Welcome " + player.getChatName());
            }

        }

        if ("95.141.47.226".equals(player.getIp())) {
            return;
        }

        String aliasList = null;
        try (IContext ctx = plugin.createContext()) {
            ILogDAO logDAO = ctx.getLogDAO();
            Set<String> aliases = logDAO.getAliases(player);

            StringBuilder buffer = new StringBuilder();
            String delim = "";
            for (String name : aliases) {
                buffer.append(delim);
                buffer.append(name);
                delim = ", ";
            }

            aliasList = buffer.toString();

            if (aliases.size() > 1) {
                Tregmine.LOGGER.info("Aliases: " + aliasList);

                for (TregminePlayer current : plugin.getOnlinePlayers()) {
                    if (!current.getRank().canSeeAliases()) {
                        continue;
                    }
                    if (player.hasFlag(TregminePlayer.Flags.HIDDEN_LOCATION)) {
                        continue;
                    }
                    current.sendMessage(ChatColor.YELLOW
                            + "This player have also used names: " + aliasList);
                }
            }
        } catch (DAOException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -959,33 +959,118 @@
 turn;%0A        %7D%0A
+        
 %0A
+        if (!player.hasFlag(TregminePlayer.Flags.HIDDEN_ANNOUNCEMENT)) %7B%0A    
         if (play
@@ -1115,17 +1115,25 @@
 BLE)) %7B%0A
-%0A
+    %0A    
         
@@ -1202,24 +1202,28 @@
             
+  
+  
 if (to.getRa
@@ -1245,24 +1245,28 @@
 enInfo()) %7B%0A
+    
             
@@ -1324,32 +1324,36 @@
                 
+    
 to.sendMessage(%0A
@@ -1376,32 +1376,36 @@
                 
+   
+ 
 ChatColor.DARK_A
@@ -1438,32 +1438,36 @@
 getChatName() +%0A
+    
                 
@@ -1559,32 +1559,36 @@
                 
+  
+  
 to.sendMessage(%0A
@@ -1579,32 +1579,36 @@
 to.sendMessage(%0A
+    
                 
@@ -1699,32 +1699,36 @@
                 
+ 
+   
 %7D else %7B%0A       
@@ -1712,32 +1712,36 @@
        %7D else %7B%0A
+    
                 
@@ -1756,32 +1756,36 @@
 to.sendMessage(%0A
+    
                 
@@ -1874,32 +1874,36 @@
                 
+  
+  
 to.sendMessage(%0A
@@ -1894,32 +1894,36 @@
 to.sendMessage(%0A
+    
                 
@@ -2014,34 +2014,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2054,35 +2054,47 @@
   %7D%0A            
+   
+ 
 %7D%0A
-%0A
+    %0A    
         %7D else %7B
@@ -2094,17 +2094,25 @@
  else %7B%0A
-%0A
+    %0A    
         
@@ -2215,32 +2215,36 @@
                 
+    
 plugin.getServer
@@ -2276,32 +2276,36 @@
                 
+   
+ 
 ChatColor.DARK_A
@@ -2338,32 +2338,36 @@
 getChatName() +%0A
+    
                 
@@ -2435,32 +2435,36 @@
 %22);%0A            
+    
 %7D else %7B%0A       
@@ -2448,32 +2448,36 @@
        %7D else %7B%0A
+    
                 
@@ -2525,32 +2525,36 @@
                 
+    
 ChatColor.DARK_A
@@ -2599,34 +2599,56 @@
 ));%0A            
+    %7D%0A    %0A            
 %7D
-%0A
 %0A        %7D%0A%0A    

Update PlayerLookupListener.java