Skip to content

Commit bc5540f

Browse files
bot now always grabs next message instead of latest
1 parent b8739c8 commit bc5540f

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/build/
44
/out/
55
/out/production/classes/
6-
/0latest.log
7-
/1latest.log
6+
/*latest.log
87
/settings.txt
98
/.idea/
109
/gradle/
@@ -14,3 +13,4 @@
1413
/settings.gradle
1514
/logs/
1615
*.xcf
16+
.gitignore

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'org.example'
7-
version '1.6'
7+
version '1.7'
88

99
repositories {
1010
mavenCentral()

src/main/java/dain/MinecraftChatBridge.java

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
public class MinecraftChatBridge implements Runnable {
1313

1414
private final TextChannel [] channels = new TextChannel[Settings.DISCORD_CHANNEL_IDS.length];
15-
private String [] lastLines = { "", "" };
16-
private String [] previousLines = { "", "" };
17-
private final JDA jda;
18-
private int id;
15+
private String line = "";
16+
private int previousLineNumber = 0;
17+
private boolean serverInitIsDone = false;
1918

20-
private int requestID = new Random().nextInt(Integer.MAX_VALUE);
19+
private final JDA jda;
20+
private final int id;
2121

2222
public MinecraftChatBridge(Bot botIn, int idIn) {
2323
jda = botIn.getJda();
2424
id = idIn;
25+
2526
for(int i = 0; i < Settings.DISCORD_CHANNEL_IDS.length; i++) {
2627
channels[i] = jda.getTextChannelById(Settings.DISCORD_CHANNEL_IDS[i]);
2728
}
@@ -30,53 +31,63 @@ public MinecraftChatBridge(Bot botIn, int idIn) {
3031
@Override
3132
public void run() {
3233
while(true) {
33-
sendNewMessages();
34+
if (!Settings.isLocal) FTPHandler.grabFile("/logs/latest.log", id); // if its local dont do that
35+
36+
boolean shouldContinue;
37+
do {
38+
shouldContinue = sendNewMessage();
39+
} while(shouldContinue); // sendNewMessages returns false once it reaches the end of the file
3440
try {
35-
Thread.sleep(10);
41+
Thread.sleep(100);
3642
} catch (InterruptedException e) {
3743
Logger.log("thread interrupted while sleeping", Logger.LoggingLevel.FATAL);
3844
System.exit(-1);
3945
}
4046
}
4147
}
4248

43-
public void sendNewMessages() {
44-
//sendMessageAllChannels("thread " + id + " sending new messages");
45-
46-
if (!Settings.isLocal) FTPHandler.grabFile("/logs/latest.log", id); // if its local dont do that
47-
49+
public boolean sendNewMessage() {
4850
try {
4951
File file = (Settings.isLocal ? new File("logs/latest.log") : new File(id + "latest.log")); // if its local grab from logs directory
50-
Scanner myReader = new Scanner(file);
52+
Scanner scanner = new Scanner(file);
5153

52-
while (myReader.hasNextLine()) { //todo change to detect new lines instead of most recent line
53-
lastLines[id] = myReader.nextLine();
54+
for (int i = 0; i < previousLineNumber; i++) {
55+
if (scanner.hasNext()) {
56+
line = scanner.nextLine();
57+
} else return false;
5458
}
5559

56-
myReader.close();
57-
} catch (FileNotFoundException e) {
60+
previousLineNumber++;
5861

62+
scanner.close();
63+
} catch (FileNotFoundException e) {
5964
Logger.log("File not found. Where is latest.log?", Logger.LoggingLevel.ERROR);
6065
e.printStackTrace();
6166
}
62-
if (lastLines[id].equals(previousLines[id])) return;
63-
previousLines[id] = lastLines[id];
64-
65-
//sendMessageAllChannels(lastLines[id]);
6667

6768
// only grabs stuff after the "[Server thread/INFO]: "
6869
try {
69-
lastLines[id] = lastLines[id].split("\\[Server thread/INFO]: ")[1];
70+
line = line.split("\\[Server thread/INFO]: ")[1];
7071
} catch (IndexOutOfBoundsException e) {
71-
return;
72+
return true;
7273
}
73-
Logger.log("Message received from Minecraft: " + lastLines[id], Logger.LoggingLevel.INFO);
74+
// if the message has square brackets or curly braces in dont send
75+
if (line.matches("(.*\\[.*].*)|(.*\\{.*}.*)")) return true;
76+
// if the message has no letters dont send
77+
if (!line.matches(".*\\w.*")) return true;
78+
// once you receive the 'Dont (X.XXXs)! For help, type "/help"' message start sending messages
79+
if (line.matches("Done \\(\\d+\\.\\d*s\\)!.*")) serverInitIsDone = true;
80+
if (!serverInitIsDone) return true;
81+
82+
Logger.log("Message received from Minecraft: " + line, Logger.LoggingLevel.INFO);
7483

75-
String processedLine = processLine(lastLines[id]);
84+
String processedLine = processLine(line);
7685

7786
sendMessageAllChannels(processedLine);
7887

7988
Logger.log("Message sent to Discord: " + processedLine, Logger.LoggingLevel.INFO);
89+
90+
return true;
8091
}
8192

8293
private void sendMessageAllChannels(String message) {
@@ -87,8 +98,8 @@ private void sendMessageAllChannels(String message) {
8798

8899
private String processLine(String line) {
89100

90-
// replace ipv4 IP addresses with "[IP ADDRESS REDACTED]"
91-
line = line.replaceAll("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", "[IP ADDRESS REDACTED]");
101+
// replace ipv4 IP addresses + port with "[IP ADDRESS REDACTED]"
102+
line = line.replaceAll("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):\\d{1,5}\\b", "[IP ADDRESS REDACTED]");
92103

93104
//break the message up into an array of strings for each word
94105
String[] lineArray = line.split(" ");
@@ -139,7 +150,9 @@ private String processLine(String line) {
139150
}
140151

141152
//say if its coming from smp or cmp
142-
line = "[" + Settings.SERVER_NAMES[id] + "] " + line;
153+
if (Settings.SERVER_NAMES.length > 1) {
154+
line = "[" + Settings.SERVER_NAMES[id] + "] " + line;
155+
}
143156

144157
if (isChatMessage) {
145158
sendMessageAllOtherServers(author, message);
@@ -212,9 +225,7 @@ public static void sendCommandToServer(String command, int serverId) {
212225
Rcon rcon = new Rcon(Settings.SERVER_IPS[serverId], Integer.decode(Settings.RCON_PORTS[serverId]), Settings.RCON_PASSWORDS[serverId].getBytes());
213226
rcon.command(command);
214227
rcon.disconnect();
215-
} catch (IOException e) {
216-
e.printStackTrace();
217-
} catch (AuthenticationException e) {
228+
} catch (IOException | AuthenticationException e) {
218229
e.printStackTrace();
219230
}
220231
}

0 commit comments

Comments
 (0)