From b352f7321f2062e9b2bea6af59190be8f2dc7689 Mon Sep 17 00:00:00 2001 From: manearrior Date: Wed, 7 Nov 2012 15:36:35 -0500 Subject: [PATCH] Added //size -c and //distr -c command options. Describes clipboard. --- .../com/sk89q/worldedit/CuboidClipboard.java | 40 ++++++++ .../worldedit/commands/SelectionCommands.java | 91 +++++++++++-------- 2 files changed, 93 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/CuboidClipboard.java b/src/main/java/com/sk89q/worldedit/CuboidClipboard.java index 577f4cfb1..3bd9676f2 100644 --- a/src/main/java/com/sk89q/worldedit/CuboidClipboard.java +++ b/src/main/java/com/sk89q/worldedit/CuboidClipboard.java @@ -22,12 +22,17 @@ package com.sk89q.worldedit; import com.sk89q.worldedit.blocks.*; import com.sk89q.worldedit.data.*; +import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.schematic.SchematicFormat; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * The clipboard remembers the state of a cuboid region. @@ -421,4 +426,39 @@ public class CuboidClipboard { this.relativePosition = entity.getPosition().getPosition().subtract(getOrigin()); } } + /** + * Get the block distribution inside a clipboard. + * + * @return + */ + public List> getBlockDistribution() { + List> distribution = new ArrayList>(); + Map> map = new HashMap>(); + + int maxX = getWidth(); + int maxY = getHeight(); + int maxZ = getLength(); + + for (int x = 0; x < maxX; ++x) { + for (int y = 0; y < maxY; ++y) { + for (int z = 0; z < maxZ; ++z) { + + int id = data[x][y][z].getId(); + + if (map.containsKey(id)) { + map.get(id).increment(); + } else { + Countable c = new Countable(id, 1); + map.put(id, c); + distribution.add(c); + } + } + } + } + + Collections.sort(distribution); + // Collections.reverse(distribution); + + return distribution; + } } diff --git a/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java b/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java index 8b6f3de55..e9e7bc443 100644 --- a/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java @@ -22,7 +22,6 @@ package com.sk89q.worldedit.commands; import java.util.ArrayList; import java.util.List; import java.util.Set; -import java.util.logging.Logger; import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandAlias; import com.sk89q.minecraft.util.commands.CommandContext; @@ -496,31 +495,49 @@ public class SelectionCommands { @Command( aliases = { "/size" }, + flags = "c", usage = "", desc = "Get information about the selection", min = 0, max = 0 ) @CommandPermissions("worldedit.selection.size") - public void size(CommandContext args, LocalSession session, LocalPlayer player, EditSession editSession) - throws WorldEditException { - + public void size(CommandContext args, LocalSession session, LocalPlayer player, + EditSession editSession) throws WorldEditException { + + if (args.hasFlag('c')) { + CuboidClipboard clipboard = session.getClipboard(); + Vector size = clipboard.getSize(); + Vector offset = clipboard.getOffset(); + + player.print("Size: " + size); + player.print("Offset: " + offset); + player.print("Cuboid distance: " + size.distance( new Vector(1, 1, 1))); + player.print("# of blocks: " + + (int) (size.getX() * size.getY() * size.getZ())); + return; + } + Region region = session.getSelection(player.getWorld()); Vector size = region.getMaximumPoint() .subtract(region.getMinimumPoint()) .add(1, 1, 1); - - player.print("Type: " + session.getRegionSelector(player.getWorld()).getTypeName()); - for (String line : session.getRegionSelector(player.getWorld()).getInformationLines()) { + player.print("Type: " + session.getRegionSelector(player.getWorld()) + .getTypeName()); + + for (String line : session.getRegionSelector(player.getWorld()) + .getInformationLines()) { player.print(line); } - + player.print("Size: " + size); - player.print("Cuboid distance: " + region.getMaximumPoint().distance(region.getMinimumPoint())); - player.print("# of blocks: " + region.getArea()); + player.print("Cuboid distance: " + region.getMaximumPoint() + .distance(region.getMinimumPoint())); + player.print("# of blocks: " + region.getArea()); } + @Command( aliases = { "/count" }, usage = "", @@ -544,7 +561,7 @@ public class SelectionCommands { desc = "Get the distribution of blocks in the selection", help = "Gets the distribution of blocks in the selection.\n" + - "The -c flag makes it print to the console as well.", + "The -c flag gets the distribution of your clipboard.", flags = "c", min = 0, max = 0 @@ -553,35 +570,33 @@ public class SelectionCommands { public void distr(CommandContext args, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { - List> distribution = - editSession.getBlockDistribution(session.getSelection(player.getWorld())); - - Logger logger = Logger.getLogger("Minecraft.WorldEdit"); - - if (distribution.size() > 0) { // *Should* always be true - int size = session.getSelection(player.getWorld()).getArea(); - - player.print("# total blocks: " + size); - - if (args.hasFlag('c')) { - logger.info("Block distribution (req. by " + player.getName() + "):"); - logger.info("# total blocks: " + size); - } - - for (Countable c : distribution) { - BlockType block = BlockType.fromID(c.getID()); - String str = String.format("%-7s (%.3f%%) %s #%d", - String.valueOf(c.getAmount()), - c.getAmount() / (double) size * 100, - block == null ? "Unknown" : block.getName(), c.getID()); - player.print(str); - - if (args.hasFlag('c')) { - logger.info(str); - } - } + List> distribution; + int size; + + if (args.hasFlag('c')) { + CuboidClipboard clip = session.getClipboard(); + distribution = clip.getBlockDistribution(); + size = clip.getHeight() * clip.getLength() * clip.getWidth(); } else { + distribution = editSession + .getBlockDistribution(session.getSelection(player.getWorld())); + size = session.getSelection(player.getWorld()).getArea(); + } + + if (distribution.size() <= 0) { // *Should* always be true player.printError("No blocks counted."); + return; + } + + player.print("# total blocks: " + size); + + for (Countable c : distribution) { + BlockType block = BlockType.fromID(c.getID()); + String str = String.format("%-7s (%.3f%%) %s #%d", + String.valueOf(c.getAmount()), + c.getAmount() / (double) size * 100, + block == null ? "Unknown" : block.getName(), c.getID()); + player.print(str); } }