package me.AKZOMBIE74;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by AKZOMBIE74 on 11/11/2015.
 */
public class Selector extends JavaPlugin{
    private Set<String> sectionKeys;
    private HashMap<UUID, String> selected;

    private ByteArrayOutputStream b = new ByteArrayOutputStream();
    private DataOutputStream out = new DataOutputStream(b);

    private static Selector instance;

    private static PML pml = new PML();

    private SCMD scmd;
    private File file;


    //onEnable stuff
    @Override
    public void onEnable() {
        instance = this;
        scmd = new SCMD();
        //Register Commands
        getCommand("ss").setExecutor(scmd);

        selected = new HashMap<>();

        Bukkit.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", getPML());
        Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");

        //Register Events
        Bukkit.getServer().getPluginManager().registerEvents(new PIE(), this);
        Bukkit.getServer().getPluginManager().registerEvents(new ICE(), this);
        Bukkit.getServer().getPluginManager().registerEvents(new PJE(), this);

        //Create Config
        createConfig();


        //Enable Log
        getLogger().info("ServSel has been enabled");
    }

    public static PML getPML() {
        return pml;
    }

    //onDisable stuff
    @Override
    public void onDisable() {
        pml = null;
        b = null;
        out = null;
        if (selected!=null && selected.size()>0) {
            selected.clear();
        }
        if (sectionKeys!=null&&sectionKeys.size()>0) {
            sectionKeys.clear();
        }
        sectionKeys = null;
        selected = null;
        scmd = null;
        file = null;
        instance = null;
        //Disable Log
        getLogger().info("Server Selector has been disabled");
    }

    public ByteArrayOutputStream getB() {
        return b;
    }

    public DataOutputStream getOut() {
        return out;
    }

    public static Selector getInstance() {
        return instance;
    }

    public Set<String> getSectionKeys() {
        return sectionKeys;
    }

    private void createConfig() {

        try {
            if (!getDataFolder().exists()) {
                getDataFolder().mkdirs();
            }
            file = new File(getDataFolder(), "config.yml");
            if (!file.exists()) {
                getLogger().info("Config.yml not found, creating!");
                saveDefaultConfig();



                String[] list = new String[]{"Go",
                        "To",
                        "this server!"
                };

                String[] list2 = {
                        "Right Click",
                        "To Open the",
                        "GUI"};

                getConfig().createSection("Servers");
                getConfig().set("slot_size", 9);
                getConfig().set("menu_name", "&bServer Selector");
                getConfig().set("Compass.name", "&bNameHere");
                getConfig().set("Compass.lore", list2);
                getConfig().set("Servers.server1.Material", "COMPASS");
                getConfig().set("Servers.server1.slot", 1);
                getConfig().set("Servers.server1.name", "server name here");
                getConfig().set("Servers.server1.display-name", "Minigames!");
                getConfig().set("Servers.server1.showcount", true);
                getConfig().set("Servers.server1.lore", list);
                getConfig().set("Servers.server1.showPlayerList", true);
                saveConfig();
            } else {
                getLogger().info("Config.yml found, loading!");
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }


    public void OpenGui(Player player) throws IOException {
        Inventory inv = Bukkit.createInventory(null, getConfig().getInt("slot_size"), ChatColor.translateAlternateColorCodes('&', getConfig().getString("menu_name")));

        sectionKeys = getConfig().getConfigurationSection("Servers").getKeys(false);

        AtomicReference<ArrayList<ItemStack>> items = new AtomicReference<>(new ArrayList<ItemStack>(sectionKeys.size()));


        for (String key : sectionKeys) {
            if (sectionKeys != null) {
                String displayName = getConfig().getString("Servers." + key + ".display-name");
                String name = getConfig().getString("Servers." + key + ".name");
                List<String> lore = getConfig().getStringList("Servers." + key + ".lore");
                List<String> finalLore = new ArrayList<>();
                Boolean showcount = getConfig().getBoolean("Servers." + key + ".showcount");
                Boolean playerList = getConfig().getBoolean("Servers."+key+".showPlayerList");
                Material m = Material.valueOf(getConfig().getString("Servers." + key + ".Material"));

                ItemStack stack = new ItemStack(m, 1);

                ItemMeta meta = stack.getItemMeta();
                meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', displayName));
                if (showcount) {

                    out.writeUTF("PlayerCount");
                    out.writeUTF(name);
                    Bukkit.getServer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
                    lore.add("Players Count: "+ getPML().getPc());
                }
                if (playerList){
                    out.writeUTF("PlayerList");
                    out.writeUTF(name);
                    Bukkit.getServer().sendPluginMessage(this, "BungeeCord", b.toByteArray());
                    lore.add("Players Online: ");
                    if (getPML().getPlayerList()!=null) {
                        lore
                                .addAll(
                                        Arrays.asList(getPML().getPlayerList()));
                    } else {
                        lore.add("None");
                    }
                }
                lore.forEach(l -> {
                    finalLore.add(ChatColor.translateAlternateColorCodes('&', l));
                });
                meta.setLore(finalLore);
                meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_DESTROYS, ItemFlag.HIDE_ENCHANTS, ItemFlag.HIDE_PLACED_ON,
                        ItemFlag.HIDE_POTION_EFFECTS, ItemFlag.HIDE_UNBREAKABLE);

                stack.setItemMeta(meta);

                items.get().add(stack);
                inv.setItem(getConfig().getInt("Servers." + key + ".slot"), stack);
            }
        }


        player.openInventory(inv);

    }


    public HashMap<UUID, String> getSelected() {
        return selected;
    }
}

@@ -658,52 +658,8 @@
 eys;
-%0A    private HashMap%3CUUID, String%3E selected;
 %0A%0A  
@@ -1105,45 +1105,8 @@
 );%0A%0A
-        selected = new HashMap%3C%3E();%0A%0A
     
@@ -1882,99 +1882,8 @@
 ll;%0A
-        if (selected!=null && selected.size()%3E0) %7B%0A            selected.clear();%0A        %7D%0A
     
@@ -2008,33 +2008,8 @@
 ll;%0A
-        selected = null;%0A
     
@@ -6949,88 +6949,6 @@
   %7D%0A
-%0A%0A    public HashMap%3CUUID, String%3E getSelected() %7B%0A        return selected;%0A    %7D%0A
 %7D%0A

Update Selector.java