1+ @file:JvmName(" BukkitExtension" )
2+ package net.onelitefeather.plugindebug
3+
4+ import io.papermc.lib.PaperLib
5+ import org.bukkit.Bukkit
6+ import org.bukkit.configuration.file.YamlConfiguration
7+ import org.bukkit.plugin.java.JavaPlugin
8+ import java.lang.management.ManagementFactory
9+ import java.nio.file.Files
10+ import java.nio.file.Path
11+ import kotlin.io.path.fileSize
12+ import kotlin.io.path.readLines
13+ import kotlin.io.path.writeLines
14+ import kotlin.io.path.writeText
15+
16+ fun DebugBuilder.enableLatestLogSpigot (): DebugBuilder {
17+ val latestLogFile = Path .of(" logs" , " latest.log" )
18+ val tempPrefixField = DebugBuilder ::class .java.getDeclaredField(" tempPrefix" )
19+ tempPrefixField.isAccessible = true
20+ val maxPartFileSizeField = DebugBuilder ::class .java.getDeclaredField(" maxPartFileSize" )
21+ maxPartFileSizeField.isAccessible = true
22+ val fileToUploadExtension = DebugBuilder ::class .java.getDeclaredField(" fileToUpload" )
23+ fileToUploadExtension.isAccessible = true
24+ if (latestLogFile.fileSize() >= maxPartFileSizeField.getLong(this ))
25+ throw IllegalStateException (" Latest log is to big only {} bytes allowed" .format( maxPartFileSizeField.getLong(this )))
26+ val logFile = Files .createTempFile(tempPrefixField.get(this ) as String , " .log" )
27+ logFile.writeLines(latestLogFile.readLines().map { it.replace(PRIVACY_REGEX , " *" ) })
28+
29+ (fileToUploadExtension.get(this ) as MutableList <DebugFile >).add(DebugFile (logFile, FileType .LOG , " Latest Log" ))
30+ return this
31+ }
32+
33+ fun DebugBuilder.defaultPaperDebugInformation (): DebugBuilder {
34+ val tempPrefixField = DebugBuilder ::class .java.getDeclaredField(" tempPrefix" )
35+ tempPrefixField.isAccessible = true
36+ val fileToUploadExtension = DebugBuilder ::class .java.getDeclaredField(" fileToUpload" )
37+ fileToUploadExtension.isAccessible = true
38+ val debugYaml = Files .createTempFile(tempPrefixField.get(this ) as String , " .yaml" )
39+ val plugins = Bukkit .getServer().pluginManager.plugins.toMutableList()
40+ plugins.sortWith(Comparator .comparing { obj -> obj.name })
41+
42+ val debugInformation = YamlConfiguration ()
43+ debugInformation.set(" debugVersion" , 1 )
44+ debugInformation.set(" server.version" , Bukkit .getVersion())
45+ debugInformation.set(" server.plugins" , plugins.count())
46+ plugins.forEach {
47+ debugInformation.set(" server.plugin.${it.name} .version" , it.description.version)
48+ debugInformation.set(" server.plugin.${it.name} .enabled" , it.isEnabled)
49+ debugInformation.set(" server.plugin.${it.name} .main" , it.description.main)
50+ debugInformation.set(" server.plugin.${it.name} .authors" , it.description.authors)
51+ debugInformation.set(" server.plugin.${it.name} .load-before" , it.description.loadBefore)
52+ debugInformation.set(" server.plugin.${it.name} .dependencies" , it.description.depend)
53+ debugInformation.set(" server.plugin.${it.name} .soft-dependencies" , it.description.softDepend)
54+ debugInformation.set(" server.plugin.${it.name} .provides" , it.description.provides)
55+ }
56+ if (PaperLib .isPaper()) {
57+ val datapacks = Bukkit .getServer().datapackManager.enabledPacks
58+ debugInformation.set(" server.datapacks.count" , datapacks.count())
59+ debugInformation.set(" server.datapacks.packs" , datapacks.map { it.name }.toList())
60+ }
61+ val runtime = Runtime .getRuntime()
62+ val rb = ManagementFactory .getRuntimeMXBean()
63+ debugInformation.set(" uptime" , rb.uptime)
64+ debugInformation.set(" jvm-flags" , rb.inputArguments)
65+ debugInformation.set(" free-memory" , runtime.freeMemory())
66+ debugInformation.set(" max-memory" , runtime.maxMemory())
67+ debugInformation.set(" total-memory" , runtime.totalMemory())
68+ debugInformation.set(" available-processors" , runtime.availableProcessors())
69+ debugInformation.set(" java-name" , rb.vmName)
70+ debugInformation.set(" java-version" , System .getProperty(" java.version" ))
71+ debugInformation.set(" java-vendor" , System .getProperty(" java.vendor" ))
72+ debugInformation.set(" operating-system" , System .getProperty(" os.name" ))
73+ debugInformation.set(" os-version" , System .getProperty(" os.version" ))
74+ debugInformation.set(" os-arch" , System .getProperty(" os.arch" ))
75+ debugYaml.writeText(debugInformation.saveToString())
76+ (fileToUploadExtension.get(this ) as MutableList <DebugFile >).add(DebugFile (debugYaml, FileType .YAML , " Default Paper Debug Information" ))
77+ return this
78+ }
0 commit comments