minecraft-src/net/minecraft/world/level/block/FenceBlock.java
2025-07-04 01:41:11 +03:00

127 lines
5.6 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.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.LeadItem;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
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.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.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
public class FenceBlock extends CrossCollisionBlock {
public static final MapCodec<FenceBlock> CODEC = simpleCodec(FenceBlock::new);
private final VoxelShape[] occlusionByIndex;
@Override
public MapCodec<FenceBlock> codec() {
return CODEC;
}
public FenceBlock(BlockBehaviour.Properties properties) {
super(2.0F, 2.0F, 16.0F, 16.0F, 24.0F, properties);
this.registerDefaultState(
this.stateDefinition.any().setValue(NORTH, false).setValue(EAST, false).setValue(SOUTH, false).setValue(WEST, false).setValue(WATERLOGGED, false)
);
this.occlusionByIndex = this.makeShapes(2.0F, 1.0F, 16.0F, 6.0F, 15.0F);
}
@Override
protected VoxelShape getOcclusionShape(BlockState state, BlockGetter level, BlockPos pos) {
return this.occlusionByIndex[this.getAABBIndex(state)];
}
@Override
protected VoxelShape getVisualShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return this.getShape(state, level, pos, context);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
public boolean connectsTo(BlockState state, boolean isSideSolid, Direction direction) {
Block block = state.getBlock();
boolean bl = this.isSameFence(state);
boolean bl2 = block instanceof FenceGateBlock && FenceGateBlock.connectsToDirection(state, direction);
return !isExceptionForConnection(state) && isSideSolid || bl || bl2;
}
private boolean isSameFence(BlockState state) {
return state.is(BlockTags.FENCES) && state.is(BlockTags.WOODEN_FENCES) == this.defaultBlockState().is(BlockTags.WOODEN_FENCES);
}
@Override
protected ItemInteractionResult useItemOn(
ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult
) {
if (level.isClientSide) {
return stack.is(Items.LEAD) ? ItemInteractionResult.SUCCESS : ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION;
} else {
return super.useItemOn(stack, state, level, pos, player, hand, hitResult);
}
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
return !level.isClientSide() ? LeadItem.bindPlayerMobs(player, level, pos) : InteractionResult.PASS;
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockGetter blockGetter = context.getLevel();
BlockPos blockPos = context.getClickedPos();
FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());
BlockPos blockPos2 = blockPos.north();
BlockPos blockPos3 = blockPos.east();
BlockPos blockPos4 = blockPos.south();
BlockPos blockPos5 = blockPos.west();
BlockState blockState = blockGetter.getBlockState(blockPos2);
BlockState blockState2 = blockGetter.getBlockState(blockPos3);
BlockState blockState3 = blockGetter.getBlockState(blockPos4);
BlockState blockState4 = blockGetter.getBlockState(blockPos5);
return super.getStateForPlacement(context)
.setValue(NORTH, this.connectsTo(blockState, blockState.isFaceSturdy(blockGetter, blockPos2, Direction.SOUTH), Direction.SOUTH))
.setValue(EAST, this.connectsTo(blockState2, blockState2.isFaceSturdy(blockGetter, blockPos3, Direction.WEST), Direction.WEST))
.setValue(SOUTH, this.connectsTo(blockState3, blockState3.isFaceSturdy(blockGetter, blockPos4, Direction.NORTH), Direction.NORTH))
.setValue(WEST, this.connectsTo(blockState4, blockState4.isFaceSturdy(blockGetter, blockPos5, Direction.EAST), Direction.EAST))
.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if ((Boolean)state.getValue(WATERLOGGED)) {
level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return direction.getAxis().getPlane() == Direction.Plane.HORIZONTAL
? state.setValue(
(Property)PROPERTY_BY_DIRECTION.get(direction),
this.connectsTo(neighborState, neighborState.isFaceSturdy(level, neighborPos, direction.getOpposite()), direction.getOpposite())
)
: super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(NORTH, EAST, WEST, SOUTH, WATERLOGGED);
}
}