notify on /stop

This commit is contained in:
mangorifo
2023-04-27 09:41:17 +08:00
parent 0384c40730
commit df967d4a22
8 changed files with 19 additions and 150 deletions
@@ -14,22 +14,6 @@ public class ServerUtils {
public static String address = Utils.getServerHostPublicIP();
public static boolean serverIsUp(String serverAddress, Integer serverPort) {
/*Socket socket;
try {
socket = new Socket(address, port);
socket.close();
return true;
} catch (Exception e) {
try {
System.out.println("Server is inactive on default address. Pinging on fallback address (" + address_fallback + ")...");
Socket socket2 = new Socket(address_fallback, port);
socket2.close();
return true;
} catch (Exception exception) {
System.out.println("Server is inactive on fallback address. No longer pinging...");
return false;
}
}*/
try {
Server server = Bukkit.getServer();
Socket s = new Socket();
-2
View File
@@ -7,9 +7,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
public class Utils {
public static String getActivePlayers() {
@@ -10,6 +10,7 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import java.awt.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
public class PlayerCommandPreprocess implements Listener {
@@ -30,5 +31,21 @@ public class PlayerCommandPreprocess implements Listener {
} catch (IOException e) {
throw new RuntimeException(e);
}
// temp: log on /stop
if (command[0].equals("/stop")) {
DiscordWebhook ntf = new DiscordWebhook(ServerUtils.webhook_notify);
DiscordWebhook.EmbedObject embed = new DiscordWebhook.EmbedObject()
.setTitle("Server stopped")
.setDescription("The server was stopped using the `/stop` command.")
.addField("Date", "<t:%n:R>".formatted(new Timestamp(System.currentTimeMillis()).getTime()), true)
.addField("Player name", player.getName(), true);
ntf.addEmbed(embed);
try {
ntf.execute();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}