Files
notify-on-start/src/main/java/cf/sobrooms/Main.java
T
2023-05-17 03:53:54 +00:00

224 lines
10 KiB
Java

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.*;
import cf.sobrooms.events.server.ServerLoad;
import cf.sobrooms.events.vehicle.VehicleEnter;
import cf.sobrooms.events.vehicle.VehicleExit;
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.WorldUnload;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;
public class Main extends JavaPlugin {
public static File isLoggingConfig;
public static File webhookConfig;
private static boolean webhooksAreValidUrls;
public static void main(String... args) {
System.out.printf("Initializing using server address: %s...%n", ServerUtils.address);
}
@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");
try {
if (isLoggingConfig.createNewFile()) {
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();
} else {
System.out.println("Tried to create config even while it already exists, skipping step...");
}
} 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();
// register events
if (getLoggingConfig().equals("true") && webhooksAreValidUrls)
registerEvents();
if (webhooksAreValidUrls) {
ServerLoad serverStart = new ServerLoad();
getServer().getPluginManager().registerEvents(serverStart, this);
PlayerJoin playerJoin = new PlayerJoin();
PlayerQuit playerQuit = new PlayerQuit();
getServer().getPluginManager().registerEvents(playerJoin, this);
getServer().getPluginManager().registerEvents(playerQuit, this);
}
}
public static boolean validateWebhook_Pub() {
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;
}
}
public static boolean validateWebhook_Ntfy() {
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;
}
}
public static String getPublicMessageWebhook() {
try {
BufferedReader brfmsg = new BufferedReader(new FileReader(webhookConfig));
JsonParser jsonReader = new JsonParser();
JsonElement MsgParse = jsonReader.parse(brfmsg.readLine());
String lineMsg = MsgParse.getAsJsonObject().get("url_publicmsg").getAsString();
brfmsg.close();
if (lineMsg.equals("default-ns")) {
System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineMsg;
} else if (lineMsg.startsWith("https://") || lineMsg.startsWith("http://")) {
webhooksAreValidUrls = true;
return lineMsg;
} else {
System.out.printf("Please set the public message webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineMsg;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getNotifyWebhook() {
try {
BufferedReader brfntfy = new BufferedReader(new FileReader(webhookConfig));
JsonParser jsonReader = new JsonParser();
JsonElement NtfyParse = jsonReader.parse(brfntfy.readLine());
String lineNtfy = NtfyParse.getAsJsonObject().get("url_notify").getAsString();
brfntfy.close();
if (lineNtfy.equals("default-ns")) {
System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
webhooksAreValidUrls = false;
return lineNtfy;
} else if (lineNtfy.startsWith("https://") || lineNtfy.startsWith("http://")) {
webhooksAreValidUrls = true;
return lineNtfy;
} else {
System.out.printf("Please set the webhook URL in %s and restart the server", webhookConfig.getAbsolutePath());
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";
}
} catch (IOException e) {
System.out.println("An error occurred while reading the configuration file.");
e.printStackTrace();
return "err";
}
return "err";
}
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();
WorldUnload worldUnload = new WorldUnload();
VehicleEnter vehicleEnter = new VehicleEnter();
VehicleExit vehicleExit = new VehicleExit();
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(worldUnload, this);
getServer().getPluginManager().registerEvents(vehicleEnter, this);
getServer().getPluginManager().registerEvents(vehicleExit, this);
}
}