Skip to content

Commit 00937bd

Browse files
committed
v1.20
1 parent 3f5a2e3 commit 00937bd

File tree

5 files changed

+96
-43
lines changed

5 files changed

+96
-43
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
All notable changes to this project will be documented in `CHANGELOG.md`.
33

44
## Added
5-
Nothing has been added.
5+
* Search bar to the Server List.
6+
* Ability to search by IP, MOTD, Version and Max Players!
7+
* `PlaceholderText` for easy `JTextField` placeholder text.
8+
* `DatabaseHandler.updateServerByIPInDB()` for updating a server in the DB by it's IP.
69

710
## Modified
8-
* `ServerList` refreshes every 10 seconds.
9-
* Fixed bug where when an offset got to ~252-255, it would stay there and miss out on ***a lot*** of IPs.
11+
* Disable selection and editing on the JTable/Server List.
1012

1113
## Removed
12-
* 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.
14+
* The thing that tells you how many IPs are left to scan.
15+
* It was inaccurate and I'm pretty sure getting & drawing that number to the screen made it lag a lot.
16+
* Clicking a row to copy it's IP.
17+
* `DatabaseHandler.writeDetailsToDB(String ip, String motd, Integer maxPlayers)` because I can just make the version number report "1.6<="
1318

1419
## TODOs
1520
- [ ] Optimise IP generation and initial scanning code.[¹][1]

src/com/stupidrepo/mcscanner/DatabaseHandler.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ public void writeDetailsToDB(String ip, String version, String motd, int maxPlay
6565
}
6666
}
6767

68-
public void writeDetailsToDB(String ip, String motd, int maxPlayers) {
68+
public void updateServerByIPInDB(String ip, String version, String motd, int maxPlayers) {
6969
try {
70-
mainCollection
71-
.insertOne(
72-
new Document("ip", ip)
73-
.append("motd", motd)
74-
.append("maxPlayers", maxPlayers)
75-
);
76-
logger.log(Level.WARNING, "[LEGACY!] Added " + ip + " to database.");
70+
mainCollection.updateOne(
71+
new Document("ip", ip),
72+
new Document("$set", new Document("version", version)
73+
.append("motd", motd)
74+
.append("maxPlayers", maxPlayers))
75+
);
76+
logger.log(Level.INFO, "Updated " + ip + " in database.");
7777
} catch (Exception e) {
78-
logger.log(Level.SEVERE, "[LEGACY!] Failed to add " + ip + " to database.");
78+
logger.log(Level.SEVERE, "Failed to update " + ip + " in database.");
7979
}
8080
}
8181

src/com/stupidrepo/mcscanner/MCScanner.java

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.stupidrepo.mcscanner;
22

3+
import org.bson.Document;
4+
5+
import javax.swing.*;
6+
import javax.swing.table.DefaultTableModel;
7+
import javax.swing.table.TableModel;
8+
import javax.swing.table.TableRowSorter;
39
import java.awt.*;
4-
import java.awt.datatransfer.Clipboard;
5-
import java.awt.datatransfer.StringSelection;
610
import java.awt.event.ActionListener;
711
import java.awt.event.WindowEvent;
812
import java.io.*;
@@ -14,12 +18,7 @@
1418
import java.util.concurrent.atomic.AtomicReference;
1519
import java.util.logging.Level;
1620
import java.util.logging.Logger;
17-
import javax.swing.*;
18-
import javax.swing.table.DefaultTableModel;
19-
import javax.swing.table.TableModel;
20-
import javax.swing.table.TableRowSorter;
21-
22-
import org.bson.Document;
21+
import java.util.regex.PatternSyntaxException;
2322

2423
public class MCScanner {
2524
private static int offsetI = 1;
@@ -37,7 +36,7 @@ public static void main(String[] var0) {
3736

3837
Logger logger = Logger.getLogger("com.stupidrepo.mcscanner");
3938

40-
float version = 1.19f;
39+
float version = 1.20f;
4140

4241
AtomicReference<String> uri = new AtomicReference<>("mongodb://localhost:27017");
4342

@@ -134,13 +133,13 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) {
134133
int thisOffsetJ = offsetJ;
135134
int thisOffsetK = offsetK;
136135
int thisOffsetL = offsetL;
137-
for (int i = 0; i <= (maxRange-thisOffsetI); ++i) {
136+
for (int i = minimumRange; i <= (maxRange-thisOffsetI); ++i) {
138137
if(stopping) {
139138
break;
140139
} else {
141140
offsetI = i;
142141
}
143-
for (int j = minimumRange; j <= (255-thisOffsetJ); ++j) {
142+
for (int j = 0; j <= (255-thisOffsetJ); ++j) {
144143
if(stopping) {
145144
break;
146145
} else {
@@ -217,9 +216,6 @@ public ScannerThread(String ip, int port, int timeout, DatabaseHandler dbHandler
217216

218217
public void run() {
219218
try {
220-
if(dbHandler.isIPInDB(ip)) {
221-
return;
222-
}
223219
Socket socket = new Socket();
224220
socket.connect(new InetSocketAddress(this.ip, this.port), this.timeout);
225221

@@ -261,11 +257,19 @@ public void run() {
261257
if (string.startsWith("§")) {
262258
data = string.split("\0");
263259

264-
dbHandler.writeDetailsToDB(ip, data[2], data[3], Integer.parseInt(data[5]));
260+
if(dbHandler.isIPInDB(ip)) {
261+
dbHandler.updateServerByIPInDB(ip, data[2], data[3], Integer.parseInt(data[5]));
262+
} else {
263+
dbHandler.writeDetailsToDB(ip, data[2], data[3], Integer.parseInt(data[5]));
264+
}
265265
} else {
266266
data = string.split("§");
267267

268-
dbHandler.writeDetailsToDB(ip, data[0], Integer.parseInt(data[2]));
268+
if(dbHandler.isIPInDB(ip)) {
269+
dbHandler.updateServerByIPInDB(ip, "1.6<=", data[0], Integer.parseInt(data[2]));
270+
} else {
271+
dbHandler.writeDetailsToDB(ip, "1.6<=", data[0], Integer.parseInt(data[2]));
272+
}
269273
}
270274

271275
socket.close();
@@ -314,6 +318,31 @@ public ServerList(DatabaseHandler dbHandler) {
314318
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
315319
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
316320

321+
JTextField searchBar = new JTextField();
322+
searchBar.setHorizontalAlignment(0);
323+
searchBar.setToolTipText("Search");
324+
searchBar.addFocusListener(new PlaceholderText("Search", searchBar).getFocusAdapter());
325+
326+
JComboBox<String> searchBy = new JComboBox<String>();
327+
searchBy.addItem("Sort By: IP");
328+
searchBy.addItem("Sort By: MOTD");
329+
searchBy.addItem("Sort By: Version");
330+
searchBy.addItem("Sort By: Max Players");
331+
searchBy.setSelectedIndex(2);
332+
333+
searchBar.addActionListener(e -> {
334+
String text = searchBar.getText();
335+
if(text.length() > 0) {
336+
try {
337+
((TableRowSorter<TableModel>) table.getRowSorter()).setRowFilter(RowFilter.regexFilter(text, searchBy.getSelectedIndex()));
338+
} catch (PatternSyntaxException pse) {
339+
JOptionPane.showMessageDialog(null, "Invalid search query/regex expression!", "Error", JOptionPane.ERROR_MESSAGE);
340+
}
341+
} else {
342+
((TableRowSorter<TableModel>) table.getRowSorter()).setRowFilter(null);
343+
}
344+
});
345+
317346
Timer timer = new Timer(10000, e -> {
318347
((DefaultTableModel) table.getModel()).setRowCount(0);
319348
ArrayList < Document > documents1 = this.dbHandler.getServers();
@@ -333,24 +362,12 @@ public ServerList(DatabaseHandler dbHandler) {
333362
timer.start();
334363
timer.getListeners(ActionListener.class)[0].actionPerformed(null);
335364

336-
// copy IP to clipboard when clicked
337-
table.addMouseListener(new java.awt.event.MouseAdapter() {
338-
@Override
339-
public void mouseClicked(java.awt.event.MouseEvent evt) {
340-
int row = table.rowAtPoint(evt.getPoint());
341-
String ip = table.getModel().getValueAt(row, 0).toString();
342-
StringSelection stringSelection = new StringSelection(ip);
343-
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
344-
clipboard.setContents(stringSelection, null);
345-
// make message box appear that saying "copied ip to clipboard"
346-
JOptionPane.showMessageDialog(null, "Copied " + ip + " to clipboard!", "Copied!", JOptionPane.INFORMATION_MESSAGE);
347-
}
348-
});
349-
350365
TableRowSorter < TableModel > sorter = new TableRowSorter < > (table.getModel());
351366
table.setRowSorter(sorter);
352367

353368
this.frame.add(scrollPane, BorderLayout.CENTER);
369+
this.frame.add(searchBy, BorderLayout.SOUTH);
370+
this.frame.add(searchBar, BorderLayout.NORTH);
354371
}
355372

356373
public boolean toggleGUI() {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.stupidrepo.mcscanner;
2+
3+
import javax.swing.*;
4+
import java.awt.event.FocusAdapter;
5+
6+
public class PlaceholderText {
7+
private final String text;
8+
private String lastText = "";
9+
private final JTextField textField;
10+
11+
public PlaceholderText(String text, JTextField textField) {
12+
this.text = text;
13+
this.textField = textField;
14+
}
15+
16+
public FocusAdapter getFocusAdapter() {
17+
return new FocusAdapter() {
18+
public void focusGained(java.awt.event.FocusEvent evt) {
19+
textField.setText(lastText);
20+
}
21+
22+
public void focusLost(java.awt.event.FocusEvent evt) {
23+
lastText = textField.getText();
24+
if (textField.getText().isEmpty()) {
25+
textField.setText(text);
26+
}
27+
}
28+
};
29+
}
30+
}

0 commit comments

Comments
 (0)