diff --git a/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java b/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java index 4890ed694..859e5abe8 100644 --- a/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java +++ b/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java @@ -1,5 +1,6 @@ package com.ldtteam.structurize.component; +import com.ldtteam.structurize.api.Log; import com.ldtteam.structurize.api.RotationMirror; import com.ldtteam.structurize.blueprints.v1.Blueprint; import com.mojang.serialization.Codec; @@ -67,22 +68,41 @@ public CapturedBlock(final BlockState blockState, */ public CapturedBlock applyRotationMirror(final RotationMirror rotationMirror, final Level level) { + final BlockState rotatedState = rotationMirror.applyToBlockState(blockState); + + // No BE data: just rotate the state. if (serializedBE.isEmpty()) { - return new CapturedBlock(rotationMirror.applyToBlockState(blockState), serializedBE, itemStack); + return new CapturedBlock(rotatedState, serializedBE, itemStack); } + // If the rotated state does not host a BE, drop any BE tag (prevents renderer/loader issues). + if (!rotatedState.hasBlockEntity()) + { + Log.getLogger().warn("Block {} is not empty, but has no block entity.", rotatedState); + return new CapturedBlock(rotatedState, Optional.empty(), itemStack); + } + + // Rotate/migrate the BE tag using the Blueprint rotation logic final Blueprint blueprint = new Blueprint((short) 1, (short) 1, (short) 1, level.registryAccess()); - blueprint.addBlockState(BlockPos.ZERO, blockState); + blueprint.addBlockState(BlockPos.ZERO, rotatedState); blueprint.getTileEntities()[0][0][0] = serializedBE.get(); blueprint.setCachePrimaryOffset(BlockPos.ZERO); blueprint.setRotationMirrorRelative(rotationMirror, level); - return new CapturedBlock(blueprint.getPalette()[blueprint.getPalleteSize()], - Optional.of(blueprint.getTileEntities()[0][0][0]), - itemStack); + final CompoundTag rotatedTag = blueprint.getTileEntities()[0][0][0]; + Optional beTag = Optional.ofNullable(rotatedTag); + + // Drop invalid/empty BE tags. + if (beTag.isEmpty() || beTag.get().isEmpty() || !beTag.get().contains("id")) + { + beTag = Optional.empty(); + } + + return new CapturedBlock(rotatedState, beTag, itemStack); } + public boolean hasBlockEntity() { return serializedBE.isPresent() && !serializedBE.get().isEmpty();