Files
notify-on-start/src/main/java/cf/sobrooms/ServerUtils.java
T

40 lines
1.7 KiB
Java
Raw Normal View History

2023-03-28 19:24:11 +08:00
package cf.sobrooms;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ServerUtils {
public static int port = 25565;
public static String address_fallback = Utils.getServerHostPublicIP();
public static String webhook_notify = Main.getNotifyWebhook();
public static String webhook_messages = Main.getPublicMessageWebhook();
public static String address = Utils.getServerHostPublicIP();
2023-03-28 19:24:11 +08:00
public static boolean serverIsUp(String serverAddress, Integer serverPort) {
try {
Server server = Bukkit.getServer();
Socket s = new Socket();
s.connect(new InetSocketAddress(serverAddress, Integer.parseInt(serverPort.toString())), 15);
2023-03-28 19:24:11 +08:00
s.close();
System.out.println("Server is online. Test notifying by sending a message to your webhook... (Pinged " + serverAddress + ":" + server.getPort() + ")");
2023-03-28 19:24:11 +08:00
return true;
} catch (Exception e) {
System.out.println("Server is inactive. Pinging fallback address...");
try {
Server server = Bukkit.getServer();
Socket s = new Socket();
s.connect(new InetSocketAddress(address_fallback, Integer.parseInt(serverPort.toString())), 15);
s.close();
System.out.println("Server is online. Sending message to webhook_notify... (Pinged " + Utils.getServerHostPublicIP() + ":" + server.getPort() + ")");
return true;
2023-03-28 19:24:11 +08:00
} catch (Exception err) {
System.out.println("Fallback address is inactive. No longer pinging...");
}
return false;
}
}
}