Skip to content

Commit

Permalink
v1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
StupidRepo committed Jul 20, 2023
1 parent 3f5a2e3 commit 00937bd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 43 deletions.
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 9 additions & 9 deletions src/com/stupidrepo/mcscanner/DatabaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

Expand Down
77 changes: 47 additions & 30 deletions src/com/stupidrepo/mcscanner/MCScanner.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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;
Expand All @@ -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<String> uri = new AtomicReference<>("mongodb://localhost:27017");

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<String> searchBy = new JComboBox<String>();
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<TableModel>) 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<TableModel>) table.getRowSorter()).setRowFilter(null);
}
});

Timer timer = new Timer(10000, e -> {
((DefaultTableModel) table.getModel()).setRowCount(0);
ArrayList < Document > documents1 = this.dbHandler.getServers();
Expand All @@ -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() {
Expand Down
30 changes: 30 additions & 0 deletions src/com/stupidrepo/mcscanner/PlaceholderText.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
}
}

0 comments on commit 00937bd

Please sign in to comment.