diff --git a/src/EditSession.java b/src/EditSession.java index e36c83493..c0e80a17d 100644 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -689,7 +689,7 @@ public class EditSession { /** * Drain nearby pools of water or lava. - * + * * @param pos * @param radius * @return number of blocks affected @@ -711,7 +711,7 @@ public class EditSession { while (!queue.empty()) { BlockVector cur = queue.pop(); - + int type = getBlock(cur).getID(); // Check block type @@ -723,7 +723,7 @@ public class EditSession { if (visited.contains(cur)) { continue; } - + visited.add(cur); // Check radius @@ -750,6 +750,75 @@ public class EditSession { return affected; } + + /** + * Level water. + * + * @param pos + * @param radius + * @return number of blocks affected + * @throws MaxChangedBlocksException + */ + public int fixWater(Vector pos, int radius) throws MaxChangedBlocksException { + int affected = 0; + + HashSet visited = new HashSet(); + Stack queue = new Stack(); + + for (int x = pos.getBlockX() - 1; x <= pos.getBlockX() + 1; x++) { + for (int z = pos.getBlockZ() - 1; z <= pos.getBlockZ() + 1; z++) { + for (int y = pos.getBlockY() - 1; y <= pos.getBlockY() + 1; y++) { + int type = getBlock(new Vector(x, y, z)).getID(); + + // Check block type + if (type == 8 || type == 9) { + queue.push(new BlockVector(x, y, z)); + } + } + } + } + + BaseBlock stationaryWater = new BaseBlock(9); + + while (!queue.empty()) { + BlockVector cur = queue.pop(); + + int type = getBlock(cur).getID(); + + // Check block type + if (type != 8 && type != 9 && type != 0) { + continue; + } + + // Don't want to revisit + if (visited.contains(cur)) { + continue; + } + + visited.add(cur); + + if (setBlock(cur, stationaryWater)) { + affected++; + } + + // Check radius + if (pos.distance(cur) > radius) { + continue; + } + + for (int x = cur.getBlockX() - 1; x <= cur.getBlockX() + 1; x++) { + for (int z = cur.getBlockZ() - 1; z <= cur.getBlockZ() + 1; z++) { + BlockVector newPos = new BlockVector(x, cur.getBlockY(), z); + + if (!cur.equals(newPos)) { + queue.push(newPos); + } + } + } + } + + return affected; + } /** diff --git a/src/WorldEdit.java b/src/WorldEdit.java index ac833247e..bc38ece23 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -141,6 +141,7 @@ public class WorldEdit { commands.put("//expand", " [Num] - Expands the selection"); commands.put("//contract", " [Num] - Contracts the selection"); commands.put("//rotate", "[Angle] - Rotate the clipboard"); + commands.put("/fixwater", "[Radius] - Level nearby pools of water"); commands.put("/forestgen", " - Make an ugly pine tree forest"); commands.put("/unstuck", "Go up to the first free spot"); commands.put("/ascend", "Go up one level"); @@ -537,6 +538,16 @@ public class WorldEdit { return true; + // Fix water + } else if(split[0].equalsIgnoreCase("/fixwater")) { + checkArgs(split, 1, 1, split[0]); + int radius = Math.max(0, Integer.parseInt(split[1])); + int affected = editSession.fixWater( + session.getPlacementPosition(player), radius); + player.print(affected + " block(s) have been changed."); + + return true; + // Replace all blocks in the region } else if(split[0].equalsIgnoreCase("//replace")) { checkArgs(split, 1, 2, split[0]);