Skip to content

Commit 648fc55

Browse files
if you have multiple servers it now also links messages between those servers, & added the "score" command in Minecraft to allow any player to change scoreboards
1 parent baa941e commit 648fc55

File tree

4 files changed

+287
-145
lines changed

4 files changed

+287
-145
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package dain;
2+
3+
import dain.events.DiscordMessageReceiver;
4+
import net.dv8tion.jda.api.events.Event;
5+
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
6+
7+
public class CommandHandler {
8+
9+
public static void handleDiscordCommand(String commandName, String[] parameters, MessageReceivedEvent event) {
10+
11+
switch (commandName) {
12+
case "help":
13+
case "h":
14+
runDiscordHelp(event);
15+
break;
16+
case "score":
17+
case "s":
18+
runDiscordScore(event);
19+
break;
20+
case "kill":
21+
case "k":
22+
runDiscordKill(event);
23+
break;
24+
default:
25+
event.getChannel().sendMessage("Unknown command. Try \"" + Settings.COMMAND_PREFIX + "help\" for options.").queue();
26+
break;
27+
}
28+
}
29+
30+
public static void handleMcCommand(String commandName, String[] parameters, int serverId) {
31+
32+
33+
34+
switch (commandName) {
35+
case "score":
36+
case "s":
37+
runMcScore(parameters, serverId);
38+
}
39+
}
40+
41+
private static void runMcScore(String[] parameters, int serverId) {
42+
if (parameters.length == 1) {
43+
if (parameters[0].equals("clear")) {
44+
MinecraftChatBridge.sendCommandToServer("scoreboard objectives setDisplay sidebar", serverId);
45+
} else {
46+
MinecraftChatBridge.sendCommandToServer("scoreboard objectives setDisplay sidebar " + parameters[0], serverId);
47+
}
48+
} else if (parameters.length == 0) {
49+
MinecraftChatBridge.sendCommandToServer("scoreboard objectives setDisplay sidebar", serverId);
50+
} else {
51+
MinecraftChatBridge.sendMessageAllServers("prodlem,,. Please provide a command in the form: " + Settings.COMMAND_PREFIX + "s [scoreboard name]");
52+
}
53+
}
54+
55+
private static void runDiscordKill(MessageReceivedEvent event) {
56+
String messageAuthor = event.getAuthor().getName();
57+
event.getChannel().sendMessage("*" + messageAuthor + " went*").queue();
58+
}
59+
60+
private static void runDiscordScore(MessageReceivedEvent event) { //TODO
61+
event.getChannel().sendMessage("soon:tm:").queue();
62+
63+
/*
64+
int port = 21;
65+
66+
FTPClient ftpClient = new FTPClient();
67+
try {
68+
69+
ftpClient.connect(Settings.SERVER_IP, port);
70+
ftpClient.login(Settings.FTP_USERNAME, Settings.FTP_PASSWORD);
71+
ftpClient.enterLocalPassiveMode();
72+
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
73+
74+
String logFile = "/logs/latest.log";
75+
File downloadFile1 = new File("latest.log");
76+
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
77+
boolean success = ftpClient.retrieveFile(logFile, outputStream1);
78+
outputStream1.close();
79+
80+
event.getChannel().sendMessage("Getting file...").queue();
81+
82+
if (success) {
83+
event.getChannel().sendMessage("File #1 has been downloaded successfully.").queue();
84+
}
85+
86+
} catch (IOException ex) {
87+
System.out.println("Error: " + ex.getMessage());
88+
ex.printStackTrace();
89+
} finally {
90+
try {
91+
if (ftpClient.isConnected()) {
92+
ftpClient.logout();
93+
ftpClient.disconnect();
94+
}
95+
} catch (IOException ex) {
96+
ex.printStackTrace();
97+
}
98+
}
99+
*/
100+
}
101+
102+
private static void runDiscordHelp(MessageReceivedEvent event) {
103+
event.getChannel().sendMessage("There are approximately 3 commands.\n" + //+ DiscordMessageReceiver.Commands.values().length + " commands.\n" +
104+
"`" + Settings.COMMAND_PREFIX + "help" + "` = displays this message\n" +
105+
"`" + Settings.COMMAND_PREFIX + "kill" + "` = kills the who sent the message").queue();
106+
}
107+
}

