use single configuration file for webhooks

This commit is contained in:
mangorifo
2023-04-19 00:33:46 +08:00
parent 7f74f5e2f3
commit 1e4410b70f
3 changed files with 22 additions and 22 deletions
+20 -20
View File
@@ -29,8 +29,7 @@ import java.util.Objects;
public class Main extends JavaPlugin { public class Main extends JavaPlugin {
public static File isLoggingConfig; public static File isLoggingConfig;
public static File notifyWebhookConfig; public static File webhookConfig;
public static File pubMessageWebhookConfig;
private static boolean webhooksAreValidUrls; private static boolean webhooksAreValidUrls;
public static void main(String... args) { public static void main(String... args) {
@@ -48,19 +47,19 @@ public class Main extends JavaPlugin {
} }
} }
isLoggingConfig = new File("./NoS/willLogCommands.bool"); isLoggingConfig = new File("./NoS/willLogCommands.bool");
notifyWebhookConfig = new File("./NoS/notifyWebhook.json"); webhookConfig = new File("./NoS/config_webhook.json");
pubMessageWebhookConfig = new File("./NoS/pubMessageWebhook.json");
try { try {
if (isLoggingConfig.createNewFile() || notifyWebhookConfig.createNewFile() || pubMessageWebhookConfig.createNewFile()) { if (isLoggingConfig.createNewFile()) {
System.out.println("Config created: " + isLoggingConfig.getName()); System.out.println("Config created: " + isLoggingConfig.getName());
FileWriter notifyWebhookConfigWriter = new FileWriter(notifyWebhookConfig); //FileWriter notifyWebhookConfigWriter = new FileWriter(notifyWebhookConfig);
FileWriter pubMessageWebhookConfigWriter = new FileWriter(pubMessageWebhookConfig); //FileWriter pubMessageWebhookConfigWriter = new FileWriter(pubMessageWebhookConfig);
notifyWebhookConfigWriter.write("{\"url\": \"default-ns\"}"); //notifyWebhookConfigWriter.write("{\"url_notify\": \"default-ns\"}");
pubMessageWebhookConfigWriter.write("{\"url\": \"default-ns\"}"); FileWriter linkConfigWriter = new FileWriter(webhookConfig);
pubMessageWebhookConfigWriter.close(); linkConfigWriter.write("{\"url_publicmsg\": \"default-ns\", \"url_notify\": \"default-ns\"}");
notifyWebhookConfigWriter.close(); linkConfigWriter.close();
//notifyWebhookConfigWriter.close();
} else { } else {
System.out.println("Created config even while it already exists, skipping step..."); System.out.println("Tried to create config even while it already exists, skipping step...");
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -128,22 +127,22 @@ public class Main extends JavaPlugin {
} }
public static String getPublicMessageWebhook() { public static String getPublicMessageWebhook() {
try { try {
BufferedReader brfmsg = new BufferedReader(new FileReader(pubMessageWebhookConfig)); BufferedReader brfmsg = new BufferedReader(new FileReader(webhookConfig));
JsonParser jsonReader = new JsonParser(); JsonParser jsonReader = new JsonParser();
JsonElement MsgParse = jsonReader.parse(brfmsg.readLine()); JsonElement MsgParse = jsonReader.parse(brfmsg.readLine());
String lineMsg = MsgParse.getAsJsonObject().get("url").getAsString(); String lineMsg = MsgParse.getAsJsonObject().get("url_publicmsg").getAsString();
// validate url being url // validate url being url
// but this won't validate the url being an actual webhook // but this won't validate the url being an actual webhook
// i can try but lazy :sob: // i can try but lazy :sob:
if (lineMsg.equals("default-ns")) { if (lineMsg.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath()); System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false; webhooksAreValidUrls = false;
return lineMsg; return lineMsg;
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) { } else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
webhooksAreValidUrls = true; webhooksAreValidUrls = true;
return lineMsg; return lineMsg;
} else { } else {
System.out.printf("Please set the webhook URL in %s and restart the server", pubMessageWebhookConfig.getAbsolutePath()); System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false; webhooksAreValidUrls = false;
return lineMsg; return lineMsg;
} }
@@ -153,22 +152,22 @@ public class Main extends JavaPlugin {
} }
public static String getNotifyWebhook() { public static String getNotifyWebhook() {
try { try {
BufferedReader brfntfy = new BufferedReader(new FileReader(notifyWebhookConfig)); BufferedReader brfntfy = new BufferedReader(new FileReader(webhookConfig));
JsonParser jsonReader = new JsonParser(); JsonParser jsonReader = new JsonParser();
JsonElement NtfyParse = jsonReader.parse(brfntfy.readLine()); JsonElement NtfyParse = jsonReader.parse(brfntfy.readLine());
String lineNtfy = NtfyParse.getAsJsonObject().get("url").getAsString(); String lineNtfy = NtfyParse.getAsJsonObject().get("url_notify").getAsString();
// validate url being url // validate url being url
// but this won't validate the url being an actual webhook // but this won't validate the url being an actual webhook
// i can try but lazy :sob: // i can try but lazy :sob:
if (lineNtfy.equals("default-ns")) { if (lineNtfy.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath()); System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false; webhooksAreValidUrls = false;
return lineNtfy; return lineNtfy;
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) { } else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
webhooksAreValidUrls = true; webhooksAreValidUrls = true;
return lineNtfy; return lineNtfy;
} else { } else {
System.out.printf("Please set the webhook URL in %s and restart the server", notifyWebhookConfig.getAbsolutePath()); System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false; webhooksAreValidUrls = false;
return lineNtfy; return lineNtfy;
} }
@@ -195,6 +194,7 @@ public class Main extends JavaPlugin {
} }
return "err"; return "err";
} }
public void registerEvents() { public void registerEvents() {
PlayerChat playerChat = new PlayerChat(); PlayerChat playerChat = new PlayerChat();
PlayerCommandPreprocess playerCommandPreprocess = new PlayerCommandPreprocess(); PlayerCommandPreprocess playerCommandPreprocess = new PlayerCommandPreprocess();
+1 -1
View File
@@ -11,7 +11,7 @@ public class ServerUtils {
public static String address_fallback = Utils.getServerHostPublicIP(); public static String address_fallback = Utils.getServerHostPublicIP();
public static String webhook_notify = Main.getNotifyWebhook(); public static String webhook_notify = Main.getNotifyWebhook();
public static String webhook_messages = Main.getPublicMessageWebhook(); public static String webhook_messages = Main.getPublicMessageWebhook();
public static String address = "mc-srv2-singapore.rrryfoo.cf"; public static String address = Utils.getServerHostPublicIP();
public static boolean serverIsUp(String serverAddress, Integer serverPort) { public static boolean serverIsUp(String serverAddress, Integer serverPort) {
/*Socket socket; /*Socket socket;
@@ -23,7 +23,7 @@ public class ServerLoad implements Listener {
public void onServerStart(ServerLoadEvent event) { public void onServerStart(ServerLoadEvent event) {
Bukkit.getConsoleSender().sendMessage("Triggered ServerLoad event."); Bukkit.getConsoleSender().sendMessage("Triggered ServerLoad event.");
if (timesCalled < 1) { if (timesCalled < 1) {
Bukkit.getConsoleSender().sendMessage("Checking if server is up on " + ServerUtils.address + " with port " + ServerUtils.port + "..."); Bukkit.getConsoleSender().sendMessage("Checking if server is up on " + ServerUtils.address + " with port " + Bukkit.getServer().getPort() + "...");
if (ServerUtils.serverIsUp(address, Bukkit.getServer().getPort())) { if (ServerUtils.serverIsUp(address, Bukkit.getServer().getPort())) {
System.out.println(ServerUtils.webhook_messages); System.out.println(ServerUtils.webhook_messages);
System.out.println(ServerUtils.webhook_notify); System.out.println(ServerUtils.webhook_notify);