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

230 lines
11 KiB
Java
Raw Normal View History

2023-03-28 19:24:11 +08:00
package cf.sobrooms;
import cf.sobrooms.commands.InfoCommand;
import cf.sobrooms.commands.PingCommand;
import cf.sobrooms.commands.SendMessageCommand;
import cf.sobrooms.commands.SetLoggingModeCommand;
import cf.sobrooms.events.enchantment.EnchantItem;
import cf.sobrooms.events.enchantment.PrepareItemEnchant;
import cf.sobrooms.events.player.*;
2023-03-29 14:50:28 +08:00
import cf.sobrooms.events.server.ServerLoad;
2023-04-03 21:32:22 +08:00
import cf.sobrooms.events.vehicle.VehicleEnter;
import cf.sobrooms.events.vehicle.VehicleExit;
2023-03-28 19:24:11 +08:00
import cf.sobrooms.events.weather.LightningStrike;
import cf.sobrooms.events.weather.ThunderChange;
import cf.sobrooms.events.weather.WeatherChange;
import cf.sobrooms.events.world.WorldInit;
import cf.sobrooms.events.world.WorldLoad;
import cf.sobrooms.events.world.WorldSave;
import cf.sobrooms.events.world.WorldUnload;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
2023-03-29 16:52:22 +08:00
import java.net.HttpURLConnection;
import java.net.URL;
2023-03-28 19:24:11 +08:00
import java.util.Objects;
public class Main extends JavaPlugin {
public static File isLoggingConfig;
public static File webhookConfig;
2023-03-28 19:24:11 +08:00
private static boolean webhooksAreValidUrls;
public static void main(String... args) {
2023-03-29 14:50:28 +08:00
System.out.printf("Initializing using server address: %s...%n", ServerUtils.address);
2023-03-28 19:24:11 +08:00
}
@Override
public void onEnable() {
String loggingDir = "./NoS";
File directory = new File(loggingDir);
if (!directory.exists()) {
if (directory.mkdir()) {
System.out.println("Config dir created: " + loggingDir);
} else {
System.out.println("Failed to create config directory: " + loggingDir);
}
}
isLoggingConfig = new File("./NoS/willLogCommands.bool");
webhookConfig = new File("./NoS/config_webhook.json");
2023-03-28 19:24:11 +08:00
try {
if (isLoggingConfig.createNewFile()) {
2023-03-28 19:24:11 +08:00
System.out.println("Config created: " + isLoggingConfig.getName());
//FileWriter notifyWebhookConfigWriter = new FileWriter(notifyWebhookConfig);
//FileWriter pubMessageWebhookConfigWriter = new FileWriter(pubMessageWebhookConfig);
//notifyWebhookConfigWriter.write("{\"url_notify\": \"default-ns\"}");
FileWriter linkConfigWriter = new FileWriter(webhookConfig);
linkConfigWriter.write("{\"url_publicmsg\": \"default-ns\", \"url_notify\": \"default-ns\"}");
linkConfigWriter.close();
//notifyWebhookConfigWriter.close();
2023-03-28 19:24:11 +08:00
} else {
System.out.println("Tried to create config even while it already exists, skipping step...");
2023-03-28 19:24:11 +08:00
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// post
Bukkit.getConsoleSender().sendMessage("Note: This Spigot/Bukkit plugin is highly verbose, so it will log a lot of events...");
// register commands
try {
Objects.requireNonNull(this.getCommand("info")).setExecutor(new InfoCommand());
Objects.requireNonNull(this.getCommand("send-message")).setExecutor(new SendMessageCommand());
Objects.requireNonNull(this.getCommand("set-logging")).setExecutor(new SetLoggingModeCommand());
Objects.requireNonNull(this.getCommand("ping-server")).setExecutor(new PingCommand());
} catch (NullPointerException e) {
throw new RuntimeException(e);
}
webhooksAreValidUrls = validateWebhook_Ntfy();
webhooksAreValidUrls = validateWebhook_Pub();
2023-03-28 19:24:11 +08:00
// register events
if (getLoggingConfig().equals("true") && webhooksAreValidUrls)
registerEvents();
if (webhooksAreValidUrls) {
2023-03-29 14:50:28 +08:00
ServerLoad serverStart = new ServerLoad();
getServer().getPluginManager().registerEvents(serverStart, this);
2023-03-28 19:24:11 +08:00
PlayerJoin playerJoin = new PlayerJoin();
PlayerQuit playerQuit = new PlayerQuit();
getServer().getPluginManager().registerEvents(playerJoin, this);
getServer().getPluginManager().registerEvents(playerQuit, this);
}
}
public static boolean validateWebhook_Pub() {
2023-03-29 16:52:22 +08:00
try {
URL url = new URL(getPublicMessageWebhook());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return true;
} else {
System.out.println("Invalid webhook URL provided. Please create a new one.");
return false;
}
} catch (Exception e) {
System.out.println("Invalid webhook URL provided. Please create a new one.. (Caught exception)");
return false;
2023-03-29 16:52:22 +08:00
}
}
public static boolean validateWebhook_Ntfy() {
2023-03-29 16:52:22 +08:00
try {
URL url = new URL(getNotifyWebhook());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return true;
} else {
System.out.println("Invalid webhook URL provided. Please create a new one.");
return false;
}
} catch (Exception e) {
System.out.println("Invalid webhook URL provided. Please create a new one.. (Caught exception)");
return false;
2023-03-29 16:52:22 +08:00
}
}
2023-03-28 19:24:11 +08:00
public static String getPublicMessageWebhook() {
try {
BufferedReader brfmsg = new BufferedReader(new FileReader(webhookConfig));
2023-03-28 19:24:11 +08:00
JsonParser jsonReader = new JsonParser();
JsonElement MsgParse = jsonReader.parse(brfmsg.readLine());
String lineMsg = MsgParse.getAsJsonObject().get("url_publicmsg").getAsString();
2023-03-28 19:24:11 +08:00
// validate url being url
// but this won't validate the url being an actual webhook
// i can try but lazy :sob:
if (lineMsg.equals("default-ns")) {
System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
2023-03-28 19:24:11 +08:00
webhooksAreValidUrls = false;
return lineMsg;
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
webhooksAreValidUrls = true;
2023-03-28 19:24:11 +08:00
return lineMsg;
} else {
System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
2023-03-28 19:24:11 +08:00
webhooksAreValidUrls = false;
return lineMsg;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getNotifyWebhook() {
try {
BufferedReader brfntfy = new BufferedReader(new FileReader(webhookConfig));
2023-03-28 19:24:11 +08:00
JsonParser jsonReader = new JsonParser();
JsonElement NtfyParse = jsonReader.parse(brfntfy.readLine());
String lineNtfy = NtfyParse.getAsJsonObject().get("url_notify").getAsString();
2023-03-28 19:24:11 +08:00
// validate url being url
// but this won't validate the url being an actual webhook
// i can try but lazy :sob:
if (lineNtfy.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
2023-03-28 19:24:11 +08:00
webhooksAreValidUrls = false;
return lineNtfy;
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
webhooksAreValidUrls = true;
2023-03-28 19:24:11 +08:00
return lineNtfy;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
2023-03-28 19:24:11 +08:00
webhooksAreValidUrls = false;
return lineNtfy;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getLoggingConfig() {
try {
File file = new File("./NoS/willLogCommands.bool");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (line.equals("true") || line.equals("false"))
return line;
else
return "Content is not a boolean";
}
reader.close();
} catch (IOException e) {
System.out.println("An error occurred while reading the configuration file.");
e.printStackTrace();
return "err";
}
return "err";
}
2023-03-28 19:24:11 +08:00
public void registerEvents() {
PlayerChat playerChat = new PlayerChat();
PlayerCommandPreprocess playerCommandPreprocess = new PlayerCommandPreprocess();
EnchantItem enchantItemEvent = new EnchantItem();
PrepareItemEnchant prepareItemEnchant = new PrepareItemEnchant();
PlayerBedEnter playerBedEnter = new PlayerBedEnter();
PlayerBedLeave playerBedLeave = new PlayerBedLeave();
LightningStrike lightningStrike = new LightningStrike();
ThunderChange thunderChange = new ThunderChange();
WeatherChange weatherChange = new WeatherChange();
WorldInit worldInit = new WorldInit();
WorldLoad worldLoad = new WorldLoad();
WorldSave worldSave = new WorldSave();
WorldUnload worldUnload = new WorldUnload();
2023-04-03 21:32:22 +08:00
VehicleEnter vehicleEnter = new VehicleEnter();
VehicleExit vehicleExit = new VehicleExit();
2023-03-28 19:24:11 +08:00
getServer().getPluginManager().registerEvents(playerChat, this);
getServer().getPluginManager().registerEvents(playerCommandPreprocess, this);
getServer().getPluginManager().registerEvents(enchantItemEvent, this);
getServer().getPluginManager().registerEvents(prepareItemEnchant, this);
getServer().getPluginManager().registerEvents(playerBedEnter, this);
getServer().getPluginManager().registerEvents(playerBedLeave, this);
getServer().getPluginManager().registerEvents(lightningStrike, this);
getServer().getPluginManager().registerEvents(thunderChange, this);
getServer().getPluginManager().registerEvents(weatherChange, this);
getServer().getPluginManager().registerEvents(worldInit, this);
getServer().getPluginManager().registerEvents(worldLoad, this);
getServer().getPluginManager().registerEvents(worldSave, this);
getServer().getPluginManager().registerEvents(worldUnload, this);
2023-04-03 21:32:22 +08:00
getServer().getPluginManager().registerEvents(vehicleEnter, this);
getServer().getPluginManager().registerEvents(vehicleExit, this);
2023-03-28 19:24:11 +08:00
}
}