Skip to content
Permalink
Browse files

Make a bunch of paginations internal, generify command boxes.

  • Loading branch information...
wizjany committed Jul 24, 2019
1 parent e504c29 commit fb526ad604761d96b0cced644bdc64739dee4dbe
@@ -26,6 +26,7 @@
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.command.util.WorldEditAsyncCommandBuilder;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.internal.anvil.ChunkDeleter;
import com.sk89q.worldedit.internal.anvil.ChunkDeletionInfo;
@@ -34,6 +35,7 @@
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
@@ -50,8 +52,8 @@
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION;
@@ -93,11 +95,11 @@ public void chunkInfo(Player player) {
@CommandPermissions("worldedit.listchunks")
public void listChunks(Player player, LocalSession session,
@ArgFlag(name = 'p', desc = "Page number.", def = "1") int page) throws WorldEditException {
Set<BlockVector2> chunks = session.getSelection(player.getWorld()).getChunks();
final Region region = session.getSelection(player.getWorld());

PaginationBox paginationBox = PaginationBox.fromStrings("Selected Chunks", "/listchunks -p %page%",
chunks.stream().map(BlockVector2::toString).collect(Collectors.toList()));
player.print(paginationBox.create(page));
WorldEditAsyncCommandBuilder.createAndSendMessage(player,
() -> new ChunkListPaginationBox(region).create(page),
"Listing chunks for " + player.getName());
}

@Command(
@@ -134,8 +136,8 @@ public void deleteChunks(Player player, LocalSession session,
newBatch.backup = true;
final Region selection = session.getSelection(player.getWorld());
if (selection instanceof CuboidRegion) {
newBatch.minChunk = BlockVector2.at(selection.getMinimumPoint().getBlockX() >> 4, selection.getMinimumPoint().getBlockZ() >> 4);
newBatch.maxChunk = BlockVector2.at(selection.getMaximumPoint().getBlockX() >> 4, selection.getMaximumPoint().getBlockZ() >> 4);
newBatch.minChunk = selection.getMinimumPoint().shr(4).toBlockVector2();
newBatch.maxChunk = selection.getMaximumPoint().shr(4).toBlockVector2();
} else {
// this has a possibility to OOM for very large selections still
Set<BlockVector2> chunks = selection.getChunks();
@@ -168,4 +170,27 @@ public void deleteChunks(Player player, LocalSession session,
.clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/stop"))));
}

private static class ChunkListPaginationBox extends PaginationBox {
//private final Region region;
private final List<BlockVector2> chunks;

ChunkListPaginationBox(Region region) {
super("Selected Chunks", "/listchunks -p %page%");
// TODO make efficient/streamable/calculable implementations of this
// for most region types, so we can just store the region and random-access get one page of chunks
// (this is non-trivial for some types of selections...)
//this.region = region.clone();
this.chunks = new ArrayList<>(region.getChunks());
}

@Override
public Component getComponent(int number) {
return TextComponent.of(chunks.get(number).toString());
}

@Override
public int getComponentsSize() {
return chunks.size();
}
}
}
@@ -19,6 +19,8 @@

package com.sk89q.worldedit.command;

import com.google.common.collect.Multimap;
import com.google.common.io.Files;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
@@ -42,10 +44,10 @@
import com.sk89q.worldedit.util.formatting.component.CodeFormat;
import com.sk89q.worldedit.util.formatting.component.ErrorFormat;
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
import com.sk89q.worldedit.util.formatting.component.SchematicPaginationBox;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.util.io.Closer;
import com.sk89q.worldedit.util.io.file.FilenameException;
@@ -68,7 +70,9 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
@@ -404,4 +408,44 @@ public Component call() throws Exception {
}
return fileList;
}

private static class SchematicPaginationBox extends PaginationBox {
private final String prefix;
private final File[] files;

SchematicPaginationBox(String rootDir, File[] files, String pageCommand) {
super("Available schematics", pageCommand);
this.prefix = rootDir == null ? "" : rootDir;
this.files = files;
}

@Override
public Component getComponent(int number) {
checkArgument(number < files.length && number >= 0);
File file = files[number];
Multimap<String, ClipboardFormat> exts = ClipboardFormats.getFileExtensionMap();
String format = exts.get(Files.getFileExtension(file.getName()))
.stream().findFirst().map(ClipboardFormat::getName).orElse("Unknown");
boolean inRoot = file.getParentFile().getName().equals(prefix);

String path = inRoot ? file.getName() : file.getPath().split(Pattern.quote(prefix + File.separator))[1];

return TextComponent.builder()
.content("")
.append(TextComponent.of("[L]")
.color(TextColor.GOLD)
.clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, "/schem load " + path))
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to load"))))
.append(TextComponent.space())
.append(TextComponent.of(path)
.color(TextColor.DARK_GREEN)
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of(format))))
.build();
}

