/**
 * The MIT License
 * Copyright (c) 2014-2015 Techcable
 *
 * 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.
 */
/*
* The MIT License
* Copyright (c) 2015 Techcable
*
* 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 net.techcable.combattag.libs;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import lombok.Getter;
import net.techcable.techutils.Reflection;
import org.bukkit.entity.Player;

import static net.techcable.techutils.Reflection.*; //Make sure to change this to the right package

/**
 * A 1.8 ActionBar
 *
 * Works on protocol hack and real 1.8
 */
public class ActionBar {
    /**
     * Creates a new action bar with the specificed text
     *
     * @param text text to keep in the action bar
     */
    public ActionBar(String text) {
        this.text = text;
    }
    @Getter
    private final String text;

    public void sendTo(Player p) {
        ActionBarHandler handler = getActionBarHandler();
        if (handler == null) return;
        handler.sendTo(p, this);
    }

    private static ActionBarHandler handler;
    private static ActionBarHandler getActionBarHandler() {
        if (handler != null) return handler;
        if (SpigotActionBarHandler.isSupported()) {
            handler = new SpigotActionBarHandler();
        } else if (NMSActionBarHandler.isSupported()) {
            handler = new NMSActionBarHandler();
        } else {
            return null;
        }
        return handler;
    }

    private static interface ActionBarHandler {
        public void sendTo(Player p, ActionBar bar);
    }

    private static class SpigotActionBarHandler implements ActionBarHandler {
        private SpigotActionBarHandler() {
            assert isSupported() : "Spigot action bar is unsupported!";
        }

        private final static Constructor packetConstructor = makeConstructor(getNmsClass("PacketPlayOutChat"), getNmsClass("IChatBaseComponent"), int.class);
        public void sendTo(Player p, ActionBar bar) {
            if (getProtocolVersion(p) < 16) return;
            Object baseComponent = serialize(bar.getText());
            Object packet = callConstructor(packetConstructor, baseComponent, 2);
            sendPacket(p, packet);
        }

        private static final Field playerConnectionField = makeField(getNmsClass("EntityPlayer"), "playerConnection");
        private static final Field networkManagerField = makeField(getNmsClass("PlayerConnection"), "networkManager");
        private static final Method getVersion = makeMethod(getNmsClass("NetworkManager"), "getVersion");
        private static int getProtocolVersion(Player player) {
            Object handle = getHandle(player);
            Object connection = getField(playerConnectionField, handle);
            Object networkManager = getField(networkManagerField, connection);
            assert getVersion() != null : "Not Protocol Hack";
            int version = callMethod(getVersion, networkManager);
            return version;
        }

        public static boolean isSupported() {
            return Reflection.getClass("org.spigotmc.ProtocolData") != null;
        }
    }

    private static class NMSActionBarHandler implements ActionBarHandler {

        private NMSActionBarHandler() {
            assert !SpigotActionBarHandler.isSupported() : "Spigot action bar is supported";
            assert NMSActionBarHandler.isSupported(): "NMS Action bar isn't supported";
        }

        private static final Constructor packetConstructor = makeConstructor(getNmsClass("PacketPlayOutChat"), getNmsClass("IChatBaseComponent"), int.class);
        public void sendTo(Player p, ActionBar bar) {
            Object baseComponent = serialize(bar.getText());
            Object packet = callConstructor(packetConstructor, baseComponent, 2);
            sendPacket(p, packet);
        }

        public static boolean isSupported() {
            return packetConstructor != null;
        }
    }

    //Utils

    private static final Field playerConnectionField = makeField(getNmsClass("EntityPlayer"), "playerConnection");
    private static final Method sendPacketMethod = makeMethod(getNmsClass("PlayerConnection"), "sendPacket", getNmsClass("Packet"));
    private static void sendPacket(Player player, Object packet) {
        Object handle = getHandle(player);
        Object connection = getField(playerConnectionField, handle);
        callMethod(sendPacketMethod, connection, packet);
    }

    private static final Method addSiblingMethod = makeMethod(getNmsClass("IChatBaseComponent"), "addSibling", getNmsClass("IChatBaseComponent"));
    private static final Method fromStringMethod = makeMethod(getCbClass("util.CraftChatMessage"), "fromString", String.class);
    private static Object serialize(String text) { //Serialize to IChatBaseComponent
        Object baseComponentArray = callMethod(fromStringMethod, null, text);;
        Object first = null;
        for (int i = 0; i < Array.getLength(baseComponentArray); i++) {
            Object baseComponent = Array.get(baseComponentArray, i);
            if (first == null) {
                first = baseComponent;
            } else {
                first = callMethod(addSiblingMethod, first, baseComponent);
            }
        }
        return first;
    }
}
@@ -1136,1157 +1136,42 @@
  */%0A
-/*%0A* The MIT License%0A* Copyright (c) 2015 Techcable%0A*%0A* Permission is hereby granted, free of charge, to any person obtaining a copy%0A* of this software and associated documentation files (the %22Software%22), to deal%0A* in the Software without restriction, including without limitation the rights%0A* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A* copies of the Software, and to permit persons to whom the Software is%0A* furnished to do so, subject to the following conditions:%0A*%0A* The above copyright notice and this permission notice shall be included in%0A* all copies or substantial portions of the Software.%0A*%0A* THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0A* THE SOFTWARE.%0A*/%0A%0Apackage net.techcable.combattag.libs
+package net.techcable.techutils.ui
 ;%0A%0Ai
@@ -6286,8 +6286,9 @@
 %0A    %7D%0A%7D
+%0A

Fix ActionBar package