Skip to content

Commit

Permalink
Truncate tags command clipboard text to 1000 entries
Browse files Browse the repository at this point in the history
  • Loading branch information
sciwhiz12 committed Sep 15, 2024
1 parent 3ae5774 commit 9de94d9
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
* </ul>
*/
class TagsCommand {
// The limit of how many elements can be in the clipboard text at once
// Roughly equal to how many 32-characters-long elements can fit into a 32,767-long string
// (which is the default limit for UTF-8 strings in FriendlyByteBuf
private static final long CLIPBOARD_TEXT_ELEMENTS_LIMIT = 1000;
private static final long PAGE_SIZE = 8;
private static final ResourceKey<Registry<Registry<?>>> ROOT_REGISTRY_KEY = ResourceKey.createRegistryKey(ResourceLocation.withDefaultNamespace("root"));

Expand Down Expand Up @@ -172,16 +176,26 @@ private static MutableComponent createMessage(final MutableComponent header,
final long currentPage,
final ChatFormatting elementColor,
final Supplier<Stream<String>> names) {
final String allElementNames = names.get().sorted().collect(Collectors.joining("\n"));
final long totalPages = (count - 1) / PAGE_SIZE + 1;
final long actualPage = (long) Mth.clamp(currentPage, 1, totalPages);

MutableComponent containsComponent = Component.translatable(containsText, count);
if (count > 0) // Highlight the count text, make it clickable, and append page counters
{
final String clipboardText;
final String clipboardElements = names.get().sorted().limit(CLIPBOARD_TEXT_ELEMENTS_LIMIT).collect(Collectors.joining("\n"));
if (count > CLIPBOARD_TEXT_ELEMENTS_LIMIT) {
// Over the limit; add additional info to clipboard text
clipboardText = "(Too many entries to fit in clipboard, showing only first " + CLIPBOARD_TEXT_ELEMENTS_LIMIT + " entries...)" + '\n'
+ clipboardElements + '\n'
+ "(..." + (count - CLIPBOARD_TEXT_ELEMENTS_LIMIT) + " more entries not shown)";
} else {
clipboardText = clipboardElements;
}

containsComponent = ComponentUtils.wrapInSquareBrackets(containsComponent.withStyle(s -> s
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, allElementNames))
.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, clipboardText))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
Component.translatable(copyHoverText)))));
containsComponent = Component.translatable("commands.neoforge.tags.page_info",
Expand Down

0 comments on commit 9de94d9

Please sign in to comment.