src/main/java/dain/FTPHandler.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dain;
2+
3+
import org.apache.commons.net.ftp.FTP;
4+
import org.apache.commons.net.ftp.FTPClient;
5+
6+
import java.io.*;
7+
8+
public class FTPHandler {
9+
10+
public static void grabFile(String file, int serverId) {
11+
12+
String filename = file.split("/")[file.split("/").length - 1]; //oops hyperslow but clean
13+
14+
int port = 21;
15+
FTPClient ftpClient = new FTPClient();
16+
try {
17+
18+
ftpClient.connect(Settings.SERVER_IPS[serverId], port);
19+
ftpClient.login(Settings.FTP_USERNAMES[serverId], Settings.FTP_PASSWORDS[serverId]);
20+
ftpClient.enterLocalPassiveMode();
21+
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
22+
23+
//sendMessageAllChannels("Getting file on thread " + serverId);
24+
25+
File downloadFile1 = new File(serverId + filename);
26+
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
27+
boolean success = ftpClient.retrieveFile(file, outputStream1);
28+
outputStream1.close();
29+
30+
/*
31+
if (success) {
32+
sendMessageAllChannels("`latest.log` has been downloaded successfully on thread " + serverId);
33+
} else {
34+
sendMessageAllChannels("issue downloading file on thread " + serverId);
35+
}
36+
*/
37+
38+
} catch (IOException e) {
39+
e.printStackTrace();
40+
} finally {
41+
try {
42+
if (ftpClient.isConnected()) {
43+
ftpClient.logout();
44+
ftpClient.disconnect();
45+
}
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
}
51+
}
Lines changed: 119 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package dain;
22

3+
import dain.events.DiscordMessageReceiver;
34
import net.dv8tion.jda.api.JDA;
5+
import net.dv8tion.jda.api.entities.Message;
46
import net.dv8tion.jda.api.entities.TextChannel;
7+
import net.kronos.rkon.core.Rcon;
8+
import net.kronos.rkon.core.ex.AuthenticationException;
59
import org.apache.commons.net.ftp.FTP;
610
import org.apache.commons.net.ftp.FTPClient;
711

812
import java.io.*;
9-
import java.net.ConnectException;
13+
import java.util.Locale;
1014
import java.util.Random;
1115
import java.util.Scanner;
1216

@@ -40,51 +44,10 @@ public void run() {
4044
}
4145
}
4246

43-
public void refreshLogFile() {
44-
45-
int port = 21;
46-
FTPClient ftpClient = new FTPClient();
47-
try {
48-
49-
ftpClient.connect(Settings.SERVER_IPS[id], port);
50-
ftpClient.login(Settings.FTP_USERNAMES[id], Settings.FTP_PASSWORDS[id]);
51-
ftpClient.enterLocalPassiveMode();
52-
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
53-
54-
//sendMessageAllChannels("Getting file on thread " + id);
55-
56-
String logFile = "/logs/latest.log";
57-
File downloadFile1 = new File(id + "latest.log");
58-
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
59-
boolean success = ftpClient.retrieveFile(logFile, outputStream1);
60-
outputStream1.close();
61-
62-
/*
63-
if (success) {
64-
sendMessageAllChannels("`latest.log` has been downloaded successfully on thread " + id);
65-
} else {
66-
sendMessageAllChannels("issue downloading file on thread " + id);
67-
}
68-
*/
69-
70-
} catch (IOException e) {
71-
e.printStackTrace();
72-
} finally {
73-
try {
74-
if (ftpClient.isConnected()) {
75-
ftpClient.logout();
76-
ftpClient.disconnect();
77-
}
78-
} catch (IOException e) {
79-
e.printStackTrace();
80-
}
81-
}
82-
}
83-
8447
public void sendNewMessages() {
8548
//sendMessageAllChannels("thread " + id + " sending new messages");
8649

87-
refreshLogFile();
50+
FTPHandler.grabFile("/logs/latest.log", id);
8851
try {
8952
File file = new File(id + "latest.log");
9053
Scanner myReader = new Scanner(file);
@@ -108,13 +71,26 @@ public void sendNewMessages() {
10871
return;
10972
}
11073

111-
lastLines[id] = lastLines[id].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]");
74+
String processedLine = processLine(lastLines[id]);
75+
76+
sendMessageAllChannels(processedLine);
77+
}
78+
79+
private void sendMessageAllChannels(String message) {
80+
for (int i = 0; i < Settings.DISCORD_CHANNEL_IDS.length; i++) {
81+
channels[i].sendMessage(message).queue();
82+
}
83+
}
84+
85+
private String processLine(String line) {
86+
87+
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]");
11288
// ^above replaces ipv4 IP addresses with "[IP ADDRESS REDACTED]"
11389

11490
//break the message up into an array of strings for each word
115-
String[] lastLineArray = lastLines[id].split(" ");
91+
String[] lineArray = line.split(" ");
11692

117-
//change @ into mentions
93+
//todo change @ into mentions
11894
/*
11995
for (int i = 0; i < lastLineArray.length; i++) {
12096
if (lastLineArray[i].startsWith("@")) {
@@ -124,35 +100,120 @@ public void sendNewMessages() {
124100
}
125101
}*/
126102

103+
//todo change :emoji: to emojis
104+
127105
// make the <name> part of chat messages bold
128106
boolean isChatMessage;
129-
if (lastLineArray[0].startsWith("<") && lastLineArray[0].endsWith(">")) {
130-
lastLineArray[0] = "**" + lastLineArray[0] + "**";
107+
String author = "";
108+
if (lineArray[0].startsWith("<") && lineArray[0].endsWith(">")) {
109+
lineArray[0] = "**" + lineArray[0] + "**";
131110
isChatMessage = true;
111+
author = lineArray[0].substring(1, lineArray[0].length() - 2);
112+
113+
if (lineArray[1].startsWith(">")) {
114+
115+
String[] parameterArray = new String[lineArray.length - 2];
116+
for (int i = 2; i < lineArray.length; i++) {
117+
parameterArray[i - 2] = lineArray[i];
118+
}
119+
120+
CommandHandler.handleMcCommand(lineArray[1].substring(1).toLowerCase(), parameterArray, id);
121+
}
132122
} else isChatMessage = false;
133123

134124
//join the array of strings back up to a complete message
135-
lastLines[id] = lastLineArray[0];
136-
for (int i = 1; i < lastLineArray.length; i++) {
137-
lastLines[id] = lastLines[id] + " " + lastLineArray[i];
125+
line = lineArray[0];
126+
String message = "";
127+
for (int i = 1; i < lineArray.length; i++) {
128+
line = line + " " + lineArray[i];
129+
message = message + lineArray[i] + " ";
138130
}
139131

140132
//make non-chat messages bold
141133
if (!isChatMessage) {
142-
lastLines[id] = "**" + lastLines[id] + "**";
134+
message = line;
135+
line = "**" + line + "**";
143136
}
144137

145138
//say if its coming from smp or cmp
146-
lastLines[id] = "[" + Settings.SERVER_NAMES[id] + "] " + lastLines[id];
139+
line = "[" + Settings.SERVER_NAMES[id] + "] " + line;
147140

148-
//scoreboard junk
141+
if (isChatMessage) {
142+
sendMessageAllOtherServers(author, message);
143+
} else {
144+
sendMessageAllOtherServers(message);
145+
}
149146

150-
sendMessageAllChannels(lastLines[id]);
147+
return line;
151148
}
152149

153-
private void sendMessageAllChannels(String message) {
154-
for (int i = 0; i < Settings.DISCORD_CHANNEL_IDS.length; i++) {
155-
channels[i].sendMessage(message).queue();
150+
private void sendMessageAllOtherServers(String author, String message) {
151+
for (int i = 0; i < Settings.SERVER_IPS.length; i++) {
152+
if (i != id) {
153+
sendMessageToServer(author, message, i);
154+
}
155+
}
156+
}
157+
158+
private void sendMessageAllOtherServers(String message) {
159+
for (int i = 0; i < Settings.SERVER_IPS.length; i++) {
160+
if (i != id) {
161+
sendMessageToServer(message, i);
162+
}
163+
}
164+
}
165+
166+
public static void sendMessageAllServers(String message) {
167+
for (int i = 0; i < Settings.SERVER_IPS.length; i++) {
168+
sendMessageToServer(message, i);
169+
}
170+
}
171+
172+
public static void sendMessageToMc(Message message) {
173+
//TODO replace emojis with :emoji_name:
174+
175+
//TODO replace attachments with text
176+
177+
String command = generateCommand(message.getAuthor().getName(), message.getContentRaw());
178+
179+
for (int id = 0; id < Settings.SERVER_IPS.length; id++) {
180+
sendCommandToServer(command, id);
181+
}
182+
}
183+
184+
public static void sendMessageToServer(String author, String message, int serverId) {
185+
186+
String command = generateCommand(author, message);
187+
188+
sendCommandToServer(command, serverId);
189+
}
190+
191+
public static void sendMessageToServer(String message, int serverId) {
192+
193+
String command = generateCommand(message);
194+
195+
sendCommandToServer(command, serverId);
196+
}
197+
198+
private static String generateCommand(String author, String message) {
199+
return "tellraw @a [\"\",{\"text\":\"<\"},{\"text\":\"@\",\"color\":\"gray\"}, {\"text\":\"" + author + "\",\"color\":\"white\"},{\"text\":\"> " + message + "\"}]";
200+
}
201+
202+
private static String generateCommand(String message) {
203+
return "tellraw @a \"" + message + "\"";
204+
}
205+
206+
public static void sendCommandToServer(String command, int serverId) {
207+
try {
208+
Rcon rcon = new Rcon(Settings.SERVER_IPS[serverId], Integer.decode(Settings.RCON_PORTS[serverId]), Settings.RCON_PASSWORDS[serverId].getBytes());
209+
rcon.command(command);
210+
rcon.disconnect();
211+
} catch (IOException e) {
212+
e.printStackTrace();
213+
} catch (AuthenticationException e) {
214+
e.printStackTrace();
156215
}
157216
}
217+
218+
158219
}

0 commit comments

Comments
 (0)