minecraft-src/net/minecraft/world/level/block/JigsawBlock.java
2025-07-04 03:45:38 +03:00

102 lines
4.1 KiB
Java

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.Builder;
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<JigsawBlock> CODEC = simpleCodec(JigsawBlock::new);
public static final EnumProperty<FrontAndTop> ORIENTATION = BlockStateProperties.ORIENTATION;
@Override
public MapCodec<JigsawBlock> codec() {
return CODEC;
}
protected JigsawBlock(BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any().setValue(ORIENTATION, FrontAndTop.NORTH_UP));
}
@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> 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 parent, StructureTemplate.JigsawBlockInfo child) {
Direction direction = getFrontFacing(parent.info().state());
Direction direction2 = getFrontFacing(child.info().state());
Direction direction3 = getTopFacing(parent.info().state());
Direction direction4 = getTopFacing(child.info().state());
JigsawBlockEntity.JointType jointType = parent.jointType();
boolean bl = jointType == JigsawBlockEntity.JointType.ROLLABLE;
return direction == direction2.getOpposite() && (bl || direction3 == direction4) && parent.target().equals(child.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();
}
}