From 3303434d52ca01be5f80a5611d165654d5146838 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Mon, 29 Jul 2019 05:03:17 +1000 Subject: [PATCH] listchunks doesn't need to be a list Nor does it need to be a string. As long as we can iterate, it should be fine. This somewhat mitigates OOM from listchunks. --- .../worldedit/command/ChunkCommands.java | 3 +- .../formatting/component/PaginationBox.java | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ChunkCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ChunkCommands.java index 36d288eb4..4a170883c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ChunkCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ChunkCommands.java @@ -94,8 +94,7 @@ public class ChunkCommands { @ArgFlag(name = 'p', desc = "Page number.", def = "1") int page) throws WorldEditException { Set chunks = session.getSelection(player.getWorld()).getChunks(); - PaginationBox paginationBox = PaginationBox.fromStrings("Selected Chunks", "/listchunks -p %page%", - chunks.stream().map(BlockVector2::toString).collect(Collectors.toList())); + PaginationBox paginationBox = PaginationBox.fromStrings("Selected Chunks", "/listchunks -p %page%", chunks); player.print(paginationBox.create(page)); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java index 3f9b95267..6125643ed 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/formatting/component/PaginationBox.java @@ -26,6 +26,9 @@ import com.sk89q.worldedit.util.formatting.text.event.HoverEvent; import com.sk89q.worldedit.util.formatting.text.format.TextColor; import javax.annotation.Nullable; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; public abstract class PaginationBox extends MessageBox { @@ -128,21 +131,44 @@ public abstract class PaginationBox extends MessageBox { throw new IllegalStateException("Pagination components must be created with a page"); } + public static PaginationBox fromStrings(String header, @Nullable String pageCommand, Collection lines) { + return new ListPaginationBox(header, pageCommand, lines); + } + public static PaginationBox fromStrings(String header, @Nullable String pageCommand, List lines) { return new ListPaginationBox(header, pageCommand, lines); } private static class ListPaginationBox extends PaginationBox { - private final List lines; + private final Collection lines; + private int iterIndex; + private Iterator iterator; ListPaginationBox(String header, String pageCommand, List lines) { + this(header, pageCommand, (Collection) lines); + } + + ListPaginationBox(String header, String pageCommand, Collection lines) { super(header, pageCommand); this.lines = lines; } @Override public Component getComponent(int number) { - return TextComponent.of(lines.get(number)); + Object obj; + if (lines instanceof List) { + obj = ((List) lines).get(number); + } else { + if (iterator == null || iterIndex > number) { + iterator = lines.iterator(); + iterIndex = 0; + } + do { + obj = iterator.next(); + iterIndex++; + } while (iterIndex < number); + } + return TextComponent.of(obj.toString()); } @Override