From fb705f4e1393c4da1c1455bcd51a29ff0c58411f Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 5 Feb 2021 15:53:29 +0100 Subject: [PATCH] Adds server console truncation after 1000 lines of output --- .../userinterface/Console.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java index f987eba..8e27080 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/Console.java @@ -26,6 +26,7 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener { private final JPanel panel; private final ArrayList commands = new ArrayList<>(); private int commandIndex; + private final int maxConsoleLines = 1000; /** * Instantiates a new console @@ -67,9 +68,32 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener { * @param text

The string to print

*/ public void output(String text) { + int outputLines = this.textOutput.getLineCount(); + if (outputLines > maxConsoleLines) { + truncateConsole(outputLines); + } this.textOutput.setText(this.textOutput.getText() + "\n" + text); } + /** + * Truncates the first 50 lines if the console output has reached the max limit + * @param outputLines

The currently readable lines in the console output field.

+ */ + private void truncateConsole(int outputLines) { + String oldText = this.textOutput.getText(); + String[] oldTextList = oldText.split("\n"); + for (int i = 0; i < outputLines - maxConsoleLines + 50; i++) { + oldTextList[i] = ""; + } + StringBuilder newTextBuilder = new StringBuilder(); + for (String line : oldTextList) { + if (!line.equals("")) { + newTextBuilder.append(line); + } + } + this.textOutput.setText(newTextBuilder.toString()); + } + @Override public void actionPerformed(ActionEvent e) { //Sends the command from the input to the server with the same name.