package mnm.mods.protocol.gui;

import com.google.common.collect.Lists;

import mnm.mods.protocol.EnumProtocols;
import mnm.mods.protocol.LiteModProtocol4;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiMultiplayer;
import net.minecraft.client.gui.GuiScreen;

import java.lang.reflect.Field;
import java.util.List;

public class MultiplayerMenu {

    private static Minecraft mc = Minecraft.getMinecraft();
    private static GuiScreen screen;

    public static void onRenderGui(GuiScreen screen) {
        MultiplayerMenu.screen = screen;
        if (screen instanceof GuiMultiplayer) {
            insertButton((GuiMultiplayer) screen, LiteModProtocol4.instance.getProtocol());
        }
    }

    @SuppressWarnings("unchecked")
    private static void insertButton(GuiMultiplayer gui, EnumProtocols proto) {
        try {
            ProtocolButton button = new ProtocolButton(-1, gui.width / 2 + 165, gui.height - 40,
                    proto);
            Field fldButtons = getFieldsOfType(GuiScreen.class, List.class)[0];
            fldButtons.setAccessible(true);
            List<GuiButton> lstButtons = (List<GuiButton>) fldButtons.get(gui);
            lstButtons.add(button);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    private static Field[] getFieldsOfType(Class<?> owner, Class<?> type) {
        List<Field> fields = Lists.newArrayList();
        for (Field field : owner.getDeclaredFields()) {
            if (field.getType().equals(type)) {
                fields.add(field);
            }
        }
        return fields.toArray(new Field[0]);
    }

    public static void refresh() {
        try {
            Field field = getFieldsOfType(GuiMultiplayer.class, GuiScreen.class)[0];
            field.setAccessible(true);
            GuiScreen screen = (GuiScreen) field.get(MultiplayerMenu.screen);
            mc.displayGuiScreen(new GuiMultiplayer(screen));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

@@ -489,30 +489,35 @@
 e static Gui
-Screen
+Multiplayer
  screen;%0A%0A  
@@ -573,49 +573,8 @@
 ) %7B%0A
-        MultiplayerMenu.screen = screen;%0A
     
@@ -613,24 +613,138 @@
 tiplayer) %7B%0A
+            MultiplayerMenu.screen = (GuiMultiplayer) screen;%0A            if (!buttonListHasButton(screen)) %7B%0A    
             
@@ -819,24 +819,38 @@
 rotocol());%0A
+            %7D%0A
         %7D%0A  
@@ -1806,32 +1806,32 @@
     %7D%0A        %7D%0A
-
         return f
@@ -1862,24 +1862,730 @@
 0%5D);%0A    %7D%0A%0A
+    private static boolean buttonListHasButton(GuiScreen screen) %7B%0A        Field field = getFieldsOfType(GuiScreen.class, List.class)%5B0%5D;%0A        if (!field.isAccessible()) %7B%0A            field.setAccessible(true);%0A        %7D%0A        try %7B%0A            List%3C?%3E list = (List%3C?%3E) field.get(screen);%0A            return listHasType(list, ProtocolButton.class);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return false;%0A    %7D%0A%0A    private static %3CT%3E boolean listHasType(List%3C?%3E list, Class%3CT%3E type) %7B%0A        for (Object obj : list) %7B%0A            if (type.isAssignableFrom(obj.getClass())) %7B%0A                return true;%0A            %7D%0A        %7D%0A%0A        return false;%0A    %7D%0A%0A
     public s

Add check to make sure we don't add the button too many times (>1).