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.
Dieser Commit ist enthalten in:
TomyLobo 2012-01-31 16:57:40 +01:00
Ursprung ffa2824c4d
Commit d2c64e9304
2 geänderte Dateien mit 91 neuen und 29 gelöschten Zeilen

Datei anzeigen

@ -1244,8 +1244,18 @@ public class EditSession {
* @return number of blocks affected * @return number of blocks affected
* @throws MaxChangedBlocksException * @throws MaxChangedBlocksException
*/ */
public int replaceBlocks(Region region, Set<BaseBlock> fromBlockTypes, public int replaceBlocks(Region region, Set<BaseBlock> fromBlockTypes, BaseBlock toBlock) throws MaxChangedBlocksException {
BaseBlock toBlock) throws MaxChangedBlocksException { Set<BaseBlock> definiteBlockTypes = new HashSet<BaseBlock>();
Set<Integer> fuzzyBlockTypes = new HashSet<Integer>();
for (BaseBlock block : fromBlockTypes) {
if (block.getData() == -1) {
fuzzyBlockTypes.add(block.getType());
} else {
definiteBlockTypes.add(block);
}
}
int affected = 0; int affected = 0;
if (region instanceof CuboidRegion) { if (region instanceof CuboidRegion) {
@ -1266,27 +1276,45 @@ public class EditSession {
Vector pt = new Vector(x, y, z); Vector pt = new Vector(x, y, z);
BaseBlock curBlockType = getBlock(pt); BaseBlock curBlockType = getBlock(pt);
if ((fromBlockTypes == null && !curBlockType.isAir()) if (definiteBlockTypes == null) {
|| (fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes))) { // Probably faster if someone adds a proper hashCode to BaseBlock //replace <to-block>
if (curBlockType.isAir()) {
continue;
}
} else {
//replace <from-block> <to-block>
if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) {
continue;
}
}
if (setBlock(pt, toBlock)) { if (setBlock(pt, toBlock)) {
++affected; ++affected;
} }
} }
} }
} }
}
} else { } else {
for (Vector pt : region) { for (Vector pt : region) {
BaseBlock curBlockType = getBlock(pt); BaseBlock curBlockType = getBlock(pt);
if (fromBlockTypes == null && !curBlockType.isAir() if (definiteBlockTypes == null) {
|| fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes)) { //replace <to-block>
if (curBlockType.isAir()) {
continue;
}
} else {
//replace <from-block> <to-block>
if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) {
continue;
}
}
if (setBlock(pt, toBlock)) { if (setBlock(pt, toBlock)) {
++affected; ++affected;
} }
} }
} }
}
return affected; return affected;
} }
@ -1300,8 +1328,18 @@ public class EditSession {
* @return number of blocks affected * @return number of blocks affected
* @throws MaxChangedBlocksException * @throws MaxChangedBlocksException
*/ */
public int replaceBlocks(Region region, Set<BaseBlock> fromBlockTypes, public int replaceBlocks(Region region, Set<BaseBlock> fromBlockTypes, Pattern pattern) throws MaxChangedBlocksException {
Pattern pattern) throws MaxChangedBlocksException { Set<BaseBlock> definiteBlockTypes = new HashSet<BaseBlock>();
Set<Integer> fuzzyBlockTypes = new HashSet<Integer>();
for (BaseBlock block : fromBlockTypes) {
if (block.getData() == -1) {
fuzzyBlockTypes.add(block.getType());
} else {
definiteBlockTypes.add(block);
}
}
int affected = 0; int affected = 0;
if (region instanceof CuboidRegion) { if (region instanceof CuboidRegion) {
@ -1322,27 +1360,45 @@ public class EditSession {
Vector pt = new Vector(x, y, z); Vector pt = new Vector(x, y, z);
BaseBlock curBlockType = getBlock(pt); BaseBlock curBlockType = getBlock(pt);
if ((fromBlockTypes == null && !curBlockType.isAir()) if (definiteBlockTypes == null) {
|| (fromBlockTypes != null && curBlockType.inIterable(fromBlockTypes))) { // Probably faster if someone adds a proper hashCode to BaseBlock //replace <to-block>
if (curBlockType.isAir()) {
continue;
}
} else {
//replace <from-block> <to-block>
if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) {
continue;
}
}
if (setBlock(pt, pattern.next(pt))) { if (setBlock(pt, pattern.next(pt))) {
++affected; ++affected;
} }
} }
} }
} }
}
} else { } else {
for (Vector pt : region) { for (Vector pt : region) {
BaseBlock curBlockType = getBlock(pt); BaseBlock curBlockType = getBlock(pt);
if (fromBlockTypes == null && !curBlockType.isAir() if (definiteBlockTypes == null) {
|| curBlockType.inIterable(fromBlockTypes)) { //replace <to-block>
if (curBlockType.isAir()) {
continue;
}
} else {
//replace <from-block> <to-block>
if (!definiteBlockTypes.contains(curBlockType) && fuzzyBlockTypes.contains(curBlockType.getType())) {
continue;
}
}
if (setBlock(pt, pattern.next(pt))) { if (setBlock(pt, pattern.next(pt))) {
++affected; ++affected;
} }
} }
} }
}
return affected; return affected;
} }

Datei anzeigen

@ -19,8 +19,6 @@
package com.sk89q.worldedit.blocks; package com.sk89q.worldedit.blocks;
import java.util.Collection;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection; import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
/** /**
@ -147,8 +145,12 @@ public class BaseBlock {
if (!(o instanceof BaseBlock)) { if (!(o instanceof BaseBlock)) {
return false; 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 @Override
@ -163,13 +165,17 @@ public class BaseBlock {
return "BaseBlock id: " + getType() + " with damage: " + getData(); return "BaseBlock id: " + getType() + " with damage: " + getData();
} }
/**
*
*
* @param iter
* @return
* @deprecated This method is silly
*/
@Deprecated
public boolean inIterable(Iterable<BaseBlock> iter) { public boolean inIterable(Iterable<BaseBlock> iter) {
if (iter instanceof Collection) {
return ((Collection<?>) iter).contains(this);
}
for (BaseBlock block : iter) { for (BaseBlock block : iter) {
if (block.equals(this)) { if (block.equalsFuzzy(this)) {
return true; return true;
} }
} }