Skip to content

Commit

Permalink
Add -f flag to //count to allow fuzzy inputs.
Browse files Browse the repository at this point in the history
Also re-implement //distr -c. And remove outdated help text on //copy.
  • Loading branch information
wizjany committed Mar 26, 2019
1 parent 692ba6f commit 8eccdc7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Expand Up @@ -75,8 +75,7 @@ public ClipboardCommands(WorldEdit worldEdit) {
help = "Copy the selection to the clipboard\n" +
"Flags:\n" +
" -e will also copy entities\n" +
" -m sets a source mask so that excluded blocks become air\n" +
"WARNING: Pasting entities cannot yet be undone!",
" -m sets a source mask so that excluded blocks become air",
min = 0,
max = 0
)
Expand Down
Expand Up @@ -36,6 +36,9 @@
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.extension.platform.permission.ActorSelectorLimits;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.block.BlockDistributionCounter;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
Expand Down Expand Up @@ -625,6 +628,7 @@ public void size(Player player, LocalSession session, CommandContext args) throw
@Command(
aliases = { "/count" },
usage = "<block>",
flags = "f",
desc = "Counts the number of a certain type of block",
min = 1,
max = 1
Expand All @@ -638,6 +642,7 @@ public void count(Player player, LocalSession session, EditSession editSession,
context.setWorld(player.getWorld());
context.setSession(session);
context.setRestricted(false);
context.setPreferringWildcard(args.hasFlag('f'));

Set<BaseBlock> searchBlocks = we.getBlockFactory().parseFromListInput(args.getString(0), context);
int count = editSession.countBlocks(session.getSelection(player.getWorld()), searchBlocks);
Expand All @@ -659,36 +664,39 @@ public void count(Player player, LocalSession session, EditSession editSession,
@CommandPermissions("worldedit.analysis.distr")
public void distr(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException, CommandException {

int size;
boolean separateStates = args.hasFlag('d');
List<Countable<BlockState>> distribution;

if (args.hasFlag('c')) {
// TODO: Update for new clipboard
throw new CommandException("Needs to be re-written again");
Clipboard clipboard = session.getClipboard().getClipboard(); // throws if missing
BlockDistributionCounter count = new BlockDistributionCounter(clipboard, separateStates);
RegionVisitor visitor = new RegionVisitor(clipboard.getRegion(), count);
Operations.completeBlindly(visitor);
distribution = count.getDistribution();
} else {
distribution = editSession.getBlockDistribution(session.getSelection(player.getWorld()), separateStates);
size = session.getSelection(player.getWorld()).getArea();
}

if (distribution.isEmpty()) { // *Should* always be false
player.printError("No blocks counted.");
return;
}

// note: doing things like region.getArea is inaccurate for non-cuboids.
int size = distribution.stream().mapToInt(Countable::getAmount).sum();
player.print("# total blocks: " + size);

for (Countable<BlockState> c : distribution) {
String name = c.getID().getBlockType().getName();
String str;
if (separateStates) {
str = String.format("%-7s (%.3f%%) %s #%s",
str = String.format("%-7s (%.3f%%) %s (%s)",
String.valueOf(c.getAmount()),
c.getAmount() / (double) size * 100,
name,
c.getID().getAsString());
} else {
str = String.format("%-7s (%.3f%%) %s #%s",
str = String.format("%-7s (%.3f%%) %s (%s)",
String.valueOf(c.getAmount()),
c.getAmount() / (double) size * 100,
name,
Expand Down

0 comments on commit 8eccdc7

Please sign in to comment.