From 91b7b8444fd43cc5ee961f2fc5094f18a643735f Mon Sep 17 00:00:00 2001 From: zml2008 Date: Sun, 21 Aug 2011 23:55:50 -0700 Subject: [PATCH] Added pattern support to /replacenear, //walls, and //faces --- .../java/com/sk89q/worldedit/EditSession.java | 116 ++++++++++++++++++ .../worldedit/commands/RegionCommands.java | 19 ++- .../worldedit/commands/UtilityCommands.java | 13 +- 3 files changed, 140 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index 879280d9b..4e2bca007 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -1162,6 +1162,70 @@ public class EditSession { return affected; } + /** + * Make faces of the region (as if it was a cuboid if it's not). + * + * @param region + * @param pattern + * @return number of blocks affected + * @throws MaxChangedBlocksException + */ + public int makeCuboidFaces(Region region, Pattern pattern) + throws MaxChangedBlocksException { + int affected = 0; + + 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) { + Vector minV = new Vector(x, y, minZ); + if (setBlock(min, pattern.next(minV))) { + ++affected; + } + Vector maxV = new Vector(x, y, maxZ); + if (setBlock(maxV, pattern.next(maxV))) { + ++affected; + } + ++affected; + } + } + + for (int y = minY; y <= maxY; ++y) { + for (int z = minZ; z <= maxZ; ++z) { + Vector minV = new Vector(minX, y, z); + if (setBlock(minV, pattern.next(minV))) { + ++affected; + } + Vector maxV = new Vector(maxX, y, z); + if (setBlock(maxV, pattern.next(maxV))) { + ++affected; + } + } + } + + for (int z = minZ; z <= maxZ; ++z) { + for (int x = minX; x <= maxX; ++x) { + Vector minV = new Vector(x, minY, z); + if (setBlock(minV, pattern.next(minV))) { + ++affected; + } + Vector maxV = new Vector(x, maxY, z); + if (setBlock(maxV, pattern.next(maxV))) { + ++affected; + } + } + } + + return affected; + } /** * Make walls of the region (as if it was a cuboid if it's not). @@ -1211,6 +1275,58 @@ public class EditSession { return affected; } + /** + * Make walls of the region (as if it was a cuboid if it's not). + * + * @param region + * @param block + * @return number of blocks affected + * @throws MaxChangedBlocksException + */ + public int makeCuboidWalls(Region region, Pattern pattern) + throws MaxChangedBlocksException { + int affected = 0; + + 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) { + Vector minV = new Vector(x, y, minZ); + if (setBlock(minV, pattern.next(minV))) { + ++affected; + } + Vector maxV = new Vector(x, y, maxZ); + if (setBlock(maxV, pattern.next(maxV))) { + ++affected; + } + ++affected; + } + } + + for (int y = minY; y <= maxY; ++y) { + for (int z = minZ; z <= maxZ; ++z) { + Vector minV = new Vector(minX, y, z); + if (setBlock(minV, pattern.next(minV))) { + ++affected; + } + Vector maxV = new Vector(maxX, y, z); + if (setBlock(maxV, pattern.next(maxV))) { + ++affected; + } + } + } + + return affected; + } + /** * Overlays a layer of blocks over a cuboid area. * diff --git a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java index d76976ec4..53f4954cd 100644 --- a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java @@ -159,8 +159,13 @@ public class RegionCommands { LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { - BaseBlock block = we.getBlock(player, args.getString(0)); - int affected = editSession.makeCuboidWalls(session.getSelection(player.getWorld()), block); + Pattern pattern = we.getBlockPattern(player, args.getString(0)); + int affected; + if (pattern instanceof SingleBlockPattern) { + affected = editSession.makeCuboidWalls(session.getSelection(player.getWorld()), ((SingleBlockPattern) pattern).getBlock()); + } else { + affected = editSession.makeCuboidWalls(session.getSelection(player.getWorld()), pattern); + } player.print(affected + " block(s) have been changed."); } @@ -177,9 +182,15 @@ public class RegionCommands { public static void faces(CommandContext args, WorldEdit we, LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { + + Pattern pattern = we.getBlockPattern(player, args.getString(0)); + int affected; + if (pattern instanceof SingleBlockPattern) { + affected = editSession.makeCuboidFaces(session.getSelection(player.getWorld()), ((SingleBlockPattern) pattern).getBlock()); + } else { + affected = editSession.makeCuboidFaces(session.getSelection(player.getWorld()), pattern); + } - BaseBlock block = we.getBlock(player, args.getString(0)); - int affected = editSession.makeCuboidFaces(session.getSelection(player.getWorld()), block); player.print(affected + " block(s) have been changed."); } diff --git a/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java b/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java index aa1134ffd..0ff9b8f9d 100644 --- a/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java @@ -238,14 +238,15 @@ public class UtilityCommands { throws WorldEditException { int size = Math.max(1, args.getInteger(0)); + int affected; Set from; - BaseBlock to; + Pattern to; if (args.argsLength() == 2) { from = null; - to = we.getBlock(player, args.getString(1)); + to = we.getBlockPattern(player, args.getString(1)); } else { from = we.getBlockIDs(player, args.getString(1), true); - to = we.getBlock(player, args.getString(2)); + to = we.getBlockPattern(player, args.getString(2)); } Vector base = session.getPlacementPosition(player); @@ -253,7 +254,11 @@ public class UtilityCommands { Vector max = base.add(size, size, size); Region region = new CuboidRegion(min, max); - int affected = editSession.replaceBlocks(region, from, to); + if (to instanceof SingleBlockPattern) { + affected = editSession.replaceBlocks(region, from, ((SingleBlockPattern) to).getBlock()); + } else { + affected = editSession.replaceBlocks(region, from, to); + } player.print(affected + " block(s) have been replaced."); }