Skip to content

Commit

Permalink
added option to strip color codes, redesigned the ui and fixed an iss…
Browse files Browse the repository at this point in the history
…ue issue with the log file encoding
  • Loading branch information
doej1367 committed Aug 25, 2022
1 parent e5c909e commit 31a70f8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 49 deletions.
1 change: 1 addition & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/main/
/icons/
/util/
Binary file added src/icons/icon-more-20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 57 additions & 44 deletions src/main/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
Expand All @@ -34,7 +35,11 @@
import util.MCLogFile;
import util.MCLogLine;
import util.OSFileSystem;
import java.awt.Checkbox;
import javax.swing.JMenu;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;

/**
*
Expand All @@ -50,20 +55,21 @@ public class MainWindow extends JFrame {
private JTextArea outputTextField;
private JScrollPane scrollPaneBottom;
private JTextArea statusTextField;
private Checkbox onlyChatCheckBox;
private Button defaultButton;
private Button addFoldersButton;
private JPanel panel_north;
private Component horizontalGlue;
private Component horizontalGlue_1;
private Component horizontalGlue_2;

private TreeSet<String> additionalFolderPaths = new TreeSet<>();
private LogRecords logRecords;

private int tmpStauslength = 0;
private JPanel panel_south;
private Component horizontalGlue_3;
private JMenuBar menuBar;
private JMenu mnOptionsMenu;
private JMenuItem addFoldersButtonMenuItem;
private JCheckBoxMenuItem onlyChatCheckBoxMenu;
private Component horizontalGlue_1;
private Button defaultButton;
private Component horizontalGlue_2;
private JCheckBoxMenuItem stripColorCodesCheckBoxMenu;

/**
* Create the frame.
Expand All @@ -86,34 +92,34 @@ public MainWindow() {
contentPane.add(panel_north, BorderLayout.NORTH);
panel_north.setLayout(new BoxLayout(panel_north, BoxLayout.X_AXIS));

horizontalGlue = Box.createHorizontalGlue();
panel_north.add(horizontalGlue);
horizontalGlue_2 = Box.createHorizontalGlue();
panel_north.add(horizontalGlue_2);

defaultButton = new Button("Start analyzing currently known .minecraft folders");
defaultButton.setBackground(new Color(240, 248, 255));
defaultButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
public void actionPerformed(ActionEvent e) {
startAnalysis();
}
});
panel_north.add(defaultButton);
defaultButton.setForeground(Color.BLUE);
defaultButton.setFont(new Font("Consolas", Font.PLAIN, 14));

horizontalGlue_3 = Box.createHorizontalGlue();
panel_north.add(horizontalGlue_3);
defaultButton.setBackground(new Color(240, 248, 255));
panel_north.add(defaultButton);

onlyChatCheckBox = new Checkbox("Only filter chat lines");
onlyChatCheckBox.setState(true);
onlyChatCheckBox.setFont(new Font("Consolas", Font.PLAIN, 14));
panel_north.add(onlyChatCheckBox);
menuBar = new JMenuBar();
menuBar.setBorderPainted(false);
panel_north.add(menuBar);

horizontalGlue_2 = Box.createHorizontalGlue();
panel_north.add(horizontalGlue_2);
mnOptionsMenu = new JMenu("");
mnOptionsMenu.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/icon-more-20.png")));
mnOptionsMenu.setSize(20, 20);
mnOptionsMenu.setHorizontalAlignment(SwingConstants.RIGHT);
mnOptionsMenu.setFont(new Font("Consolas", Font.PLAIN, 14));
menuBar.add(mnOptionsMenu);

addFoldersButton = new Button("Add custom .minecraft folder locations");
addFoldersButton.addActionListener(new ActionListener() {
addFoldersButtonMenuItem = new JMenuItem("Add custom .minecraft folder locations");
addFoldersButtonMenuItem.setFont(new Font("Consolas", Font.PLAIN, 14));
addFoldersButtonMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
Expand All @@ -129,10 +135,17 @@ public void run() {
});
}
});
addFoldersButton.setBackground(new Color(255, 240, 245));
panel_north.add(addFoldersButton);
addFoldersButton.setForeground(new Color(153, 0, 102));
addFoldersButton.setFont(new Font("Consolas", Font.PLAIN, 14));
mnOptionsMenu.add(addFoldersButtonMenuItem);

stripColorCodesCheckBoxMenu = new JCheckBoxMenuItem("Strip color codes");
stripColorCodesCheckBoxMenu.setSelected(true);
stripColorCodesCheckBoxMenu.setFont(new Font("Consolas", Font.PLAIN, 14));
mnOptionsMenu.add(stripColorCodesCheckBoxMenu);

onlyChatCheckBoxMenu = new JCheckBoxMenuItem("Only filter chat lines");
onlyChatCheckBoxMenu.setFont(new Font("Consolas", Font.PLAIN, 14));
onlyChatCheckBoxMenu.setSelected(true);
mnOptionsMenu.add(onlyChatCheckBoxMenu);

horizontalGlue_1 = Box.createHorizontalGlue();
panel_north.add(horizontalGlue_1);
Expand Down Expand Up @@ -174,7 +187,19 @@ public void run() {
scrollPaneBottom.setViewportView(statusTextField);
}

private synchronized void analyze(boolean onlyChat) {
private void startAnalysis() {
defaultButton.setEnabled(false);
addFoldersButtonMenuItem.setEnabled(false);
Thread t0 = new Thread() {
@Override
public void run() {
mainWindow.analyze(onlyChatCheckBoxMenu.getState(), stripColorCodesCheckBoxMenu.getState());
}
};
t0.start();
}

private synchronized void analyze(boolean onlyChat, boolean stripColorCodes) {
try {
OSFileSystem fileSystem = new OSFileSystem(mainWindow);
ArrayList<File> minecraftLogFolders = fileSystem.lookForMinecraftLogFolders();
Expand Down Expand Up @@ -215,7 +240,7 @@ public int compare(File f1, File f2) {
addStatusTemporaryly(
"INFO: Loading " + fileCount + " files - " + (counter * 100 / fileCount) + "%");
try {
minecraftLogFile = new MCLogFile(logFile, getPreviousFileInFolder(counter, allFiles), onlyChat);
minecraftLogFile = new MCLogFile(logFile, getPreviousFileInFolder(counter, allFiles), onlyChat, stripColorCodes);
if (minecraftLogFile.getPlayerName() != null) {
lastLoginName = minecraftLogFile.getPlayerName();
playerNames.put(lastLoginName, playerNames.getOrDefault(lastLoginName, 0) + 1);
Expand Down Expand Up @@ -251,7 +276,7 @@ public int compare(File f1, File f2) {
setOutput(sb.toString());
addStatus("INFO: Found " + logRecords.size() + " results!");
defaultButton.setEnabled(true);
addFoldersButton.setEnabled(true);
addFoldersButtonMenuItem.setEnabled(true);
} catch (Exception e) {
addStatus("ERROR: " + e.toString());
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -281,18 +306,6 @@ public void run() {
});
}

private void startAnalysis() {
defaultButton.setEnabled(false);
addFoldersButton.setEnabled(false);
Thread t0 = new Thread() {
@Override
public void run() {
mainWindow.analyze(onlyChatCheckBox.getState());
}
};
t0.start();
}

/**
* Adds a new line of text to the outputTextField
*
Expand Down
10 changes: 7 additions & 3 deletions src/util/MCLogFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@

public class MCLogFile {
private static String tmpPlayerName; // needs to be static. Streams are weird...
private boolean stripColorCodes;
private long creationTime;
private Long startingTimeOfFile;
private String playerName;
private Stream<String> linesStream;

public MCLogFile(File logFile, File previousLogFile, boolean onlyChat) throws FileNotFoundException, IOException {
public MCLogFile(File logFile, File previousLogFile, boolean onlyChat, boolean stripColorCodes)
throws FileNotFoundException, IOException {
this.stripColorCodes = stripColorCodes;
creationTime = previousLogFile != null ? getCreationTime(previousLogFile) : getCreationTime(logFile) - 21600000;
startingTimeOfFile = null;
InputStream inputStream = new FileInputStream(logFile);
if (logFile.getName().endsWith(".gz"))
inputStream = new GZIPInputStream(inputStream);
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "ISO-8859-1"));
final String userSettingLine = "\\[[0-9:]{8}\\] \\[Client thread/INFO\\]: Setting user: ";
linesStream = br.lines().filter(a -> {
if (tmpPlayerName == null && a.matches(userSettingLine + ".*"))
Expand All @@ -53,7 +56,8 @@ public String getPlayerName() {
public List<MCLogLine> filterLines(String logLineFilterRegex, String lastPlayerName) {
List<MCLogLine> filteredlogLines = linesStream
.map(a -> new MCLogLine(getTime(creationTime, a.substring(1, 9)), lastPlayerName,
a.replaceAll("\\[[0-9:]{8}\\] \\[Client thread/INFO\\]: \\[CHAT\\] ", "").trim()))
a.replaceAll("\\[[0-9:]{8}\\] \\[Client thread/INFO\\]: \\[CHAT\\] ", "").trim(),
stripColorCodes))
.filter(a -> a.getText().matches(logLineFilterRegex)).collect(Collectors.toList());
linesStream.close();
return filteredlogLines;
Expand Down
4 changes: 2 additions & 2 deletions src/util/MCLogLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class MCLogLine implements Comparable<MCLogLine> {
private String playerName;
private String text;

public MCLogLine(long creationTime, String playerName, String text) {
public MCLogLine(long creationTime, String playerName, String text, boolean stripColorCodes) {
this.creationTime = creationTime;
this.playerName = playerName;
this.text = text;
this.text = stripColorCodes ? text.replaceAll("§.", "") : text;
}

public long getCreationTime() {
Expand Down

0 comments on commit 31a70f8

Please sign in to comment.