From 4969dac39c759de43cceb05c72a261d9decabc7d Mon Sep 17 00:00:00 2001 From: Matthew Miller Date: Thu, 20 Sep 2018 16:56:46 +1000 Subject: [PATCH] Potential minor performance improvements when checking fuzzy equality. --- .../worldedit/world/block/BaseBlock.java | 5 ++++- .../worldedit/world/block/BlockState.java | 4 ++++ .../worldedit/world/block/BlockType.java | 15 +++++++++++++-- .../worldedit/world/chunk/AnvilChunk.java | 19 +++++++++++++------ .../worldedit/world/chunk/AnvilChunk13.java | 10 +++++++--- .../sk89q/worldedit/world/chunk/Chunk.java | 4 ++-- .../sk89q/worldedit/world/chunk/OldChunk.java | 16 +++++++++++++--- .../world/snapshot/SnapshotRestore.java | 3 +-- .../sponge/SpongePermissionsProvider.java | 4 +--- 9 files changed, 58 insertions(+), 22 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java index aa3100326..e3dd5d7d3 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java @@ -134,12 +134,15 @@ public class BaseBlock implements BlockStateHolder, TileEntityBlock { @Override public boolean equals(Object o) { if (!(o instanceof BaseBlock)) { + if (!hasNbtData() && o instanceof BlockStateHolder) { + return Objects.equals(toImmutableState(), ((BlockStateHolder) o).toImmutableState()); + } return false; } final BaseBlock otherBlock = (BaseBlock) o; - return this.toImmutableState().equals(otherBlock.toImmutableState()) && Objects.equals(getNbtData(), otherBlock.getNbtData()); + return Objects.equals(this.toImmutableState(), otherBlock.toImmutableState()) && Objects.equals(getNbtData(), otherBlock.getNbtData()); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java index 2b89d9195..de527baf4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java @@ -168,6 +168,10 @@ public class BlockState implements BlockStateHolder { @Override public boolean equalsFuzzy(BlockStateHolder o) { + if (this == o) { + // Added a reference equality check for + return true; + } if (!getBlockType().equals(o.getBlockType())) { return false; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java index 05cd6543b..2b9db5ea6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockType.java @@ -22,12 +22,12 @@ package com.sk89q.worldedit.world.block; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.world.registry.BlockMaterial; import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemTypes; +import com.sk89q.worldedit.world.registry.BlockMaterial; import com.sk89q.worldedit.world.registry.BundledBlockData; import com.sk89q.worldedit.world.registry.LegacyMapper; @@ -46,6 +46,7 @@ public class BlockType { private BlockState defaultState; private Map properties; private BlockMaterial blockMaterial; + private Map, Object>, BlockState> blockStatesMap; public BlockType(String id) { this(id, null); @@ -57,7 +58,8 @@ public class BlockType { id = "minecraft:" + id; } this.id = id; - this.defaultState = new ArrayList<>(BlockState.generateStateMap(this).values()).get(0); + this.blockStatesMap = BlockState.generateStateMap(this); + this.defaultState = new ArrayList<>(this.blockStatesMap.values()).get(0); if (values != null) { this.defaultState = values.apply(this.defaultState); } @@ -127,6 +129,15 @@ public class BlockType { return this.defaultState; } + /** + * Gets a list of all possible states for this BlockType. + * + * @return All possible states + */ + public List getAllStates() { + return ImmutableList.copyOf(this.blockStatesMap.values()); + } + /** * Gets whether this block type has an item representation. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java index 0f0d70d05..6b1265854 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk.java @@ -28,10 +28,12 @@ import com.sk89q.jnbt.NBTUtils; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.Vector; -import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.world.DataException; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.world.block.BlockStateHolder; +import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.storage.InvalidFormatException; @@ -51,8 +53,6 @@ public class AnvilChunk implements Chunk { private int rootZ; private Map> tileEntities; - @SuppressWarnings("unused") - private World world; // TODO: remove if stays unused. /** * Construct the chunk with a compound tag. @@ -63,7 +63,6 @@ public class AnvilChunk implements Chunk { */ public AnvilChunk(World world, CompoundTag tag) throws DataException { rootTag = tag; - this.world = world; rootX = NBTUtils.getChildTag(rootTag.getValue(), "xPos", IntTag.class).getValue(); rootZ = NBTUtils.getChildTag(rootTag.getValue(), "zPos", IntTag.class).getValue(); @@ -255,14 +254,22 @@ public class AnvilChunk implements Chunk { } @Override - public BaseBlock getBlock(Vector position) throws DataException { + public BlockStateHolder getBlock(Vector position) throws DataException { int id = getBlockID(position); int data = getBlockData(position); BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, data); + if (state == null) { + WorldEdit.logger.warning("Unknown legacy block " + id + ":" + data + " found when loading legacy anvil chunk."); + return BlockTypes.AIR.getDefaultState(); + } CompoundTag tileEntity = getBlockTileEntity(position); - return state.toBaseBlock(tileEntity); + if (tileEntity != null) { + return state.toBaseBlock(tileEntity); + } + + return state; } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java index 1f673ab6b..025390c66 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/AnvilChunk13.java @@ -29,9 +29,9 @@ import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.registry.state.Property; -import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.DataException; import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.storage.InvalidFormatException; @@ -230,7 +230,7 @@ public class AnvilChunk13 implements Chunk { } @Override - public BaseBlock getBlock(Vector position) throws DataException { + public BlockStateHolder getBlock(Vector position) throws DataException { int x = position.getBlockX() - rootX * 16; int y = position.getBlockY(); int z = position.getBlockZ() - rootZ * 16; @@ -247,7 +247,11 @@ public class AnvilChunk13 implements Chunk { CompoundTag tileEntity = getBlockTileEntity(position); - return state.toBaseBlock(tileEntity); + if (tileEntity != null) { + return state.toBaseBlock(tileEntity); + } + + return state; } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/Chunk.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/Chunk.java index 6ba2e8a54..2f261570e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/Chunk.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/Chunk.java @@ -20,8 +20,8 @@ package com.sk89q.worldedit.world.chunk; import com.sk89q.worldedit.Vector; -import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.DataException; +import com.sk89q.worldedit.world.block.BlockStateHolder; /** * A 16 by 16 block chunk. @@ -35,6 +35,6 @@ public interface Chunk { * @return block the block * @throws DataException thrown on data error */ - BaseBlock getBlock(Vector position) throws DataException; + BlockStateHolder getBlock(Vector position) throws DataException; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java index 631470ac2..c6f983ced 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/chunk/OldChunk.java @@ -27,10 +27,11 @@ import com.sk89q.jnbt.NBTUtils; import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.Vector; -import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.world.DataException; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.block.BlockState; +import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.storage.InvalidFormatException; @@ -153,7 +154,7 @@ public class OldChunk implements Chunk { } @Override - public BaseBlock getBlock(Vector position) throws DataException { + public BlockStateHolder getBlock(Vector position) throws DataException { if(position.getBlockY() >= 128) BlockTypes.VOID_AIR.getDefaultState().toBaseBlock(); int id, dataVal; @@ -181,9 +182,18 @@ public class OldChunk implements Chunk { } BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, dataVal); + if (state == null) { + WorldEdit.logger.warning("Unknown legacy block " + id + ":" + dataVal + " found when loading legacy anvil chunk."); + return BlockTypes.AIR.getDefaultState(); + } + CompoundTag tileEntity = getBlockTileEntity(position); - return state.toBaseBlock(tileEntity); + if (tileEntity != null) { + return state.toBaseBlock(tileEntity); + } + + return state; } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java index 814818a5c..a7ef7ca9d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java @@ -147,8 +147,7 @@ public class SnapshotRestore { // Now just copy blocks! for (Vector pos : entry.getValue()) { try { - BaseBlock block = chunk.getBlock(pos); - editSession.setBlock(pos, block); + editSession.setBlock(pos, chunk.getBlock(pos)); } catch (DataException e) { // this is a workaround: just ignore for now } diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePermissionsProvider.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePermissionsProvider.java index d9970ecb1..986dd2555 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePermissionsProvider.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongePermissionsProvider.java @@ -26,8 +26,6 @@ import org.spongepowered.api.service.permission.PermissionDescription; import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.SubjectReference; -import java.util.stream.Collectors; - public class SpongePermissionsProvider { public boolean hasPermission(Player player, String permission) { @@ -44,6 +42,6 @@ public class SpongePermissionsProvider { public String[] getGroups(Player player) { return player.getParents().stream() .map(SubjectReference::getSubjectIdentifier) - .collect(Collectors.toList()).toArray(new String[0]); + .toArray(String[]::new); } }