Added //size -c and //distr -c command options. Describes clipboard.

This commit is contained in:
manearrior 2012-11-07 15:36:35 -05:00 committed by TomyLobo
parent 14aa9a524d
commit b352f7321f
2 changed files with 93 additions and 38 deletions

View File

@ -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<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;
}
}

View File

@ -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 = "<block>",
@ -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<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);
}
}