From 7ef8b2f95e87cf0f0b087552f4435b975926fa90 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 10 Jun 2021 13:39:36 +0100 Subject: [PATCH] Create default TileEntity data where required. Fixes #1093 Fixes #1042 --- .../mc1_15_2/BlockMaterial_1_15_2.java | 18 +++++++++++++++ .../mc1_16_1/BlockMaterial_1_16_1.java | 18 +++++++++++++++ .../mc1_16_2/BlockMaterial_1_16_2.java | 18 +++++++++++++++ .../mc1_16_5/BlockMaterial_1_16_5.java | 18 +++++++++++++++ .../worldedit/world/block/BaseBlock.java | 2 +- .../worldedit/world/block/BlockState.java | 4 ++-- .../world/block/BlockStateHolder.java | 2 +- .../world/block/BlockTypesCache.java | 4 ++-- .../world/block/FuzzyBlockState.java | 2 +- .../world/block/ImmutableBaseBlock.java | 7 ++++-- .../world/registry/BlockMaterial.java | 16 ++++++++++++++ .../registry/PassthroughBlockMaterial.java | 12 ++++++++++ .../world/registry/SimpleBlockMaterial.java | 22 +++++++++++++++++++ 13 files changed, 134 insertions(+), 9 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BlockMaterial_1_15_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BlockMaterial_1_15_2.java index 086b6e776..1355a9a74 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BlockMaterial_1_15_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BlockMaterial_1_15_2.java @@ -1,5 +1,8 @@ package com.boydti.fawe.bukkit.adapter.mc1_15_2; +import com.boydti.fawe.bukkit.adapter.mc1_15_2.nbt.LazyCompoundTag_1_15_2; +import com.google.common.base.Suppliers; +import com.sk89q.jnbt.CompoundTag; import com.sk89q.util.ReflectionUtil; import com.sk89q.worldedit.world.registry.BlockMaterial; import net.minecraft.server.v1_15_R1.Block; @@ -9,6 +12,8 @@ import net.minecraft.server.v1_15_R1.EnumPistonReaction; import net.minecraft.server.v1_15_R1.IBlockData; import net.minecraft.server.v1_15_R1.ITileEntity; import net.minecraft.server.v1_15_R1.Material; +import net.minecraft.server.v1_15_R1.NBTTagCompound; +import net.minecraft.server.v1_15_R1.TileEntity; import org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData; public class BlockMaterial_1_15_2 implements BlockMaterial { @@ -19,6 +24,7 @@ public class BlockMaterial_1_15_2 implements BlockMaterial { private final CraftBlockData craftBlockData; private final org.bukkit.Material craftMaterial; private final int opacity; + private final CompoundTag tile; public BlockMaterial_1_15_2(Block block) { this(block, block.getBlockData()); @@ -32,6 +38,8 @@ public class BlockMaterial_1_15_2 implements BlockMaterial { this.craftMaterial = craftBlockData.getMaterial(); this.isTranslucent = !(boolean) ReflectionUtil.getField(Block.class, block, "v"); opacity = defaultState.b(BlockAccessAir.INSTANCE, BlockPosition.ZERO); + TileEntity tileEntity = !block.isTileEntity() ? null : ((ITileEntity)block).createTile(null); + tile = new LazyCompoundTag_1_15_2(Suppliers.memoize(() -> tileEntity.save(new NBTTagCompound()))); } public Block getBlock() { @@ -150,6 +158,16 @@ public class BlockMaterial_1_15_2 implements BlockMaterial { return block instanceof ITileEntity; } + @Override + public boolean isTile() { + return block.isTileEntity(); + } + + @Override + public CompoundTag getDefaultTile() { + return tile; + } + @Override public int getMapColor() { return material.i().rgb; diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BlockMaterial_1_16_1.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BlockMaterial_1_16_1.java index 5c99c6c4c..97e2fd1f9 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BlockMaterial_1_16_1.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BlockMaterial_1_16_1.java @@ -1,5 +1,8 @@ package com.boydti.fawe.bukkit.adapter.mc1_16_1; +import com.boydti.fawe.bukkit.adapter.mc1_16_1.nbt.LazyCompoundTag_1_16_1; +import com.google.common.base.Suppliers; +import com.sk89q.jnbt.CompoundTag; import com.sk89q.util.ReflectionUtil; import com.sk89q.worldedit.world.registry.BlockMaterial; import net.minecraft.server.v1_16_R1.Block; @@ -10,6 +13,8 @@ import net.minecraft.server.v1_16_R1.EnumPistonReaction; import net.minecraft.server.v1_16_R1.IBlockData; import net.minecraft.server.v1_16_R1.ITileEntity; import net.minecraft.server.v1_16_R1.Material; +import net.minecraft.server.v1_16_R1.NBTTagCompound; +import net.minecraft.server.v1_16_R1.TileEntity; import org.bukkit.craftbukkit.v1_16_R1.block.data.CraftBlockData; public class BlockMaterial_1_16_1 implements BlockMaterial { @@ -20,6 +25,7 @@ public class BlockMaterial_1_16_1 implements BlockMaterial { private final CraftBlockData craftBlockData; private final org.bukkit.Material craftMaterial; private final int opacity; + private final CompoundTag tile; public BlockMaterial_1_16_1(Block block) { this(block, block.getBlockData()); @@ -34,6 +40,8 @@ public class BlockMaterial_1_16_1 implements BlockMaterial { BlockBase.Info blockInfo = ReflectionUtil.getField(BlockBase.class, block, "aB"); this.isTranslucent = !(boolean)ReflectionUtil.getField(BlockBase.Info.class, blockInfo, "n"); opacity = defaultState.b(BlockAccessAir.INSTANCE, BlockPosition.ZERO); + TileEntity tileEntity = !block.isTileEntity() ? null : ((ITileEntity)block).createTile(null); + tile = new LazyCompoundTag_1_16_1(Suppliers.memoize(() -> tileEntity.save(new NBTTagCompound()))); } public Block getBlock() { @@ -153,6 +161,16 @@ public class BlockMaterial_1_16_1 implements BlockMaterial { return block instanceof ITileEntity; } + @Override + public boolean isTile() { + return block.isTileEntity(); + } + + @Override + public CompoundTag getDefaultTile() { + return tile; + } + @Override public int getMapColor() { return material.h().rgb; diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BlockMaterial_1_16_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BlockMaterial_1_16_2.java index 239d4ed49..e3584c2d3 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BlockMaterial_1_16_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BlockMaterial_1_16_2.java @@ -1,5 +1,8 @@ package com.boydti.fawe.bukkit.adapter.mc1_16_2; +import com.boydti.fawe.bukkit.adapter.mc1_16_2.nbt.LazyCompoundTag_1_16_2; +import com.google.common.base.Suppliers; +import com.sk89q.jnbt.CompoundTag; import com.sk89q.util.ReflectionUtil; import com.sk89q.worldedit.world.registry.BlockMaterial; import net.minecraft.server.v1_16_R2.Block; @@ -10,6 +13,8 @@ import net.minecraft.server.v1_16_R2.EnumPistonReaction; import net.minecraft.server.v1_16_R2.IBlockData; import net.minecraft.server.v1_16_R2.ITileEntity; import net.minecraft.server.v1_16_R2.Material; +import net.minecraft.server.v1_16_R2.NBTTagCompound; +import net.minecraft.server.v1_16_R2.TileEntity; import org.bukkit.craftbukkit.v1_16_R2.block.data.CraftBlockData; public class BlockMaterial_1_16_2 implements BlockMaterial { @@ -20,6 +25,7 @@ public class BlockMaterial_1_16_2 implements BlockMaterial { private final CraftBlockData craftBlockData; private final org.bukkit.Material craftMaterial; private final int opacity; + private final CompoundTag tile; public BlockMaterial_1_16_2(Block block) { this(block, block.getBlockData()); @@ -34,6 +40,8 @@ public class BlockMaterial_1_16_2 implements BlockMaterial { BlockBase.Info blockInfo = ReflectionUtil.getField(BlockBase.class, block, "aB"); this.isTranslucent = !(boolean)ReflectionUtil.getField(BlockBase.Info.class, blockInfo, "n"); opacity = defaultState.b(BlockAccessAir.INSTANCE, BlockPosition.ZERO); + TileEntity tileEntity = !block.isTileEntity() ? null : ((ITileEntity)block).createTile(null); + tile = new LazyCompoundTag_1_16_2(Suppliers.memoize(() -> tileEntity.save(new NBTTagCompound()))); } public Block getBlock() { @@ -153,6 +161,16 @@ public class BlockMaterial_1_16_2 implements BlockMaterial { return block instanceof ITileEntity; } + @Override + public boolean isTile() { + return block.isTileEntity(); + } + + @Override + public CompoundTag getDefaultTile() { + return tile; + } + @Override public int getMapColor() { return material.h().rgb; diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/BlockMaterial_1_16_5.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/BlockMaterial_1_16_5.java index d03c2abf4..243593540 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/BlockMaterial_1_16_5.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_5/BlockMaterial_1_16_5.java @@ -1,5 +1,8 @@ package com.boydti.fawe.bukkit.adapter.mc1_16_5; +import com.boydti.fawe.bukkit.adapter.mc1_16_5.nbt.LazyCompoundTag_1_16_5; +import com.google.common.base.Suppliers; +import com.sk89q.jnbt.CompoundTag; import com.sk89q.util.ReflectionUtil; import com.sk89q.worldedit.world.registry.BlockMaterial; import net.minecraft.server.v1_16_R3.Block; @@ -10,6 +13,8 @@ import net.minecraft.server.v1_16_R3.EnumPistonReaction; import net.minecraft.server.v1_16_R3.IBlockData; import net.minecraft.server.v1_16_R3.ITileEntity; import net.minecraft.server.v1_16_R3.Material; +import net.minecraft.server.v1_16_R3.NBTTagCompound; +import net.minecraft.server.v1_16_R3.TileEntity; import org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData; public class BlockMaterial_1_16_5 implements BlockMaterial { @@ -20,6 +25,7 @@ public class BlockMaterial_1_16_5 implements BlockMaterial { private final CraftBlockData craftBlockData; private final org.bukkit.Material craftMaterial; private final int opacity; + private final CompoundTag tile; public BlockMaterial_1_16_5(Block block) { this(block, block.getBlockData()); @@ -34,6 +40,8 @@ public class BlockMaterial_1_16_5 implements BlockMaterial { BlockBase.Info blockInfo = ReflectionUtil.getField(BlockBase.class, block, "aB"); this.isTranslucent = !(boolean)ReflectionUtil.getField(BlockBase.Info.class, blockInfo, "n"); opacity = defaultState.b(BlockAccessAir.INSTANCE, BlockPosition.ZERO); + TileEntity tileEntity = !block.isTileEntity() ? null : ((ITileEntity)block).createTile(null); + tile = new LazyCompoundTag_1_16_5(Suppliers.memoize(() -> tileEntity.save(new NBTTagCompound()))); } public Block getBlock() { @@ -153,6 +161,16 @@ public class BlockMaterial_1_16_5 implements BlockMaterial { return block instanceof ITileEntity; } + @Override + public boolean isTile() { + return block.isTileEntity(); + } + + @Override + public CompoundTag getDefaultTile() { + return tile; + } + @Override public int getMapColor() { return material.h().rgb; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java index c4dda7db1..e16c803f2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java @@ -69,7 +69,7 @@ public class BaseBlock implements BlockStateHolder, TileEntityBlock { */ public BaseBlock(BlockState blockState) { this.blockState = blockState; - nbtData = null; + this.nbtData = null; } /** 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 b2fac7206..709bd7c54 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 @@ -62,12 +62,12 @@ public class BlockState implements BlockStateHolder, 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); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java index 97cd8ebde..9457dbb98 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java @@ -186,7 +186,7 @@ public interface BlockStateHolder> extends TileEnt */ @Nullable default CompoundTag getNbtData() { - return null; + return getBlockType().getMaterial().isTile() ? getBlockType().getMaterial().getDefaultTile() : null; } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypesCache.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypesCache.java index c3bbd0a17..a1b2cf507 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypesCache.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockTypesCache.java @@ -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); } } 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 c749879ae..5c92bc373 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()); + super(state.getBlockType(), state.getInternalId(), state.getOrdinal(), null); 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 index 08469e2c6..94caf8747 100644 --- 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 @@ -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 diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java index 064a58c86..ac06f04f6 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java @@ -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 diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java index 5fe681b9b..b6bdb6502 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java @@ -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(); + } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java index e9bb13c48..4eeb5f139 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java @@ -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; }