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