From 396faf6732379d810dba3a059e70bbb88956b5b7 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sun, 19 Jun 2022 22:35:36 +0100 Subject: [PATCH] Support using //distr on console properly (#1827) --- .../java/com/sk89q/worldedit/EditSession.java | 5 ++- .../worldedit/command/SelectionCommands.java | 39 ++++++++++++++++--- .../com/sk89q/worldedit/extent/Extent.java | 1 - 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index d32b579fe..0335a2e92 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -2832,13 +2832,16 @@ public class EditSession extends PassthroughExtent implements AutoCloseable { public List> getBlockDistribution(Region region, boolean separateStates) { //FAWE start - get distr if (separateStates) { - return getBlockDistributionWithData(region); + List> distr = getBlockDistributionWithData(region); + Collections.reverse(distr); + return distr; } List> normalDistr = getBlockDistribution(region); List> distribution = new ArrayList<>(); for (Countable count : normalDistr) { distribution.add(new Countable<>(count.getID().getDefaultState(), count.getAmount())); } + Collections.reverse(distribution); //FAWE end return distribution; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java index a248fae3b..e6e06e9a2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java @@ -770,6 +770,10 @@ public class SelectionCommands { private final List> distribution; private final int totalBlocks; private final boolean separateStates; + //FAWE start + private final int maxDigits; + private boolean consoleFormat = false; + //FAWE end public BlockDistributionResult(List> distribution, boolean separateStates) { this(distribution, separateStates, "//distr -p %page%" + (separateStates ? " -d" : "")); @@ -782,6 +786,9 @@ public class SelectionCommands { this.totalBlocks = distribution.stream().mapToInt(Countable::getAmount).sum(); this.separateStates = separateStates; setComponentsPerPage(7); + //FAWE start + this.maxDigits = (int) (Math.log10(distribution.get(0).getAmount()) + 1); + //FAWE end } @Override @@ -791,17 +798,31 @@ public class SelectionCommands { final int count = c.getAmount(); + //FAWE start - better formatting, support console 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); + // Assume console uses monospaced font + final String space = consoleFormat ? " " : " "; + + line.append(String.format("%s%.3f%% ", perc < 10 ? space : "", perc), TextColor.GOLD); + final int diff = maxDigits - curDigits; + final int multipler; + if (consoleFormat) { + multipler = diff + 2; + } else { + multipler = diff == 0 ? 2 : 2 * diff + 1; + } + String pad = Strings.repeat(" ", multipler); + //FAWE end line.append(String.format("%s%s", count, pad), TextColor.YELLOW); final BlockState state = c.getID(); final BlockType blockType = state.getBlockType(); - Component blockName = blockType.getRichName(); + + //FAWE start - better formatting, support console; Translation keys will not work on console + Component blockName = consoleFormat ? TextComponent.of(blockType.getName()) : blockType.getRichName(); + //FAWE end + TextComponent toolTip; if (separateStates && state != blockType.getDefaultState()) { toolTip = TextComponent.of(state.getAsString()); @@ -820,6 +841,14 @@ public class SelectionCommands { return distribution.size(); } + //FAWE start - support for console + @Override + public void formatForConsole() { + this.consoleFormat = true; + super.formatForConsole(); + } + //FAWE end + @Override public Component create(int page) throws InvalidComponentException { super.getContents().append(Caption.of("worldedit.distr.total", TextComponent.of(totalBlocks))) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java index 334ab4da4..fa53076eb 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/Extent.java @@ -621,7 +621,6 @@ public interface Extent extends InputExtent, OutputExtent { } } } - // Collections.reverse(distribution); return distribution; }