Potential minor performance improvements when checking fuzzy equality.

Dieser Commit ist enthalten in:
Matthew Miller 2018-09-20 16:56:46 +10:00
Ursprung 6a71cd2155
Commit 4969dac39c
9 geänderte Dateien mit 58 neuen und 22 gelöschten Zeilen

Datei anzeigen

@ -134,12 +134,15 @@ public class BaseBlock implements BlockStateHolder<BaseBlock>, TileEntityBlock {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof BaseBlock)) { if (!(o instanceof BaseBlock)) {
if (!hasNbtData() && o instanceof BlockStateHolder) {
return Objects.equals(toImmutableState(), ((BlockStateHolder) o).toImmutableState());
}
return false; return false;
} }
final BaseBlock otherBlock = (BaseBlock) o; 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());
} }

Datei anzeigen

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

Datei anzeigen

@ -22,12 +22,12 @@ package com.sk89q.worldedit.world.block;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.NamespacedRegistry; import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes; 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.BundledBlockData;
import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.registry.LegacyMapper;
@ -46,6 +46,7 @@ public class BlockType {
private BlockState defaultState; private BlockState defaultState;
private Map<String, ? extends Property> properties; private Map<String, ? extends Property> properties;
private BlockMaterial blockMaterial; private BlockMaterial blockMaterial;
private Map<Map<Property<?>, Object>, BlockState> blockStatesMap;
public BlockType(String id) { public BlockType(String id) {
this(id, null); this(id, null);
@ -57,7 +58,8 @@ public class BlockType {
id = "minecraft:" + id; id = "minecraft:" + id;
} }
this.id = 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) { if (values != null) {
this.defaultState = values.apply(this.defaultState); this.defaultState = values.apply(this.defaultState);
} }
@ -127,6 +129,15 @@ public class BlockType {
return this.defaultState; 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. * Gets whether this block type has an item representation.
* *

Datei anzeigen

@ -28,10 +28,12 @@ import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.Tag; import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector; 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.DataException;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockState; 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.registry.LegacyMapper;
import com.sk89q.worldedit.world.storage.InvalidFormatException; import com.sk89q.worldedit.world.storage.InvalidFormatException;
@ -51,8 +53,6 @@ public class AnvilChunk implements Chunk {
private int rootZ; private int rootZ;
private Map<BlockVector, Map<String,Tag>> tileEntities; private Map<BlockVector, Map<String,Tag>> tileEntities;
@SuppressWarnings("unused")
private World world; // TODO: remove if stays unused.
/** /**
* Construct the chunk with a compound tag. * Construct the chunk with a compound tag.
@ -63,7 +63,6 @@ public class AnvilChunk implements Chunk {
*/ */
public AnvilChunk(World world, CompoundTag tag) throws DataException { public AnvilChunk(World world, CompoundTag tag) throws DataException {
rootTag = tag; rootTag = tag;
this.world = world;
rootX = NBTUtils.getChildTag(rootTag.getValue(), "xPos", IntTag.class).getValue(); rootX = NBTUtils.getChildTag(rootTag.getValue(), "xPos", IntTag.class).getValue();
rootZ = NBTUtils.getChildTag(rootTag.getValue(), "zPos", IntTag.class).getValue(); rootZ = NBTUtils.getChildTag(rootTag.getValue(), "zPos", IntTag.class).getValue();
@ -255,14 +254,22 @@ public class AnvilChunk implements Chunk {
} }
@Override @Override
public BaseBlock getBlock(Vector position) throws DataException { public BlockStateHolder getBlock(Vector position) throws DataException {
int id = getBlockID(position); int id = getBlockID(position);
int data = getBlockData(position); int data = getBlockData(position);
BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, data); 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); CompoundTag tileEntity = getBlockTileEntity(position);
if (tileEntity != null) {
return state.toBaseBlock(tileEntity); return state.toBaseBlock(tileEntity);
} }
return state;
}
} }

Datei anzeigen

