package nl.tomudding.plugins.visibility.managers;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import nl.tomudding.plugins.visibility.Visibility;

public class ChatManager {
	private static ChatManager instance = new ChatManager();

	public static ChatManager getInstance() {
		return instance;
	}
	
	public void log(String s) {
		Bukkit.getConsoleSender().sendMessage(ChatColor.BLUE + "Player Visibility >> " + ChatColor.translateAlternateColorCodes('&', s));
	}

	public void sendMessage(Player player, String message, boolean cmd) {
		if (Visibility.actionBar) {
			try {
				Object chatComponentText = Visibility.getNMSClass("ChatComponentText").getConstructor(new Class[] { String.class }).newInstance(new Object[] { ChatColor.translateAlternateColorCodes('&', message)});
				Class<?> iChatBaseComponent = Visibility.getNMSClass("IChatBaseComponent");
				Object packetPlayOutChat = Visibility.getNMSClass("PacketPlayOutChat").getConstructor(new Class[] { iChatBaseComponent, Byte.TYPE }).newInstance(new Object[] { chatComponentText, Byte.valueOf((byte) 2) });
				
				Object playerNMS = player.getClass().getMethod("getHandle", new Class[0]).invoke(player, new Object[0]);
				Object playerConnection = playerNMS.getClass().getField("playerConnection").get(playerNMS);
				Class<?> playerPacket = Visibility.getNMSClass("Packet");
				playerConnection.getClass().getMethod("sendPacket", new Class[] { playerPacket }).invoke(playerConnection, new Object[] { packetPlayOutChat });
			} catch (Exception exception) {
				exception.printStackTrace();
			}
		} else if (cmd) {
			player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
		} else {
			player.sendMessage(ChatColor.translateAlternateColorCodes('&', Visibility.messagePrefix + message));
		}
	}
}

@@ -561,21 +561,1247 @@
 sage
-, boolean cmd
+) %7B%0A%09%09if (Visibility.actionBar) %7B%0A%09%09%09try %7B%0A%09%09%09%09Object chatComponentText = Visibility.getNMSClass(%22ChatComponentText%22).getConstructor(new Class%5B%5D %7B String.class %7D).newInstance(new Object%5B%5D %7B ChatColor.translateAlternateColorCodes('&', message)%7D);%0A%09%09%09%09Class%3C?%3E iChatBaseComponent = Visibility.getNMSClass(%22IChatBaseComponent%22);%0A%09%09%09%09Object packetPlayOutChat = Visibility.getNMSClass(%22PacketPlayOutChat%22).getConstructor(new Class%5B%5D %7B iChatBaseComponent, Byte.TYPE %7D).newInstance(new Object%5B%5D %7B chatComponentText, Byte.valueOf((byte) 2) %7D);%0A%09%09%09%09%0A%09%09%09%09Object playerNMS = player.getClass().getMethod(%22getHandle%22, new Class%5B0%5D).invoke(player, new Object%5B0%5D);%0A%09%09%09%09Object playerConnection = playerNMS.getClass().getField(%22playerConnection%22).get(playerNMS);%0A%09%09%09%09Class%3C?%3E playerPacket = Visibility.getNMSClass(%22Packet%22);%0A%09%09%09%09playerConnection.getClass().getMethod(%22sendPacket%22, new Class%5B%5D %7B playerPacket %7D).invoke(playerConnection, new Object%5B%5D %7B packetPlayOutChat %7D);%0A%09%09%09%7D catch (Exception exception) %7B%0A%09%09%09%09exception.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09player.sendMessage(ChatColor.translateAlternateColorCodes('&', Visibility.messagePrefix + message));%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void sendMessage(Player player, String message, boolean cmd, boolean cmdPrefix
 ) %7B%0A
@@ -2845,16 +2845,154 @@
 (cmd) %7B%0A
+%09%09%09if (cmdPrefix) %7B%0A%09%09%09%09player.sendMessage(ChatColor.translateAlternateColorCodes('&', Visibility.messagePrefix + message));%0A%09%09%09%7D else %7B%0A%09
 %09%09%09playe
@@ -3060,16 +3060,21 @@
 sage));%0A
+%09%09%09%7D%0A
 %09%09%7D else
@@ -3184,13 +3184,15 @@
 ));%0A%09%09%7D%0A
-
 %09%7D%0A
+%09%0A
 %7D%0A

Update to v2.4