From ed1b30c6f68c94194119e48d16191e93aa305782 Mon Sep 17 00:00:00 2001
From: rockyhawk64 <rockyhawk99@gmail.com>
Date: Fri, 12 Apr 2024 10:02:09 +1000
Subject: [PATCH] 3.20.1.0 #4

---
 .../editor/CommandPanelsEditor.java           | 28 ++++++++++++-------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/src/me/rockyhawk/commandpanels/editor/CommandPanelsEditor.java b/src/me/rockyhawk/commandpanels/editor/CommandPanelsEditor.java
index d0f0f1a..7137adb 100644
--- a/src/me/rockyhawk/commandpanels/editor/CommandPanelsEditor.java
+++ b/src/me/rockyhawk/commandpanels/editor/CommandPanelsEditor.java
@@ -52,18 +52,26 @@ public class CommandPanelsEditor implements CommandExecutor {
 
                         // Prepend "fileName: {name}" and "filePath: {relativePath}" to the YAML content
                         String yamlWithFileNameAndPath = "fileName: " + (relativePath.replaceFirst("[.][^.]+$", "")) + "\n" + fileContents;
+                        byte[] contentBytes = yamlWithFileNameAndPath.getBytes(StandardCharsets.UTF_8);
 
-                        // Create a clickable text component with the modified YAML content
-                        BaseComponent[] components = new ComponentBuilder(plugin.tag +
-                                net.md_5.bungee.api.ChatColor.GREEN + "Click here to copy " +
-                                net.md_5.bungee.api.ChatColor.WHITE + panel.getFile().getName() +
-                                net.md_5.bungee.api.ChatColor.GREEN + " to the clipboard!")
-                                .event(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, yamlWithFileNameAndPath))
-                                .create();
+                        //65535 is maximum value that can be represented by an unsigned 16-bit binary number
+                        if (contentBytes.length > 65535) {
+                            // If the content is too large, notify the player to use a different method
+                            sender.sendMessage(plugin.tag + ChatColor.RED +
+                                    "Content too long to paste in chat. Please copy and paste the panel file into the editor manually.");
+                        } else {
+                            // Safe to send
+                            BaseComponent[] components = new ComponentBuilder(plugin.tag +
+                                    net.md_5.bungee.api.ChatColor.GREEN + "Click here to copy " +
+                                    net.md_5.bungee.api.ChatColor.WHITE + panel.getFile().getName() +
+                                    net.md_5.bungee.api.ChatColor.GREEN + " to the clipboard!")
+                                    .event(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, yamlWithFileNameAndPath))
+                                    .create();
+
+                            Player player = (Player) sender;
+                            player.spigot().sendMessage(components);
+                        }
 
-                        // Send the clickable text to the player
-                        Player player = (Player) sender;
-                        player.spigot().sendMessage(components);
                         return true;
                     }
                 }