From 00937bdc45ef2a52db9b899dfcc5965f6b680711 Mon Sep 17 00:00:00 2001 From: Bradlee Barnes <69256931+StupidRepo@users.noreply.github.com> Date: Thu, 20 Jul 2023 18:22:46 +0100 Subject: [PATCH] v1.20 --- .idea/inspectionProfiles/Project_Default.xml | 1 + CHANGELOG.md | 13 +++- .../stupidrepo/mcscanner/DatabaseHandler.java | 18 ++--- src/com/stupidrepo/mcscanner/MCScanner.java | 77 +++++++++++-------- .../stupidrepo/mcscanner/PlaceholderText.java | 30 ++++++++ 5 files changed, 96 insertions(+), 43 deletions(-) create mode 100644 src/com/stupidrepo/mcscanner/PlaceholderText.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 7405c3a..8d75a9b 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -2,5 +2,6 @@ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index e57910c..7ea2939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,19 @@ All notable changes to this project will be documented in `CHANGELOG.md`. ## Added -Nothing has been added. +* Search bar to the Server List. +* Ability to search by IP, MOTD, Version and Max Players! +* `PlaceholderText` for easy `JTextField` placeholder text. +* `DatabaseHandler.updateServerByIPInDB()` for updating a server in the DB by it's IP. ## Modified -* `ServerList` refreshes every 10 seconds. -* Fixed bug where when an offset got to ~252-255, it would stay there and miss out on ***a lot*** of IPs. +* Disable selection and editing on the JTable/Server List. ## Removed -* The thing that tells you how many IPs are left to scan. It was inaccurate and I'm pretty sure getting & drawing that number to the screen made it lag a lot. +* The thing that tells you how many IPs are left to scan. + * It was inaccurate and I'm pretty sure getting & drawing that number to the screen made it lag a lot. +* Clicking a row to copy it's IP. +* `DatabaseHandler.writeDetailsToDB(String ip, String motd, Integer maxPlayers)` because I can just make the version number report "1.6<=" ## TODOs - [ ] Optimise IP generation and initial scanning code.[¹][1] diff --git a/src/com/stupidrepo/mcscanner/DatabaseHandler.java b/src/com/stupidrepo/mcscanner/DatabaseHandler.java index d9a3dfc..f0c32b8 100644 --- a/src/com/stupidrepo/mcscanner/DatabaseHandler.java +++ b/src/com/stupidrepo/mcscanner/DatabaseHandler.java @@ -65,17 +65,17 @@ public void writeDetailsToDB(String ip, String version, String motd, int maxPlay } } - public void writeDetailsToDB(String ip, String motd, int maxPlayers) { + public void updateServerByIPInDB(String ip, String version, String motd, int maxPlayers) { try { - mainCollection - .insertOne( - new Document("ip", ip) - .append("motd", motd) - .append("maxPlayers", maxPlayers) - ); - logger.log(Level.WARNING, "[LEGACY!] Added " + ip + " to database."); + mainCollection.updateOne( + new Document("ip", ip), + new Document("$set", new Document("version", version) + .append("motd", motd) + .append("maxPlayers", maxPlayers)) + ); + logger.log(Level.INFO, "Updated " + ip + " in database."); } catch (Exception e) { - logger.log(Level.SEVERE, "[LEGACY!] Failed to add " + ip + " to database."); + logger.log(Level.SEVERE, "Failed to update " + ip + " in database."); } } diff --git a/src/com/stupidrepo/mcscanner/MCScanner.java b/src/com/stupidrepo/mcscanner/MCScanner.java index 8d76a37..e11130d 100644 --- a/src/com/stupidrepo/mcscanner/MCScanner.java +++ b/src/com/stupidrepo/mcscanner/MCScanner.java @@ -1,8 +1,12 @@ package com.stupidrepo.mcscanner; +import org.bson.Document; + +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableModel; +import javax.swing.table.TableRowSorter; import java.awt.*; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.StringSelection; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.io.*; @@ -14,12 +18,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; -import javax.swing.*; -import javax.swing.table.DefaultTableModel; -import javax.swing.table.TableModel; -import javax.swing.table.TableRowSorter; - -import org.bson.Document; +import java.util.regex.PatternSyntaxException; public class MCScanner { private static int offsetI = 1; @@ -37,7 +36,7 @@ public static void main(String[] var0) { Logger logger = Logger.getLogger("com.stupidrepo.mcscanner"); - float version = 1.19f; + float version = 1.20f; AtomicReference uri = new AtomicReference<>("mongodb://localhost:27017"); @@ -134,13 +133,13 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) { int thisOffsetJ = offsetJ; int thisOffsetK = offsetK; int thisOffsetL = offsetL; - for (int i = 0; i <= (maxRange-thisOffsetI); ++i) { + for (int i = minimumRange; i <= (maxRange-thisOffsetI); ++i) { if(stopping) { break; } else { offsetI = i; } - for (int j = minimumRange; j <= (255-thisOffsetJ); ++j) { + for (int j = 0; j <= (255-thisOffsetJ); ++j) { if(stopping) { break; } else { @@ -217,9 +216,6 @@ public ScannerThread(String ip, int port, int timeout, DatabaseHandler dbHandler public void run() { try { - if(dbHandler.isIPInDB(ip)) { - return; - } Socket socket = new Socket(); socket.connect(new InetSocketAddress(this.ip, this.port), this.timeout); @@ -261,11 +257,19 @@ public void run() { if (string.startsWith("§")) { data = string.split("\0"); - dbHandler.writeDetailsToDB(ip, data[2], data[3], Integer.parseInt(data[5])); + if(dbHandler.isIPInDB(ip)) { + dbHandler.updateServerByIPInDB(ip, data[2], data[3], Integer.parseInt(data[5])); + } else { + dbHandler.writeDetailsToDB(ip, data[2], data[3], Integer.parseInt(data[5])); + } } else { data = string.split("§"); - dbHandler.writeDetailsToDB(ip, data[0], Integer.parseInt(data[2])); + if(dbHandler.isIPInDB(ip)) { + dbHandler.updateServerByIPInDB(ip, "1.6<=", data[0], Integer.parseInt(data[2])); + } else { + dbHandler.writeDetailsToDB(ip, "1.6<=", data[0], Integer.parseInt(data[2])); + } } socket.close(); @@ -314,6 +318,31 @@ public ServerList(DatabaseHandler dbHandler) { scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); + JTextField searchBar = new JTextField(); + searchBar.setHorizontalAlignment(0); + searchBar.setToolTipText("Search"); + searchBar.addFocusListener(new PlaceholderText("Search", searchBar).getFocusAdapter()); + + JComboBox searchBy = new JComboBox(); + searchBy.addItem("Sort By: IP"); + searchBy.addItem("Sort By: MOTD"); + searchBy.addItem("Sort By: Version"); + searchBy.addItem("Sort By: Max Players"); + searchBy.setSelectedIndex(2); + + searchBar.addActionListener(e -> { + String text = searchBar.getText(); + if(text.length() > 0) { + try { + ((TableRowSorter) table.getRowSorter()).setRowFilter(RowFilter.regexFilter(text, searchBy.getSelectedIndex())); + } catch (PatternSyntaxException pse) { + JOptionPane.showMessageDialog(null, "Invalid search query/regex expression!", "Error", JOptionPane.ERROR_MESSAGE); + } + } else { + ((TableRowSorter) table.getRowSorter()).setRowFilter(null); + } + }); + Timer timer = new Timer(10000, e -> { ((DefaultTableModel) table.getModel()).setRowCount(0); ArrayList < Document > documents1 = this.dbHandler.getServers(); @@ -333,24 +362,12 @@ public ServerList(DatabaseHandler dbHandler) { timer.start(); timer.getListeners(ActionListener.class)[0].actionPerformed(null); - // copy IP to clipboard when clicked - table.addMouseListener(new java.awt.event.MouseAdapter() { - @Override - public void mouseClicked(java.awt.event.MouseEvent evt) { - int row = table.rowAtPoint(evt.getPoint()); - String ip = table.getModel().getValueAt(row, 0).toString(); - StringSelection stringSelection = new StringSelection(ip); - Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); - clipboard.setContents(stringSelection, null); - // make message box appear that saying "copied ip to clipboard" - JOptionPane.showMessageDialog(null, "Copied " + ip + " to clipboard!", "Copied!", JOptionPane.INFORMATION_MESSAGE); - } - }); - TableRowSorter < TableModel > sorter = new TableRowSorter < > (table.getModel()); table.setRowSorter(sorter); this.frame.add(scrollPane, BorderLayout.CENTER); + this.frame.add(searchBy, BorderLayout.SOUTH); + this.frame.add(searchBar, BorderLayout.NORTH); } public boolean toggleGUI() { diff --git a/src/com/stupidrepo/mcscanner/PlaceholderText.java b/src/com/stupidrepo/mcscanner/PlaceholderText.java new file mode 100644 index 0000000..4c430cd --- /dev/null +++ b/src/com/stupidrepo/mcscanner/PlaceholderText.java @@ -0,0 +1,30 @@ +package com.stupidrepo.mcscanner; + +import javax.swing.*; +import java.awt.event.FocusAdapter; + +public class PlaceholderText { + private final String text; + private String lastText = ""; + private final JTextField textField; + + public PlaceholderText(String text, JTextField textField) { + this.text = text; + this.textField = textField; + } + + public FocusAdapter getFocusAdapter() { + return new FocusAdapter() { + public void focusGained(java.awt.event.FocusEvent evt) { + textField.setText(lastText); + } + + public void focusLost(java.awt.event.FocusEvent evt) { + lastText = textField.getText(); + if (textField.getText().isEmpty()) { + textField.setText(text); + } + } + }; + } +}