Added /editdrain, added BlockPoint that compares using ints.

This commit is contained in:
sk89q 2010-10-11 11:17:32 -07:00
parent 7823aa2a6c
commit f64cc76906
4 changed files with 183 additions and 2 deletions

View File

@ -621,4 +621,77 @@ public class EditSession {
return affected; return affected;
} }
/**
* Drain nearby pools of water or lava.
*
* @param pos
* @param radius
* @return number of blocks affected
* @throws MaxChangedBlocksException
*/
public int drainArea(Point pos, int radius) throws MaxChangedBlocksException {
int affected = 0;
HashSet<BlockPoint> visited = new HashSet<BlockPoint>();
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++) {
affected += drainPool(new Point(x, y, z), pos, radius, visited);
}
}
}
return affected;
}
/**
* Drains a water or lava block and any lava/water blocks next to it.
*
* @param pos
* @param origin
* @param radius
* @param visited
* @return number of blocks affected
* @throws MaxChangedBlocksException
*/
private int drainPool(Point pos, Point origin, int radius, HashSet<BlockPoint> visited)
throws MaxChangedBlocksException {
int type = getBlock(pos);
int affected = 0;
if (type != 8 && type != 9 && type != 10 && type != 11) {
return 0;
}
BlockPoint blockPos = new BlockPoint(pos);
if (visited.contains(blockPos)) {
return 0;
}
visited.add(blockPos);
if (pos.distance(origin) > radius) {
return 0;
}
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++) {
Point newPos = new Point(x, y, z);
if (!pos.equals(newPos)) {
affected += drainPool(newPos, origin, radius, visited);
}
}
}
}
if (setBlock(pos, 0)) {
affected++;
}
return affected;
}
} }

View File

@ -73,6 +73,7 @@ public class WorldEdit extends Plugin {
commands.put("/editload", "[Filename] - Load .schematic into clipboard"); commands.put("/editload", "[Filename] - Load .schematic into clipboard");
commands.put("/editsave", "[Filename] - Save clipboard to .schematic"); commands.put("/editsave", "[Filename] - Save clipboard to .schematic");
commands.put("/editfill", "[ID] [Radius] <Depth> - Fill a hole"); commands.put("/editfill", "[ID] [Radius] <Depth> - Fill a hole");
commands.put("/editdrain", "[Radius] - Drain nearby water/lava pools");
commands.put("/editscript", "[Filename] <Args...> - Run a WorldEdit script"); commands.put("/editscript", "[Filename] <Args...> - Run a WorldEdit script");
commands.put("/editlimit", "[Num] - See documentation"); commands.put("/editlimit", "[Num] - See documentation");
commands.put("/unstuck", "Go up to the first free spot"); commands.put("/unstuck", "Go up to the first free spot");
@ -573,6 +574,15 @@ public class WorldEdit extends Plugin {
return true; return true;
// Drain pools
} else if(split[0].equalsIgnoreCase("/editdrain")) {
checkArgs(split, 1, 1, split[0]);
int radius = Math.max(0, Integer.parseInt(split[1]));
int affected = editSession.drainArea(player.getBlockIn(), radius);
player.print(affected + " block(s) have been changed.");
return true;
// Replace all blocks in the region // Replace all blocks in the region
} else if(split[0].equalsIgnoreCase("/editreplace")) { } else if(split[0].equalsIgnoreCase("/editreplace")) {
checkArgs(split, 1, 2, split[0]); checkArgs(split, 1, 2, split[0]);

View File

@ -0,0 +1,65 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit;
/**
* Extension of Point that supports being compared as ints (for accuracy).
*
* @author sk89q
*/
public class BlockPoint extends Point {
/**
* Construct the Point object.
*
* @param pt
*/
public BlockPoint(Point pt) {
super(pt);
}
/**
* Checks if another object is equivalent.
*
* @param obj
* @return whether the other object is equivalent
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Point)) {
return false;
}
Point other = (Point)obj;
return (int)other.x == (int)this.x && (int)other.y == (int)this.y
&& (int)other.z == (int)this.z;
}
/**
* Gets the hash code.
*
* @return hash code
*/
@Override
public int hashCode() {
return (Integer.valueOf((int)x).hashCode() >> 13) ^
(Integer.valueOf((int)y).hashCode() >> 7) ^
Integer.valueOf((int)z).hashCode();
}
}

View File

@ -23,8 +23,8 @@ package com.sk89q.worldedit;
* *
* @author Albert * @author Albert
*/ */
public final class Point { public class Point {
private final double x, y, z; protected final double x, y, z;
/** /**
* Construct the Point object. * Construct the Point object.
@ -65,6 +65,17 @@ public final class Point {
this.z = (double)z; this.z = (double)z;
} }
/**
* Construct the Point object.
*
* @param pt
*/
public Point(Point pt) {
this.x = pt.x;
this.y = pt.y;
this.z = pt.z;
}
/** /**
* Construct the Point object. * Construct the Point object.
*/ */
@ -303,6 +314,18 @@ public final class Point {
return new Point(this.x / x, this.y / y, this.z / z); return new Point(this.x / x, this.y / y, this.z / z);
} }
/**
* Get the distance away from a point.
*
* @param pt
* @return distance
*/
public double distance(Point pt) {
return Math.sqrt(Math.pow(pt.x - x, 2) +
Math.pow(pt.y - y, 2) +
Math.pow(pt.z - z, 2));
}
/** /**
* Get a block point from a point. * Get a block point from a point.
* *
@ -344,4 +367,14 @@ public final class Point {
((new Double(y)).hashCode() >> 7) ^ ((new Double(y)).hashCode() >> 7) ^
(new Double(z)).hashCode(); (new Double(z)).hashCode();
} }
/**
* Returns string representation "(x, y, z)".
*
* @return string
*/
@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}
} }