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
committed by IronApollo
parent 4d6045813c
commit 628c9cc0b7
185 changed files with 307 additions and 268 deletions

View File

@ -0,0 +1,241 @@
/*
* 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.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
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.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import com.sk89q.worldedit.blocks.TileEntityBlock;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.registry.state.Property;
import javax.annotation.Nullable;
import java.util.Objects;
/**
* Represents a "snapshot" of a block with NBT Data.
*
* <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>
*/
public class BaseBlock extends BlockState {
private final BlockState blockState;
@Nullable
protected CompoundTag nbtData;
@Deprecated
public BaseBlock() {
this(BlockTypes.AIR.getDefaultState());
}
// /**
// * Construct a block with a state.
// * @deprecated Just use the BlockStateHolder instead
// * @param blockState The blockstate
// */
// @Deprecated
// public BaseBlock(BlockStateHolder blockState) {
// this(blockState, blockState.getNbtData());
// }
@Deprecated
public BaseBlock(BlockTypes id) {
this(id.getDefaultState());
}
/**
* Construct a block with the given type and default data.
* @deprecated Just use the BlockType.getDefaultState()
* @param blockType The block type
*/
@Deprecated
public BaseBlock(BlockType blockType) {
this(blockType.getDefaultState());
}
public BaseBlock(BlockState blockState) {
this(blockState, blockState.getNbtData());
}
/**
* 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
*/
public BaseBlock(BlockState state, CompoundTag nbtData) {
super(state.getBlockType());
checkNotNull(nbtData);
this.blockState = state;
this.nbtData = nbtData;
}
/**
* Construct a block with the given ID and data value.
*
* @param id ID value
* @param data data value
*/
@Deprecated
public BaseBlock(int id, int data) {
this(getState(id, data));
}
private static final BlockState getState(int id, int data) {
BlockState blockState = LegacyMapper.getInstance().getBlockFromLegacy(id, data);
if (blockState == null) {
blockState = BlockTypes.AIR.getDefaultState();
}
return blockState;
}
protected BaseBlock(int internalId, CompoundTag nbtData) {
this(BlockState.getFromInternalId(internalId), nbtData);
}
@Deprecated
public static BaseBlock getFromInternalId(int id, CompoundTag nbtData) {
return new BaseBlock(id, nbtData);
}
/**
* Create a clone of another block.
*
* @param other the other block
*/
@Deprecated
public BaseBlock(BaseBlock other) {
this(other.toImmutableState(), other.getNbtData());
}
@Override
public BlockState toFuzzy() {
return blockState;
}
@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) {
this.nbtData = nbtData;
}
/**
* 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.equals(otherBlock) && Objects.equals(getNbtData(), otherBlock.getNbtData());
}
@Override
public final BlockState toImmutableState() {
return blockState;
}
@Override
public int getInternalId() {
return blockState.getInternalId();
}
@Override
public BlockMaterial getMaterial() {
return blockState.getMaterial();
}
@Override
public BlockTypes getBlockType() {
return blockState.getBlockType();
}
@Override
public int getOrdinal() {
return blockState.getOrdinal();
}
@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() {
return getOrdinal();
}
@Override
public String toString() {
if (this.getNbtData() != null) {
return getAsString() + " {" + String.valueOf(getNbtData()) + "}";
} else {
return getAsString();
}
}
}

View File

@ -26,10 +26,10 @@ import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
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.WorldEditException;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extent.Extent;
@ -85,6 +85,14 @@ public abstract class BlockState implements BlockStateHolder<BlockState> {
public static BlockState get(@Nullable BlockType type, String state) throws InputParseException {
return get(type, state, null);
}
private BaseBlock emptyBaseBlock;
// Neighbouring state table.
private Table<Property<?>, Object, BlockState> states;
protected BlockState(BlockType blockType) {
this.emptyBaseBlock = new BaseBlock(this);
}
/**
* Returns a temporary BlockState for a given type and string
@ -298,6 +306,22 @@ public abstract class BlockState implements BlockStateHolder<BlockState> {
return (Map<Property<?>, Object>) map;
}
@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);
}
/**
* Deprecated, use masks - not try to this fuzzy/non fuzzy state nonsense
* @return

View File

@ -19,13 +19,14 @@
package com.sk89q.worldedit.world.block;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.blocks.TileEntityBlock;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.pattern.FawePattern;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.registry.state.PropertyKey;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import java.util.Map;
import java.util.stream.Collectors;
@ -131,12 +132,31 @@ public interface BlockStateHolder<T extends BlockStateHolder> extends FawePatter
boolean equalsFuzzy(BlockStateHolder o);
/**
<<<<<<< HEAD
* Returns an immutable BlockStateHolder from this BlockStateHolder.
=======
* Returns an immutable {@link BlockState} from this BlockStateHolder.
>>>>>>> f54d6afb... Make BaseBlock more memory efficient, and make it clear in the API that it's not intended to be used for every single block.
*
* @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

@ -1,8 +1,8 @@
package com.sk89q.worldedit.world.block;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.world.registry.BlockMaterial;
public class BlockStateImpl extends BlockState {
private final int internalId;
@ -11,6 +11,7 @@ public class BlockStateImpl extends BlockState {
private BlockMaterial material;
protected BlockStateImpl(BlockTypes type, int internalId, int ordinal) {
super(type);
this.type = type;
this.internalId = internalId;
this.ordinal = ordinal;

View File

@ -21,12 +21,16 @@ package com.sk89q.worldedit.world.block;
import com.google.common.collect.ImmutableList;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.SingleBlockTypeMask;
import com.sk89q.worldedit.function.pattern.FawePattern;
import com.sk89q.worldedit.math.BlockVector3;
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.registry.state.PropertyKey;
import com.sk89q.worldedit.world.item.ItemType;

View File

@ -28,7 +28,6 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extent.Extent;
@ -40,6 +39,7 @@ import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.registry.state.PropertyKey;
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;
import it.unimi.dsi.fastutil.ints.IntCollections;