diff --git a/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java b/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java index 4890ed694..e650da23d 100644 --- a/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java +++ b/src/main/java/com/ldtteam/structurize/component/CapturedBlock.java @@ -67,22 +67,40 @@ 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 cannot host a BE, drop any BE tag (prevents renderer/loader issues). + if (!rotatedState.hasBlockEntity()) + { + 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();