This commit is contained in:
rattatwinko
2025-04-26 21:43:14 +02:00
commit c794b19531
25 changed files with 538 additions and 0 deletions

View File

@@ -0,0 +1,123 @@
package org.server_info.MOTD
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin
class MOTD : JavaPlugin() {
private var quotesTask: Int = -1
private val quotesList = ArrayList<String>()
override fun onEnable() {
// Save default config if it doesn't exist
saveDefaultConfig()
// Load quotes from config
loadQuotes()
// Register command
getCommand("motd")?.setExecutor(MOTDCommand(this))
// Schedule the MOTD update task
startQuoteTask()
logger.info("MOTD Quote Plugin enabled! Loaded ${quotesList.size} quotes.")
}
override fun onDisable() {
// Cancel the task when the plugin is disabled
if (quotesTask != -1) {
Bukkit.getScheduler().cancelTask(quotesTask)
}
logger.info("MOTD Quote Plugin disabled!")
}
private fun loadQuotes() {
// Clear existing quotes
quotesList.clear()
// Load quotes from config
val configQuotes = config.getStringList("quotes")
if (configQuotes.isNotEmpty()) {
quotesList.addAll(configQuotes)
} else {
// Add default quotes if none found in config
quotesList.add("If at first you don't succeed, then skydiving definitely isn't for you.")
quotesList.add("I told my wife she was drawing her eyebrows too high. She looked surprised.")
quotesList.add("I'm not lazy, I'm just on energy-saving mode.")
quotesList.add("I don't have a bad handwriting, I have my own font.")
quotesList.add("When life gives you lemons, squirt someone in the eye.")
// Save default quotes to config
config.set("quotes", quotesList)
saveConfig()
logger.info("No quotes found in config, created default quotes list.")
}
}
fun startQuoteTask() {
// Cancel existing task if running
if (quotesTask != -1) {
Bukkit.getScheduler().cancelTask(quotesTask)
}
// Get update interval from config (default: 60 minutes)
val intervalMinutes = config.getLong("update-interval-minutes", 60)
// Schedule repeating task
quotesTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(
this,
{ updateServerMOTD() },
20L, // Initial delay (1 second)
intervalMinutes * 60 * 20L // Convert minutes to ticks (20 ticks = 1 second)
)
logger.info("MOTD update task scheduled to run every $intervalMinutes minutes.")
}
fun updateServerMOTD() {
if (quotesList.isEmpty()) {
logger.warning("No quotes available to set MOTD!")
return
}
// Get a random quote
val quote = getRandomQuote()
// Format the quote
val formattedQuote = formatMOTD(quote)
// Set the server MOTD
val server = Bukkit.getServer()
server.motd = formattedQuote
logger.info("MOTD updated: $formattedQuote")
}
fun getRandomQuote(): String {
return if (quotesList.isNotEmpty()) {
quotesList.random()
} else {
"No quotes available"
}
}
private fun formatMOTD(quote: String): String {
// Get prefix and suffix from config
val prefix = config.getString("motd-prefix", "§e§l") ?: "§e§l"
val suffix = config.getString("motd-suffix", "") ?: ""
// Trim and limit the quote length if it's too long (Minecraft has MOTD length limitations)
val maxLength = config.getInt("max-motd-length", 50)
var trimmedQuote = quote
if (trimmedQuote.length > maxLength) {
trimmedQuote = trimmedQuote.substring(0, maxLength - 3) + "..."
}
return prefix + trimmedQuote + suffix
}
fun reloadQuotes() {
reloadConfig()
loadQuotes()
}
}

View File

@@ -0,0 +1,48 @@
package org.server_info.MOTD
import org.bukkit.ChatColor
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
class MOTDCommand(private val plugin: MOTD) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (args.isEmpty()) {
// Display help
sender.sendMessage("${ChatColor.GOLD}==== MOTD Quote Plugin ====")
sender.sendMessage("${ChatColor.YELLOW}/motd refresh ${ChatColor.WHITE}- Set a new random quote")
sender.sendMessage("${ChatColor.YELLOW}/motd reload ${ChatColor.WHITE}- Reload quotes from config")
return true
}
when (args[0].lowercase()) {
"refresh" -> {
if (!sender.hasPermission("motd.refresh")) {
sender.sendMessage("${ChatColor.RED}You don't have permission to use this command.")
return true
}
sender.sendMessage("${ChatColor.YELLOW}Setting a new random quote...")
plugin.updateServerMOTD()
sender.sendMessage("${ChatColor.GREEN}MOTD has been updated!")
return true
}
"reload" -> {
if (!sender.hasPermission("motd.reload")) {
sender.sendMessage("${ChatColor.RED}You don't have permission to use this command.")
return true
}
plugin.reloadQuotes()
plugin.startQuoteTask()
sender.sendMessage("${ChatColor.GREEN}MOTD quotes reloaded from config!")
return true
}
else -> {
sender.sendMessage("${ChatColor.RED}Unknown command. Use /motd for help.")
return true
}
}
}
}