From d2c64e930410911ab75aa3b318a2dd744d43542a Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Tue, 31 Jan 2012 16:57:40 +0100 Subject: [PATCH] BaseBlock changes - Changed up EditSession.replaceBlocks to make BaseBlock.inIterable unnecessary. - Returned BaseBlock.inIterable to its old state and deprecated it. - Renamed the old BaseBlock.equals to equalsFuzzy, changed the parameter type and took the casts and type checks out. - Made a new BaseBlock.equals that is consistent with the rules java prescribes. --- .../java/com/sk89q/worldedit/EditSession.java | 96 +++++++++++++++---- .../com/sk89q/worldedit/blocks/BaseBlock.java | 24 +++-- 2 files changed, 91 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index b91e8899b..401edd3c1 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -1244,8 +1244,18 @@ public class EditSession { * @return number of blocks affected * @throws MaxChangedBlocksException */ - public int replaceBlocks(Region region, Set fromBlockTypes, - BaseBlock toBlock) throws MaxChangedBlocksException { + public int replaceBlocks(Region region, Set fromBlockTypes, BaseBlock toBlock) throws MaxChangedBlocksException { + Set definiteBlockTypes = new HashSet(); + Set fuzzyBlockTypes = new HashSet(); + + for (BaseBlock block : fromBlockTypes) { + if (block.getData() == -1) { + fuzzyBlockTypes.add(block.getType()); + } else { + definiteBlockTypes.add(block); + } + } + int affected = 0; if (region instanceof CuboidRegion) { @@ -1266,11 +1276,20 @@ public class EditSession { Vector pt = new Vector(x, y, z); BaseBlock curBlockType = getBlock(pt); - if ((fromBlockTypes == null && !curBlockType.isAir()) - || (fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes))) { // Probably faster if someone adds a proper hashCode to BaseBlock - if (setBlock(pt, toBlock)) { - ++affected; + if (definiteBlockTypes == null) { + //replace + if (curBlockType.isAir()) { + continue; } + } else { + //replace + if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) { + continue; + } + } + + if (setBlock(pt, toBlock)) { + ++affected; } } } @@ -1279,11 +1298,20 @@ public class EditSession { for (Vector pt : region) { BaseBlock curBlockType = getBlock(pt); - if (fromBlockTypes == null && !curBlockType.isAir() - || fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes)) { - if (setBlock(pt, toBlock)) { - ++affected; + if (definiteBlockTypes == null) { + //replace + if (curBlockType.isAir()) { + continue; } + } else { + //replace + if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) { + continue; + } + } + + if (setBlock(pt, toBlock)) { + ++affected; } } } @@ -1300,8 +1328,18 @@ public class EditSession { * @return number of blocks affected * @throws MaxChangedBlocksException */ - public int replaceBlocks(Region region, Set fromBlockTypes, - Pattern pattern) throws MaxChangedBlocksException { + public int replaceBlocks(Region region, Set fromBlockTypes, Pattern pattern) throws MaxChangedBlocksException { + Set definiteBlockTypes = new HashSet(); + Set fuzzyBlockTypes = new HashSet(); + + for (BaseBlock block : fromBlockTypes) { + if (block.getData() == -1) { + fuzzyBlockTypes.add(block.getType()); + } else { + definiteBlockTypes.add(block); + } + } + int affected = 0; if (region instanceof CuboidRegion) { @@ -1322,11 +1360,20 @@ public class EditSession { Vector pt = new Vector(x, y, z); BaseBlock curBlockType = getBlock(pt); - if ((fromBlockTypes == null && !curBlockType.isAir()) - || (fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes))) { // Probably faster if someone adds a proper hashCode to BaseBlock - if (setBlock(pt, pattern.next(pt))) { - ++affected; + if (definiteBlockTypes == null) { + //replace + if (curBlockType.isAir()) { + continue; } + } else { + //replace + if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) { + continue; + } + } + + if (setBlock(pt, pattern.next(pt))) { + ++affected; } } } @@ -1335,11 +1382,20 @@ public class EditSession { for (Vector pt : region) { BaseBlock curBlockType = getBlock(pt); - if (fromBlockTypes == null && !curBlockType.isAir() - || curBlockType.inIterable(fromBlockTypes)) { - if (setBlock(pt, pattern.next(pt))) { - ++affected; + if (definiteBlockTypes == null) { + //replace + if (curBlockType.isAir()) { + continue; } + } else { + //replace + if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) { + continue; + } + } + + if (setBlock(pt, pattern.next(pt))) { + ++affected; } } } diff --git a/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java b/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java index 81b43926b..4ae3f00b2 100644 --- a/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java +++ b/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java @@ -19,8 +19,6 @@ package com.sk89q.worldedit.blocks; -import java.util.Collection; - import com.sk89q.worldedit.CuboidClipboard.FlipDirection; /** @@ -147,8 +145,12 @@ public class BaseBlock { if (!(o instanceof BaseBlock)) { return false; } - return (type == ((BaseBlock) o).type) - && (data == ((BaseBlock) o).data || data == -1 || ((BaseBlock) o).data == -1); + + return type == ((BaseBlock) o).type && data == ((BaseBlock) o).data; + } + + public boolean equalsFuzzy(BaseBlock o) { + return (type == o.type && data == o.data) || data == -1 || o.data == -1; } @Override @@ -163,13 +165,17 @@ public class BaseBlock { return "BaseBlock id: " + getType() + " with damage: " + getData(); } + /** + * + * + * @param iter + * @return + * @deprecated This method is silly + */ + @Deprecated public boolean inIterable(Iterable iter) { - if (iter instanceof Collection) { - return ((Collection) iter).contains(this); - } - for (BaseBlock block : iter) { - if (block.equals(this)) { + if (block.equalsFuzzy(this)) { return true; } }