Advertisement
TNT_Block

Mein Chat :D

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