    001    package net.minecraft.client.gui;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import net.minecraft.client.multiplayer.ServerData;
    006    import net.minecraft.util.StringTranslate;
    007    import org.lwjgl.input.Keyboard;
    008
    009    @SideOnly(Side.CLIENT)
    010    public class GuiScreenServerList extends GuiScreen
    011    {
    012        /**
    013         * Remembers the last hostname or IP address entered into text field between invocations of the GUI.
    014         */
    015        private static String lastServerName = "";
    016
    017        /** Needed a change as a local variable was conflicting on construct */
    018        private final GuiScreen guiScreen;
    019
    020        /** Instance of ServerData. */
    021        private final ServerData theServerData;
    022        private GuiTextField serverTextField;
    023
    024        public GuiScreenServerList(GuiScreen par1GuiScreen, ServerData par2ServerData)
    025        {
    026            this.guiScreen = par1GuiScreen;
    027            this.theServerData = par2ServerData;
    028        }
    029
    030        /**
    031         * Called from the main game loop to update the screen.
    032         */
    033        public void updateScreen()
    034        {
    035            this.serverTextField.updateCursorCounter();
    036        }
    037
    038        /**
    039         * Adds the buttons (and other controls) to the screen in question.
    040         */
    041        public void initGui()
    042        {
    043            StringTranslate var1 = StringTranslate.getInstance();
    044            Keyboard.enableRepeatEvents(true);
    045            this.controlList.clear();
    046            this.controlList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, var1.translateKey("selectServer.select")));
    047            this.controlList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, var1.translateKey("gui.cancel")));
    048            this.serverTextField = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 116, 200, 20);
    049            this.serverTextField.setMaxStringLength(128);
    050            this.serverTextField.setFocused(true);
    051            this.serverTextField.setText(lastServerName);
    052            ((GuiButton)this.controlList.get(0)).enabled = this.serverTextField.getText().length() > 0 && this.serverTextField.getText().split(":").length > 0;
    053        }
    054
    055        /**
    056         * Called when the screen is unloaded. Used to disable keyboard repeat events
    057         */
    058        public void onGuiClosed()
    059        {
    060            Keyboard.enableRepeatEvents(false);
    061            lastServerName = this.serverTextField.getText();
    062        }
    063
    064        /**
    065         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    066         */
    067        protected void actionPerformed(GuiButton par1GuiButton)
    068        {
    069            if (par1GuiButton.enabled)
    070            {
    071                if (par1GuiButton.id == 1)
    072                {
    073                    this.guiScreen.confirmClicked(false, 0);
    074                }
    075                else if (par1GuiButton.id == 0)
    076                {
    077                    this.theServerData.serverIP = this.serverTextField.getText();
    078                    this.guiScreen.confirmClicked(true, 0);
    079                }
    080            }
    081        }
    082
    083        /**
    084         * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
    085         */
    086        protected void keyTyped(char par1, int par2)
    087        {
    088            if (this.serverTextField.textboxKeyTyped(par1, par2))
    089            {
    090                ((GuiButton)this.controlList.get(0)).enabled = this.serverTextField.getText().length() > 0 && this.serverTextField.getText().split(":").length > 0;
    091            }
    092            else if (par2 == 28)
    093            {
    094                this.actionPerformed((GuiButton)this.controlList.get(0));
    095            }
    096        }
    097
    098        /**
    099         * Called when the mouse is clicked.
    100         */
    101        protected void mouseClicked(int par1, int par2, int par3)
    102        {
    103            super.mouseClicked(par1, par2, par3);
    104            this.serverTextField.mouseClicked(par1, par2, par3);
    105        }
    106
    107        /**
    108         * Draws the screen and all the components in it.
    109         */
    110        public void drawScreen(int par1, int par2, float par3)
    111        {
    112            StringTranslate var4 = StringTranslate.getInstance();
    113            this.drawDefaultBackground();
    114            this.drawCenteredString(this.fontRenderer, var4.translateKey("selectServer.direct"), this.width / 2, this.height / 4 - 60 + 20, 16777215);
    115            this.drawString(this.fontRenderer, var4.translateKey("addServer.enterIp"), this.width / 2 - 100, 100, 10526880);
    116            this.serverTextField.drawTextBox();
    117            super.drawScreen(par1, par2, par3);
    118        }
    119    }