From 84cde9a19ee2b86329ed3458af15855faaab68f3 Mon Sep 17 00:00:00 2001 From: Madeline Miller Date: Sun, 3 Sep 2023 17:44:56 +1000 Subject: [PATCH] Apply a few micro-optimisations --- .../sk89q/worldedit/bukkit/BukkitWorld.java | 4 +- .../command/tool/brush/GravityBrush.java | 2 +- .../worldedit/extent/ChangeSetExtent.java | 1 + .../extent/cache/LastAccessExtentCache.java | 13 ++---- .../extent/reorder/ChunkBatchingExtent.java | 4 ++ .../extent/reorder/MultiStageReorder.java | 4 ++ .../transform/BlockTransformExtent.java | 29 ++++++------- .../function/operation/SetLocatedBlocks.java | 2 +- .../changeset/BlockOptimizedHistory.java | 18 ++++---- .../sk89q/worldedit/util/LocatedBlock.java | 41 ++++++++----------- .../util/collection/LocatedBlockList.java | 4 +- 11 files changed, 57 insertions(+), 65 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java index eeb786785f..ad7a839916 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -458,9 +458,9 @@ public > 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); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/GravityBrush.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/GravityBrush.java index f1135799f9..6003d41221 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/GravityBrush.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/GravityBrush.java @@ -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) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java index 6230e899c7..53f5cd706e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/ChangeSetExtent.java @@ -88,6 +88,7 @@ public boolean isEnabled() { */ public void setEnabled(boolean enabled) { this.enabled = enabled; + this.changeSet.setRecordChanges(enabled); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/cache/LastAccessExtentCache.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/cache/LastAccessExtentCache.java index 9a5a6d804d..eacce8bc69 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/cache/LastAccessExtentCache.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/cache/LastAccessExtentCache.java @@ -50,6 +50,8 @@ public BlockState getBlock(BlockVector3 position) { CachedBlock 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); @@ -72,7 +74,7 @@ public BaseBlock getFullBlock(BlockVector3 position) { @Override public > boolean setBlock(BlockVector3 location, T block) throws WorldEditException { if (super.setBlock(location, block)) { - if (lastFullBlock != null && lastFullBlock.position.equals(location)) { + if (block instanceof BaseBlock && lastFullBlock != null && lastFullBlock.position.equals(location)) { this.lastFullBlock = new CachedBlock<>(location, block.toBaseBlock()); } if (lastBlock != null && lastBlock.position.equals(location)) { @@ -84,14 +86,7 @@ public > boolean setBlock(BlockVector3 location, T return false; } - private static class CachedBlock> { - private final BlockVector3 position; - private final B block; - - private CachedBlock(BlockVector3 position, B block) { - this.position = position; - this.block = block; - } + private record CachedBlock>(BlockVector3 position, B block) { } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java index 181baf3498..49834fe9a4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/ChunkBatchingExtent.java @@ -77,6 +77,10 @@ public > 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); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java index a68f6e8432..aa83257ad4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/reorder/MultiStageReorder.java @@ -259,6 +259,10 @@ public > 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 blocks : stages.values()) { BaseBlock baseBlock = blocks.get(position); if (baseBlock != null) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/transform/BlockTransformExtent.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/transform/BlockTransformExtent.java index e5598997cf..a20b37e915 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extent/transform/BlockTransformExtent.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extent/transform/BlockTransformExtent.java @@ -117,12 +117,15 @@ public static > B transform(B block, Transform tra checkNotNull(block); checkNotNull(transform); + if (transform.isIdentity()) { + return block; + } + B result = block; List> 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,23 +133,16 @@ public static > 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; + case "x" -> value = Direction.EAST; + case "y" -> value = Direction.UP; + case "z" -> value = Direction.NORTH; + default -> { + } } if (value != null) { Vector3 newValue = getNewStateValue(Direction.valuesOf(Direction.Flag.UPRIGHT | Direction.Flag.CARDINAL), transform, value.toVector()); @@ -225,8 +221,7 @@ public static > 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.fromRotationIndex(block.getState(intProp)); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/operation/SetLocatedBlocks.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/operation/SetLocatedBlocks.java index c7673f7b95..b0e5aafada 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/operation/SetLocatedBlocks.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/operation/SetLocatedBlocks.java @@ -38,7 +38,7 @@ public SetLocatedBlocks(Extent extent, Iterable 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; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/history/changeset/BlockOptimizedHistory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/history/changeset/BlockOptimizedHistory.java index a22ec0c3d3..fdb646a41b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/history/changeset/BlockOptimizedHistory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/history/changeset/BlockOptimizedHistory.java @@ -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,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); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/LocatedBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/LocatedBlock.java index 2ea1d1d59b..5b4152d98e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/LocatedBlock.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/LocatedBlock.java @@ -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) { this.location = checkNotNull(location); this.block = 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; } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/collection/LocatedBlockList.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/collection/LocatedBlockList.java index 67beee2950..8169f31c23 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/collection/LocatedBlockList.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/collection/LocatedBlockList.java @@ -47,13 +47,13 @@ public LocatedBlockList() { public LocatedBlockList(Collection 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 > void add(BlockVector3 location, B block) {