package moe.sob; import moe.sob.commands.InfoCommand; import moe.sob.commands.PingCommand; import moe.sob.commands.SendMessageCommand; import moe.sob.commands.SetLoggingModeCommand; import moe.sob.commands.SetGreetMessageCommand; import moe.sob.events.player.*; import moe.sob.events.server.ServerLoad; import moe.sob.events.world.WorldLoad; import moe.sob.events.world.WorldUnload; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import java.awt.*; import java.io.*; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import java.util.Date; import java.util.Objects; public class Main extends JavaPlugin { public static Main mainSmall; private static boolean webhooksAreValidUrls; public static boolean hdc; public static File config; public static FileConfiguration configR; public static void main(String... args) { mainSmall.getLogger().info(String.format("Initializing using server address: %s...%n", ServerUtils.address)); } @Override public void onDisable() { if (Main.webhooksAreValidUrls) { DiscordWebhook notify = new DiscordWebhook(Main.getNotifyWebhook()); DiscordWebhook.EmbedObject embedObject = new DiscordWebhook.EmbedObject().setTitle("The server has been disabled.").setDescription(String.format("The server at %s:%d is now shutting down.", Utils.getServerHostPublicIP(), Bukkit.getServer().getPort())) .addField("Date occurred (as a local timestamp)", new Date().toString(), false) .setColor(Color.DARK_GRAY); notify.addEmbed(embedObject); try { notify.execute(); } catch (Exception e) { throw new RuntimeException(e); } } } @Override public void onEnable() { mainSmall = this; config = new File(getDataFolder(), "config.yml"); /*if (!config.exists()) { saveResource("config.yml", false); }*/ configR = getConfig(); configR = YamlConfiguration.loadConfiguration(config); if (!configR.contains("firstrun")) { configR.set("server.name", "Minecraft Server"); configR.set("server.entry_message", "Welcome to @servername, @playername!"); configR.set("server.webhooks.notify", "default-ns"); configR.set("server.webhooks.broadcast", "default-ns"); configR.set("log", true); configR.set("firstrun", 0); try { configR.save(config); } catch (IOException e) { throw new RuntimeException(e); } } // such cap //Main.mainSmall.getLogger().info("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()); Objects.requireNonNull(this.getCommand("set-greet-message")).setExecutor(new SetGreetMessageCommand()); } catch (NullPointerException e) { throw new RuntimeException(e); } webhooksAreValidUrls = validateWebhook_Ntfy(); hdc = validateWebhook_Pub(); // register events if (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 { if (getPublicMessageWebhook().equals("default-ns")) { mainSmall.getLogger().warning("There is no webhook set for the public messages function. The send-message command cannot be used."); return false; } URL url = new URI(getPublicMessageWebhook()).toURL(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { return true; } else { mainSmall.getLogger().warning("Invalid webhook URL provided for your public messages. Please provide the correct webhook and check for any typos."); return false; } } catch (Exception e) { mainSmall.getLogger().severe("Invalid webhook URL provide for your public messages. Please provide the correct webhook and check for any typos. (Caught exception)"); //mainSmall.getLogger().severe(String.format("Exception: %s%n", e)); return false; } } public static boolean validateWebhook_Ntfy() { try { if (getNotifyWebhook().equals("default-ns")) { mainSmall.getLogger().warning("There is no webhook set for the notify function. No events will be logged."); return false; } URL url = new URI(getNotifyWebhook()).toURL(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { return true; } else { mainSmall.getLogger().warning("Invalid webhook URL provided for the event logs. Please provide the correct webhook and check for any typos."); return false; } } catch (Exception e) { mainSmall.getLogger().severe("Invalid webhook URL provided for the event logs. Please provide the correct webhook and check for any typos. (Caught exception)"); return false; } } public static String getPublicMessageWebhook() { String webhook = configR.getString("server.webhooks.broadcast"); if (webhook.startsWith("https://") || webhook.startsWith("http://")) { webhooksAreValidUrls = true; return webhook; } return "default-ns"; } public static String getServerName() { return configR.getString("server.name"); } public static String getGreetMessage(Player player) { try { String entryMessage = configR.getString("server.entry_message"); String serverName = getServerName(); String playername = player.getName(); String finalOut = ""; finalOut = entryMessage.replaceAll("@servername", serverName).replaceAll("@playername", playername); return finalOut; } catch (Exception e) { throw new RuntimeException(e); } } public static String getNotifyWebhook() { String webhook = configR.getString("server.webhooks.notify"); if (webhook.startsWith("https://") || webhook.startsWith("http://")) { webhooksAreValidUrls = true; return webhook; } return "default-ns"; } public void registerEvents() { PlayerChat playerChat = new PlayerChat(); PlayerCommandPreprocess playerCommandPreprocess = new PlayerCommandPreprocess(); //PlayerBedEnter playerBedEnter = new PlayerBedEnter(); //PlayerBedLeave playerBedLeave = new PlayerBedLeave(); // Redundant event WorldLoad worldLoad = new WorldLoad(); WorldUnload worldUnload = new WorldUnload(); getServer().getPluginManager().registerEvents(playerChat, this); getServer().getPluginManager().registerEvents(playerCommandPreprocess, this); //getServer().getPluginManager().registerEvents(playerBedEnter, this); //getServer().getPluginManager().registerEvents(playerBedLeave, this); getServer().getPluginManager().registerEvents(worldLoad, this); getServer().getPluginManager().registerEvents(worldUnload, this); } }