commit 4731a659b9544fe15f74c6f4e17079100f097c8a Author: rattatwinko Date: Sat Apr 26 15:19:44 2025 +0200 initial diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..7bc07ec --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..9cf14f4 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..942f3a2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..8c89be5 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..bd1edea --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f88aef9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e9f6464 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..c4044aa --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + org.server_info + server_info + server_info + 1.0-SNAPSHOT + + ${project.basedir}/src/main/kotlin + clean package + + + true + src/main/resources + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + + ${java.version} + + + + maven-shade-plugin + 3.5.3 + + + package + + shade + + + + + + + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + io.papermc.paper + paper-api + 1.21.5-R0.1-SNAPSHOT + provided + + + + UTF-8 + 2.2.0-Beta1 + 21 + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..db0229f --- /dev/null +++ b/pom.xml @@ -0,0 +1,87 @@ + + + 4.0.0 + + org.server_info + server_info + 1.0-SNAPSHOT + jar + + server_info + + + 21 + 2.2.0-Beta1 + UTF-8 + + + + clean package + ${project.basedir}/src/main/kotlin + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.3 + + + package + + shade + + + + + + + + src/main/resources + true + + + + + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + io.papermc.paper + paper-api + 1.21.5-R0.1-SNAPSHOT + provided + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + diff --git a/server_info.iml b/server_info.iml new file mode 100644 index 0000000..3cf00db --- /dev/null +++ b/server_info.iml @@ -0,0 +1,13 @@ + + + + + + + PAPER + + 1 + + + + \ No newline at end of file diff --git a/src/main/kotlin/org/server_info/server_info/Server_info.kt b/src/main/kotlin/org/server_info/server_info/Server_info.kt new file mode 100644 index 0000000..93b7082 --- /dev/null +++ b/src/main/kotlin/org/server_info/server_info/Server_info.kt @@ -0,0 +1,187 @@ +package org.server_info.server_info + +import net.kyori.adventure.text.Component +import org.bukkit.Bukkit +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.plugin.java.JavaPlugin +import org.bukkit.scheduler.BukkitRunnable +import java.lang.management.ManagementFactory +import java.text.DecimalFormat + +class Server_info : JavaPlugin() { + + private var lastTickTime = System.currentTimeMillis() + private var tickCount = 0 + private val tickTimeList = ArrayList() + private var tps = 20.0 + private val df = DecimalFormat("#.##") + + override fun onEnable() { + // Register command + getCommand("serverinfo")?.setExecutor(ServerInfoCommand(this)) + + // Start server info collectors + startTPSCalculation() + startTabListUpdater() + + logger.info("ServerInfo plugin has been enabled!") + } + + override fun onDisable() { + logger.info("ServerInfo plugin has been disabled!") + } + + private fun startTPSCalculation() { + // Better TPS calculation method with moving average + object : BukkitRunnable() { + override fun run() { + tickCount++ + + val now = System.currentTimeMillis() + val elapsed = now - lastTickTime + lastTickTime = now + + // Store data point - time per tick in ms + // Only add if we've gone through at least one tick to avoid startup anomalies + if (tickCount > 1) { + tickTimeList.add(elapsed) + } + + // Keep only the last 600 data points (30 seconds at 20 TPS) + while (tickTimeList.size > 600) { + tickTimeList.removeAt(0) + } + + // Calculate average tick time and TPS if we have data + if (tickTimeList.isNotEmpty()) { + val avgTickTime = tickTimeList.average() + tps = 1000.0 / avgTickTime + + // Clamp TPS between 0 and 20 + if (tps > 20.0) tps = 20.0 + if (tps < 0.0) tps = 0.0 + } + } + }.runTaskTimer(this, 1L, 1L) // Run every tick + } + + private fun startTabListUpdater() { + object : BukkitRunnable() { + override fun run() { + updateTabList() + } + }.runTaskTimer(this, 20L, 20L) // Update every second + } + + private fun updateTabList() { + val header = getTabListHeader() + val footer = getTabListFooter() + + for (player in Bukkit.getOnlinePlayers()) { + player.sendPlayerListHeaderAndFooter(header, footer) + } + } + + private fun getTabListHeader(): Component { + val serverName = server.name + return Component.text("§6§l$serverName\n§r§7Online players: §f${server.onlinePlayers.size}/${server.maxPlayers}") + } + + private fun getTabListFooter(): Component { + // Memory information + val runtime = Runtime.getRuntime() + val maxMemory = runtime.maxMemory() / (1024 * 1024) + val allocatedMemory = runtime.totalMemory() / (1024 * 1024) + val freeMemory = runtime.freeMemory() / (1024 * 1024) + val usedMemory = allocatedMemory - freeMemory + + // CPU usage + val osBean = ManagementFactory.getOperatingSystemMXBean() + var cpuLoad = osBean.systemLoadAverage + if (cpuLoad < 0) { + cpuLoad = osBean.availableProcessors * ManagementFactory.getThreadMXBean().threadCount / 100.0 + } + + val tpsColor = when { + tps >= 18.0 -> "§2" // Dark green + tps >= 15.0 -> "§e" // Yellow + else -> "§c" // Red + } + + return Component.text( + "\n§7RAM: §f${usedMemory}MB / ${maxMemory}MB" + + "\n§7CPU: §f${df.format(cpuLoad)}%" + + "\n§7TPS: $tpsColor${df.format(tps)}" + ) + } + + fun getServerInfo(): String { + val info = StringBuilder() + + // Memory information + val runtime = Runtime.getRuntime() + val maxMemory = runtime.maxMemory() / (1024 * 1024) + val allocatedMemory = runtime.totalMemory() / (1024 * 1024) + val freeMemory = runtime.freeMemory() / (1024 * 1024) + val usedMemory = allocatedMemory - freeMemory + + // CPU usage + val osBean = ManagementFactory.getOperatingSystemMXBean() + var cpuLoad = osBean.systemLoadAverage + if (cpuLoad < 0) { + cpuLoad = osBean.availableProcessors * ManagementFactory.getThreadMXBean().threadCount / 100.0 + } + + // Format server info using string templates + info.append("§6===== SERVER INFO =====\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("§aTPS: ${formatTPS(tps)}\n") + + return info.toString() + } + + private fun formatTPS(tps: Double): String { + return when { + tps >= 18.0 -> "§2${df.format(tps)}" // Dark green + tps >= 15.0 -> "§e${df.format(tps)}" // Yellow + else -> "§c${df.format(tps)}" // Red + } + } + + fun getPingForPlayer(player: Player): Int { + return try { + // Get player ping using reflection + val entityPlayer = player.javaClass.getMethod("getHandle").invoke(player) + val pingField = entityPlayer.javaClass.getField("ping") + pingField.getInt(entityPlayer) + } catch (e: Exception) { + logger.warning("Failed to get ping for player: ${e.message}") + -1 + } + } + + // Command executor inner class + private inner class ServerInfoCommand(private val plugin: Server_info) : CommandExecutor { + + override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array): Boolean { + if (cmd.name.equals("serverinfo", ignoreCase = true)) { + // Send server info + sender.sendMessage(plugin.getServerInfo()) + + // If the sender is a player, show their ping + if (sender is Player) { + val ping = plugin.getPingForPlayer(sender) + sender.sendMessage("§aYour Ping: §f${ping}ms") + } + + return true + } + return false + } + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..b765b72 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,15 @@ +name: ServerInfo +version: 1.0-SNAPSHOT +main: org.server_info.server_info.Server_info +api-version: 1.19 +description: Displays server performance metrics +commands: + serverinfo: + description: Shows server performance metrics + usage: / + aliases: [sinfo, si] + permission: serverinfo.use +permissions: + serverinfo.use: + description: Allows using the serverinfo command + default: true \ No newline at end of file diff --git a/target/classes/META-INF/server_info.kotlin_module b/target/classes/META-INF/server_info.kotlin_module new file mode 100644 index 0000000..ef915de Binary files /dev/null and b/target/classes/META-INF/server_info.kotlin_module differ diff --git a/target/classes/org/server_info/server_info/Server_info$ServerInfoCommand.class b/target/classes/org/server_info/server_info/Server_info$ServerInfoCommand.class new file mode 100644 index 0000000..e6b4530 Binary files /dev/null and b/target/classes/org/server_info/server_info/Server_info$ServerInfoCommand.class differ diff --git a/target/classes/org/server_info/server_info/Server_info$startTPSCalculation$1.class b/target/classes/org/server_info/server_info/Server_info$startTPSCalculation$1.class new file mode 100644 index 0000000..1302502 Binary files /dev/null and b/target/classes/org/server_info/server_info/Server_info$startTPSCalculation$1.class differ diff --git a/target/classes/org/server_info/server_info/Server_info$startTabListUpdater$1.class b/target/classes/org/server_info/server_info/Server_info$startTabListUpdater$1.class new file mode 100644 index 0000000..c6ed5ec Binary files /dev/null and b/target/classes/org/server_info/server_info/Server_info$startTabListUpdater$1.class differ diff --git a/target/classes/org/server_info/server_info/Server_info.class b/target/classes/org/server_info/server_info/Server_info.class new file mode 100644 index 0000000..e2b1750 Binary files /dev/null and b/target/classes/org/server_info/server_info/Server_info.class differ diff --git a/target/classes/plugin.yml b/target/classes/plugin.yml new file mode 100644 index 0000000..b765b72 --- /dev/null +++ b/target/classes/plugin.yml @@ -0,0 +1,15 @@ +name: ServerInfo +version: 1.0-SNAPSHOT +main: org.server_info.server_info.Server_info +api-version: 1.19 +description: Displays server performance metrics +commands: + serverinfo: + description: Shows server performance metrics + usage: / + aliases: [sinfo, si] + permission: serverinfo.use +permissions: + serverinfo.use: + description: Allows using the serverinfo command + default: true \ No newline at end of file diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 0000000..03694f8 --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,3 @@ +artifactId=server_info +groupId=org.server_info +version=1.0-SNAPSHOT diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 0000000..e69de29 diff --git a/target/original-server_info-1.0-SNAPSHOT.jar b/target/original-server_info-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..b44c033 Binary files /dev/null and b/target/original-server_info-1.0-SNAPSHOT.jar differ diff --git a/target/server_info-1.0-SNAPSHOT.jar b/target/server_info-1.0-SNAPSHOT.jar new file mode 100644 index 0000000..3a12142 Binary files /dev/null and b/target/server_info-1.0-SNAPSHOT.jar differ