@Override
public int getComponentsSize() {
return files.length;
}
}
}
@@ -19,6 +19,7 @@

package com.sk89q.worldedit.command;

import com.google.common.base.Strings;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
@@ -56,16 +57,19 @@
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.Countable;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.component.BlockDistributionResult;
import com.sk89q.worldedit.util.formatting.component.CommandListBox;
import com.sk89q.worldedit.util.formatting.component.InvalidComponentException;
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
import com.sk89q.worldedit.util.formatting.component.SubtleFormat;
import com.sk89q.worldedit.util.formatting.component.TextComponentProducer;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.storage.ChunkStore;
@@ -570,7 +574,7 @@ public void select(Player player, LocalSession session,
}
case LIST:
default:
CommandListBox box = new CommandListBox("Selection modes", null);
CommandListBox box = new CommandListBox("Selection modes", null, null);
box.setHidingHelp(true);
TextComponentProducer contents = box.getContents();
contents.append(SubtleFormat.wrap("Select one of the modes below:")).newline();
@@ -608,4 +612,62 @@ public void select(Player player, LocalSession session,
session.dispatchCUISelection(player);
}

private static class BlockDistributionResult extends PaginationBox {

private final List<Countable<BlockState>> distribution;
private final int totalBlocks;
private final boolean separateStates;

BlockDistributionResult(List<Countable<BlockState>> distribution, boolean separateStates) {
super("Block Distribution", "//distr -p %page%" + (separateStates ? " -d" : ""));
this.distribution = distribution;
// note: doing things like region.getArea is inaccurate for non-cuboids.
this.totalBlocks = distribution.stream().mapToInt(Countable::getAmount).sum();
this.separateStates = separateStates;
setComponentsPerPage(7);
}

@Override
public Component getComponent(int number) {
Countable<BlockState> c = distribution.get(number);
TextComponent.Builder line = TextComponent.builder();

final int count = c.getAmount();

final double perc = count / (double) totalBlocks * 100;
final int maxDigits = (int) (Math.log10(totalBlocks) + 1);
final int curDigits = (int) (Math.log10(count) + 1);
line.append(String.format("%s%.3f%% ", perc < 10 ? " " : "", perc), TextColor.GOLD);
final int space = maxDigits - curDigits;
String pad = Strings.repeat(" ", space == 0 ? 2 : 2 * space + 1);
line.append(String.format("%s%s", count, pad), TextColor.YELLOW);

final BlockState state = c.getID();
final BlockType blockType = state.getBlockType();
TextComponent blockName = TextComponent.of(blockType.getName(), TextColor.LIGHT_PURPLE);
TextComponent toolTip;
if (separateStates && state != blockType.getDefaultState()) {
toolTip = TextComponent.of(state.getAsString(), TextColor.GRAY);
blockName = blockName.append(TextComponent.of("*", TextColor.LIGHT_PURPLE));
} else {
toolTip = TextComponent.of(blockType.getId(), TextColor.GRAY);
}
blockName = blockName.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, toolTip));
line.append(blockName);

return line.build();
}

@Override
public int getComponentsSize() {
return distribution.size();
}

