Added support for replacing a list of blocks with one block with //replace.

This commit is contained in:
sk89q 2010-11-06 09:35:05 -07:00
parent cd16ca3abb
commit 01340152e3
4 changed files with 42 additions and 13 deletions

View File

@ -546,7 +546,7 @@ public class EditSession {
* @return number of blocks affected * @return number of blocks affected
* @throws MaxChangedBlocksException * @throws MaxChangedBlocksException
*/ */
public int replaceBlocks(Region region, int fromBlockType, BaseBlock toBlock) public int replaceBlocks(Region region, Set<Integer> fromBlockTypes, BaseBlock toBlock)
throws MaxChangedBlocksException { throws MaxChangedBlocksException {
int affected = 0; int affected = 0;
@ -561,8 +561,8 @@ public class EditSession {
Vector pt = new Vector(x, y, z); Vector pt = new Vector(x, y, z);
int curBlockType = getBlock(pt).getID(); int curBlockType = getBlock(pt).getID();
if (fromBlockType == -1 && curBlockType != 0 || if (fromBlockTypes == null && curBlockType != 0 ||
curBlockType == fromBlockType) { fromBlockTypes.contains(curBlockType)) {
if (setBlock(pt, toBlock)) { if (setBlock(pt, toBlock)) {
affected++; affected++;
} }
@ -574,8 +574,8 @@ public class EditSession {
for (Vector pt : region) { for (Vector pt : region) {
int curBlockType = getBlock(pt).getID(); int curBlockType = getBlock(pt).getID();
if (fromBlockType == -1 && curBlockType != 0 || if (fromBlockTypes == null && curBlockType != 0 ||
curBlockType == fromBlockType) { fromBlockTypes.contains(curBlockType)) {
if (setBlock(pt, toBlock)) { if (setBlock(pt, toBlock)) {
affected++; affected++;
} }

View File

@ -227,7 +227,7 @@ public class WorldEditListener extends PluginListener {
} }
if (blockType == null) { if (blockType == null) {
throw new UnknownItemException(); throw new UnknownItemException(arg);
} }
// Check if the item is allowed // Check if the item is allowed
@ -245,7 +245,7 @@ public class WorldEditListener extends PluginListener {
return new BaseBlock(blockType.getID(), data); return new BaseBlock(blockType.getID(), data);
} }
throw new DisallowedItemException(); throw new DisallowedItemException(arg);
} }
/** /**
@ -257,10 +257,23 @@ public class WorldEditListener extends PluginListener {
* @throws DisallowedItemException * @throws DisallowedItemException
*/ */
public BaseBlock getBlock(String id) throws UnknownItemException, public BaseBlock getBlock(String id) throws UnknownItemException,
DisallowedItemException { DisallowedItemException {
return getBlock(id, false); return getBlock(id, false);
} }
/**
* Get a list of blocks as a set.
*/
public Set<Integer> getBlockIDs(String list) throws UnknownItemException,
DisallowedItemException {
String[] items = list.split(",");
Set<Integer> blocks = new HashSet<Integer>();
for (String s : items) {
blocks.add(getBlock(s).getID());
}
return blocks;
}
/** /**
* Checks to make sure that there are enough but not too many arguments. * Checks to make sure that there are enough but not too many arguments.
* *
@ -764,13 +777,13 @@ public class WorldEditListener extends PluginListener {
// Replace all blocks in the region // Replace all blocks in the region
} else if(split[0].equalsIgnoreCase("//replace")) { } else if(split[0].equalsIgnoreCase("//replace")) {
checkArgs(split, 1, 2, split[0]); checkArgs(split, 1, 2, split[0]);
int from; Set<Integer> from;
BaseBlock to; BaseBlock to;
if (split.length == 2) { if (split.length == 2) {
from = -1; from = null;
to = getBlock(split[1], true); to = getBlock(split[1], true);
} else { } else {
from = getBlock(split[1]).getID(); from = getBlockIDs(split[1]);
to = getBlock(split[2]); to = getBlock(split[2]);
} }
@ -1374,9 +1387,9 @@ public class WorldEditListener extends PluginListener {
} catch (IncompleteRegionException e2) { } catch (IncompleteRegionException e2) {
ply.sendMessage(Colors.Rose + "The edit region has not been fully defined."); ply.sendMessage(Colors.Rose + "The edit region has not been fully defined.");
} catch (UnknownItemException e3) { } catch (UnknownItemException e3) {
ply.sendMessage(Colors.Rose + "Block name was not recognized."); ply.sendMessage(Colors.Rose + "Block name '" + e3.getID() + "' was not recognized.");
} catch (DisallowedItemException e4) { } catch (DisallowedItemException e4) {
ply.sendMessage(Colors.Rose + "Block not allowed (see WorldEdit configuration)."); ply.sendMessage(Colors.Rose + "Block '" + e4.getID() + "' not allowed (see WorldEdit configuration).");
} catch (MaxChangedBlocksException e5) { } catch (MaxChangedBlocksException e5) {
ply.sendMessage(Colors.Rose + "The maximum number of blocks changed (" ply.sendMessage(Colors.Rose + "The maximum number of blocks changed ("
+ e5.getBlockLimit() + ") in an instance was reached."); + e5.getBlockLimit() + ") in an instance was reached.");

View File

@ -24,5 +24,13 @@ package com.sk89q.worldedit;
* @author sk89q * @author sk89q
*/ */
public class DisallowedItemException extends WorldEditException { public class DisallowedItemException extends WorldEditException {
private String type;
public DisallowedItemException(String type) {
this.type = type;
}
public String getID() {
return type;
}
} }

View File

@ -25,5 +25,13 @@ package com.sk89q.worldedit;
* @author sk89q * @author sk89q
*/ */
public class UnknownItemException extends WorldEditException { public class UnknownItemException extends WorldEditException {
private String type;
public UnknownItemException(String type) {
this.type = type;
}
public String getID() {
return type;
}
} }