| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.io.IOException;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.network.play.client.C14PacketTabComplete;
|
| 7 | import net.minecraft.util.BlockPos;
|
| 8 | import net.minecraft.util.ChatComponentText;
|
| 9 | import net.minecraft.util.IChatComponent;
|
| 10 | import net.minecraft.util.MathHelper;
|
| 11 | import net.minecraft.util.MovingObjectPosition;
|
| 12 | import org.apache.commons.lang3.StringUtils;
|
| 13 | import org.apache.logging.log4j.LogManager;
|
| 14 | import org.apache.logging.log4j.Logger;
|
| 15 | import org.lwjgl.input.Keyboard;
|
| 16 | import org.lwjgl.input.Mouse;
|
| 17 |
|
| 18 | public class GuiChat extends GuiScreen
|
| 19 | {
|
| 20 | private static final Logger logger = LogManager.getLogger();
|
| 21 | private String historyBuffer = "";
|
| 22 |
|
| 23 | /**
|
| 24 | * keeps position of which chat message you will select when you press up, (does not increase for duplicated
|
| 25 | * messages sent immediately after each other)
|
| 26 | */
|
| 27 | private int sentHistoryCursor = -1;
|
| 28 | private boolean playerNamesFound;
|
| 29 | private boolean waitingOnAutocomplete;
|
| 30 | private int autocompleteIndex;
|
| 31 | private List<String> foundPlayerNames = Lists.<String>newArrayList();
|
| 32 |
|
| 33 | /** Chat entry field */
|
| 34 | protected GuiTextField inputField;
|
| 35 |
|
| 36 | /**
|
| 37 | * is the text that appears when you press the chat key and the input box appears pre-filled
|
| 38 | */
|
| 39 | private String defaultInputFieldText = "";
|
| 40 |
|
| 41 | public GuiChat()
|
| 42 | {
|
| 43 | }
|
| 44 |
|
| 45 | public GuiChat(String defaultText)
|
| 46 | {
|
| 47 | this.defaultInputFieldText = defaultText;
|
| 48 | }
|
| 49 |
|
| 50 | /**
|
| 51 | * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
|
| 52 | * window resizes, the buttonList is cleared beforehand.
|
| 53 | */
|
| 54 | public void initGui()
|
| 55 | {
|
| 56 | Keyboard.enableRepeatEvents(true);
|
| 57 | this.sentHistoryCursor = this.mc.ingameGUI.getChatGUI().getSentMessages().size();
|
| 58 | this.inputField = new GuiTextField(0, this.fontRendererObj, 4, this.height - 12, this.width - 4, 12);
|
| 59 | this.inputField.setMaxStringLength(100);
|
| 60 | this.inputField.setEnableBackgroundDrawing(false);
|
| 61 | this.inputField.setFocused(true);
|
| 62 | this.inputField.setText(this.defaultInputFieldText);
|
| 63 | this.inputField.setCanLoseFocus(false);
|
| 64 | }
|
| 65 |
|
| 66 | /**
|
| 67 | * Called when the screen is unloaded. Used to disable keyboard repeat events
|
| 68 | */
|
| 69 | public void onGuiClosed()
|
| 70 | {
|
| 71 | Keyboard.enableRepeatEvents(false);
|
| 72 | this.mc.ingameGUI.getChatGUI().resetScroll();
|
| 73 | }
|
| 74 |
|
| 75 | /**
|
| 76 | * Called from the main game loop to update the screen.
|
| 77 | */
|
| 78 | public void updateScreen()
|
| 79 | {
|
| 80 | this.inputField.updateCursorCounter();
|
| 81 | }
|
| 82 |
|
| 83 | /**
|
| 84 | * Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
|
| 85 | * KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
|
| 86 | */
|
| 87 | protected void keyTyped(char typedChar, int keyCode) throws IOException
|
| 88 | {
|
| 89 | this.waitingOnAutocomplete = false;
|
| 90 |
|
| 91 | if (keyCode == 15)
|
| 92 | {
|
| 93 | this.autocompletePlayerNames();
|
| 94 | }
|
| 95 | else
|
| 96 | {
|
| 97 | this.playerNamesFound = false;
|
| 98 | }
|
| 99 |
|
| 100 | if (keyCode == 1)
|
| 101 | {
|
| 102 | this.mc.displayGuiScreen((GuiScreen)null);
|
| 103 | }
|
| 104 | else if (keyCode != 28 && keyCode != 156)
|
| 105 | {
|
| 106 | if (keyCode == 200)
|
| 107 | {
|
| 108 | this.getSentHistory(-1);
|
| 109 | }
|
| 110 | else if (keyCode == 208)
|
| 111 | {
|
| 112 | this.getSentHistory(1);
|
| 113 | }
|
| 114 | else if (keyCode == 201)
|
| 115 | {
|
| 116 | this.mc.ingameGUI.getChatGUI().scroll(this.mc.ingameGUI.getChatGUI().getLineCount() - 1);
|
| 117 | }
|
| 118 | else if (keyCode == 209)
|
| 119 | {
|
| 120 | this.mc.ingameGUI.getChatGUI().scroll(-this.mc.ingameGUI.getChatGUI().getLineCount() + 1);
|
| 121 | }
|
| 122 | else
|
| 123 | {
|
| 124 | this.inputField.textboxKeyTyped(typedChar, keyCode);
|
| 125 | }
|
| 126 | }
|
| 127 | else
|
| 128 | {
|
| 129 | String s = this.inputField.getText().trim();
|
| 130 |
|
| 131 | if (s.length() > 0)
|
| 132 | {
|
| 133 | this.sendChatMessage(s);
|
| 134 | }
|
| 135 |
|
| 136 | this.mc.displayGuiScreen((GuiScreen)null);
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | /**
|
| 141 | * Handles mouse input.
|
| 142 | */
|
| 143 | public void handleMouseInput() throws IOException
|
| 144 | {
|
| 145 | super.handleMouseInput();
|
| 146 | int i = Mouse.getEventDWheel();
|
| 147 |
|
| 148 | if (i != 0)
|
| 149 | {
|
| 150 | if (i > 1)
|
| 151 | {
|
| 152 | i = 1;
|
| 153 | }
|
| 154 |
|
| 155 | if (i < -1)
|
| 156 | {
|
| 157 | i = -1;
|
| 158 | }
|
| 159 |
|
| 160 | if (!isShiftKeyDown())
|
| 161 | {
|
| 162 | i *= 7;
|
| 163 | }
|
| 164 |
|
| 165 | this.mc.ingameGUI.getChatGUI().scroll(i);
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | /**
|
| 170 | * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
|
| 171 | */
|
| 172 | protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
|
| 173 | {
|
| 174 | if (mouseButton == 0)
|
| 175 | {
|
| 176 | IChatComponent ichatcomponent = this.mc.ingameGUI.getChatGUI().getChatComponent(Mouse.getX(), Mouse.getY());
|
| 177 |
|
| 178 | if (this.handleComponentClick(ichatcomponent))
|
| 179 | {
|
| 180 | return;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | this.inputField.mouseClicked(mouseX, mouseY, mouseButton);
|
| 185 | super.mouseClicked(mouseX, mouseY, mouseButton);
|
| 186 | }
|
| 187 |
|
| 188 | /**
|
| 189 | * Sets the text of the chat
|
| 190 | */
|
| 191 | protected void setText(String newChatText, boolean shouldOverwrite)
|
| 192 | {
|
| 193 | if (shouldOverwrite)
|
| 194 | {
|
| 195 | this.inputField.setText(newChatText);
|
| 196 | }
|
| 197 | else
|
| 198 | {
|
| 199 | this.inputField.writeText(newChatText);
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | public void autocompletePlayerNames()
|
| 204 | {
|
| 205 | if (this.playerNamesFound)
|
| 206 | {
|
| 207 | this.inputField.deleteFromCursor(this.inputField.func_146197_a(-1, this.inputField.getCursorPosition(), false) - this.inputField.getCursorPosition());
|
| 208 |
|
| 209 | if (this.autocompleteIndex >= this.foundPlayerNames.size())
|
| 210 | {
|
| 211 | this.autocompleteIndex = 0;
|
| 212 | }
|
| 213 | }
|
| 214 | else
|
| 215 | {
|
| 216 | int i = this.inputField.func_146197_a(-1, this.inputField.getCursorPosition(), false);
|
| 217 | this.foundPlayerNames.clear();
|
| 218 | this.autocompleteIndex = 0;
|
| 219 | String s = this.inputField.getText().substring(i).toLowerCase();
|
| 220 | String s1 = this.inputField.getText().substring(0, this.inputField.getCursorPosition());
|
| 221 | this.sendAutocompleteRequest(s1, s);
|
| 222 |
|
| 223 | if (this.foundPlayerNames.isEmpty())
|
| 224 | {
|
| 225 | return;
|
| 226 | }
|
| 227 |
|
| 228 | this.playerNamesFound = true;
|
| 229 | this.inputField.deleteFromCursor(i - this.inputField.getCursorPosition());
|
| 230 | }
|
| 231 |
|
| 232 | if (this.foundPlayerNames.size() > 1)
|
| 233 | {
|
| 234 | StringBuilder stringbuilder = new StringBuilder();
|
| 235 |
|
| 236 | for (String s2 : this.foundPlayerNames)
|
| 237 | {
|
| 238 | if (stringbuilder.length() > 0)
|
| 239 | {
|
| 240 | stringbuilder.append(", ");
|
| 241 | }
|
| 242 |
|
| 243 | stringbuilder.append(s2);
|
| 244 | }
|
| 245 |
|
| 246 | this.mc.ingameGUI.getChatGUI().printChatMessageWithOptionalDeletion(new ChatComponentText(stringbuilder.toString()), 1);
|
| 247 | }
|
| 248 |
|
| 249 | this.inputField.writeText((String)this.foundPlayerNames.get(this.autocompleteIndex++));
|
| 250 | }
|
| 251 |
|
| 252 | private void sendAutocompleteRequest(String p_146405_1_, String p_146405_2_)
|
| 253 | {
|
| 254 | if (p_146405_1_.length() >= 1)
|
| 255 | {
|
| 256 | BlockPos blockpos = null;
|
| 257 |
|
| 258 | if (this.mc.objectMouseOver != null && this.mc.objectMouseOver.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
|
| 259 | {
|
| 260 | blockpos = this.mc.objectMouseOver.getBlockPos();
|
| 261 | }
|
| 262 |
|
| 263 | this.mc.thePlayer.sendQueue.addToSendQueue(new C14PacketTabComplete(p_146405_1_, blockpos));
|
| 264 | this.waitingOnAutocomplete = true;
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | /**
|
| 269 | * input is relative and is applied directly to the sentHistoryCursor so -1 is the previous message, 1 is the next
|
| 270 | * message from the current cursor position
|
| 271 | */
|
| 272 | public void getSentHistory(int msgPos)
|
| 273 | {
|
| 274 | int i = this.sentHistoryCursor + msgPos;
|
| 275 | int j = this.mc.ingameGUI.getChatGUI().getSentMessages().size();
|
| 276 | i = MathHelper.clamp_int(i, 0, j);
|
| 277 |
|
| 278 | if (i != this.sentHistoryCursor)
|
| 279 | {
|
| 280 | if (i == j)
|
| 281 | {
|
| 282 | this.sentHistoryCursor = j;
|
| 283 | this.inputField.setText(this.historyBuffer);
|
| 284 | }
|
| 285 | else
|
| 286 | {
|
| 287 | if (this.sentHistoryCursor == j)
|
| 288 | {
|
| 289 | this.historyBuffer = this.inputField.getText();
|
| 290 | }
|
| 291 |
|
| 292 | this.inputField.setText((String)this.mc.ingameGUI.getChatGUI().getSentMessages().get(i));
|
| 293 | this.sentHistoryCursor = i;
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 |
|
| 298 | /**
|
| 299 | * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
|
| 300 | */
|
| 301 | public void drawScreen(int mouseX, int mouseY, float partialTicks)
|
| 302 | {
|
| 303 | drawRect(2, this.height - 14, this.width - 2, this.height - 2, Integer.MIN_VALUE);
|
| 304 | this.inputField.drawTextBox();
|
| 305 | IChatComponent ichatcomponent = this.mc.ingameGUI.getChatGUI().getChatComponent(Mouse.getX(), Mouse.getY());
|
| 306 |
|
| 307 | if (ichatcomponent != null && ichatcomponent.getChatStyle().getChatHoverEvent() != null)
|
| 308 | {
|
| 309 | this.handleComponentHover(ichatcomponent, mouseX, mouseY);
|
| 310 | }
|
| 311 |
|
| 312 | super.drawScreen(mouseX, mouseY, partialTicks);
|
| 313 | }
|
| 314 |
|
| 315 | public void onAutocompleteResponse(String[] p_146406_1_)
|
| 316 | {
|
| 317 | if (this.waitingOnAutocomplete)
|
| 318 | {
|
| 319 | this.playerNamesFound = false;
|
| 320 | this.foundPlayerNames.clear();
|
| 321 |
|
| 322 | for (String s : p_146406_1_)
|
| 323 | {
|
| 324 | if (s.length() > 0)
|
| 325 | {
|
| 326 | this.foundPlayerNames.add(s);
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | String s1 = this.inputField.getText().substring(this.inputField.func_146197_a(-1, this.inputField.getCursorPosition(), false));
|
| 331 | String s2 = StringUtils.getCommonPrefix(p_146406_1_);
|
| 332 |
|
| 333 | if (s2.length() > 0 && !s1.equalsIgnoreCase(s2))
|
| 334 | {
|
| 335 | this.inputField.deleteFromCursor(this.inputField.func_146197_a(-1, this.inputField.getCursorPosition(), false) - this.inputField.getCursorPosition());
|
| 336 | this.inputField.writeText(s2);
|
| 337 | }
|
| 338 | else if (this.foundPlayerNames.size() > 0)
|
| 339 | {
|
| 340 | this.playerNamesFound = true;
|
| 341 | this.autocompletePlayerNames();
|
| 342 | }
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | /**
|
| 347 | * Returns true if this GUI should pause the game when it is displayed in single-player
|
| 348 | */
|
| 349 | public boolean doesGuiPauseGame()
|
| 350 | {
|
| 351 | return false;
|
| 352 | }
|
| 353 | }
|
| 354 |
|