2025-10-15 21:12:07 +08:00
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 ;
2026-01-17 13:05:32 +08:00
import java.net.URI ;
2025-10-15 21:12:07 +08:00
import java.net.URL ;
import java.util.Date ;
import java.util.Objects ;
public class Main extends JavaPlugin {
2026-01-17 12:45:24 +08:00
public static Main mainSmall ;
2025-10-15 21:12:07 +08:00
private static boolean webhooksAreValidUrls ;
public static boolean hdc ;
public static File config ;
public static FileConfiguration configR ;
public static void main ( String . . . args ) {
2026-01-17 12:18:22 +08:00
mainSmall . getLogger ( ) . info ( String . format ( " Initializing using server address: %s...%n " , ServerUtils . address ) ) ;
2025-10-15 21:12:07 +08:00
}
@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 ( ) ;
2026-01-17 13:05:32 +08:00
} catch ( Exception e ) {
2025-10-15 21:12:07 +08:00
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
2026-01-17 13:05:32 +08:00
//Main.mainSmall.getLogger().info("Note: This Spigot/Bukkit plugin is highly verbose, so it will log a lot of events...");
2025-10-15 21:12:07 +08:00
// 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 ;
}
2026-01-17 13:05:32 +08:00
URL url = new URI ( getPublicMessageWebhook ( ) ) . toURL ( ) ;
2025-10-15 21:12:07 +08:00
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 ) {
2026-01-17 13:05:32 +08:00
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));
2025-10-15 21:12:07 +08:00
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 ;
}
2026-01-17 13:05:32 +08:00
URL url = new URI ( getNotifyWebhook ( ) ) . toURL ( ) ;
2025-10-15 21:12:07 +08:00
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 ) {
2026-01-17 13:05:32 +08:00
mainSmall . getLogger ( ) . severe ( " Invalid webhook URL provided for the event logs. Please provide the correct webhook and check for any typos. (Caught exception) " ) ;
2025-10-15 21:12:07 +08:00
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 ( ) ;
2026-01-17 13:05:32 +08:00
//PlayerBedEnter playerBedEnter = new PlayerBedEnter();
//PlayerBedLeave playerBedLeave = new PlayerBedLeave(); // Redundant event
2025-10-15 21:12:07 +08:00
WorldLoad worldLoad = new WorldLoad ( ) ;
WorldUnload worldUnload = new WorldUnload ( ) ;
getServer ( ) . getPluginManager ( ) . registerEvents ( playerChat , this ) ;
getServer ( ) . getPluginManager ( ) . registerEvents ( playerCommandPreprocess , this ) ;
2026-01-17 13:05:32 +08:00
//getServer().getPluginManager().registerEvents(playerBedEnter, this);
//getServer().getPluginManager().registerEvents(playerBedLeave, this);
2025-10-15 21:12:07 +08:00
getServer ( ) . getPluginManager ( ) . registerEvents ( worldLoad , this ) ;
getServer ( ) . getPluginManager ( ) . registerEvents ( worldUnload , this ) ;
}
}