@ -29,9 +29,9 @@ import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.registry.state.Property; 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.DataException;
import com.sk89q.worldedit.world.block.BlockState; 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.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.storage.InvalidFormatException; import com.sk89q.worldedit.world.storage.InvalidFormatException;
@ -230,7 +230,7 @@ public class AnvilChunk13 implements Chunk {
} }
@Override @Override
public BaseBlock getBlock(Vector position) throws DataException { public BlockStateHolder getBlock(Vector position) throws DataException {
int x = position.getBlockX() - rootX * 16; int x = position.getBlockX() - rootX * 16;
int y = position.getBlockY(); int y = position.getBlockY();
int z = position.getBlockZ() - rootZ * 16; int z = position.getBlockZ() - rootZ * 16;
@ -247,7 +247,11 @@ public class AnvilChunk13 implements Chunk {
CompoundTag tileEntity = getBlockTileEntity(position); CompoundTag tileEntity = getBlockTileEntity(position);
if (tileEntity != null) {
return state.toBaseBlock(tileEntity); return state.toBaseBlock(tileEntity);
} }
return state;
}
} }

Datei anzeigen

@ -20,8 +20,8 @@
package com.sk89q.worldedit.world.chunk; package com.sk89q.worldedit.world.chunk;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.DataException; import com.sk89q.worldedit.world.DataException;
import com.sk89q.worldedit.world.block.BlockStateHolder;
/** /**
* A 16 by 16 block chunk. * A 16 by 16 block chunk.
@ -35,6 +35,6 @@ public interface Chunk {
* @return block the block * @return block the block
* @throws DataException thrown on data error * @throws DataException thrown on data error
*/ */
BaseBlock getBlock(Vector position) throws DataException; BlockStateHolder getBlock(Vector position) throws DataException;
} }

Datei anzeigen

@ -27,10 +27,11 @@ import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.Tag; import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector; 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.DataException;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockState; 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.block.BlockTypes;
import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.registry.LegacyMapper;
import com.sk89q.worldedit.world.storage.InvalidFormatException; import com.sk89q.worldedit.world.storage.InvalidFormatException;
@ -153,7 +154,7 @@ public class OldChunk implements Chunk {
} }
@Override @Override
public BaseBlock getBlock(Vector position) throws DataException { public BlockStateHolder getBlock(Vector position) throws DataException {
if(position.getBlockY() >= 128) BlockTypes.VOID_AIR.getDefaultState().toBaseBlock(); if(position.getBlockY() >= 128) BlockTypes.VOID_AIR.getDefaultState().toBaseBlock();
int id, dataVal; int id, dataVal;
@ -181,9 +182,18 @@ public class OldChunk implements Chunk {
} }
BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, dataVal); 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); CompoundTag tileEntity = getBlockTileEntity(position);
if (tileEntity != null) {
return state.toBaseBlock(tileEntity); return state.toBaseBlock(tileEntity);
} }
return state;
}
} }

Datei anzeigen

@ -147,8 +147,7 @@ public class SnapshotRestore {
// Now just copy blocks! // Now just copy blocks!
for (Vector pos : entry.getValue()) { for (Vector pos : entry.getValue()) {
try { try {
BaseBlock block = chunk.getBlock(pos); editSession.setBlock(pos, chunk.getBlock(pos));
editSession.setBlock(pos, block);
} catch (DataException e) { } catch (DataException e) {
// this is a workaround: just ignore for now // this is a workaround: just ignore for now
} }

Datei anzeigen

@ -26,8 +26,6 @@ import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.service.permission.SubjectReference; import org.spongepowered.api.service.permission.SubjectReference;
import java.util.stream.Collectors;
public class SpongePermissionsProvider { public class SpongePermissionsProvider {
public boolean hasPermission(Player player, String permission) { public boolean hasPermission(Player player, String permission) {
@ -44,6 +42,6 @@ public class SpongePermissionsProvider {
public String[] getGroups(Player player) { public String[] getGroups(Player player) {
return player.getParents().stream() return player.getParents().stream()
.map(SubjectReference::getSubjectIdentifier) .map(SubjectReference::getSubjectIdentifier)
.collect(Collectors.toList()).toArray(new String[0]); .toArray(String[]::new);
} }
} }