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
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.scheduler.BukkitRunnable
@@ -19,9 +21,22 @@ class Server_info : JavaPlugin() {
private var tps = 20.0
private val df = DecimalFormat("#.##")
// Server title that appears at the top
private var serverTitle = "Paper"
override fun onEnable() {
// Save default config if it doesn't exist
saveDefaultConfig()
// Load configuration
loadConfiguration()
// 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
startTPSCalculation()
@@ -32,6 +47,27 @@ class Server_info : JavaPlugin() {
override fun onDisable() {
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() {
@@ -86,8 +122,7 @@ class Server_info : JavaPlugin() {
}
private fun getTabListHeader(): Component {
val serverName = server.name
return Component.text("§6§l$serverName\n§r§7Online players: §f${server.onlinePlayers.size}/${server.maxPlayers}")
return Component.text("§6§l$serverTitle\n§r§7Online players: §f${server.onlinePlayers.size}/${server.maxPlayers}")
}
private fun getTabListFooter(): Component {
@@ -137,9 +172,10 @@ class Server_info : JavaPlugin() {
// Format server info using string templates
info.append("§6===== SERVER INFO =====\n")
info.append("§6Title: §f$serverTitle\n")
info.append("§aRAM Usage: §f${usedMemory}MB / ${allocatedMemory}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")
return info.toString()
@@ -165,12 +201,48 @@ class Server_info : JavaPlugin() {
}
}
// Command executor inner class
private inner class ServerInfoCommand(private val plugin: Server_info) : CommandExecutor {
// Command executor inner class with tab completion
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 {
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())
// If the sender is a player, show their ping
@@ -183,5 +255,24 @@ class Server_info : JavaPlugin() {
}
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:
serverinfo:
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]
permission: serverinfo.use
permissions:
serverinfo.use:
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:
serverinfo:
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]
permission: serverinfo.use
permissions:
serverinfo.use:
description: Allows using the serverinfo command
default: true
default: true
serverinfo.admin:
description: Allows changing server settings
default: op

Binary file not shown.