Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply a few micro-optimisations #2391

Merged
merged 3 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public boolean isEnabled() {
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
this.changeSet.setRecordChanges(enabled);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,32 @@ 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());
if (newValue != null) {
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;
case "x" -> value = Direction.EAST;
case "y" -> value = Direction.UP;
case "z" -> value = Direction.NORTH;
default -> {
}
me4502 marked this conversation as resolved.
Show resolved Hide resolved
}
if (value != null) {
Vector3 newValue = getNewStateValue(Direction.valuesOf(Direction.Flag.UPRIGHT | Direction.Flag.CARDINAL), transform, value.toVector());
Expand Down Expand Up @@ -225,8 +221,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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -52,14 +52,16 @@ private static Change createChange(LocatedBlock block) {
public void add(Change change) {
checkNotNull(change);

if (change instanceof BlockChange blockChange) {
BlockVector3 position = blockChange.getPosition();
if (!previous.containsLocation(position)) {
previous.add(position, blockChange.getPrevious());
if (isRecordingChanges()) {
if (change instanceof BlockChange blockChange) {
BlockVector3 position = blockChange.getPosition();
if (!previous.containsLocation(position)) {
previous.add(position, blockChange.getPrevious());
}
current.add(position, blockChange.getCurrent());
} else {
super.add(change);
}
current.add(position, blockChange.getCurrent());
} else {
super.add(change);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

private final BlockVector3 location;
private final BaseBlock block;
public record LocatedBlock(BlockVector3 location, BaseBlock block) {

public LocatedBlock(BlockVector3 location, BaseBlock block) {
me4502 marked this conversation as resolved.
Show resolved Hide resolved
this.location = checkNotNull(location);
this.block = checkNotNull(block);
me4502 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading