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

123 lines
5 KiB
Java

package net.minecraft.world.level.block;
import com.mojang.serialization.MapCodec;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
public abstract class CrossCollisionBlock extends Block implements SimpleWaterloggedBlock {
public static final BooleanProperty NORTH = PipeBlock.NORTH;
public static final BooleanProperty EAST = PipeBlock.EAST;
public static final BooleanProperty SOUTH = PipeBlock.SOUTH;
public static final BooleanProperty WEST = PipeBlock.WEST;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
public static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = (Map<Direction, BooleanProperty>)PipeBlock.PROPERTY_BY_DIRECTION
.entrySet()
.stream()
.filter(entry -> ((Direction)entry.getKey()).getAxis().isHorizontal())
.collect(Util.toMap());
private final Function<BlockState, VoxelShape> collisionShapes;
private final Function<BlockState, VoxelShape> shapes;
protected CrossCollisionBlock(
float nodeWidth, float extensionWidth, float nodeHeight, float extensionHeight, float collisionHeight, BlockBehaviour.Properties properties
) {
super(properties);
this.collisionShapes = this.makeShapes(nodeWidth, collisionHeight, nodeHeight, 0.0F, collisionHeight);
this.shapes = this.makeShapes(nodeWidth, extensionWidth, nodeHeight, 0.0F, extensionHeight);
}
@Override
protected abstract MapCodec<? extends CrossCollisionBlock> codec();
protected Function<BlockState, VoxelShape> makeShapes(float nodeWidth, float extensionWidth, float nodeHeight, float extensionBottom, float extensionHeight) {
VoxelShape voxelShape = Block.column(nodeWidth, 0.0, extensionWidth);
Map<Direction, VoxelShape> map = Shapes.rotateHorizontal(Block.boxZ(nodeHeight, extensionBottom, extensionHeight, 0.0, 8.0));
return this.getShapeForEachState(blockState -> {
VoxelShape voxelShape2 = voxelShape;
for (Entry<Direction, BooleanProperty> entry : PROPERTY_BY_DIRECTION.entrySet()) {
if ((Boolean)blockState.getValue((Property)entry.getValue())) {
voxelShape2 = Shapes.or(voxelShape2, (VoxelShape)map.get(entry.getKey()));
}
}
return voxelShape2;
}, new Property[]{WATERLOGGED});
}
@Override
protected boolean propagatesSkylightDown(BlockState state) {
return !(Boolean)state.getValue(WATERLOGGED);
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return (VoxelShape)this.shapes.apply(state);
}
@Override
protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return (VoxelShape)this.collisionShapes.apply(state);
}
@Override
protected FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
switch (rotation) {
case CLOCKWISE_180:
return state.setValue(NORTH, (Boolean)state.getValue(SOUTH))
.setValue(EAST, (Boolean)state.getValue(WEST))
.setValue(SOUTH, (Boolean)state.getValue(NORTH))
.setValue(WEST, (Boolean)state.getValue(EAST));
case COUNTERCLOCKWISE_90:
return state.setValue(NORTH, (Boolean)state.getValue(EAST))
.setValue(EAST, (Boolean)state.getValue(SOUTH))
.setValue(SOUTH, (Boolean)state.getValue(WEST))
.setValue(WEST, (Boolean)state.getValue(NORTH));
case CLOCKWISE_90:
return state.setValue(NORTH, (Boolean)state.getValue(WEST))
.setValue(EAST, (Boolean)state.getValue(NORTH))
.setValue(SOUTH, (Boolean)state.getValue(EAST))
.setValue(WEST, (Boolean)state.getValue(SOUTH));
default:
return state;
}
}
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
switch (mirror) {
case LEFT_RIGHT:
return state.setValue(NORTH, (Boolean)state.getValue(SOUTH)).setValue(SOUTH, (Boolean)state.getValue(NORTH));
case FRONT_BACK:
return state.setValue(EAST, (Boolean)state.getValue(WEST)).setValue(WEST, (Boolean)state.getValue(EAST));
default:
return super.mirror(state, mirror);
}
}
}