199 lines
7.6 KiB
Java
199 lines
7.6 KiB
Java
package nl.sbdeveloper.mctpaudio.socket;
|
|
|
|
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
|
import nl.sbdeveloper.mctpaudio.MCTPAudio;
|
|
import nl.sbdeveloper.mctpaudio.api.events.AudioConnectionUpdateEvent;
|
|
import nl.sbdeveloper.mctpaudio.managers.PinManager;
|
|
import nl.sbdeveloper.mctpaudio.managers.WGManager;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
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;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.stream.Collectors;
|
|
|
|
/* Copyright (C) McThemeParks - All Rights Reserved
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited
|
|
* Proprietary and confidential
|
|
* Written by Stijn Bannink <stijnbannink23@gmail.com>, July 2020
|
|
*/
|
|
|
|
public class Client {
|
|
private final String url;
|
|
|
|
private int taskID = 0;
|
|
private int controlID = 0;
|
|
|
|
private WebSocketClient wsc;
|
|
|
|
private boolean connected = false;
|
|
|
|
/**
|
|
* Construct a new client
|
|
*
|
|
* @param url The URL to connect to
|
|
*/
|
|
public Client(String url) {
|
|
this.url = url;
|
|
}
|
|
|
|
/**
|
|
* Connect to the websocket
|
|
*/
|
|
public void connect() {
|
|
if (!this.connected) {
|
|
this.connected = true;
|
|
this.controlID = Bukkit.getScheduler().runTaskTimer(MCTPAudio.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) {
|
|
JSONObject json = JSONUtil.parse(s);
|
|
if (json == null) return;
|
|
|
|
String str = JSONUtil.getValue(json, "task");
|
|
if (str == null || str.isEmpty()) return;
|
|
|
|
if (str.equals("VERIFY")) {
|
|
String uuid = JSONUtil.getValue(json, "uuid");
|
|
if (uuid == null || uuid.isEmpty()) return;
|
|
|
|
String pin = JSONUtil.getValue(json, "pin");
|
|
if (pin == null || pin.isEmpty()) return;
|
|
|
|
UUID pUUID = JSONUtil.formatFromInput(uuid);
|
|
boolean verified = false;
|
|
if (Bukkit.getPlayer(pUUID) != null) {
|
|
verified = PinManager.checkPin(pUUID, pin);
|
|
}
|
|
|
|
Player p = Bukkit.getPlayer(pUUID);
|
|
if (p != null && p.isOnline()) {
|
|
AudioConnectionUpdateEvent event = new AudioConnectionUpdateEvent(p, true);
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
|
|
List<String> regions = WGManager.getRegionsIn(p.getLocation()).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
|
|
Set<String> list = MCTPAudio.getPlugin().getConfig().getConfigurationSection("Regions").getKeys(false);
|
|
Optional<String> regionName = regions.stream().filter(list::contains).findFirst();
|
|
regionName.ifPresent(name -> {
|
|
String regionURL = MCTPAudio.getPlugin().getConfig().getString("Regions." + name);
|
|
|
|
JSONObject data = new JSONObject();
|
|
data.put("task", "MUSIC");
|
|
data.put("path", regionURL);
|
|
data.put("uuid", p.getUniqueId().toString().replace("-", ""));
|
|
MCTPAudio.getClient().sendData(data);
|
|
});
|
|
}
|
|
|
|
JSONObject reply = new JSONObject();
|
|
reply.put("task", "AUTHENTICATION");
|
|
reply.put("verified", verified);
|
|
reply.put("uuid", uuid);
|
|
this.send(reply.toJSONString());
|
|
} else if (str.equals("DISCONNECTED")) {
|
|
String uuid = JSONUtil.getValue(json, "uuid");
|
|
if (uuid == null || uuid.isEmpty()) return;
|
|
|
|
UUID pUUID = JSONUtil.formatFromInput(uuid);
|
|
Player p = Bukkit.getPlayer(pUUID);
|
|
if (p != null && p.isOnline()) {
|
|
AudioConnectionUpdateEvent event = new AudioConnectionUpdateEvent(p, false);
|
|
Bukkit.getPluginManager().callEvent(event);
|
|
}
|
|
}
|
|
}
|
|
|
|
@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(MCTPAudio.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();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Disconnect from socket
|
|
*/
|
|
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());
|
|
}
|
|
}
|
|
}
|