@@ -26,7 +26,6 @@ public static void main(String[] var0) throws IllegalArgumentException, IllegalA
2626 int maxRange = 255 ;
2727 int port = 25565 ;
2828
29- // TODO: Optimize all of this code.
3029 Logger logger = Logger .getLogger ("com.stupidrepo.mcscanner" );
3130
3231 float version = 1.14f ;
@@ -59,6 +58,7 @@ public static void main(String[] var0) throws IllegalArgumentException, IllegalA
5958 double progressThing = (maxRange - minimumRange + 1 ) * 256 * 256 ;
6059
6160 ArrayList < Thread > threadList = new ArrayList < Thread > ();
61+ ArrayList < String > ips = new ArrayList < String > ();
6262
6363 JLabel scannedLabel = new JLabel ("Scanned: 0/" + progressThing * 256 );
6464 scannedLabel .setHorizontalAlignment (0 );
@@ -69,31 +69,44 @@ public static void main(String[] var0) throws IllegalArgumentException, IllegalA
6969
7070 frame .setVisible (true );
7171
72+ // TODO: Optimise this code. (1)
73+
7274 ExecutorService executor = Executors .newFixedThreadPool (threads .get ());
7375
7476 for (int i = minimumRange ; i <= maxRange ; ++i ) {
7577 for (int j = 0 ; j <= 255 ; ++j ) {
7678 for (int k = 0 ; k <= 255 ; ++k ) {
7779 for (int l = 0 ; l <= 255 ; ++l ) {
7880 String ip = i + "." + j + "." + k + "." + l ;
79- Thread scannerThread = new Thread (new ScannerThread (ip , port , timeout , databaseHandler ));
80- threadList .add (scannerThread );
81- executor .execute (scannerThread );
82-
83- if (threadList .size () >= threads .get ()) {
84- for (Thread nextThread : threadList ) {
85- try {
86- nextThread .join ();
87- ++scanned ;
88- scannedLabel .setText ("Scanned: " + scanned + "/" + progressThing *256 + " (" + (scanned / (progressThing *256 )) * 100 + "%)" );
89- } catch (InterruptedException timeout2 ) {
90- // eh
91- }
92- }
93- threadList .clear ();
94- }
81+ ips .add (ip );
82+ }
83+ }
84+ }
85+ }
86+
87+ for (int i = 0 ; i < ips .size (); ++i ) {
88+ int randomIndex = (int )(Math .random () * ips .size ());
89+ String temp = ips .get (i );
90+ ips .set (i , ips .get (randomIndex ));
91+ ips .set (randomIndex , temp );
92+ }
93+
94+ for (String ip : ips ) {
95+ Thread scannerThread = new Thread (new ScannerThread (ip , port , timeout , databaseHandler ));
96+ threadList .add (scannerThread );
97+ executor .execute (scannerThread );
98+
99+ if (threadList .size () >= threads .get ()) {
100+ for (Thread nextThread : threadList ) {
101+ try {
102+ nextThread .join ();
103+ ++scanned ;
104+ scannedLabel .setText ("Scanned: " + scanned + "/" + progressThing *256 + " (" + (scanned / (progressThing *256 )) * 100 + "%)" );
105+ } catch (InterruptedException timeout2 ) {
106+ // eh
95107 }
96108 }
109+ threadList .clear ();
97110 }
98111 }
99112
0 commit comments