From 717a1b5db4c64d04f2b5f4bdf5d4e9eeadbadbab Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 10 Jun 2021 19:47:34 +0100 Subject: [PATCH] Replace ImmutableBaseBlock with BlanketBaseBlock - ImmutableBaseBlock is no longer needed as NBT was made immutable previously - BlanketBaseBlock represents to masks that the block has NBT, but does not need to match a specific state - Allow FuzzyBlockState to create BaseBlocks without NBT data (fixed tab completion issues) --- .../factory/parser/DefaultBlockParser.java | 4 +- .../worldedit/function/mask/BlockMask.java | 4 +- .../world/block/BlanketBaseBlock.java | 33 +++++++++++ .../worldedit/world/block/BlockState.java | 13 ++++- .../world/block/FuzzyBlockState.java | 2 +- .../world/block/ImmutableBaseBlock.java | 58 ------------------- 6 files changed, 50 insertions(+), 64 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlanketBaseBlock.java delete mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/world/block/ImmutableBaseBlock.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java index acf4874d1..f60e5490a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java @@ -57,6 +57,7 @@ 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.block.FuzzyBlockState; +import com.sk89q.worldedit.world.block.BlanketBaseBlock; import com.sk89q.worldedit.world.entity.EntityType; import com.sk89q.worldedit.world.entity.EntityTypes; import com.sk89q.worldedit.world.registry.LegacyMapper; @@ -417,7 +418,8 @@ public class DefaultBlockParser extends InputParser { } if (nbt != null) { - return validate(context, state.toBaseBlock(nbt)); + BaseBlock result = blockStates.size() > 0 ? state.toBaseBlock(nbt) : new BlanketBaseBlock(state, nbt); + return validate(context, result); } if (blockType == BlockTypes.SIGN || blockType == BlockTypes.WALL_SIGN diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMask.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMask.java index efe910dd3..26e6e8da0 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMask.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/mask/BlockMask.java @@ -27,7 +27,7 @@ import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypesCache; -import com.sk89q.worldedit.world.block.ImmutableBaseBlock; +import com.sk89q.worldedit.world.block.BlanketBaseBlock; import javax.annotation.Nullable; import java.util.Arrays; @@ -155,7 +155,7 @@ public class BlockMask extends ABlockMask { public void add(Collection blocks) { checkNotNull(blocks); for (BaseBlock block : blocks) { - if (block instanceof ImmutableBaseBlock) { + if (block instanceof BlanketBaseBlock) { for (BlockState state : block.getBlockType().getAllStates()) { ordinals[state.getOrdinal()] = true; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlanketBaseBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlanketBaseBlock.java new file mode 100644 index 000000000..116c6adc5 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlanketBaseBlock.java @@ -0,0 +1,33 @@ +package com.sk89q.worldedit.world.block; + +import com.sk89q.jnbt.CompoundTag; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.extent.Extent; +import com.sk89q.worldedit.extent.OutputExtent; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.registry.state.Property; +import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nullable; + +/** + * BaseBlock that when parsed to masks represents all BlockStates of a BlockType, whilst allowing for NBT storage + */ +public final class BlanketBaseBlock extends BaseBlock { + + public BlanketBaseBlock(BlockState blockState) { + super(blockState); + } + + public BlanketBaseBlock(BlockState blockState, @NotNull CompoundTag tile) { + super(blockState, tile); + } + + @Override + public BaseBlock toBaseBlock(CompoundTag compoundTag) { + if (compoundTag != null) { + return new BaseBlock(this.toImmutableState(), compoundTag); + } + return this; + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java index 709bd7c54..18a709988 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java @@ -41,6 +41,7 @@ import com.sk89q.worldedit.registry.state.AbstractProperty; import com.sk89q.worldedit.registry.state.Property; import com.sk89q.worldedit.registry.state.PropertyKey; import com.sk89q.worldedit.world.registry.BlockMaterial; +import org.jetbrains.annotations.NotNull; import java.util.Collections; import java.util.List; @@ -62,12 +63,20 @@ public class BlockState implements BlockStateHolder, Pattern { private final BaseBlock emptyBaseBlock; private CompoundInput compoundInput = CompoundInput.NULL; - protected BlockState(BlockType blockType, int internalId, int ordinal, CompoundTag tile) { + protected BlockState(BlockType blockType, int internalId, int ordinal) { this.blockType = blockType; this.internalId = internalId; this.ordinal = ordinal; this.ordinalChar = (char) ordinal; - this.emptyBaseBlock = new ImmutableBaseBlock(this, tile); + this.emptyBaseBlock = new BlanketBaseBlock(this); + } + + protected BlockState(BlockType blockType, int internalId, int ordinal, @NotNull CompoundTag tile) { + this.blockType = blockType; + this.internalId = internalId; + this.ordinal = ordinal; + this.ordinalChar = (char) ordinal; + this.emptyBaseBlock = new BlanketBaseBlock(this, tile); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java index 5c92bc373..c749879ae 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/FuzzyBlockState.java @@ -48,7 +48,7 @@ public class FuzzyBlockState extends BlockState { } private FuzzyBlockState(BlockState state, Map, Object> values) { - super(state.getBlockType(), state.getInternalId(), state.getOrdinal(), null); + super(state.getBlockType(), state.getInternalId(), state.getOrdinal()); if (values == null || values.isEmpty()) { props = Collections.emptyMap(); this.values = Collections.emptyMap(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/ImmutableBaseBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/ImmutableBaseBlock.java deleted file mode 100644 index 94caf8747..000000000 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/ImmutableBaseBlock.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.sk89q.worldedit.world.block; - -import com.sk89q.jnbt.CompoundTag; -import com.sk89q.worldedit.WorldEditException; -import com.sk89q.worldedit.extent.Extent; -import com.sk89q.worldedit.extent.OutputExtent; -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.registry.state.Property; - -import javax.annotation.Nullable; - -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 getBlockType().getMaterial().isTile() ? getBlockType().getMaterial().getDefaultTile() : null; - } - - @Override - public boolean hasNbtData() { - return getBlockType().getMaterial().isTile(); - } - - @Override - public String getNbtId() { - return ""; - } - - @Override - public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException { - return set.setBlock(extent, toBlockState()); - } - - @Override - public void applyTileEntity(OutputExtent output, int x, int y, int z) { - - } - - @Override - public BaseBlock with(Property property, V value) { - return toImmutableState().with(property, value).toBaseBlock(); - } - - @Override - public BaseBlock toBaseBlock(CompoundTag compoundTag) { - if (compoundTag != null) { - return new BaseBlock(this.toImmutableState(), compoundTag); - } - return this; - } -}