fixed stuff and added ability to change server name in tablist

This commit is contained in:
rattatwinko
2025-04-26 15:25:52 +02:00
parent 4731a659b9
commit b6cc8ea7b2
10 changed files with 118 additions and 11 deletions

View File

@@ -1,10 +1,12 @@
package org.server_info.server_info package org.server_info.server_info
import net.kyori.adventure.text.Component import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.command.Command import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.scheduler.BukkitRunnable import org.bukkit.scheduler.BukkitRunnable
@@ -19,9 +21,22 @@ class Server_info : JavaPlugin() {
private var tps = 20.0 private var tps = 20.0
private val df = DecimalFormat("#.##") private val df = DecimalFormat("#.##")
// Server title that appears at the top
private var serverTitle = "Paper"
override fun onEnable() { override fun onEnable() {
// Save default config if it doesn't exist
saveDefaultConfig()
// Load configuration
loadConfiguration()
// Register command // Register command
getCommand("serverinfo")?.setExecutor(ServerInfoCommand(this)) getCommand("serverinfo")?.let { command ->
val commandExecutor = ServerInfoCommand(this)
command.setExecutor(commandExecutor)
command.tabCompleter = commandExecutor
}
// Start server info collectors // Start server info collectors
startTPSCalculation() startTPSCalculation()
@@ -32,6 +47,27 @@ class Server_info : JavaPlugin() {
override fun onDisable() { override fun onDisable() {
logger.info("ServerInfo plugin has been disabled!") logger.info("ServerInfo plugin has been disabled!")
// Save configuration
saveConfig()
}
private fun loadConfiguration() {
// Load server title from config, default to "Paper" if not set
serverTitle = config.getString("server-title") ?: "Paper"
// Create config options if they don't exist
if (!config.contains("server-title")) {
config.set("server-title", serverTitle)
saveConfig()
}
}
fun setServerTitle(title: String) {
serverTitle = title
config.set("server-title", title)
saveConfig()
// Update tablist immediately to reflect the change
updateTabList()
} }
private fun startTPSCalculation() { private fun startTPSCalculation() {
@@ -86,8 +122,7 @@ class Server_info : JavaPlugin() {
} }
private fun getTabListHeader(): Component { private fun getTabListHeader(): Component {
val serverName = server.name return Component.text("§6§l$serverTitle\n§r§7Online players: §f${server.onlinePlayers.size}/${server.maxPlayers}")
return Component.text("§6§l$serverName\n§r§7Online players: §f${server.onlinePlayers.size}/${server.maxPlayers}")
} }
private fun getTabListFooter(): Component { private fun getTabListFooter(): Component {
@@ -137,9 +172,10 @@ class Server_info : JavaPlugin() {
// Format server info using string templates // Format server info using string templates
info.append("§6===== SERVER INFO =====\n") info.append("§6===== SERVER INFO =====\n")
info.append("§6Title: §f$serverTitle\n")
info.append("§aRAM Usage: §f${usedMemory}MB / ${allocatedMemory}MB\n") info.append("§aRAM Usage: §f${usedMemory}MB / ${allocatedMemory}MB\n")
info.append("§aMax RAM: §f${maxMemory}MB\n") info.append("§aMax RAM: §f${maxMemory}MB\n")
info.append("§aCPU Load: §f${df.format(cpuLoad)}%\n") // Added % sign info.append("§aCPU Load: §f${df.format(cpuLoad)}%\n")
info.append("§aTPS: ${formatTPS(tps)}\n") info.append("§aTPS: ${formatTPS(tps)}\n")
return info.toString() return info.toString()
@@ -165,12 +201,48 @@ class Server_info : JavaPlugin() {
} }
} }
// Command executor inner class // Command executor inner class with tab completion
private inner class ServerInfoCommand(private val plugin: Server_info) : CommandExecutor { private inner class ServerInfoCommand(private val plugin: Server_info) : CommandExecutor, TabCompleter {
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean { override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<out String>): Boolean {
if (cmd.name.equals("serverinfo", ignoreCase = true)) { if (cmd.name.equals("serverinfo", ignoreCase = true)) {
// Send server info // If there are arguments, handle them
if (args.isNotEmpty()) {
when (args[0].lowercase()) {
"title", "settitle" -> {
// Check permission
if (!sender.hasPermission("serverinfo.admin")) {
sender.sendMessage("§cYou don't have permission to change the server title.")
return true
}
if (args.size < 2) {
sender.sendMessage("§cUsage: /serverinfo settitle <title>")
return true
}
// Join remaining args as the title
val newTitle = args.copyOfRange(1, args.size).joinToString(" ")
plugin.setServerTitle(newTitle)
sender.sendMessage("§aServer title changed to: §f$newTitle")
return true
}
"reload" -> {
// Check permission
if (!sender.hasPermission("serverinfo.admin")) {
sender.sendMessage("§cYou don't have permission to reload the plugin.")
return true
}
plugin.reloadConfig()
plugin.loadConfiguration()
sender.sendMessage("§aServerInfo plugin configuration reloaded.")
return true
}
}
}
// Default behavior - show server info
sender.sendMessage(plugin.getServerInfo()) sender.sendMessage(plugin.getServerInfo())
// If the sender is a player, show their ping // If the sender is a player, show their ping
@@ -183,5 +255,24 @@ class Server_info : JavaPlugin() {
} }
return false return false
} }
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<out String>
): List<String>? {
if (command.name.equals("serverinfo", ignoreCase = true)) {
if (args.size == 1) {
val completions = mutableListOf<String>()
if (sender.hasPermission("serverinfo.admin")) {
completions.add("settitle")
completions.add("reload")
}
return completions.filter { it.startsWith(args[0].lowercase()) }
}
}
return null
}
} }
} }

View File

@@ -0,0 +1,4 @@
# ServerInfo Plugin Configuration
# The title displayed at the top of the server info
server-title: "Paper"

View File

@@ -6,10 +6,16 @@ description: Displays server performance metrics
commands: commands:
serverinfo: serverinfo:
description: Shows server performance metrics description: Shows server performance metrics
usage: /<command> usage: |
/<command> - Shows server information
/<command> settitle <title> - Sets the server title
/<command> reload - Reloads the configuration
aliases: [sinfo, si] aliases: [sinfo, si]
permission: serverinfo.use permission: serverinfo.use
permissions: permissions:
serverinfo.use: serverinfo.use:
description: Allows using the serverinfo command description: Allows using the serverinfo command
default: true default: true
serverinfo.admin:
description: Allows changing server settings
default: op

View File

@@ -6,10 +6,16 @@ description: Displays server performance metrics
commands: commands:
serverinfo: serverinfo:
description: Shows server performance metrics description: Shows server performance metrics
usage: /<command> usage: |
/<command> - Shows server information
/<command> settitle <title> - Sets the server title
/<command> reload - Reloads the configuration
aliases: [sinfo, si] aliases: [sinfo, si]
permission: serverinfo.use permission: serverinfo.use
permissions: permissions:
serverinfo.use: serverinfo.use:
description: Allows using the serverinfo command description: Allows using the serverinfo command
default: true default: true
serverinfo.admin:
description: Allows changing server settings
default: op

Binary file not shown.