Make BaseBlock more memory efficient, and make it clear in the API that it's not intended to be used for every single block.

This commit is contained in:
Matthew Miller
2018-08-10 20:29:06 +10:00
parent 5f4cc3e694
commit f54d6afb65
50 changed files with 118 additions and 90 deletions

View File

@ -24,7 +24,7 @@ import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
@ -126,7 +126,7 @@ public class NullWorld extends AbstractWorld {
@Override
public BaseBlock getFullBlock(Vector position) {
return new BaseBlock(getBlock(position));
return getBlock(position).toBaseBlock();
}
@Override

View File

@ -0,0 +1,211 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.block;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.TileEntityBlock;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.registry.state.Property;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* Represents a mutable "snapshot" of a block.
*
* <p>An instance of this block contains all the information needed to
* accurately reproduce the block, provided that the instance was
* made correctly. In some implementations, it may not be possible to get a
* snapshot of blocks correctly, so, for example, the NBT data for a block
* may be missing.</p>
*
* <p>A peculiar detail of this class is that it accepts {@code -1} as a
* valid data value. This is due to legacy reasons: WorldEdit uses -1
* as a "wildcard" block value, even though a {@link Mask} would be
* more appropriate.</p>
*/
public class BaseBlock implements BlockStateHolder<BaseBlock>, TileEntityBlock {
private BlockState blockState;
@Nullable private CompoundTag nbtData;
/**
* Construct a block with a state.
*
* @param blockState The blockstate
*/
protected BaseBlock(BlockState blockState) {
this.blockState = blockState;
}
/**
* Construct a block with the given ID, data value and NBT data structure.
*
* @param state The block state
* @param nbtData NBT data, which must be provided
*/
protected BaseBlock(BlockState state, CompoundTag nbtData) {
checkNotNull(nbtData);
this.blockState = state;
this.nbtData = nbtData;
}
/**
* Create a clone of another block.
*
* @param other the other block
*/
public BaseBlock(BaseBlock other) {
this(other.toImmutableState(), other.getNbtData());
}
/**
* Gets a map of state to statevalue
*
* @return The state map
*/
@Override
public Map<Property<?>, Object> getStates() {
return this.blockState.getStates();
}
@Override
public BlockType getBlockType() {
return this.blockState.getBlockType();
}
@Override
public <V> BaseBlock with(Property<V> property, V value) {
return new BaseBlock(this.blockState.with(property, value), getNbtData());
}
/**
* Gets the State for this Block.
*
* @param property The state to get the value for
* @return The state value
*/
@Override
public <V> V getState(Property<V> property) {
return this.blockState.getState(property);
}
@Override
public boolean hasNbtData() {
return getNbtData() != null;
}
@Override
public String getNbtId() {
CompoundTag nbtData = getNbtData();
if (nbtData == null) {
return "";
}
Tag idTag = nbtData.getValue().get("id");
if (idTag instanceof StringTag) {
return ((StringTag) idTag).getValue();
} else {
return "";
}
}
@Nullable
@Override
public CompoundTag getNbtData() {
return this.nbtData;
}
@Override
public void setNbtData(@Nullable CompoundTag nbtData) {
throw new UnsupportedOperationException("This class is immutable.");
}
/**
* Checks whether the type ID and data value are equal.
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof BaseBlock)) {
return false;
}
final BaseBlock otherBlock = (BaseBlock) o;
return this.toImmutableState().equals(otherBlock.toImmutableState()) && Objects.equals(getNbtData(), otherBlock.getNbtData());
}
/**
* Checks if the type is the same, and if the matched states are the same.
*
* @param o other block
* @return true if equal
*/
@Override
public boolean equalsFuzzy(BlockStateHolder o) {
return this.toImmutableState().equalsFuzzy(o);
}
@Override
public BlockState toImmutableState() {
return this.blockState;
}
@Override
public BaseBlock toBaseBlock() {
return this;
}
@Override
public BaseBlock toBaseBlock(CompoundTag compoundTag) {
if (compoundTag == null) {
return this.blockState.toBaseBlock();
} else if (compoundTag == this.nbtData) {
return this;
} else {
return new BaseBlock(this.blockState, compoundTag);
}
}
@Override
public int hashCode() {
int ret = toImmutableState().hashCode() << 3;
if (hasNbtData()) {
ret += getNbtData().hashCode();
}
return ret;
}
@Override
public String toString() {
// if (getNbtData() != null) { // TODO Maybe make some JSON serialiser to make this not awful.
// return blockState.getAsString() + " {" + String.valueOf(getNbtData()) + "}";
// } else {
return blockState.getAsString();
// }
}
}

View File

@ -24,6 +24,7 @@ import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.registry.state.Property;
@ -47,12 +48,15 @@ public class BlockState implements BlockStateHolder<BlockState> {
private final Map<Property<?>, Object> values;
private final boolean fuzzy;
private BaseBlock emptyBaseBlock;
// Neighbouring state table.
private Table<Property<?>, Object, BlockState> states;
private BlockState(BlockType blockType) {
this.blockType = blockType;
this.values = new LinkedHashMap<>();
this.emptyBaseBlock = new BaseBlock(this);
this.fuzzy = false;
}
@ -197,6 +201,22 @@ public class BlockState implements BlockStateHolder<BlockState> {
return this;
}
@Override
public BaseBlock toBaseBlock() {
if (this.fuzzy) {
throw new IllegalArgumentException("Can't create a BaseBlock from a fuzzy BlockState!");
}
return this.emptyBaseBlock;
}
@Override
public BaseBlock toBaseBlock(CompoundTag compoundTag) {
if (compoundTag == null) {
return toBaseBlock();
}
return new BaseBlock(this, compoundTag);
}
/**
* Internal method used for creating the initial BlockState.
*

View File

@ -19,6 +19,7 @@
package com.sk89q.worldedit.world.block;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.registry.state.Property;
import java.util.Map;
@ -66,12 +67,27 @@ public interface BlockStateHolder<T extends BlockStateHolder> {
boolean equalsFuzzy(BlockStateHolder o);
/**
* Returns an immutable BlockState from this BlockStateHolder.
* Returns an immutable {@link BlockState} from this BlockStateHolder.
*
* @return A BlockState
*/
BlockState toImmutableState();
/**
* Gets a {@link BaseBlock} from this BlockStateHolder.
*
* @return The BaseBlock
*/
BaseBlock toBaseBlock();
/**
* Gets a {@link BaseBlock} from this BlockStateHolder.
*
* @param compoundTag The NBT Data to apply
* @return The BaseBlock
*/
BaseBlock toBaseBlock(CompoundTag compoundTag);
default String getAsString() {
if (getStates().isEmpty()) {
return this.getBlockType().getId();

View File

@ -22,7 +22,7 @@ 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.blocks.BlockMaterial;
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;

View File

@ -28,7 +28,7 @@ import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.DataException;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockState;
@ -262,7 +262,7 @@ public class AnvilChunk implements Chunk {
BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, data);
CompoundTag tileEntity = getBlockTileEntity(position);
return new BaseBlock(state, tileEntity);
return state.toBaseBlock(tileEntity);
}
}

View File

@ -20,7 +20,7 @@
package com.sk89q.worldedit.world.chunk;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.DataException;
/**

View File

@ -27,7 +27,7 @@ import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.DataException;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.block.BlockState;
@ -154,7 +154,7 @@ public class OldChunk implements Chunk {
@Override
public BaseBlock getBlock(Vector position) throws DataException {
if(position.getBlockY() >= 128) new BaseBlock(BlockTypes.AIR);
if(position.getBlockY() >= 128) BlockTypes.AIR.getDefaultState().toBaseBlock();
int id, dataVal;
int x = position.getBlockX() - rootX * 16;
@ -183,7 +183,7 @@ public class OldChunk implements Chunk {
BlockState state = LegacyMapper.getInstance().getBlockFromLegacy(id, dataVal);
CompoundTag tileEntity = getBlockTileEntity(position);
return new BaseBlock(state, tileEntity);
return state.toBaseBlock(tileEntity);
}
}

View File

@ -0,0 +1,154 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry;
/**
* Describes the material for a block.
*/
public interface BlockMaterial {
/**
* Get whether this block is a full sized cube.
*
* @return the value of the test
*/
boolean isFullCube();
/**
* Get whether this block is opaque.
*
* @return the value of the test
*/
boolean isOpaque();
/**
* Get whether this block emits a Redstone signal.
*
* @return the value of the test
*/
boolean isPowerSource();
/**
* Get whether this block is a liquid.
*
* @return the value of the test
*/
boolean isLiquid();
/**
* Get whether this block is a solid.
*
* @return the value of the test
*/
boolean isSolid();
/**
* Get the hardness factor for this block.
*
* @return the hardness factor
*/
float getHardness();
/**
* Get the resistance factor for this block.
*
* @return the resistance factor
*/
float getResistance();
/**
* Get the slipperiness factor for this block.
*
* @return the slipperiness factor
*/
float getSlipperiness();
/**
* Get the light value for this block.
*
* @return the light value
*/
int getLightValue();
/**
* Get whether this block breaks when it is pushed by a piston.
*
* @return true if the block breaks
*/
boolean isFragileWhenPushed();
/**
* Get whether this block can be pushed by a piston.
*
* @return true if the block cannot be pushed
*/
boolean isUnpushable();
/**
* Get whether this block is ticked randomly.
*
* @return true if this block is ticked randomly
*/
boolean isTicksRandomly();
/**
* Get whether this block prevents movement.
*
* @return true if this block blocks movement
*/
boolean isMovementBlocker();
/**
* Get whether this block will burn.
*
* @return true if this block will burn
*/
boolean isBurnable();
/**
* Get whether this block needs to be broken by a tool for maximum
* speed.
*
* @return true if a tool is required
*/
boolean isToolRequired();
/**
* Get whether this block is replaced when a block is placed over it
* (for example, tall grass).
*
* @return true if the block is replaced
*/
boolean isReplacedDuringPlacement();
/**
* Get whether this block is translucent.
*
* @return true if the block is translucent
*/
boolean isTranslucent();
/**
* Gets whether the block has a container (Item container)
*
* @return If it has a container
*/
boolean hasContainer();
}

View File

@ -19,7 +19,6 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockType;

View File

@ -24,7 +24,6 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.util.gson.VectorAdapter;
import java.io.IOException;

View File

@ -19,7 +19,6 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockType;

View File

@ -19,8 +19,6 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BlockMaterial;
import javax.annotation.Nullable;
public class PassthroughBlockMaterial implements BlockMaterial {

View File

@ -19,8 +19,6 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BlockMaterial;
class SimpleBlockMaterial implements BlockMaterial {
private boolean fullCube;

View File

@ -24,7 +24,7 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.DataException;