Skip to content

Commit 83c295b

Browse files
committed
Implemented receiving messages from Discord
1 parent ef6a52f commit 83c295b

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/main/java/nl/ordewittetafel/minecom_plugin/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
public class Constants {
77
public static final String DC_BOT_IP_KEY = "dcbot-ip";
88
public static final String DC_BOT_PORT_KEY = "dcbot-port";
9+
public static final String PLUGIN_PORT_KEY = "plugin-port";
910
}

src/main/java/nl/ordewittetafel/minecom_plugin/Main.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import nl.ordewittetafel.minecom_plugin.listeners.ChatListener;
44
import nl.ordewittetafel.minecom_plugin.listeners.JoinQuitListener;
5+
import nl.ordewittetafel.minecom_plugin.network.MessageReceiver;
56
import org.bukkit.Bukkit;
67
import org.bukkit.configuration.file.FileConfiguration;
78
import org.bukkit.plugin.java.JavaPlugin;
89

10+
import java.io.IOException;
11+
912
public class Main extends JavaPlugin {
1013
@Override
1114
public void onEnable() {
@@ -16,10 +19,18 @@ public void onEnable() {
1619
*/
1720
config.addDefault(Constants.DC_BOT_IP_KEY, "127.0.0.1"); // The address of the Discord bot
1821
config.addDefault(Constants.DC_BOT_PORT_KEY, 500); // The port of the Discord bot
22+
config.addDefault(Constants.PLUGIN_PORT_KEY, 501); // The port of this plugin
1923
config.options().copyDefaults(true);
2024
saveConfig();
2125

2226
Bukkit.getPluginManager().registerEvents(new JoinQuitListener(this), this);
2327
Bukkit.getPluginManager().registerEvents(new ChatListener(this), this);
28+
29+
try {
30+
new MessageReceiver(this);
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
System.err.println("If this error shows up, it means you cannot receive any messages from the Discord bot, because the ServerSocket could not be created");
34+
}
2435
}
2536
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package nl.ordewittetafel.minecom_plugin.network;
2+
3+
import nl.ordewittetafel.minecom_plugin.Constants;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
9+
import java.io.DataInputStream;
10+
import java.io.IOException;
11+
import java.net.ServerSocket;
12+
import java.net.Socket;
13+
import java.util.Timer;
14+
import java.util.TimerTask;
15+
16+
public class MessageReceiver {
17+
public MessageReceiver(JavaPlugin plugin) throws IOException {
18+
ServerSocket ss = new ServerSocket(plugin.getConfig().getInt(Constants.PLUGIN_PORT_KEY));
19+
Timer timer = new Timer();
20+
timer.schedule(new TimerTask() {
21+
@Override
22+
public void run() {
23+
try {
24+
Socket s = ss.accept();
25+
DataInputStream din = new DataInputStream(s.getInputStream());
26+
byte[] buffer = new byte[1024];
27+
int read = din.read(buffer);
28+
din.close();
29+
s.close();
30+
31+
JSONObject messageObj = new JSONObject(new String(buffer, 0, read));
32+
Bukkit.getServer().broadcastMessage("<" + messageObj.getString("sender") + "> " + messageObj.getString("message"));
33+
} catch (IOException | JSONException e) {
34+
Bukkit.getServer().broadcastMessage("<Unknown sender> \u00A74Message data lost");
35+
}
36+
}
37+
}, 0, 1);
38+
}
39+
}

0 commit comments

Comments
 (0)