Skip to content

Commit 058fe64

Browse files
committed
Implemented basis message handling
1 parent 07d19eb commit 058fe64

6 files changed

Lines changed: 105 additions & 2 deletions

File tree

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package nl.ordewittetafel.minecom_plugin;
2+
3+
/**
4+
* A class containing all the global constants
5+
*/
6+
public class Constants {
7+
public static final String DC_BOT_IP_KEY = "dcbot-ip";
8+
public static final String DC_BOT_PORT_KEY = "dcbot-port";
9+
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
package nl.ordewittetafel.minecom_plugin;
22

3-
public class Main {
4-
public static void main(String[] args) {
3+
import nl.ordewittetafel.minecom_plugin.listeners.ChatListener;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.configuration.file.FileConfiguration;
6+
import org.bukkit.plugin.java.JavaPlugin;
57

8+
public class Main extends JavaPlugin {
9+
@Override
10+
public void onEnable() {
11+
FileConfiguration config = getConfig();
12+
/*
13+
* The next config values may be changed according to your Discord bot setting in config.yml after the plugin
14+
* has been run once
15+
*/
16+
config.addDefault(Constants.DC_BOT_IP_KEY, "127.0.0.1"); // The address of the Discord bot
17+
config.addDefault(Constants.DC_BOT_PORT_KEY, "500"); // The port of the Discord bot
18+
config.options().copyDefaults(true);
19+
saveConfig();
20+
21+
Bukkit.getPluginManager().registerEvents(new ChatListener(this), this);
622
}
723
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package nl.ordewittetafel.minecom_plugin.listeners;
2+
3+
import nl.ordewittetafel.minecom_plugin.Constants;
4+
import nl.ordewittetafel.minecom_plugin.network.MessageTransmitter;
5+
import org.bukkit.configuration.file.FileConfiguration;
6+
import org.bukkit.event.EventHandler;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.player.AsyncPlayerChatEvent;
9+
import org.bukkit.plugin.java.JavaPlugin;
10+
import org.json.JSONObject;
11+
12+
import java.io.IOException;
13+
14+
/**
15+
* This class waits for a message in the chat and then sends it to the Discord bot connected to this plugin
16+
*/
17+
public class ChatListener implements Listener {
18+
private final JavaPlugin plugin;
19+
private final FileConfiguration config;
20+
21+
public ChatListener(JavaPlugin plugin) {
22+
this.plugin = plugin;
23+
config = plugin.getConfig();
24+
}
25+
26+
@EventHandler
27+
public void onPlayerChat(AsyncPlayerChatEvent e) {
28+
JSONObject messageData = new JSONObject();
29+
messageData.put("sender", e.getPlayer().getName());
30+
messageData.put("message", e.getMessage());
31+
32+
try {
33+
MessageTransmitter.transmit(
34+
config.getString(Constants.DC_BOT_IP_KEY),
35+
config.getInt(Constants.DC_BOT_PORT_KEY),
36+
messageData
37+
);
38+
} catch (IOException err) {
39+
err.printStackTrace();
40+
}
41+
}
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package nl.ordewittetafel.minecom_plugin.network;
2+
3+
import org.json.JSONObject;
4+
5+
import java.io.DataOutputStream;
6+
import java.io.IOException;
7+
import java.net.Socket;
8+
9+
public class MessageTransmitter {
10+
/**
11+
* Transmits a message over TCP
12+
* @param address The host to send the message to
13+
* @param port The host's port
14+
* @param message The message to send
15+
* @throws IOException If a socket could not be created
16+
*/
17+
public static void transmit(String address, int port, JSONObject message) throws IOException {
18+
Socket s = new Socket(address, port);
19+
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
20+
dout.writeBytes(message.toString());
21+
dout.flush();
22+
dout.close();
23+
s.close();
24+
}
25+
}

src/main/resources/plugin.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: MineCom-Plugin
2+
main: nl.ordewittetafel.minecom_plugin
3+
version: 1.0
4+
author: Kelvin Bouma

0 commit comments

Comments
 (0)