| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import java.io.IOException;
|
| 5 | import java.net.IDN;
|
| 6 | import net.minecraft.client.multiplayer.ServerData;
|
| 7 | import net.minecraft.client.resources.I18n;
|
| 8 | import org.lwjgl.input.Keyboard;
|
| 9 |
|
| 10 | public class GuiScreenAddServer extends GuiScreen
|
| 11 | {
|
| 12 | private final GuiScreen parentScreen;
|
| 13 | private final ServerData serverData;
|
| 14 | private GuiTextField serverIPField;
|
| 15 | private GuiTextField serverNameField;
|
| 16 | private GuiButton serverResourcePacks;
|
| 17 | private Predicate<String> field_181032_r = new Predicate<String>()
|
| 18 | {
|
| 19 | public boolean apply(String p_apply_1_)
|
| 20 | {
|
| 21 | if (p_apply_1_.length() == 0)
|
| 22 | {
|
| 23 | return true;
|
| 24 | }
|
| 25 | else
|
| 26 | {
|
| 27 | String[] astring = p_apply_1_.split(":");
|
| 28 |
|
| 29 | if (astring.length == 0)
|
| 30 | {
|
| 31 | return true;
|
| 32 | }
|
| 33 | else
|
| 34 | {
|
| 35 | try
|
| 36 | {
|
| 37 | String s = IDN.toASCII(astring[0]);
|
| 38 | return true;
|
| 39 | }
|
| 40 | catch (IllegalArgumentException var4)
|
| 41 | {
|
| 42 | return false;
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 | }
|
| 47 | };
|
| 48 |
|
| 49 | public GuiScreenAddServer(GuiScreen p_i1033_1_, ServerData p_i1033_2_)
|
| 50 | {
|
| 51 | this.parentScreen = p_i1033_1_;
|
| 52 | this.serverData = p_i1033_2_;
|
| 53 | }
|
| 54 |
|
| 55 | /**
|
| 56 | * Called from the main game loop to update the screen.
|
| 57 | */
|
| 58 | public void updateScreen()
|
| 59 | {
|
| 60 | this.serverNameField.updateCursorCounter();
|
| 61 | this.serverIPField.updateCursorCounter();
|
| 62 | }
|
| 63 |
|
| 64 | /**
|
| 65 | * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
|
| 66 | * window resizes, the buttonList is cleared beforehand.
|
| 67 | */
|
| 68 | public void initGui()
|
| 69 | {
|
| 70 | Keyboard.enableRepeatEvents(true);
|
| 71 | this.buttonList.clear();
|
| 72 | this.buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 18, I18n.format("addServer.add", new Object[0])));
|
| 73 | this.buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 18, I18n.format("gui.cancel", new Object[0])));
|
| 74 | this.buttonList.add(this.serverResourcePacks = new GuiButton(2, this.width / 2 - 100, this.height / 4 + 72, I18n.format("addServer.resourcePack", new Object[0]) + ": " + this.serverData.getResourceMode().getMotd().getFormattedText()));
|
| 75 | this.serverNameField = new GuiTextField(0, this.fontRendererObj, this.width / 2 - 100, 66, 200, 20);
|
| 76 | this.serverNameField.setFocused(true);
|
| 77 | this.serverNameField.setText(this.serverData.serverName);
|
| 78 | this.serverIPField = new GuiTextField(1, this.fontRendererObj, this.width / 2 - 100, 106, 200, 20);
|
| 79 | this.serverIPField.setMaxStringLength(128);
|
| 80 | this.serverIPField.setText(this.serverData.serverIP);
|
| 81 | this.serverIPField.setValidator(this.field_181032_r);
|
| 82 | ((GuiButton)this.buttonList.get(0)).enabled = this.serverIPField.getText().length() > 0 && this.serverIPField.getText().split(":").length > 0 && this.serverNameField.getText().length() > 0;
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * Called when the screen is unloaded. Used to disable keyboard repeat events
|
| 87 | */
|
| 88 | public void onGuiClosed()
|
| 89 | {
|
| 90 | Keyboard.enableRepeatEvents(false);
|
| 91 | }
|
| 92 |
|
| 93 | /**
|
| 94 | * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
|
| 95 | */
|
| 96 | protected void actionPerformed(GuiButton button) throws IOException
|
| 97 | {
|
| 98 | if (button.enabled)
|
| 99 | {
|
| 100 | if (button.id == 2)
|
| 101 | {
|
| 102 | this.serverData.setResourceMode(ServerData.ServerResourceMode.values()[(this.serverData.getResourceMode().ordinal() + 1) % ServerData.ServerResourceMode.values().length]);
|
| 103 | this.serverResourcePacks.displayString = I18n.format("addServer.resourcePack", new Object[0]) + ": " + this.serverData.getResourceMode().getMotd().getFormattedText();
|
| 104 | }
|
| 105 | else if (button.id == 1)
|
| 106 | {
|
| 107 | this.parentScreen.confirmClicked(false, 0);
|
| 108 | }
|
| 109 | else if (button.id == 0)
|
| 110 | {
|
| 111 | this.serverData.serverName = this.serverNameField.getText();
|
| 112 | this.serverData.serverIP = this.serverIPField.getText();
|
| 113 | this.parentScreen.confirmClicked(true, 0);
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | /**
|
| 119 | * Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
|
| 120 | * KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
|
| 121 | */
|
| 122 | protected void keyTyped(char typedChar, int keyCode) throws IOException
|
| 123 | {
|
| 124 | this.serverNameField.textboxKeyTyped(typedChar, keyCode);
|
| 125 | this.serverIPField.textboxKeyTyped(typedChar, keyCode);
|
| 126 |
|
| 127 | if (keyCode == 15)
|
| 128 | {
|
| 129 | this.serverNameField.setFocused(!this.serverNameField.isFocused());
|
| 130 | this.serverIPField.setFocused(!this.serverIPField.isFocused());
|
| 131 | }
|
| 132 |
|
| 133 | if (keyCode == 28 || keyCode == 156)
|
| 134 | {
|
| 135 | this.actionPerformed((GuiButton)this.buttonList.get(0));
|
| 136 | }
|
| 137 |
|
| 138 | ((GuiButton)this.buttonList.get(0)).enabled = this.serverIPField.getText().length() > 0 && this.serverIPField.getText().split(":").length > 0 && this.serverNameField.getText().length() > 0;
|
| 139 | }
|
| 140 |
|
| 141 | /**
|
| 142 | * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
|
| 143 | */
|
| 144 | protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
|
| 145 | {
|
| 146 | super.mouseClicked(mouseX, mouseY, mouseButton);
|
| 147 | this.serverIPField.mouseClicked(mouseX, mouseY, mouseButton);
|
| 148 | this.serverNameField.mouseClicked(mouseX, mouseY, mouseButton);
|
| 149 | }
|
| 150 |
|
| 151 | /**
|
| 152 | * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
|
| 153 | */
|
| 154 | public void drawScreen(int mouseX, int mouseY, float partialTicks)
|
| 155 | {
|
| 156 | this.drawDefaultBackground();
|
| 157 | this.drawCenteredString(this.fontRendererObj, I18n.format("addServer.title", new Object[0]), this.width / 2, 17, 16777215);
|
| 158 | this.drawString(this.fontRendererObj, I18n.format("addServer.enterName", new Object[0]), this.width / 2 - 100, 53, 10526880);
|
| 159 | this.drawString(this.fontRendererObj, I18n.format("addServer.enterIp", new Object[0]), this.width / 2 - 100, 94, 10526880);
|
| 160 | this.serverNameField.drawTextBox();
|
| 161 | this.serverIPField.drawTextBox();
|
| 162 | super.drawScreen(mouseX, mouseY, partialTicks);
|
| 163 | }
|
| 164 | }
|
| 165 |
|