@Override
public Component create(int page) throws InvalidComponentException {
super.getContents().append(TextComponent.of("Total Block Count: " + totalBlocks, TextColor.GRAY))
.append(TextComponent.newline());
return super.create(page);
}
}
}
@@ -523,7 +523,8 @@ public void help(Actor actor,
int page,
@Arg(desc = "The command to retrieve help for", def = "", variable = true)
List<String> command) throws WorldEditException {
PrintCommandHelp.help(command, page, listSubCommands, we, actor);
PrintCommandHelp.help(command, page, listSubCommands,
we.getPlatformManager().getPlatformCommandManager().getCommandManager(), actor, "//help");
}

}
@@ -164,6 +164,7 @@ public void help(Actor actor,
int page,
@Arg(desc = "The command to retrieve help for", def = "", variable = true)
List<String> command) throws WorldEditException {
PrintCommandHelp.help(command, page, listSubCommands, we, actor);
PrintCommandHelp.help(command, page, listSubCommands,
we.getPlatformManager().getPlatformCommandManager().getCommandManager(), actor, "/worldedit help");
}
}
@@ -67,11 +67,11 @@ private static Command detectCommand(CommandManager manager, String command) {
return mapping.orElse(null);
}

public static void help(List<String> commandPath, int page, boolean listSubCommands, WorldEdit we, Actor actor) throws InvalidComponentException {
CommandManager manager = we.getPlatformManager().getPlatformCommandManager().getCommandManager();
public static void help(List<String> commandPath, int page, boolean listSubCommands,
CommandManager manager, Actor actor, String helpRootCommand) throws InvalidComponentException {

if (commandPath.isEmpty()) {
printCommands(page, manager.getAllCommands(), actor, ImmutableList.of());
printCommands(page, manager.getAllCommands(), actor, ImmutableList.of(), helpRootCommand);
return;
}

@@ -93,7 +93,7 @@ public static void help(List<String> commandPath, int page, boolean listSubComma
toCommandString(visited), subCommand));
// full help for single command
CommandUsageBox box = new CommandUsageBox(visited, visited.stream()
.map(Command::getName).collect(Collectors.joining(" ")));
.map(Command::getName).collect(Collectors.joining(" ")), helpRootCommand);
actor.print(box.create());
return;
}
@@ -105,7 +105,7 @@ public static void help(List<String> commandPath, int page, boolean listSubComma
actor.printError(String.format("The sub-command '%s' under '%s' could not be found.",
subCommand, toCommandString(visited)));
// list subcommands for currentCommand
printCommands(page, getSubCommands(Iterables.getLast(visited)).values().stream(), actor, visited);
printCommands(page, getSubCommands(Iterables.getLast(visited)).values().stream(), actor, visited, helpRootCommand);
return;
}
}
@@ -114,10 +114,10 @@ public static void help(List<String> commandPath, int page, boolean listSubComma

if (subCommands.isEmpty() || !listSubCommands) {
// Create the message
CommandUsageBox box = new CommandUsageBox(visited, toCommandString(visited));
CommandUsageBox box = new CommandUsageBox(visited, toCommandString(visited), helpRootCommand);
actor.print(box.create());
} else {
printCommands(page, subCommands.values().stream(), actor, visited);
printCommands(page, subCommands.values().stream(), actor, visited, helpRootCommand);
}
}

@@ -126,7 +126,7 @@ private static String toCommandString(List<Command> visited) {
}

private static void printCommands(int page, Stream<Command> commandStream, Actor actor,
List<Command> commandList) throws InvalidComponentException {
List<Command> commandList, String helpRootCommand) throws InvalidComponentException {
// Get a list of aliases
List<Command> commands = commandStream
.sorted(byCleanName())
@@ -135,7 +135,8 @@ private static void printCommands(int page, Stream<Command> commandStream, Actor
String used = commandList.isEmpty() ? null : toCommandString(commandList);
CommandListBox box = new CommandListBox(
(used == null ? "Help" : "Subcommands: " + used),
"//help -s -p %page%" + (used == null ? "" : " " + used));
helpRootCommand + " -s -p %page%" + (used == null ? "" : " " + used),
helpRootCommand);
if (!actor.isPlayer()) {
box.formatForConsole();
}

0 comments on commit fb526ad

Please sign in to comment.
You can’t perform that action at this time.