Skip to content

Commit

Permalink
Added //size -c and //distr -c command options. Describes clipboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
manearrior authored and TomyLobo committed Nov 9, 2012
1 parent 14aa9a5 commit b352f73
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 38 deletions.
40 changes: 40 additions & 0 deletions src/main/java/com/sk89q/worldedit/CuboidClipboard.java
Expand Up @@ -22,12 +22,17 @@

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.
Expand Down Expand Up @@ -421,4 +426,39 @@ public CopiedEntity(LocalEntity entity) {
this.relativePosition = entity.getPosition().getPosition().subtract(getOrigin());
}
}
/**
* Get the block distribution inside a clipboard.
*
* @return
*/
public List<Countable<Integer>> getBlockDistribution() {
List<Countable<Integer>> distribution = new ArrayList<Countable<Integer>>();
Map<Integer, Countable<Integer>> map = new HashMap<Integer, Countable<Integer>>();

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<Integer> c = new Countable<Integer>(id, 1);
map.put(id, c);
distribution.add(c);
}
}
}
}

Collections.sort(distribution);
// Collections.reverse(distribution);

return distribution;
}
}
91 changes: 53 additions & 38 deletions src/main/java/com/sk89q/worldedit/commands/SelectionCommands.java
Expand Up @@ -22,7 +22,6 @@
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;
Expand Down Expand Up @@ -496,31 +495,49 @@ private Vector[] getChangesForEachDir(CommandContext args) {

@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 = "<block>",
Expand All @@ -544,7 +561,7 @@ public void count(CommandContext args, LocalSession session, LocalPlayer player,
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
Expand All @@ -553,35 +570,33 @@ public void count(CommandContext args, LocalSession session, LocalPlayer player,
public void distr(CommandContext args, LocalSession session, LocalPlayer player,
EditSession editSession) throws WorldEditException {

List<Countable<Integer>> 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<Integer> 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<Countable<Integer>> 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<Integer> 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);
}
}

Expand Down

0 comments on commit b352f73

Please sign in to comment.