package me.AKZOMBIE74;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Created by AKZOMBIE74 on 1/22/2016.
 */
public class SCMD implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {


        if (cmd.getName().equalsIgnoreCase("ss")) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (args.length>0){
                    teleportServer(p, args[0]);
                } else {
                    help(p);
                    updateMessage(p);
                }
            } else {
                sender.sendMessage(Selector.getInstance().ONLY_PLAYERS);
            }
        } else if (cmd.getName().equalsIgnoreCase("ssr") && sender.hasPermission("servsel.reload")){
            Selector.getInstance().checkForServers();
            Selector.getInstance().setLangVars();
            Selector.getInstance().checkForUpdates();
            updateMessage(sender);
            sender.sendMessage(ChatColor.GREEN+"Successfully reloaded ServSel!");
        }
        return false;
    }

    public void teleportServer(Player p, String server){
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(b);

        try {
            out.writeUTF("Connect");
            out.writeUTF(server);
            p.sendPluginMessage(Selector.getInstance(), "BungeeCord", b.toByteArray());
        } catch (IOException eee) {
            // Fehler
            eee.printStackTrace();
        }
        if (serverExists(server)) {
            p.sendMessage(Selector.getInstance().TELEPORTED.replaceAll("%pn", p.getName()).replaceAll("%s", server)
                    .replaceAll("%pdn", p.getDisplayName()));
        } else {
            p.sendMessage(Selector.getInstance().SERVER_NOT_FOUND.replaceAll("%pn", p.getName()).replaceAll("%s", server)
                    .replaceAll("%pdn", p.getDisplayName()));
        }

    }
    public void help(Player p){
        p.sendMessage(ChatColor.GOLD+"=====ServSel=====");
        p.sendMessage(ChatColor.GREEN+"By: AKZOMBIE74");
        p.sendMessage(ChatColor.AQUA+"Version: "+Selector.getInstance().CURRENT_VERSION);
        p.sendMessage(ChatColor.BLUE+"/ss <server> - Teleports you to the specified server");
        p.sendMessage(ChatColor.BLUE+"/ssr - Reloads ServSel");
        Selector.getInstance().checkForUpdates();
    }
    public void updateMessage(CommandSender p) {
        if (Selector.getInstance().shouldUpdate){
            p.sendMessage(ChatColor.DARK_RED+"[ServSel] It is recommended you update to version "+Selector.getInstance().VERSION);
            p.sendMessage(ChatColor.YELLOW+Selector.getInstance().CHANGELOG);
        }
    }
    public boolean serverExists(String server){
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(b);
        try {
            out.writeUTF("ServerIP");
            out.writeUTF(server);
            Selector.getInstance().getServer().sendPluginMessage(Selector.getInstance(), "BungeeCord", b.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (Selector.getInstance().getServerExists().containsKey(server)) {
            if (Selector.getInstance().getServerExists().get(server) == null) {
                Selector.getInstance().getServerExists().remove(server);
                return false;
            }
            return true;
        }
        return false;
    }
}

@@ -297,16 +297,41 @@
 ception;
+%0Aimport java.util.Arrays;
 %0A%0A/**%0A *
@@ -3108,731 +3108,123 @@
     
-ByteArrayOutputStream b = new ByteArrayOutputStream();%0A        DataOutputStream out = new DataOutputStream(b);%0A        try %7B%0A            out.writeUTF(%22ServerIP%22);%0A            out.writeUTF(server);%0A            Selector.getInstance().getServer().sendPluginMessage(Selector.getInstance(), %22BungeeCord%22, b.toByteArray());%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        if (Selector.getInstance().getServerExists().containsKey(server)) %7B%0A            if (Selector.getInstance().getServerExists().get(server) == null) %7B%0A                Selector.getInstance().getServerExists().remove(server);%0A                return false;%0A            %7D%0A            return true;%0A        %7D%0A        return false
+String servers = Arrays.toString(Selector.getInstance().getServerExists());%0A        return servers.contains(server)
 ;%0A  

Update SCMD.java