|
| 1 | +package dev.themeinerlp.plugindebug; |
| 2 | + |
| 3 | +import io.papermc.lib.PaperLib; |
| 4 | +import io.papermc.paper.datapack.Datapack; |
| 5 | +import org.bukkit.Bukkit; |
| 6 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 7 | +import org.bukkit.plugin.Plugin; |
| 8 | +import org.jetbrains.annotations.NotNull; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.lang.management.ManagementFactory; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +/** |
| 18 | + * Bukkit Debug builder |
| 19 | + */ |
| 20 | +public final class BukkitDebugBuilder extends DebugBuilder<BukkitDebugBuilder> { |
| 21 | + |
| 22 | + private final Pattern privacyRegex = Pattern.compile("\\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\\b"); |
| 23 | + private BukkitDebugBuilder(String uploadServer) { |
| 24 | + super(uploadServer); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Collects the latest log file from bukkit |
| 29 | + * @return the builder |
| 30 | + * @throws IOException if the file empty/null |
| 31 | + */ |
| 32 | + public BukkitDebugBuilder collectLatestSpigotLog() throws IOException { |
| 33 | + var latestLogFile = Path.of("logs", "latest.log"); |
| 34 | + if (Files.size(latestLogFile) >= maxZipFileSize) throw new IllegalStateException( |
| 35 | + String.format("Latest log file is to big only %d bytes allowed", maxZipFileSize) |
| 36 | + ); |
| 37 | + var tempLogFile = Files.createTempFile(tempFile, ".log"); |
| 38 | + var cleanedList = Files.readAllLines(latestLogFile).stream().map(s -> s.replaceAll(privacyRegex.pattern(), "*")).toList(); |
| 39 | + Files.write(tempLogFile, cleanedList); |
| 40 | + addFile(tempLogFile, FileType.LOG, "Latest Log"); |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Collects default paper debug information |
| 46 | + * @return the bukkit builder |
| 47 | + * @throws IOException if the file empty/null |
| 48 | + */ |
| 49 | + |
| 50 | + public BukkitDebugBuilder defaultPaperDebugInformation() throws IOException { |
| 51 | + var tempLogFile = Files.createTempFile(tempFile, ".yaml"); |
| 52 | + var plugins = Arrays.asList(Bukkit.getServer().getPluginManager().getPlugins()); |
| 53 | + plugins.sort(this::sortPluginByName); |
| 54 | + var debugInformation = new YamlConfiguration(); |
| 55 | + debugInformation.set("server.version", Bukkit.getVersion()); |
| 56 | + debugInformation.set("server.plugins", plugins.size()); |
| 57 | + for (Plugin plugin : plugins) { |
| 58 | + var name = plugin.getName(); |
| 59 | + debugInformation.set(String.format("server.plugin.%s.version", name), plugin.getDescription().getVersion()); |
| 60 | + debugInformation.set(String.format("server.plugin.%s.main", name), plugin.getDescription().getMain()); |
| 61 | + debugInformation.set(String.format("server.plugin.%s.authors", name), plugin.getDescription().getAuthors()); |
| 62 | + debugInformation.set(String.format("server.plugin.%s.load-before", name), plugin.getDescription().getLoadBefore()); |
| 63 | + debugInformation.set(String.format("server.plugin.%s.dependencies", name), plugin.getDescription().getDepend()); |
| 64 | + debugInformation.set(String.format("server.plugin.%s.soft-dependencies", name), plugin.getDescription().getSoftDepend()); |
| 65 | + debugInformation.set(String.format("server.plugin.%s.provides", name), plugin.getDescription().getProvides()); |
| 66 | + debugInformation.set(String.format("server.plugin.%s.enabled", name), plugin.isEnabled()); |
| 67 | + } |
| 68 | + if (PaperLib.isPaper()) { |
| 69 | + var dataPacks = Bukkit.getServer().getDatapackManager().getEnabledPacks(); |
| 70 | + debugInformation.set("server.datapacks.count", dataPacks.size()); |
| 71 | + debugInformation.set("server.datapacks.packs", dataPacks.stream().map(Datapack::getName).toList()); |
| 72 | + } |
| 73 | + var runtime = Runtime.getRuntime(); |
| 74 | + var rb = ManagementFactory.getRuntimeMXBean(); |
| 75 | + debugInformation.set("uptime", rb.getUptime()); |
| 76 | + debugInformation.set("jvm-flags", rb.getInputArguments()); |
| 77 | + debugInformation.set("free-memory", runtime.freeMemory()); |
| 78 | + debugInformation.set("max-memory", runtime.maxMemory()); |
| 79 | + debugInformation.set("total-memory", runtime.totalMemory()); |
| 80 | + debugInformation.set("available-processors", runtime.availableProcessors()); |
| 81 | + debugInformation.set("java-name", rb.getVmName()); |
| 82 | + debugInformation.set("java-version", System.getProperty("java.version")); |
| 83 | + debugInformation.set("java-vendor", System.getProperty("java.vendor")); |
| 84 | + debugInformation.set("operating-system", System.getProperty("os.name")); |
| 85 | + debugInformation.set("os-version", System.getProperty("os.version")); |
| 86 | + debugInformation.set("os-arch", System.getProperty("os.arch")); |
| 87 | + addYAML(debugInformation.saveToString(), "Default Paper Debug Information"); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + private int sortPluginByName(@NotNull Plugin A, @NotNull Plugin B) { |
| 92 | + return A.getName().compareTo(B.getName()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Creates a bukkit builder instance with the given bytebin server |
| 97 | + * |
| 98 | + * @param uploadServer bytebin server base url |
| 99 | + */ |
| 100 | + public static BukkitDebugBuilder builder(String uploadServer) { |
| 101 | + return new BukkitDebugBuilder(uploadServer); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments