File tree Expand file tree Collapse file tree
src/main/java/nl/ordewittetafel/minecom_plugin Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66public 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}
Original file line number Diff line number Diff line change 22
33import nl .ordewittetafel .minecom_plugin .listeners .ChatListener ;
44import nl .ordewittetafel .minecom_plugin .listeners .JoinQuitListener ;
5+ import nl .ordewittetafel .minecom_plugin .network .MessageReceiver ;
56import org .bukkit .Bukkit ;
67import org .bukkit .configuration .file .FileConfiguration ;
78import org .bukkit .plugin .java .JavaPlugin ;
89
10+ import java .io .IOException ;
11+
912public 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}
Original file line number Diff line number Diff line change 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> \u00A7 4Message data lost" );
35+ }
36+ }
37+ }, 0 , 1 );
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments