Adds server console truncation after 1000 lines of output
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2021-02-05 15:53:29 +01:00
parent 15dd8db31b
commit fb705f4e13

View File

@ -26,6 +26,7 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener {
private final JPanel panel; private final JPanel panel;
private final ArrayList<String> commands = new ArrayList<>(); private final ArrayList<String> commands = new ArrayList<>();
private int commandIndex; private int commandIndex;
private final int maxConsoleLines = 1000;
/** /**
* Instantiates a new console * Instantiates a new console
@ -67,9 +68,32 @@ public class Console extends KeyAdapter implements ActionListener, KeyListener {
* @param text <p>The string to print</p> * @param text <p>The string to print</p>
*/ */
public void output(String text) { public void output(String text) {
int outputLines = this.textOutput.getLineCount();
if (outputLines > maxConsoleLines) {
truncateConsole(outputLines);
}
this.textOutput.setText(this.textOutput.getText() + "\n" + text); this.textOutput.setText(this.textOutput.getText() + "\n" + text);
} }
/**
* Truncates the first 50 lines if the console output has reached the max limit
* @param outputLines <p>The currently readable lines in the console output field.</p>
*/
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 @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
//Sends the command from the input to the server with the same name. //Sends the command from the input to the server with the same name.