From 01340152e3e627380fc3ffaa90b1921e2278226d Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 6 Nov 2010 09:35:05 -0700 Subject: [PATCH] Added support for replacing a list of blocks with one block with //replace. --- src/EditSession.java | 10 +++---- src/WorldEditListener.java | 29 ++++++++++++++----- .../worldedit/DisallowedItemException.java | 8 +++++ .../sk89q/worldedit/UnknownItemException.java | 8 +++++ 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/EditSession.java b/src/EditSession.java index ba90523b3..865c3567f 100644 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -546,7 +546,7 @@ public int setBlocks(Region region, BaseBlock block) * @return number of blocks affected * @throws MaxChangedBlocksException */ - public int replaceBlocks(Region region, int fromBlockType, BaseBlock toBlock) + public int replaceBlocks(Region region, Set fromBlockTypes, BaseBlock toBlock) throws MaxChangedBlocksException { int affected = 0; @@ -561,8 +561,8 @@ public int replaceBlocks(Region region, int fromBlockType, BaseBlock toBlock) Vector pt = new Vector(x, y, z); int curBlockType = getBlock(pt).getID(); - if (fromBlockType == -1 && curBlockType != 0 || - curBlockType == fromBlockType) { + if (fromBlockTypes == null && curBlockType != 0 || + fromBlockTypes.contains(curBlockType)) { if (setBlock(pt, toBlock)) { affected++; } @@ -574,8 +574,8 @@ public int replaceBlocks(Region region, int fromBlockType, BaseBlock toBlock) for (Vector pt : region) { int curBlockType = getBlock(pt).getID(); - if (fromBlockType == -1 && curBlockType != 0 || - curBlockType == fromBlockType) { + if (fromBlockTypes == null && curBlockType != 0 || + fromBlockTypes.contains(curBlockType)) { if (setBlock(pt, toBlock)) { affected++; } diff --git a/src/WorldEditListener.java b/src/WorldEditListener.java index 7ddf324de..f6b355c1d 100644 --- a/src/WorldEditListener.java +++ b/src/WorldEditListener.java @@ -227,7 +227,7 @@ public BaseBlock getBlock(String arg, boolean allAllowed) } if (blockType == null) { - throw new UnknownItemException(); + throw new UnknownItemException(arg); } // Check if the item is allowed @@ -245,7 +245,7 @@ public BaseBlock getBlock(String arg, boolean allAllowed) return new BaseBlock(blockType.getID(), data); } - throw new DisallowedItemException(); + throw new DisallowedItemException(arg); } /** @@ -257,10 +257,23 @@ public BaseBlock getBlock(String arg, boolean allAllowed) * @throws DisallowedItemException */ public BaseBlock getBlock(String id) throws UnknownItemException, - DisallowedItemException { + DisallowedItemException { return getBlock(id, false); } + /** + * Get a list of blocks as a set. + */ + public Set getBlockIDs(String list) throws UnknownItemException, + DisallowedItemException { + String[] items = list.split(","); + Set blocks = new HashSet(); + for (String s : items) { + blocks.add(getBlock(s).getID()); + } + return blocks; + } + /** * Checks to make sure that there are enough but not too many arguments. * @@ -764,13 +777,13 @@ public boolean performCommand(WorldEditPlayer player, // Replace all blocks in the region } else if(split[0].equalsIgnoreCase("//replace")) { checkArgs(split, 1, 2, split[0]); - int from; + Set from; BaseBlock to; if (split.length == 2) { - from = -1; + from = null; to = getBlock(split[1], true); } else { - from = getBlock(split[1]).getID(); + from = getBlockIDs(split[1]); to = getBlock(split[2]); } @@ -1374,9 +1387,9 @@ public boolean onCommand(Player ply, String[] split) { } catch (IncompleteRegionException e2) { ply.sendMessage(Colors.Rose + "The edit region has not been fully defined."); } 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) { - 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) { ply.sendMessage(Colors.Rose + "The maximum number of blocks changed (" + e5.getBlockLimit() + ") in an instance was reached."); diff --git a/src/com/sk89q/worldedit/DisallowedItemException.java b/src/com/sk89q/worldedit/DisallowedItemException.java index 132d6c147..90e8b3198 100644 --- a/src/com/sk89q/worldedit/DisallowedItemException.java +++ b/src/com/sk89q/worldedit/DisallowedItemException.java @@ -24,5 +24,13 @@ * @author sk89q */ public class DisallowedItemException extends WorldEditException { + private String type; + + public DisallowedItemException(String type) { + this.type = type; + } + public String getID() { + return type; + } } diff --git a/src/com/sk89q/worldedit/UnknownItemException.java b/src/com/sk89q/worldedit/UnknownItemException.java index ed0a5b167..f734ec9f9 100644 --- a/src/com/sk89q/worldedit/UnknownItemException.java +++ b/src/com/sk89q/worldedit/UnknownItemException.java @@ -25,5 +25,13 @@ * @author sk89q */ public class UnknownItemException extends WorldEditException { + private String type; + public UnknownItemException(String type) { + this.type = type; + } + + public String getID() { + return type; + } }