Added socket
This commit is contained in:
parent
56de051229
commit
08d14e1122
6 changed files with 163 additions and 6 deletions
9
.idea/discord.xml
generated
Normal file
9
.idea/discord.xml
generated
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="true" />
|
||||
</component>
|
||||
<component name="ProjectNotificationSettings">
|
||||
<option name="askShowProject" value="false" />
|
||||
</component>
|
||||
</project>
|
11
.idea/libraries/spigot_1_14_4.xml
generated
Normal file
11
.idea/libraries/spigot_1_14_4.xml
generated
Normal file
|
@ -0,0 +1,11 @@
|
|||
<component name="libraryTable">
|
||||
<library name="spigot-1.14.4">
|
||||
<CLASSES>
|
||||
<root url="jar://$USER_HOME$/Downloads/spigot-1.14.4.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES>
|
||||
<root url="jar://$USER_HOME$/Downloads/spigot-1.14.4.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
5
.idea/misc.xml
generated
5
.idea/misc.xml
generated
|
@ -1,5 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="EntryPointsManager">
|
||||
<list size="1">
|
||||
<item index="0" class="java.lang.String" itemvalue="org.bukkit.event.EventHandler" />
|
||||
</list>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="minecraft" name="Minecraft">
|
||||
<configuration>
|
||||
<autoDetectTypes>
|
||||
<platformType>SPIGOT</platformType>
|
||||
</autoDetectTypes>
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
|
@ -18,5 +27,7 @@
|
|||
</SOURCES>
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="library" name="spigot-1.14.4" level="project" />
|
||||
<orderEntry type="library" name="Java-WebSocket-1.3.8" level="project" />
|
||||
</component>
|
||||
</module>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
115
src/me/mctp/socket/Client.java
Normal file
115
src/me/mctp/socket/Client.java
Normal file
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue