package net.minecraft.world.level.block; import com.mojang.serialization.MapCodec; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.FrontAndTop; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.JigsawBlockEntity; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; import net.minecraft.world.phys.BlockHitResult; public class JigsawBlock extends Block implements EntityBlock, GameMasterBlock { public static final MapCodec CODEC = simpleCodec(JigsawBlock::new); public static final EnumProperty ORIENTATION = BlockStateProperties.ORIENTATION; @Override public MapCodec codec() { return CODEC; } protected JigsawBlock(BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(ORIENTATION, FrontAndTop.NORTH_UP)); } @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { builder.add(ORIENTATION); } @Override protected BlockState rotate(BlockState state, Rotation rotation) { return state.setValue(ORIENTATION, rotation.rotation().rotate(state.getValue(ORIENTATION))); } @Override protected BlockState mirror(BlockState state, Mirror mirror) { return state.setValue(ORIENTATION, mirror.rotation().rotate(state.getValue(ORIENTATION))); } @Override public BlockState getStateForPlacement(BlockPlaceContext context) { Direction direction = context.getClickedFace(); Direction direction2; if (direction.getAxis() == Direction.Axis.Y) { direction2 = context.getHorizontalDirection().getOpposite(); } else { direction2 = Direction.UP; } return this.defaultBlockState().setValue(ORIENTATION, FrontAndTop.fromFrontAndTop(direction, direction2)); } @Override public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { return new JigsawBlockEntity(pos, state); } @Override protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) { BlockEntity blockEntity = level.getBlockEntity(pos); if (blockEntity instanceof JigsawBlockEntity && player.canUseGameMasterBlocks()) { player.openJigsawBlock((JigsawBlockEntity)blockEntity); return InteractionResult.SUCCESS; } else { return InteractionResult.PASS; } } public static boolean canAttach(StructureTemplate.JigsawBlockInfo jigsawBlockInfo, StructureTemplate.JigsawBlockInfo jigsawBlockInfo2) { Direction direction = getFrontFacing(jigsawBlockInfo.info().state()); Direction direction2 = getFrontFacing(jigsawBlockInfo2.info().state()); Direction direction3 = getTopFacing(jigsawBlockInfo.info().state()); Direction direction4 = getTopFacing(jigsawBlockInfo2.info().state()); JigsawBlockEntity.JointType jointType = jigsawBlockInfo.jointType(); boolean bl = jointType == JigsawBlockEntity.JointType.ROLLABLE; return direction == direction2.getOpposite() && (bl || direction3 == direction4) && jigsawBlockInfo.target().equals(jigsawBlockInfo2.name()); } /** * This represents the face that the puzzle piece is on. To connect: 2 jigsaws must have their puzzle piece face facing each other. */ public static Direction getFrontFacing(BlockState state) { return ((FrontAndTop)state.getValue(ORIENTATION)).front(); } /** * This represents the face that the line connector is on. To connect, if the OrientationType is ALIGNED, the two lines must be in the same direction. (Their textures will form one straight line) */ public static Direction getTopFacing(BlockState state) { return ((FrontAndTop)state.getValue(ORIENTATION)).top(); } }