diff --git a/.idea/discord.xml b/.idea/discord.xml
new file mode 100644
index 0000000..59b11d1
--- /dev/null
+++ b/.idea/discord.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/spigot_1_14_4.xml b/.idea/libraries/spigot_1_14_4.xml
new file mode 100644
index 0000000..2b62677
--- /dev/null
+++ b/.idea/libraries/spigot_1_14_4.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 0548357..305dac1 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,5 +1,10 @@
+
+
+
+
+
diff --git a/MCTPAudio.iml b/MCTPAudio.iml
index a865827..c185603 100644
--- a/MCTPAudio.iml
+++ b/MCTPAudio.iml
@@ -1,5 +1,14 @@
+
+
+
+
+ SPIGOT
+
+
+
+
@@ -18,5 +27,7 @@
+
+
\ No newline at end of file
diff --git a/src/me/mctp/Main.java b/src/me/mctp/Main.java
index 932c3a3..54f6a4a 100644
--- a/src/me/mctp/Main.java
+++ b/src/me/mctp/Main.java
@@ -1,5 +1,6 @@
package me.mctp;
+import me.mctp.socket.Client;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;
@@ -8,8 +9,8 @@ import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
- public static Plugin pl;
- private static Main plugin;
+ private static Plugin pl;
+ private static Client client;
public static String prefix = (ChatColor.GOLD + "[" + ChatColor.YELLOW + "MCTP" + ChatColor.GOLD + "] " + ChatColor.GRAY);
@@ -18,8 +19,10 @@ public class Main extends JavaPlugin implements Listener {
Bukkit.getLogger().info(prefix + "loading...");
- pl = (Plugin) this;
- plugin = this;
+ pl = this;
+
+ client = new Client("SOCKET URL");
+ client.connect();
getCommand("mctpaudio").setExecutor(new MCTPAudioCmd());
@@ -35,8 +38,11 @@ public class Main extends JavaPlugin implements Listener {
Bukkit.getLogger().info(prefix + "successfully disabled!");
}
- public static Main getPlugin() {
- return plugin;
+ public static Plugin getPlugin() {
+ return pl;
}
+ public static Client getClient() {
+ return client;
+ }
}
diff --git a/src/me/mctp/socket/Client.java b/src/me/mctp/socket/Client.java
new file mode 100644
index 0000000..8e733d5
--- /dev/null
+++ b/src/me/mctp/socket/Client.java
@@ -0,0 +1,115 @@
+package me.mctp.socket;
+
+import me.mctp.Main;
+import org.bukkit.Bukkit;
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.handshake.ServerHandshake;
+import org.json.simple.JSONObject;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+public class Client {
+ private String url;
+ private int taskID = 0;
+ private int controlID = 0;
+ private WebSocketClient wsc;
+ private boolean connected = false;
+
+ public Client(String url) {
+ this.url = url;
+ }
+
+ public void connect() {
+ if (!this.connected) {
+ this.connected = true;
+ this.controlID = Bukkit.getScheduler().runTaskTimer(Main.getPlugin(), () -> {
+ if (!this.connected) {
+ Bukkit.getScheduler().cancelTask(this.controlID);
+ this.controlID = 0;
+ } else if (this.wsc == null || !this.wsc.isOpen()) {
+ if (this.wsc != null) {
+ this.wsc.closeConnection(404, "Disconnected from socket");
+ this.wsc = null;
+ }
+
+ this.connect();
+ }
+ }, 600L, 600L).getTaskId();
+ }
+
+ if (this.url != null && this.wsc == null) {
+ try {
+ URI uri = new URI(this.url + "?type=SERVER&");
+
+ this.wsc = new WebSocketClient(uri) {
+ @Override
+ public void onOpen(ServerHandshake serverHandshake) {}
+
+ @Override
+ public void onMessage(String s) {
+ //TODO Add message handler
+ }
+
+ @Override
+ public void onClose(int i, String s, boolean b) {
+ Client.this.wsc = null;
+ Bukkit.getScheduler().cancelTask(Client.this.taskID);
+ Client.this.taskID = 0;
+ }
+
+ @Override
+ public void onError(Exception e) {
+ e.printStackTrace();
+ }
+ };
+
+ this.wsc.connect();
+
+ if (this.taskID == 0) {
+ this.taskID = Bukkit.getScheduler().runTaskTimerAsynchronously(Main.getPlugin(), () -> {
+ if (Client.this.wsc != null && Client.this.wsc.isOpen()) {
+ Client.this.wsc.send("__PING__");
+ } else {
+ if (Client.this.wsc != null) {
+ Client.this.wsc.closeConnection(404, "Disconnected from socket");
+ Client.this.wsc = null;
+ }
+
+ Bukkit.getScheduler().cancelTask(Client.this.taskID);
+ Client.this.taskID = 0;
+ }
+ }, 200L, 200L).getTaskId();
+ }
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void disconnect() {
+ if (this.wsc != null) {
+ this.wsc.closeConnection(404, "Disconnected from socket");
+ }
+
+ this.wsc = null;
+
+ if (this.taskID != 0) {
+ Bukkit.getScheduler().cancelTask(this.taskID);
+ this.taskID = 0;
+ }
+
+ this.connected = false;
+
+ if (this.controlID != 0) {
+ Bukkit.getScheduler().cancelTask(this.controlID);
+ this.controlID = 0;
+ }
+ }
+
+ public void sendData(JSONObject object) {
+ if (this.wsc != null && this.wsc.isOpen() && object != null && object.toJSONString() != null) {
+ this.wsc.send(object.toJSONString());
+ }
+ }
+}