Create default TileEntity data where required.

Fixes #1093
Fixes #1042
This commit is contained in:
dordsor21
2021-06-10 13:39:36 +01:00
parent 71130025e8
commit 7ef8b2f95e
13 changed files with 134 additions and 9 deletions

View File

@ -69,7 +69,7 @@ public class BaseBlock implements BlockStateHolder<BaseBlock>, TileEntityBlock {
*/
public BaseBlock(BlockState blockState) {
this.blockState = blockState;
nbtData = null;
this.nbtData = null;
}
/**

View File

@ -62,12 +62,12 @@ public class BlockState implements BlockStateHolder<BlockState>, Pattern {
private final BaseBlock emptyBaseBlock;
private CompoundInput compoundInput = CompoundInput.NULL;
protected BlockState(BlockType blockType, int internalId, int ordinal) {
protected BlockState(BlockType blockType, int internalId, int ordinal, CompoundTag tile) {
this.blockType = blockType;
this.internalId = internalId;
this.ordinal = ordinal;
this.ordinalChar = (char) ordinal;
this.emptyBaseBlock = new ImmutableBaseBlock(this);
this.emptyBaseBlock = new ImmutableBaseBlock(this, tile);
}
/**

View File

@ -186,7 +186,7 @@ public interface BlockStateHolder<B extends BlockStateHolder<B>> extends TileEnt
*/
@Nullable
default CompoundTag getNbtData() {
return null;
return getBlockType().getMaterial().isTile() ? getBlockType().getMaterial().getDefaultTile() : null;
}
/**

View File

@ -98,7 +98,7 @@ public class BlockTypesCache {
int ordinal = this.stateOrdinals[propId];
if (ordinal != -1) {
int stateId = internalId + (propId << BIT_OFFSET);
BlockState state = new BlockState(type, stateId, ordinal);
BlockState state = new BlockState(type, stateId, ordinal, blockMaterial.getDefaultTile());
states.add(state);
}
}
@ -106,7 +106,7 @@ public class BlockTypesCache {
this.defaultState = states.get(this.stateOrdinals[defaultPropId]);
} else {
this.defaultState = new BlockState(type, internalId, states.size());
this.defaultState = new BlockState(type, internalId, states.size(), blockMaterial.getDefaultTile());
states.add(this.defaultState);
}
}

View File

@ -48,7 +48,7 @@ public class FuzzyBlockState extends BlockState {
}
private FuzzyBlockState(BlockState state, Map<Property<?>, Object> values) {
super(state.getBlockType(), state.getInternalId(), state.getOrdinal());
super(state.getBlockType(), state.getInternalId(), state.getOrdinal(), null);
if (values == null || values.isEmpty()) {
props = Collections.emptyMap();
this.values = Collections.emptyMap();

View File

@ -13,16 +13,19 @@ public final class ImmutableBaseBlock extends BaseBlock {
public ImmutableBaseBlock(BlockState blockState) {
super(blockState);
}
public ImmutableBaseBlock(BlockState blockState, CompoundTag tile) {
super(blockState, tile);
}
@Nullable
@Override
public CompoundTag getNbtData() {
return null;
return getBlockType().getMaterial().isTile() ? getBlockType().getMaterial().getDefaultTile() : null;
}
@Override
public boolean hasNbtData() {
return false;
return getBlockType().getMaterial().isTile();
}
@Override

View File

@ -19,6 +19,8 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.jnbt.CompoundTag;
/**
* Describes the material for a block.
*/
@ -165,6 +167,20 @@ public interface BlockMaterial {
*/
boolean hasContainer();
/**
* Gets whether the block is a tile entity.
*
* @return If it is a tile entity
*/
boolean isTile();
/**
* Gets the default (empty) tile entity data
*
* @return default tile entity data
*/
CompoundTag getDefaultTile();
/**
* Get the map color.
* @return or 0

View File

@ -19,6 +19,8 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.jnbt.CompoundTag;
import javax.annotation.Nullable;
import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull;
@ -151,4 +153,14 @@ public class PassthroughBlockMaterial implements BlockMaterial {
public boolean hasContainer() {
return blockMaterial.hasContainer();
}
@Override
public boolean isTile() {
return blockMaterial.isTile();
}
@Override
public CompoundTag getDefaultTile() {
return blockMaterial.getDefaultTile();
}
}

View File

@ -19,6 +19,8 @@
package com.sk89q.worldedit.world.registry;
import com.sk89q.jnbt.CompoundTag;
class SimpleBlockMaterial implements BlockMaterial {
private boolean isAir;
@ -42,6 +44,8 @@ class SimpleBlockMaterial implements BlockMaterial {
private boolean hasContainer;
private int lightOpacity;
private int mapColor;
private boolean isTile;
private CompoundTag tile = null;
@Override
public boolean isAir() {
@ -228,6 +232,24 @@ class SimpleBlockMaterial implements BlockMaterial {
return this.hasContainer;
}
public void setIsTile(boolean isTile) {
this.isTile = isTile;
}
@Override
public boolean isTile() {
return isTile;
}
public void setDefaultTile(CompoundTag tile) {
this.tile = tile;
}
@Override
public CompoundTag getDefaultTile() {
return tile;
}
public void setHasContainer(boolean hasContainer) {
this.hasContainer = hasContainer;
}