Potential minor performance improvements when checking fuzzy equality.

This commit is contained in:
Matthew Miller
2018-09-20 16:56:46 +10:00
parent 6a71cd2155
commit 4969dac39c
9 changed files with 58 additions and 22 deletions

View File

@ -134,12 +134,15 @@ public class BaseBlock implements BlockStateHolder<BaseBlock>, 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());
}

View File

@ -168,6 +168,10 @@ public class BlockState implements BlockStateHolder<BlockState> {
@Override
public boolean equalsFuzzy(BlockStateHolder o) {
if (this == o) {
// Added a reference equality check for
return true;
}
if (!getBlockType().equals(o.getBlockType())) {
return false;
}

View File

@ -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<String, ? extends Property> properties;
private BlockMaterial blockMaterial;
private Map<Map<Property<?>, 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<BlockState> getAllStates() {
return ImmutableList.copyOf(this.blockStatesMap.values());
}
/**
* Gets whether this block type has an item representation.
*