From 1562f1754006a6405f7f3425eb29a53074813f9c Mon Sep 17 00:00:00 2001 From: zml2008 Date: Sun, 28 Aug 2011 23:57:07 -0700 Subject: [PATCH] Added block damage support to //replace and //replacenear --- .../java/com/sk89q/worldedit/EditSession.java | 30 +++++++++---------- .../java/com/sk89q/worldedit/WorldEdit.java | 15 ++++++++++ .../com/sk89q/worldedit/blocks/BaseBlock.java | 22 ++++++++++++++ .../worldedit/commands/RegionCommands.java | 4 +-- .../worldedit/commands/UtilityCommands.java | 4 +-- 5 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index 1f7be6319..8993e72a1 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -999,7 +999,7 @@ public class EditSession { * @return number of blocks affected * @throws MaxChangedBlocksException */ - public int replaceBlocks(Region region, Set fromBlockTypes, + public int replaceBlocks(Region region, Set fromBlockTypes, BaseBlock toBlock) throws MaxChangedBlocksException { int affected = 0; @@ -1019,11 +1019,10 @@ public class EditSession { for (int y = minY; y <= maxY; ++y) { for (int z = minZ; z <= maxZ; ++z) { Vector pt = new Vector(x, y, z); - int curBlockType = getBlockType(pt); + BaseBlock curBlockType = getBlock(pt); - if ((fromBlockTypes == null && curBlockType != 0) - || (fromBlockTypes != null && fromBlockTypes - .contains(curBlockType))) { + if ((fromBlockTypes == null && !curBlockType.isAir()) + || (fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes))) { if (setBlock(pt, toBlock)) { ++affected; } @@ -1033,10 +1032,10 @@ public class EditSession { } } else { for (Vector pt : region) { - int curBlockType = getBlockType(pt); + BaseBlock curBlockType = getBlock(pt); - if (fromBlockTypes == null && curBlockType != 0 - || fromBlockTypes.contains(curBlockType)) { + if (fromBlockTypes == null && !curBlockType.isAir() + || fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes)) { if (setBlock(pt, toBlock)) { ++affected; } @@ -1056,7 +1055,7 @@ public class EditSession { * @return number of blocks affected * @throws MaxChangedBlocksException */ - public int replaceBlocks(Region region, Set fromBlockTypes, + public int replaceBlocks(Region region, Set fromBlockTypes, Pattern pattern) throws MaxChangedBlocksException { int affected = 0; @@ -1076,11 +1075,10 @@ public class EditSession { for (int y = minY; y <= maxY; ++y) { for (int z = minZ; z <= maxZ; ++z) { Vector pt = new Vector(x, y, z); - int curBlockType = getBlockType(pt); + BaseBlock curBlockType = getBlock(pt); - if ((fromBlockTypes == null && curBlockType != 0) - || (fromBlockTypes != null && fromBlockTypes - .contains(curBlockType))) { + if ((fromBlockTypes == null && !curBlockType.isAir()) + || (fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes))) { if (setBlock(pt, pattern.next(pt))) { ++affected; } @@ -1090,10 +1088,10 @@ public class EditSession { } } else { for (Vector pt : region) { - int curBlockType = getBlockType(pt); + BaseBlock curBlockType = getBlock(pt); - if (fromBlockTypes == null && curBlockType != 0 - || fromBlockTypes.contains(curBlockType)) { + if (fromBlockTypes == null && !curBlockType.isAir() + || curBlockType.inIterable(fromBlockTypes)) { if (setBlock(pt, pattern.next(pt))) { ++affected; } diff --git a/src/main/java/com/sk89q/worldedit/WorldEdit.java b/src/main/java/com/sk89q/worldedit/WorldEdit.java index 445cb981d..4d7d515fe 100644 --- a/src/main/java/com/sk89q/worldedit/WorldEdit.java +++ b/src/main/java/com/sk89q/worldedit/WorldEdit.java @@ -400,6 +400,21 @@ public class WorldEdit { return getBlock(player, id, false); } + public Set getBlocks (LocalPlayer player, String list, boolean allAllowed) + throws DisallowedItemException, UnknownItemException { + String[] items = list.split(","); + Set blocks = new HashSet(); + for (String id : items) { + blocks.add(getBlock(player, id, allAllowed)); + } + return blocks; + } + + public Set getBlocks(LocalPlayer player, String list) + throws DisallowedItemException, UnknownItemException { + return getBlocks(player, list, false); + } + /** * Get a list of blocks as a set. This returns a Pattern. * diff --git a/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java b/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java index 920508409..dcc58d25f 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java @@ -115,4 +115,26 @@ public class BaseBlock { public void flip(FlipDirection direction) { data = (char)BlockData.flip(type, data, direction); } + + @Override + public boolean equals(Object o) { + if (!(o instanceof BaseBlock)) { + return false; + } + return (type == ((BaseBlock)o).type) && (data == ((BaseBlock)o).data); + } + + @Override + public String toString() { + return "BaseBlock id: " + getType() + " with damage: " + getData(); + } + + public boolean inIterable(Iterable iter) { + for (BaseBlock block : iter) { + if (block.equals(this)) { + return true; + } + } + return false; + } } diff --git a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java index 53f4954cd..d3488219f 100644 --- a/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/RegionCommands.java @@ -81,13 +81,13 @@ public class RegionCommands { LocalSession session, LocalPlayer player, EditSession editSession) throws WorldEditException { - Set from; + Set from; Pattern to; if (args.argsLength() == 1) { from = null; to = we.getBlockPattern(player, args.getString(0)); } else { - from = we.getBlockIDs(player, args.getString(0), true); + from = we.getBlocks(player, args.getString(0), true); to = we.getBlockPattern(player, args.getString(1)); } diff --git a/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java b/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java index 0ff9b8f9d..96642daab 100644 --- a/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java +++ b/src/main/java/com/sk89q/worldedit/commands/UtilityCommands.java @@ -239,13 +239,13 @@ public class UtilityCommands { int size = Math.max(1, args.getInteger(0)); int affected; - Set from; + Set from; Pattern to; if (args.argsLength() == 2) { from = null; to = we.getBlockPattern(player, args.getString(1)); } else { - from = we.getBlockIDs(player, args.getString(1), true); + from = we.getBlocks(player, args.getString(1), true); to = we.getBlockPattern(player, args.getString(2)); }