package com.jumanjicraft.BungeeChatClient;

import com.dthielke.api.Channel;
import com.dthielke.channel.ChannelManager;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;

public class BungeeChatListener implements PluginMessageListener {

    private final BungeeChatClient plugin;

    /**
     *
     * @param plugin
     */
    public BungeeChatListener(BungeeChatClient plugin) {
        this.plugin = plugin;
        register();
    }

    private void register() {
        plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this);
        plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
    }

    /**
     *
     * @param cm
     */
    public void TransmitChatMessage(ChatMessage cm) {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();

        /* SubChannel */
        out.writeUTF(cm.getSubChannel());

        /* Herochat tokens */
        out.writeUTF(cm.getChannel());
        out.writeUTF(cm.getMessage());
        out.writeUTF(cm.getSender());
        out.writeUTF(cm.getHeroColor());
        out.writeUTF(cm.getHeroNick());

        /* Vault tokens */
        out.writeUTF(cm.getPlayerPrefix());
        out.writeUTF(cm.getPlayerSuffix());
        out.writeUTF(cm.getGroupPrefix());
        out.writeUTF(cm.getGroupSuffix());
        out.writeUTF(cm.getPlayerGroup());

        plugin.getServer().sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
    }

    /**
     *
     * @param tag
     * @param player
     * @param data
     */
    @Override
    public void onPluginMessageReceived(String tag, Player player, byte[] data) {
        if (!tag.equalsIgnoreCase("BungeeCord")) {
            return;
        }

        ByteArrayDataInput in = ByteStreams.newDataInput(data);

        String subChannel = in.readUTF();
        String channelName = in.readUTF();
        String message = in.readUTF();
        plugin.logDebug("SubChannel: " + subChannel);

        if (subChannel.equals("PurpleBungeeIRC")) {
            Channel channel = ChannelManager.getInstance().getChannel(channelName);
            channel.sendRawMessage(message);
        } 

    }

}

@@ -2029,24 +2029,43 @@
 t(data);%0D%0A%0D%0A
+        try %7B%0D%0A    
         Stri
@@ -2087,32 +2087,36 @@
  in.readUTF();%0D%0A
+    
         String c
@@ -2135,32 +2135,36 @@
  in.readUTF();%0D%0A
+    
         String m
@@ -2191,24 +2191,28 @@
 );%0D%0A        
+    
 plugin.logDe
@@ -2242,26 +2242,28 @@
 bChannel);%0D%0A
-%0D%0A
+    
         if (
@@ -2299,24 +2299,28 @@
 eeIRC%22)) %7B%0D%0A
+    
             
@@ -2388,24 +2388,28 @@
 nnelName);%0D%0A
+    
             
@@ -2454,10 +2454,106 @@
     
-%7D
  
+   %7D %0D%0A        %7D catch (Exception ex) %7B%0D%0A            plugin.logDebug(ex.getMessage());%0D%0A        %7D
 %0D%0A%0D%0A

handle EOFException