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

@ -1,214 +0,0 @@
/*
* 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.blocks;
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.LegacyMapper;
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());
}
/**
* Construct a block with the given ID, data value and NBT data structure.
*
* @param state The block state
* @param nbtData NBT data, which may be null
*/
public BaseBlock(BlockStateHolder state, @Nullable CompoundTag nbtData) {
super();
this.blockState = state.toImmutableState();
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 int hashCode() {
return getOrdinal();
}
@Override
public String toString() {
if (this.getNbtData() != null) {
return getAsString() + " {" + String.valueOf(getNbtData()) + "}";
} else {
return getAsString();
}
}
}

View File

@ -1,173 +0,0 @@
/*
* 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.blocks;
/**
* Describes the material for a block.
*/
public interface BlockMaterial {
/**
* Gets if this block is a type of air.
*
* @return If it's air
*/
boolean isAir();
/**
* 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 the opacity of the block
* @return opacity
*/
int getLightOpacity();
/**
* 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();
/**
* Get the map color
* @return or 0
*/
int getMapColor();
}