From f07cda9bb925ed4f9782bcfb5f98b31c66be381b Mon Sep 17 00:00:00 2001
From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
Date: Sat, 19 Nov 2022 20:10:13 +0100
Subject: [PATCH] Sync chunk load command improvements (#8554)

---
 .../Add-debug-for-sync-chunk-loads.patch      | 26 +++++++++++++------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/patches/server/Add-debug-for-sync-chunk-loads.patch b/patches/server/Add-debug-for-sync-chunk-loads.patch
index 9909c2cb52..4075a8f74e 100644
--- a/patches/server/Add-debug-for-sync-chunk-loads.patch
+++ b/patches/server/Add-debug-for-sync-chunk-loads.patch
@@ -231,9 +231,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +import java.io.FileOutputStream;
 +import java.io.PrintStream;
 +import java.io.StringWriter;
++import java.nio.charset.StandardCharsets;
 +import java.time.LocalDateTime;
 +import java.time.format.DateTimeFormatter;
 +import java.util.List;
++
++import net.kyori.adventure.text.event.ClickEvent;
++import net.kyori.adventure.text.event.HoverEvent;
++import net.minecraft.server.MinecraftServer;
 +import org.bukkit.command.CommandSender;
 +import org.checkerframework.checker.nullness.qual.NonNull;
 +import org.checkerframework.framework.qual.DefaultQualifier;
@@ -242,6 +247,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
 +import static net.kyori.adventure.text.format.NamedTextColor.GREEN;
 +import static net.kyori.adventure.text.format.NamedTextColor.RED;
++import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
 +
 +@DefaultQualifier(NonNull.class)
 +public final class SyncLoadInfoCommand implements PaperSubcommand {
@@ -256,9 +262,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        return CommandUtil.getListMatchingLast(sender, args, "clear");
 +    }
 +
++    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss");
++
 +    private void doSyncLoadInfo(final CommandSender sender, final String[] args) {
 +        if (!SyncLoadFinder.ENABLED) {
-+            sender.sendMessage(text("This command requires the server startup flag '-Dpaper.debug-sync-loads=true' to be set.", RED));
++            String systemFlag = "-Dpaper.debug-sync-loads=true";
++            sender.sendMessage(text().color(RED).append(text("This command requires the server startup flag '")).append(
++                text(systemFlag, WHITE).clickEvent(ClickEvent.copyToClipboard(systemFlag))
++                                       .hoverEvent(HoverEvent.showText(text("Click to copy the system flag")))).append(
++                text("' to be set.")));
 +            return;
 +        }
 +
@@ -269,7 +281,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        }
 +
 +        File file = new File(new File(new File("."), "debug"),
-+            "sync-load-info" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss").format(LocalDateTime.now()) + ".txt");
++            "sync-load-info" + FORMATTER.format(LocalDateTime.now()) + ".txt");
 +        file.getParentFile().mkdirs();
 +        sender.sendMessage(text("Writing sync load info to " + file, GREEN));
 +
@@ -283,17 +295,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +            jsonWriter.setLenient(false);
 +            Streams.write(data, jsonWriter);
 +
-+            String fileData = stringWriter.toString();
-+
 +            try (
-+                PrintStream out = new PrintStream(new FileOutputStream(file), false, "UTF-8")
++                PrintStream out = new PrintStream(new FileOutputStream(file), false, StandardCharsets.UTF_8)
 +            ) {
-+                out.print(fileData);
++                out.print(stringWriter);
 +            }
 +            sender.sendMessage(text("Successfully written sync load information!", GREEN));
 +        } catch (Throwable thr) {
-+            sender.sendMessage(text("Failed to write sync load information!", RED));
-+            thr.printStackTrace();
++            sender.sendMessage(text("Failed to write sync load information! See the console for more info.", RED));
++            MinecraftServer.LOGGER.warn("Error occurred while dumping sync chunk load info", thr);
 +        }
 +    }
 +}