Merge Moo0's data flag for distr.

Also added data flag to //count.
  //count -d 35 will now only search for white wool.
  //count 35:5 will only search for green wool.
  //count 35 will work as normal.
  //count 35:-1 will also work.
This commit is contained in:
Wizjany
2013-01-19 09:31:00 -05:00
parent c2154b0f86
commit d78bbc4f68
3 changed files with 193 additions and 34 deletions

View File

@ -525,16 +525,40 @@ public class EditSession {
return false;
}
public int countBlock(Region region, Set<Integer> searchIDs) {
Set<BaseBlock> passOn = new HashSet<BaseBlock>();
for (Integer i : searchIDs) {
passOn.add(new BaseBlock(i, -1));
}
return countBlocks(region, passOn);
}
/**
* Count the number of blocks of a list of types in a region.
*
* @param region
* @param searchIDs
* @param searchBlocks
* @return
*/
public int countBlocks(Region region, Set<Integer> searchIDs) {
public int countBlocks(Region region, Set<BaseBlock> searchBlocks) {
int count = 0;
// allow -1 data in the searchBlocks to match any type
Set<BaseBlock> newSet = new HashSet<BaseBlock>() {
@Override
public boolean contains(Object o) {
for (BaseBlock b : this.toArray(new BaseBlock[this.size()])) {
if (o instanceof BaseBlock) {
if (b.equalsFuzzy((BaseBlock) o)) {
return true;
}
}
}
return false;
}
};
newSet.addAll(searchBlocks);
if (region instanceof CuboidRegion) {
// Doing this for speed
Vector min = region.getMinimumPoint();
@ -552,7 +576,8 @@ public class EditSession {
for (int z = minZ; z <= maxZ; ++z) {
Vector pt = new Vector(x, y, z);
if (searchIDs.contains(getBlockType(pt))) {
BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (newSet.contains(compare)) {
++count;
}
}
@ -560,7 +585,8 @@ public class EditSession {
}
} else {
for (Vector pt : region) {
if (searchIDs.contains(getBlockType(pt))) {
BaseBlock compare = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (newSet.contains(compare)) {
++count;
}
}
@ -2671,6 +2697,65 @@ public class EditSession {
return distribution;
}
/**
* Get the block distribution (with data values) inside a region.
*
* @param region
* @return
*/
// TODO reduce code duplication - probably during ops-redux
public List<Countable<BaseBlock>> getBlockDistributionWithData(Region region) {
List<Countable<BaseBlock>> distribution = new ArrayList<Countable<BaseBlock>>();
Map<BaseBlock, Countable<BaseBlock>> map = new HashMap<BaseBlock, Countable<BaseBlock>>();
if (region instanceof CuboidRegion) {
// Doing this for speed
Vector min = region.getMinimumPoint();
Vector max = region.getMaximumPoint();
int minX = min.getBlockX();
int minY = min.getBlockY();
int minZ = min.getBlockZ();
int maxX = max.getBlockX();
int maxY = max.getBlockY();
int maxZ = max.getBlockZ();
for (int x = minX; x <= maxX; ++x) {
for (int y = minY; y <= maxY; ++y) {
for (int z = minZ; z <= maxZ; ++z) {
Vector pt = new Vector(x, y, z);
BaseBlock blk = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (map.containsKey(blk)) {
map.get(blk).increment();
} else {
Countable<BaseBlock> c = new Countable<BaseBlock>(blk, 1);
map.put(blk, c);
distribution.add(c);
}
}
}
}
} else {
for (Vector pt : region) {
BaseBlock blk = new BaseBlock(getBlockType(pt), getBlockData(pt));
if (map.containsKey(blk)) {
map.get(blk).increment();
} else {
Countable<BaseBlock> c = new Countable<BaseBlock>(blk, 1);
map.put(blk, c);
}
}
}
Collections.sort(distribution);
// Collections.reverse(distribution);
return distribution;
}
public int makeShape(final Region region, final Vector zero, final Vector unit, final Pattern pattern, final String expressionString, final boolean hollow) throws ExpressionException, MaxChangedBlocksException {
final Expression expression = Expression.compile(expressionString, "x", "y", "z", "type", "data");
expression.optimize();
@ -2855,4 +2940,5 @@ public class EditSession {
}
} // while
}
}