    001package net.minecraft.client.gui;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.DataInputStream;
    006import java.io.DataOutputStream;
    007import java.io.IOException;
    008import java.net.InetSocketAddress;
    009import java.net.Socket;
    010import java.util.Collections;
    011import java.util.Iterator;
    012import java.util.List;
    013import net.minecraft.client.Minecraft;
    014import net.minecraft.client.mco.ExceptionMcoService;
    015import net.minecraft.client.mco.McoClient;
    016import net.minecraft.client.mco.McoServer;
    017import net.minecraft.client.mco.McoServerList;
    018import net.minecraft.client.multiplayer.ServerAddress;
    019import net.minecraft.network.packet.Packet;
    020import net.minecraft.util.ChatAllowedCharacters;
    021import net.minecraft.util.EnumChatFormatting;
    022import net.minecraft.util.MathHelper;
    023import net.minecraft.util.StringTranslate;
    024import org.lwjgl.input.Keyboard;
    025import org.lwjgl.opengl.GL11;
    026
    027@SideOnly(Side.CLIENT)
    028public class GuiScreenOnlineServers extends GuiScreen
    029{
    030    private GuiScreen field_96188_a;
    031    private GuiSlotOnlineServerList field_96186_b;
    032    private static int field_96187_c = 0;
    033    private static final Object field_96185_d = new Object();
    034    private int field_96189_n = -1;
    035    private GuiButton field_96190_o;
    036    private GuiButton field_96198_p;
    037    private GuiButtonLink field_96197_q;
    038    private GuiButton field_96196_r;
    039    private String field_96195_s = null;
    040    private McoServerList field_96194_t;
    041    private boolean field_96193_u;
    042    private List field_96192_v = Collections.emptyList();
    043    private String field_96191_w = "http://minecraft.net";
    044    private volatile int field_96199_x;
    045
    046    public GuiScreenOnlineServers(GuiScreen par1)
    047    {
    048        this.field_96188_a = par1;
    049    }
    050
    051    /**
    052     * Adds the buttons (and other controls) to the screen in question.
    053     */
    054    public void initGui()
    055    {
    056        Keyboard.enableRepeatEvents(true);
    057        this.buttonList.clear();
    058        this.field_96194_t = new McoServerList(this.mc.session);
    059
    060        if (!this.field_96193_u)
    061        {
    062            this.field_96193_u = true;
    063            this.field_96186_b = new GuiSlotOnlineServerList(this);
    064        }
    065        else
    066        {
    067            this.field_96186_b.func_77207_a(this.width, this.height, 32, this.height - 64);
    068        }
    069
    070        (new ThreadOnlineScreen(this)).start();
    071        this.func_96178_g();
    072    }
    073
    074    public void func_96178_g()
    075    {
    076        StringTranslate stringtranslate = StringTranslate.getInstance();
    077        this.buttonList.add(this.field_96196_r = new GuiButton(1, this.width / 2 - 154, this.height - 52, 100, 20, stringtranslate.translateKey("mco.selectServer.select")));
    078        this.buttonList.add(this.field_96198_p = new GuiButton(2, this.width / 2 - 48, this.height - 52, 100, 20, stringtranslate.translateKey("mco.selectServer.create")));
    079        this.buttonList.add(this.field_96190_o = new GuiButton(3, this.width / 2 + 58, this.height - 52, 100, 20, stringtranslate.translateKey("mco.selectServer.configure")));
    080        this.buttonList.add(this.field_96197_q = new GuiButtonLink(4, this.width / 2 - 154, this.height - 28, 154, 20, stringtranslate.translateKey("mco.selectServer.moreinfo")));
    081        this.buttonList.add(new GuiButton(0, this.width / 2 + 6, this.height - 28, 153, 20, stringtranslate.translateKey("gui.cancel")));
    082        boolean flag = this.field_96189_n >= 0 && this.field_96189_n < this.field_96186_b.getSize();
    083        this.field_96196_r.enabled = flag && ((McoServer)this.field_96192_v.get(this.field_96189_n)).field_96404_d.equals("OPEN") && !((McoServer)this.field_96192_v.get(this.field_96189_n)).field_98166_h;
    084        this.field_96190_o.enabled = flag && this.mc.session.username.equals(((McoServer)this.field_96192_v.get(this.field_96189_n)).field_96405_e);
    085        this.field_96198_p.enabled = this.field_96199_x > 0;
    086    }
    087
    088    /**
    089     * Called from the main game loop to update the screen.
    090     */
    091    public void updateScreen()
    092    {
    093        super.updateScreen();
    094
    095        if (this.field_96194_t.func_98251_a())
    096        {
    097            List list = this.field_96194_t.func_98252_c();
    098            Iterator iterator = list.iterator();
    099
    100            while (iterator.hasNext())
    101            {
    102                McoServer mcoserver = (McoServer)iterator.next();
    103                Iterator iterator1 = this.field_96192_v.iterator();
    104
    105                while (iterator1.hasNext())
    106                {
    107                    McoServer mcoserver1 = (McoServer)iterator1.next();
    108
    109                    if (mcoserver.field_96408_a == mcoserver1.field_96408_a)
    110                    {
    111                        mcoserver.func_96401_a(mcoserver1);
    112                        break;
    113                    }
    114                }
    115            }
    116
    117            this.field_96192_v = list;
    118            this.field_96194_t.func_98250_b();
    119        }
    120
    121        this.field_96198_p.enabled = this.field_96199_x > 0;
    122    }
    123
    124    /**
    125     * Called when the screen is unloaded. Used to disable keyboard repeat events
    126     */
    127    public void onGuiClosed()
    128    {
    129        Keyboard.enableRepeatEvents(false);
    130    }
    131
    132    /**
    133     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    134     */
    135    protected void actionPerformed(GuiButton par1GuiButton)
    136    {
    137        if (par1GuiButton.enabled)
    138        {
    139            if (par1GuiButton.id == 1)
    140            {
    141                this.func_96159_a(this.field_96189_n);
    142            }
    143            else if (par1GuiButton.id == 3)
    144            {
    145                McoServer mcoserver = (McoServer)this.field_96194_t.func_98252_c().get(this.field_96189_n);
    146                McoServer mcoserver1 = this.func_98086_a(mcoserver.field_96408_a);
    147
    148                if (mcoserver1 != null)
    149                {
    150                    this.field_96194_t.func_98248_d();
    151                    this.mc.displayGuiScreen(new GuiScreenConfigureWorld(this, mcoserver1));
    152                }
    153            }
    154            else if (par1GuiButton.id == 0)
    155            {
    156                this.field_96194_t.func_98248_d();
    157                this.mc.displayGuiScreen(this.field_96188_a);
    158            }
    159            else if (par1GuiButton.id == 2)
    160            {
    161                this.field_96194_t.func_98248_d();
    162                this.mc.displayGuiScreen(new GuiScreenCreateOnlineWorld(this));
    163            }
    164            else if (par1GuiButton.id == 4)
    165            {
    166                this.field_96197_q.func_96135_a(this.field_96191_w);
    167            }
    168            else
    169            {
    170                this.field_96186_b.actionPerformed(par1GuiButton);
    171            }
    172        }
    173    }
    174
    175    private McoServer func_98086_a(long par1)
    176    {
    177        McoClient mcoclient = new McoClient(this.mc.session);
    178
    179        try
    180        {
    181            return mcoclient.func_98176_a(par1);
    182        }
    183        catch (ExceptionMcoService exceptionmcoservice)
    184        {
    185            ;
    186        }
    187        catch (IOException ioexception)
    188        {
    189            ;
    190        }
    191
    192        return null;
    193    }
    194
    195    /**
    196     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
    197     */
    198    protected void keyTyped(char par1, int par2)
    199    {
    200        if (par2 == 59)
    201        {
    202            this.mc.gameSettings.hideServerAddress = !this.mc.gameSettings.hideServerAddress;
    203            this.mc.gameSettings.saveOptions();
    204        }
    205        else
    206        {
    207            if (par1 == 13)
    208            {
    209                this.actionPerformed((GuiButton)this.buttonList.get(2));
    210            }
    211            else
    212            {
    213                super.keyTyped(par1, par2);
    214            }
    215        }
    216    }
    217
    218    /**
    219     * Draws the screen and all the components in it.
    220     */
    221    public void drawScreen(int par1, int par2, float par3)
    222    {
    223        this.field_96195_s = null;
    224        StringTranslate stringtranslate = StringTranslate.getInstance();
    225        this.drawDefaultBackground();
    226        this.field_96186_b.drawScreen(par1, par2, par3);
    227        this.drawCenteredString(this.fontRenderer, stringtranslate.translateKey("mco.title"), this.width / 2, 20, 16777215);
    228        super.drawScreen(par1, par2, par3);
    229
    230        if (this.field_96195_s != null)
    231        {
    232            this.func_96165_a(this.field_96195_s, par1, par2);
    233        }
    234    }
    235
    236    private void func_96159_a(int par1)
    237    {
    238        if (par1 >= 0 && par1 < this.field_96192_v.size())
    239        {
    240            McoServer mcoserver = (McoServer)this.field_96192_v.get(par1);
    241            this.field_96194_t.func_98248_d();
    242            GuiScreenLongRunningTask guiscreenlongrunningtask = new GuiScreenLongRunningTask(this.mc, this, new TaskOnlineConnect(this, mcoserver));
    243            guiscreenlongrunningtask.func_98117_g();
    244            this.mc.displayGuiScreen(guiscreenlongrunningtask);
    245        }
    246    }
    247
    248    private void func_101008_c(int par1, int par2, int par3, int par4)
    249    {
    250        this.mc.renderEngine.func_98187_b("/gui/gui.png");
    251        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    252        GL11.glPushMatrix();
    253        GL11.glScalef(0.5F, 0.5F, 0.5F);
    254        this.drawTexturedModalRect(par1 * 2, par2 * 2, 191, 0, 16, 15);
    255        GL11.glPopMatrix();
    256
    257        if (par3 >= par1 && par3 <= par1 + 9 && par4 >= par2 && par4 <= par2 + 9)
    258        {
    259            this.field_96195_s = "Expired World";
    260        }
    261    }
    262
    263    private void func_101006_d(int par1, int par2, int par3, int par4)
    264    {
    265        this.mc.renderEngine.func_98187_b("/gui/gui.png");
    266        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    267        GL11.glPushMatrix();
    268        GL11.glScalef(0.5F, 0.5F, 0.5F);
    269        this.drawTexturedModalRect(par1 * 2, par2 * 2, 207, 0, 16, 15);
    270        GL11.glPopMatrix();
    271
    272        if (par3 >= par1 && par3 <= par1 + 9 && par4 >= par2 && par4 <= par2 + 9)
    273        {
    274            this.field_96195_s = "Open World";
    275        }
    276    }
    277
    278    private void func_101001_e(int par1, int par2, int par3, int par4)
    279    {
    280        this.mc.renderEngine.func_98187_b("/gui/gui.png");
    281        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    282        GL11.glPushMatrix();
    283        GL11.glScalef(0.5F, 0.5F, 0.5F);
    284        this.drawTexturedModalRect(par1 * 2, par2 * 2, 223, 0, 16, 15);
    285        GL11.glPopMatrix();
    286
    287        if (par3 >= par1 && par3 <= par1 + 9 && par4 >= par2 && par4 <= par2 + 9)
    288        {
    289            this.field_96195_s = "Closed World";
    290        }
    291    }
    292
    293    protected void func_96165_a(String par1Str, int par2, int par3)
    294    {
    295        if (par1Str != null)
    296        {
    297            int k = par2 + 12;
    298            int l = par3 - 12;
    299            int i1 = this.fontRenderer.getStringWidth(par1Str);
    300            this.drawGradientRect(k - 3, l - 3, k + i1 + 3, l + 8 + 3, -1073741824, -1073741824);
    301            this.fontRenderer.drawStringWithShadow(par1Str, k, l, -1);
    302        }
    303    }
    304
    305    private void func_96174_a(McoServer par1McoServer) throws IOException
    306    {
    307        ServerAddress serveraddress = ServerAddress.func_78860_a(par1McoServer.field_96403_g);
    308        Socket socket = null;
    309        DataInputStream datainputstream = null;
    310        DataOutputStream dataoutputstream = null;
    311
    312        try
    313        {
    314            socket = new Socket();
    315            socket.setSoTimeout(3000);
    316            socket.setTcpNoDelay(true);
    317            socket.setTrafficClass(18);
    318            socket.connect(new InetSocketAddress(serveraddress.getIP(), serveraddress.getPort()), 3000);
    319            datainputstream = new DataInputStream(socket.getInputStream());
    320            dataoutputstream = new DataOutputStream(socket.getOutputStream());
    321            dataoutputstream.write(254);
    322            dataoutputstream.write(1);
    323
    324            if (datainputstream.read() != 255)
    325            {
    326                throw new IOException("Bad message");
    327            }
    328
    329            String s = Packet.readString(datainputstream, 256);
    330            char[] achar = s.toCharArray();
    331
    332            for (int i = 0; i < achar.length; ++i)
    333            {
    334                if (achar[i] != 167 && achar[i] != 0 && ChatAllowedCharacters.allowedCharacters.indexOf(achar[i]) < 0)
    335                {
    336                    achar[i] = 63;
    337                }
    338            }
    339
    340            s = new String(achar);
    341            int j;
    342            int k;
    343            String[] astring;
    344
    345            if (s.startsWith("\u00a7") && s.length() > 1)
    346            {
    347                astring = s.substring(1).split("\u0000");
    348
    349                if (MathHelper.parseIntWithDefault(astring[0], 0) == 1)
    350                {
    351                    par1McoServer.field_96415_h = MathHelper.parseIntWithDefault(astring[1], par1McoServer.field_96415_h);
    352                    par1McoServer.field_96413_j = astring[2];
    353                    j = MathHelper.parseIntWithDefault(astring[4], 0);
    354                    k = MathHelper.parseIntWithDefault(astring[5], 0);
    355
    356                    if (j >= 0 && k >= 0)
    357                    {
    358                        par1McoServer.field_96414_k = EnumChatFormatting.GRAY + "" + j;
    359                    }
    360                    else
    361                    {
    362                        par1McoServer.field_96414_k = "" + EnumChatFormatting.DARK_GRAY + "???";
    363                    }
    364                }
    365                else
    366                {
    367                    par1McoServer.field_96413_j = "???";
    368                    par1McoServer.field_96415_h = 61;
    369                    par1McoServer.field_96414_k = "" + EnumChatFormatting.DARK_GRAY + "???";
    370                }
    371            }
    372            else
    373            {
    374                astring = s.split("\u00a7");
    375                s = astring[0];
    376                j = -1;
    377                k = -1;
    378
    379                try
    380                {
    381                    j = Integer.parseInt(astring[1]);
    382                    k = Integer.parseInt(astring[2]);
    383                }
    384                catch (Exception exception)
    385                {
    386                    ;
    387                }
    388
    389                par1McoServer.field_96407_c = EnumChatFormatting.GRAY + s;
    390
    391                if (j >= 0 && k > 0)
    392                {
    393                    par1McoServer.field_96414_k = EnumChatFormatting.GRAY + "" + j;
    394                }
    395                else
    396                {
    397                    par1McoServer.field_96414_k = "" + EnumChatFormatting.DARK_GRAY + "???";
    398                }
    399
    400                par1McoServer.field_96413_j = "1.3";
    401                par1McoServer.field_96415_h = 59;
    402            }
    403        }
    404        finally
    405        {
    406            try
    407            {
    408                if (datainputstream != null)
    409                {
    410                    datainputstream.close();
    411                }
    412            }
    413            catch (Throwable throwable)
    414            {
    415                ;
    416            }
    417
    418            try
    419            {
    420                if (dataoutputstream != null)
    421                {
    422                    dataoutputstream.close();
    423                }
    424            }
    425            catch (Throwable throwable1)
    426            {
    427                ;
    428            }
    429
    430            try
    431            {
    432                if (socket != null)
    433                {
    434                    socket.close();
    435                }
    436            }
    437            catch (Throwable throwable2)
    438            {
    439                ;
    440            }
    441        }
    442    }
    443
    444    static Minecraft func_96177_a(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    445    {
    446        return par0GuiScreenOnlineServers.mc;
    447    }
    448
    449    static int func_98081_a(GuiScreenOnlineServers par0GuiScreenOnlineServers, int par1)
    450    {
    451        return par0GuiScreenOnlineServers.field_96199_x = par1;
    452    }
    453
    454    static Minecraft func_98075_b(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    455    {
    456        return par0GuiScreenOnlineServers.mc;
    457    }
    458
    459    static List func_98094_c(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    460    {
    461        return par0GuiScreenOnlineServers.field_96192_v;
    462    }
    463
    464    static int func_98089_b(GuiScreenOnlineServers par0GuiScreenOnlineServers, int par1)
    465    {
    466        return par0GuiScreenOnlineServers.field_96189_n = par1;
    467    }
    468
    469    static int func_98072_d(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    470    {
    471        return par0GuiScreenOnlineServers.field_96189_n;
    472    }
    473
    474    static GuiButton func_96161_f(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    475    {
    476        return par0GuiScreenOnlineServers.field_96190_o;
    477    }
    478
    479    static Minecraft func_98076_f(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    480    {
    481        return par0GuiScreenOnlineServers.mc;
    482    }
    483
    484    static GuiButton func_98092_g(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    485    {
    486        return par0GuiScreenOnlineServers.field_96196_r;
    487    }
    488
    489    static void func_98078_c(GuiScreenOnlineServers par0GuiScreenOnlineServers, int par1)
    490    {
    491        par0GuiScreenOnlineServers.func_96159_a(par1);
    492    }
    493
    494    static FontRenderer func_98091_h(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    495    {
    496        return par0GuiScreenOnlineServers.fontRenderer;
    497    }
    498
    499    static void func_101003_a(GuiScreenOnlineServers par0GuiScreenOnlineServers, int par1, int par2, int par3, int par4)
    500    {
    501        par0GuiScreenOnlineServers.func_101008_c(par1, par2, par3, par4);
    502    }
    503
    504    static void func_101012_b(GuiScreenOnlineServers par0GuiScreenOnlineServers, int par1, int par2, int par3, int par4)
    505    {
    506        par0GuiScreenOnlineServers.func_101001_e(par1, par2, par3, par4);
    507    }
    508
    509    static void func_101009_c(GuiScreenOnlineServers par0GuiScreenOnlineServers, int par1, int par2, int par3, int par4)
    510    {
    511        par0GuiScreenOnlineServers.func_101006_d(par1, par2, par3, par4);
    512    }
    513
    514    static FontRenderer func_98084_i(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    515    {
    516        return par0GuiScreenOnlineServers.fontRenderer;
    517    }
    518
    519    static FontRenderer func_101005_j(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    520    {
    521        return par0GuiScreenOnlineServers.fontRenderer;
    522    }
    523
    524    static Object func_101007_h()
    525    {
    526        return field_96185_d;
    527    }
    528
    529    static int func_101010_i()
    530    {
    531        return field_96187_c;
    532    }
    533
    534    static int func_101014_j()
    535    {
    536        return field_96187_c++;
    537    }
    538
    539    static void func_101002_a(GuiScreenOnlineServers par0GuiScreenOnlineServers, McoServer par1McoServer) throws IOException
    540    {
    541        par0GuiScreenOnlineServers.func_96174_a(par1McoServer);
    542    }
    543
    544    static int func_101013_k()
    545    {
    546        return field_96187_c--;
    547    }
    548
    549    static FontRenderer func_98079_k(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    550    {
    551        return par0GuiScreenOnlineServers.fontRenderer;
    552    }
    553
    554    static FontRenderer func_98087_l(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    555    {
    556        return par0GuiScreenOnlineServers.fontRenderer;
    557    }
    558
    559    static FontRenderer func_98074_m(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    560    {
    561        return par0GuiScreenOnlineServers.fontRenderer;
    562    }
    563
    564    static FontRenderer func_101000_n(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    565    {
    566        return par0GuiScreenOnlineServers.fontRenderer;
    567    }
    568
    569    static Minecraft func_101004_o(GuiScreenOnlineServers par0GuiScreenOnlineServers)
    570    {
    571        return par0GuiScreenOnlineServers.mc;
    572    }
    573
    574    static String func_101011_a(GuiScreenOnlineServers par0GuiScreenOnlineServers, String par1Str)
    575    {
    576        return par0GuiScreenOnlineServers.field_96195_s = par1Str;
    577    }
    578}