/*
 * This file is part of Chitchat, licensed under the MIT License (MIT).
 *
 * Copyright (c) DemigodsRPG.com <http://www.demigodsrpg.com>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.demigodsrpg.chitchat;

import com.demigodsrpg.chitchat.format.ChatFormat;
import com.demigodsrpg.chitchat.tag.DefaultPlayerTag;
import com.demigodsrpg.chitchat.tag.SpecificPlayerTag;
import com.demigodsrpg.chitchat.tag.WorldPlayerTag;
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.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;

import java.io.*;

/**
 * The simplest plugin for chitchat.
 */
public class Chitchat extends JavaPlugin implements Listener, PluginMessageListener {
    // -- STATIC OBJECTS -- //

    private static Chitchat INST;
    private static ChatFormat FORMAT;

    // -- BUKKIT ENABLE/DISABLE -- //

    @Override
    public void onEnable() {
        // Define static instance
        INST = this;
        FORMAT = new ChatFormat();

        // Handle config
        getConfig().options().copyDefaults(true);
        saveConfig();

        // Default tags
        if(getConfig().getBoolean("use_examples", true)) {
            FORMAT.add(new WorldPlayerTag())
            .add(new DefaultPlayerTag("prefix", "chitchat.admin", "&4[A]", 3))
            .add(new SpecificPlayerTag("nablu", "Nablu", "&4[N]", 3))
            .add(new SpecificPlayerTag("hqm", "HmmmQuestionMark", "&8[DEV]", 3));
        }

        // Register events
        getServer().getPluginManager().registerEvents(this, this);
        getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
        getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
    }

    @Override
    public void onDisable() {
        // Manually unregister events
        HandlerList.unregisterAll((Listener) this);
    }

    // -- API METHODS -- //

    /**
     * Get the chat format for adding tags or changing other settings.
     *
     * @return The enabled chat format.
     */
    public static ChatFormat getChatFormat() {
        return FORMAT;
    }

    /**
     * Set the entire chat format to a custom version.
     *
     * @param chatFormat A custom chat format.
     * @deprecated Only use this if you know what you are doing.
     */
    @Deprecated
    public static void setChatFormat(ChatFormat chatFormat) {
        FORMAT = chatFormat;
    }

    /**
     * Get the instance of this plugin.
     *
     * @return The current instance of this plugin.
     */
    public static Chitchat getInst() {
        return INST;
    }

    // -- BUKKIT CHAT LISTENER -- //

    @EventHandler(priority = EventPriority.LOWEST)
    public void onChat(AsyncPlayerChatEvent chat) {
        chat.setFormat(FORMAT.getFormattedMessage(chat.getPlayer(), chat.getMessage()));
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onFinalChat(AsyncPlayerChatEvent chat) {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();

        out.writeUTF("Forward"); // So BungeeCord knows to forward it
        out.writeUTF("ALL");
        out.writeUTF("chitchat"); // The channel name to check if this your data

        ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
        DataOutputStream msgout = new DataOutputStream(msgbytes);
        try {
            msgout.writeUTF(chat.getFormat());
        } catch (IOException ignored) {
        }

        out.writeShort(msgbytes.toByteArray().length);
        out.write(msgbytes.toByteArray());

        chat.getPlayer().sendPluginMessage(this, "BungeeCord", out.toByteArray());
    }

    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if("BungeeCord".equals(channel)) {
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            String subchannel = in.readUTF();

            if("chitchat".equals(subchannel)) {
                short len = in.readShort();
                byte[] msgbytes = new byte[len];
                in.readFully(msgbytes);

                DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));
                try {
                    player.sendMessage(msgin.readUTF());
                } catch (IOException ignored) {
                }
            }
        }
    }
}

@@ -1493,56 +1493,8 @@
 ag;%0A
-import com.google.common.io.ByteArrayDataInput;%0A
 impo
@@ -1583,41 +1583,8 @@
 ms;%0A
-import org.bukkit.entity.Player;%0A
 impo
@@ -1826,66 +1826,79 @@
 in;%0A
+%0A
 import 
-org.bukkit.plugin.messaging.PluginMessageListener;%0A
+java.io.ByteArrayOutputStream;%0Aimport java.io.DataOutputStream;
 %0Aimp
@@ -1909,17 +1909,27 @@
 java.io.
-*
+IOException
 ;%0A%0A/**%0A 
@@ -2032,31 +2032,8 @@
 ener
-, PluginMessageListener
  %7B%0A 
@@ -2961,100 +2961,8 @@
 %22);%0A
-        getServer().getMessenger().registerIncomingPluginChannel(this, %22BungeeCord%22, this);%0A
     
@@ -4861,726 +4861,7 @@
    %7D
-%0A%0A    @Override%0A    public void onPluginMessageReceived(String channel, Player player, byte%5B%5D message) %7B%0A        if(%22BungeeCord%22.equals(channel)) %7B%0A            ByteArrayDataInput in = ByteStreams.newDataInput(message);%0A            String subchannel = in.readUTF();%0A%0A            if(%22chitchat%22.equals(subchannel)) %7B%0A                short len = in.readShort();%0A                byte%5B%5D msgbytes = new byte%5Blen%5D;%0A                in.readFully(msgbytes);%0A%0A                DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));%0A                try %7B%0A                    player.sendMessage(msgin.readUTF());%0A                %7D catch (IOException ignored) %7B%0A                %7D%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Remove useless code.