Added getLazyBlock() to Extent for performance.

This commit is contained in:
sk89q
2014-03-31 18:22:44 -07:00
parent dcf35e5312
commit fa24eb60fa
11 changed files with 224 additions and 109 deletions

View File

@ -47,27 +47,36 @@ public interface Extent {
* Calls to this method can actually be quite expensive, so cache results
* whenever it is possible, while being aware of the mutability aspect.
* The cost, however, depends on the implementation and particular extent.
* If only basic information about the block is required, then use of
* {@link #getLazyBlock(Vector)} is recommended.
*
* @param position position of the block
* @return the block, or null if the block does not exist
* @return the block
*/
BaseBlock getBlock(Vector position);
/**
* Get the block ID at the given location.
* Get a lazy, immutable snapshot of the block at the given location that only
* immediately contains information about the block's type (and metadata).
* </p>
* Further information (such as NBT data) will be available <strong>by the
* time of access</strong>. Therefore, it is not recommended that
* this method is used if the world is being simulated at the time of
* call. If the block needs to be stored for future use, then this method should
* definitely not be used. Moreover, the block that is returned is immutable (or
* should be), and therefore modifications should not be attempted on it. If a
* modifiable copy is required, then the block should be cloned.
* </p>
* This method exists because it is sometimes important to inspect the block
* at a given location, but {@link #getBlock(Vector)} may be too expensive in
* the underlying implementation. It is also not possible to implement
* caching if the returned object is mutable, so this methods allows caching
* implementations to be used.
*
* @param position position of the block
* @return the block ID
* @return the block
*/
int getBlockType(Vector position);
/**
* Get the data value of the block at the given location.
*
* @param position position of the block
* @return the block data value
*/
int getBlockData(Vector position);
BaseBlock getLazyBlock(Vector position);
/**
* Change the block at the given location to the given block. The operation may

View File

@ -56,18 +56,13 @@ public class ExtentDelegate implements Extent {
}
@Override
public BaseBlock getBlock(Vector location) {
return extent.getBlock(location);
public BaseBlock getBlock(Vector position) {
return extent.getBlock(position);
}
@Override
public int getBlockType(Vector location) {
return extent.getBlockType(location);
}
@Override
public int getBlockData(Vector location) {
return extent.getBlockData(location);
public BaseBlock getLazyBlock(Vector position) {
return extent.getLazyBlock(position);
}
@Override

View File

@ -83,6 +83,8 @@ public class SimpleBlockReorder extends ExtentDelegate {
@Override
public boolean setBlock(Vector location, BaseBlock block) throws WorldEditException {
BaseBlock lazyBlock = getLazyBlock(location);
if (!enabled) {
return super.setBlock(location, block);
}
@ -90,18 +92,18 @@ public class SimpleBlockReorder extends ExtentDelegate {
if (BlockType.shouldPlaceLast(block.getType())) {
// Place torches, etc. last
stage2.put(location.toBlockVector(), block);
return !(getBlockType(location) == block.getType() && getBlockData(location) == block.getData());
return !(lazyBlock.getType() == block.getType() && lazyBlock.getData() == block.getData());
} else if (BlockType.shouldPlaceFinal(block.getType())) {
// Place signs, reed, etc even later
stage3.put(location.toBlockVector(), block);
return !(getBlockType(location) == block.getType() && getBlockData(location) == block.getData());
} else if (BlockType.shouldPlaceLast(getBlockType(location))) {
return !(lazyBlock.getType() == block.getType() && lazyBlock.getData() == block.getData());
} else if (BlockType.shouldPlaceLast(lazyBlock.getType())) {
// Destroy torches, etc. first
super.setBlock(location, new BaseBlock(BlockID.AIR));
return super.setBlock(location, block);
} else {
stage1.put(location.toBlockVector(), block);
return !(getBlockType(location) == block.getType() && getBlockData(location) == block.getData());
return !(lazyBlock.getType() == block.getType() && lazyBlock.getData() == block.getData());
}
}