Skip to content

Commit 57c4431

Browse files
author
Petar Kresoja
committed
Various code optimisations
1 parent 40bb606 commit 57c4431

File tree

10 files changed

+102
-52
lines changed

10 files changed

+102
-52
lines changed

build/built-jar.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Sat, 11 Apr 2020 21:07:01 +0200
1+
#Tue, 07 Jul 2020 03:47:28 +0200
22

33

44
C\:\\Users\\petar\\Documents\\NetBeansProjects\\JavaRcon=
3.61 KB
Binary file not shown.

build/classes/javarcon/JavaRcon.class

-1.85 KB
Binary file not shown.

config.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#JavaRcon configuration file.
2+
#Created by: Pequla ( https://pequla.github.io/ )
3+
#Tue Jul 07 03:22:40 CEST 2020
4+
rcon.password=password
5+
rcon.port=25575
6+
rcon.host=localhost

dist/JavaRcon.jar

1.89 KB
Binary file not shown.

lib/rkon-core-1.1.2.jar

5.87 KB
Binary file not shown.

nbproject/private/private.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
33
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
44
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
5-
<group>
6-
<file>file:/C:/Users/petar/Documents/NetBeansProjects/JavaRcon/src/javarcon/JavaRcon.java</file>
7-
</group>
5+
<group/>
86
</open-files>
97
</project-private>

nbproject/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dist.jar=${dist.dir}/JavaRcon.jar
2929
dist.javadoc.dir=${dist.dir}/javadoc
3030
endorsed.classpath=
3131
excludes=
32-
file.reference.rkon-core-1.1.2.jar=C:\\Users\\petar\\Documents\\Libraries\\rkon-core-1.1.2.jar
32+
file.reference.rkon-core-1.1.2.jar=lib/rkon-core-1.1.2.jar
3333
includes=**
3434
jar.compress=false
3535
javac.classpath=\

src/javarcon/CustomRcon.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package javarcon;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.net.Socket;
10+
import java.util.Properties;
11+
import net.kronos.rkon.core.Rcon;
12+
import net.kronos.rkon.core.ex.AuthenticationException;
13+
14+
public class CustomRcon extends Rcon {
15+
16+
private static CustomRcon instance;
17+
18+
private CustomRcon(String host, int port, byte[] password) throws IOException, AuthenticationException {
19+
super(host, port, password);
20+
}
21+
22+
public static CustomRcon getInstance() throws IOException, AuthenticationException {
23+
if (instance == null) {
24+
25+
//Variable declaration
26+
String host;
27+
int port;
28+
byte[] password;
29+
30+
//Reading configuration from file
31+
File config = new File("config.properties");
32+
Properties prop = new Properties();
33+
if (!config.exists()) {
34+
try (OutputStream output = new FileOutputStream(config)) {
35+
prop.setProperty("rcon.host", "localhost");
36+
prop.setProperty("rcon.port", "25575");
37+
prop.setProperty("rcon.password", "password");
38+
prop.store(output, "JavaRcon configuration file." + System.lineSeparator() + "Created by: Pequla ( https://pequla.github.io/ )");
39+
}
40+
}
41+
try (InputStream input = new FileInputStream(config)) {
42+
prop.load(input);
43+
host = prop.getProperty("rcon.host", "localhost");
44+
port = Integer.valueOf(prop.getProperty("rcon.port", "25575"));
45+
password = prop.getProperty("rcon.password", "password").getBytes();
46+
}
47+
48+
//Constructing the instance
49+
instance = new CustomRcon(host, port, password);
50+
}
51+
//Returns the instance
52+
return instance;
53+
}
54+
55+
//Short-circuit evaluation
56+
private static boolean empty(final String s) {
57+
return s == null || s.trim().isEmpty();
58+
}
59+
60+
@Override
61+
public String command(String payload) throws IOException {
62+
String uc = "Unknown command";
63+
if (!empty(payload)) {
64+
String response = super.command(payload);
65+
if (empty(response)) {
66+
return "No command response";
67+
}
68+
if (response.startsWith(uc)) {
69+
return uc;
70+
}
71+
return response;
72+
}
73+
return uc;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
Socket s = getSocket();
79+
return s.getInetAddress() + ":" + s.getLocalPort();
80+
}
81+
82+
}

src/javarcon/JavaRcon.java

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,22 @@
11
package javarcon;
22

3-
import java.io.File;
4-
import java.io.FileInputStream;
5-
import java.io.FileOutputStream;
63
import java.io.IOException;
7-
import java.io.InputStream;
8-
import java.io.OutputStream;
9-
import java.util.Properties;
104
import java.util.Scanner;
11-
import net.kronos.rkon.core.Rcon;
125
import net.kronos.rkon.core.ex.AuthenticationException;
136

14-
/**
15-
*
16-
* @author Petar Kresoja
17-
*/
187
public class JavaRcon {
198

20-
private static Rcon rcon = null;
9+
private static CustomRcon rcon;
2110

2211
public static void main(String[] args) {
23-
System.out.println("JavaRcon started...");
24-
System.out.println("To exit type in logout");
25-
2612
Scanner scn = new Scanner(System.in);
2713
String clientInput;
28-
String response;
29-
String host = null;
30-
int port = 0;
31-
byte[] password = null;
3214

3315
try {
34-
File config = new File("config.properties");
35-
Properties prop = new Properties();
36-
if (!config.exists()) {
37-
try (OutputStream output = new FileOutputStream(config)) {
38-
prop.setProperty("rcon.host", "localhost");
39-
prop.setProperty("rcon.port", "25575");
40-
prop.setProperty("rcon.password", "password");
41-
prop.store(output, "JavaRcon configuration file." + System.lineSeparator() + "Created by: Pequla ( https://pequla.github.io/ )");
42-
}
43-
}
44-
try (InputStream input = new FileInputStream(config)) {
45-
prop.load(input);
46-
host = prop.getProperty("rcon.host");
47-
port = Integer.valueOf(prop.getProperty("rcon.port"));
48-
password = prop.getProperty("rcon.password").getBytes();
49-
}
50-
} catch (IOException ex) {
51-
ex.printStackTrace(System.out);
52-
}
16+
rcon = CustomRcon.getInstance();
17+
System.out.println("Connected to: " + rcon);
18+
System.out.println("To exit type in logout!");
5319

54-
try {
55-
rcon = new Rcon(host, port, password);
5620
while (true) {
5721
System.out.print("> ");
5822
clientInput = scn.nextLine().trim();
@@ -61,23 +25,23 @@ public static void main(String[] args) {
6125
disconnect();
6226
break;
6327
case "stop":
64-
System.out.println("Do you really want to stop the server ?");
28+
System.out.println("Do you really want to stop the server?");
6529
System.out.print("Answer: (Y/N) > ");
66-
if (scn.nextLine().trim().toUpperCase().equals("Y")) {
30+
String answer = scn.nextLine().trim();
31+
if (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")) {
6732
rcon.command(clientInput);
6833
disconnect();
34+
} else {
35+
System.out.println("Will not stop the server!");
6936
}
7037
break;
7138
default:
72-
response = rcon.command(clientInput);
73-
if (response.length() != 0) {
74-
System.out.println(response);
75-
}
39+
System.out.println(rcon.command(clientInput));
7640
break;
7741
}
7842
}
7943
} catch (IOException ex) {
80-
System.err.println("There was a communication error, " + ex.getMessage());
44+
System.err.println("There was an I/O error, " + ex.getMessage());
8145
} catch (AuthenticationException ex) {
8246
System.err.println("Wrong rcon password, " + ex.getMessage());
8347
}

0 commit comments

Comments
 (0)