75 lines
2.8 KiB
Java
75 lines
2.8 KiB
Java
package com.minster586.tiktokstream.websocket;
|
|
|
|
import org.java_websocket.client.WebSocketClient;
|
|
import org.java_websocket.handshake.ServerHandshake;
|
|
import java.net.URI;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
import com.minster586.tiktokstream.TikTokStreamPlugin;
|
|
import com.minster586.tiktokstream.config.ConfigManager;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
|
|
public class StreamerBotWebSocketClient extends WebSocketClient {
|
|
private final Gson gson = new Gson();
|
|
private int retryCount = 0;
|
|
private final ConfigManager configManager;
|
|
private final int maxRetries;
|
|
private final String notifyPermission;
|
|
private final String notifyMessage;
|
|
|
|
public StreamerBotWebSocketClient(URI serverUri) {
|
|
super(serverUri);
|
|
this.configManager = TikTokStreamPlugin.getInstance().getConfigManager();
|
|
this.maxRetries = configManager != null ? configManager.getIntOrDefault("config.websocket.retry-count", 3) : 3;
|
|
this.notifyPermission = configManager != null
|
|
? configManager.getStringOrDefault("config.websocket.notify-permission", "tiktok.live")
|
|
: "tiktok.live";
|
|
this.notifyMessage = configManager != null ? configManager.getStringOrDefault("config.websocket.notify-message",
|
|
"§c[StreamerBot] Could not connect after %retries% attempts. Check your config or Streamer.bot status.")
|
|
: "§c[StreamerBot] Could not connect after %retries% attempts. Check your config or Streamer.bot status.";
|
|
}
|
|
|
|
@Override
|
|
public void onOpen(ServerHandshake handshakedata) {
|
|
retryCount = 0; // Reset on successful connection
|
|
}
|
|
|
|
@Override
|
|
public void onMessage(String message) {
|
|
// Handle incoming messages from Streamer.bot here
|
|
}
|
|
|
|
@Override
|
|
public void onClose(int code, String reason, boolean remote) {
|
|
if (retryCount < maxRetries) {
|
|
retryCount++;
|
|
try {
|
|
Thread.sleep(2000L * retryCount); // Exponential backoff
|
|
} catch (InterruptedException ignored) {
|
|
}
|
|
this.reconnect();
|
|
} else {
|
|
String msg = notifyMessage.replace("%retries%", String.valueOf(maxRetries));
|
|
notifyOps(msg);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onError(Exception ex) {
|
|
// Optionally log errors
|
|
}
|
|
|
|
private void notifyOps(String message) {
|
|
Bukkit.getScheduler().runTask(Bukkit.getPluginManager().getPlugin("TikTokStreamPlugin"), () -> {
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
if (player.isOp() || player.hasPermission(notifyPermission)) {
|
|
player.sendMessage(message);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|