Apply a few micro-optimisations (#2391)

* Apply a few micro-optimisations

* Remove this bit as it alters behaviour

* Review changes
This commit is contained in:
Maddy Miller 2023-09-03 19:01:26 +10:00 committed by GitHub
parent 3f165638b6
commit 854fa63851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 60 additions and 70 deletions

View File

@ -458,9 +458,9 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B
try {
return worldNativeAccess.setBlock(position, block, sideEffects);
} catch (Exception e) {
if (block instanceof BaseBlock && ((BaseBlock) block).getNbt() != null) {
if (block instanceof BaseBlock baseBlock && baseBlock.getNbt() != null) {
LOGGER.warn("Tried to set a corrupt tile entity at " + position.toString()
+ ": " + ((BaseBlock) block).getNbt(), e);
+ ": " + baseBlock.getNbt(), e);
} else {
LOGGER.warn("Failed to set block via adapter, falling back to generic", e);
}

View File

@ -89,7 +89,7 @@ public void build(EditSession editSession, BlockVector3 position, Pattern patter
}
for (LocatedBlock block : column) {
editSession.setBlock(block.getLocation(), block.getBlock());
editSession.setBlock(block.location(), block.block());
}
for (BlockVector3 removedBlock : removedBlocks) {

View File

@ -88,6 +88,7 @@ public boolean isEnabled() {
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
this.changeSet.setRecordChanges(enabled);
}
@Override

View File

@ -50,6 +50,8 @@ public BlockState getBlock(BlockVector3 position) {
CachedBlock<BlockState> lastBlock = this.lastBlock;
if (lastBlock != null && lastBlock.position.equals(position)) {
return lastBlock.block;
} else if (lastFullBlock != null && lastFullBlock.position.equals(position)) {
return lastFullBlock.block().toImmutableState();
} else {
BlockState block = super.getBlock(position);
this.lastBlock = new CachedBlock<>(position, block);
@ -84,14 +86,7 @@ public <T extends BlockStateHolder<T>> boolean setBlock(BlockVector3 location, T
return false;
}
private static class CachedBlock<B extends BlockStateHolder<B>> {
private final BlockVector3 position;
private final B block;
private CachedBlock(BlockVector3 position, B block) {
this.position = position;
this.block = block;
}
private record CachedBlock<B extends BlockStateHolder<B>>(BlockVector3 position, B block) {
}
}

View File

@ -77,6 +77,10 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B
@Override
protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
if (!enabled) {
// Early exit if we're not enabled.
return null;
}
return blockMap.get(position);
}

View File

@ -259,6 +259,10 @@ public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B
@Override
protected BaseBlock getBufferedFullBlock(BlockVector3 position) {
if (!enabled) {
// Early-exit if we know we're not enabled.
return null;
}
for (BlockMap<BaseBlock> blocks : stages.values()) {
BaseBlock baseBlock = blocks.get(position);
if (baseBlock != null) {

View File

@ -117,12 +117,15 @@ public static <B extends BlockStateHolder<B>> B transform(B block, Transform tra
checkNotNull(block);
checkNotNull(transform);
if (transform.isIdentity()) {
return block;
}
B result = block;
List<? extends Property<?>> properties = block.getBlockType().getProperties();
for (Property<?> property : properties) {
if (property instanceof DirectionalProperty) {
DirectionalProperty dirProp = (DirectionalProperty) property;
if (property instanceof DirectionalProperty dirProp) {
Direction value = (Direction) block.getState(property);
if (value != null) {
Vector3 newValue = getNewStateValue(dirProp.getValues(), transform, value.toVector());
@ -130,24 +133,15 @@ public static <B extends BlockStateHolder<B>> B transform(B block, Transform tra
result = result.with(dirProp, Direction.findClosest(newValue, Direction.Flag.ALL));
}
}
} else if (property instanceof EnumProperty) {
EnumProperty enumProp = (EnumProperty) property;
} else if (property instanceof EnumProperty enumProp) {
if (property.getName().equals("axis")) {
// We have an axis - this is something we can do the rotations to :sunglasses:
Direction value = null;
switch ((String) block.getState(property)) {
case "x":
value = Direction.EAST;
break;
case "y":
value = Direction.UP;
break;
case "z":
value = Direction.NORTH;
break;
default:
break;
}
Direction value = switch ((String) block.getState(property)) {
case "x" -> Direction.EAST;
case "y" -> Direction.UP;
case "z" -> Direction.NORTH;
default -> null;
};
if (value != null) {
Vector3 newValue = getNewStateValue(Direction.valuesOf(Direction.Flag.UPRIGHT | Direction.Flag.CARDINAL), transform, value.toVector());
if (newValue != null) {
@ -225,8 +219,7 @@ public static <B extends BlockStateHolder<B>> B transform(B block, Transform tra
}
}
}
} else if (property instanceof IntegerProperty) {
IntegerProperty intProp = (IntegerProperty) property;
} else if (property instanceof IntegerProperty intProp) {
if (property.getName().equals("rotation")) {
if (intProp.getValues().size() == 16) {
Optional<Direction> direction = Direction.fromRotationIndex(block.getState(intProp));

View File

@ -38,7 +38,7 @@ public SetLocatedBlocks(Extent extent, Iterable<LocatedBlock> blocks) {
@Override
public Operation resume(RunContext run) throws WorldEditException {
for (LocatedBlock block : blocks) {
extent.setBlock(block.getLocation(), block.getBlock());
extent.setBlock(block.location(), block.block());
}
return null;
}

View File

@ -42,7 +42,7 @@
public class BlockOptimizedHistory extends ArrayListHistory {
private static Change createChange(LocatedBlock block) {
return new BlockChange(block.getLocation(), block.getBlock(), block.getBlock());
return new BlockChange(block.location(), block.block(), block.block());
}
private final LocatedBlockList previous = new LocatedBlockList();
@ -52,6 +52,7 @@ private static Change createChange(LocatedBlock block) {
public void add(Change change) {
checkNotNull(change);
if (isRecordingChanges()) {
if (change instanceof BlockChange blockChange) {
BlockVector3 position = blockChange.getPosition();
if (!previous.containsLocation(position)) {
@ -62,6 +63,7 @@ public void add(Change change) {
super.add(change);
}
}
}
@Override
public Iterator<Change> forwardIterator() {

View File

@ -22,46 +22,37 @@
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Represents a block located at some position.
*/
public final class LocatedBlock {
public record LocatedBlock(BlockVector3 location, BaseBlock block) {
private final BlockVector3 location;
private final BaseBlock block;
public LocatedBlock(BlockVector3 location, BaseBlock block) {
this.location = checkNotNull(location);
this.block = checkNotNull(block);
public LocatedBlock {
checkNotNull(location);
checkNotNull(block);
}
/**
* Gets the location.
*
* @return The location
* @deprecated This class is now a record. Use {@link #location()} instead.
*/
@Deprecated
public BlockVector3 getLocation() {
return location;
return this.location;
}
/**
* Gets the block.
*
* @return The block
* @deprecated This class is now a record. Use {@link #block()} instead.
*/
public BaseBlock getBlock() {
return block;
}
@Override
public int hashCode() {
return Objects.hash(location, block);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (this.getClass() != obj.getClass()) {
return false;
}
LocatedBlock lb = (LocatedBlock) obj;
return Objects.equals(location, lb.location) && Objects.equals(block, lb.block);
return this.block;
}
}

View File

@ -47,13 +47,13 @@ public LocatedBlockList() {
public LocatedBlockList(Collection<? extends LocatedBlock> collection) {
for (LocatedBlock locatedBlock : collection) {
add(locatedBlock.getLocation(), locatedBlock.getBlock());
add(locatedBlock.location(), locatedBlock.block());
}
}
public void add(LocatedBlock setBlockCall) {
checkNotNull(setBlockCall);
add(setBlockCall.getLocation(), setBlockCall.getBlock());
add(setBlockCall.location(), setBlockCall.block());
}
public <B extends BlockStateHolder<B>> void add(BlockVector3 location, B block) {