219 lines
9.8 KiB
Java
219 lines
9.8 KiB
Java
package net.minecraft.world.level.block;
|
|
|
|
import com.mojang.serialization.MapCodec;
|
|
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
|
import java.util.function.BiConsumer;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.tags.BlockTags;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.context.BlockPlaceContext;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.Explosion;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.level.ScheduledTickAccess;
|
|
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.BooleanProperty;
|
|
import net.minecraft.world.level.block.state.properties.WoodType;
|
|
import net.minecraft.world.level.gameevent.GameEvent;
|
|
import net.minecraft.world.level.gameevent.GameEvent.Context;
|
|
import net.minecraft.world.level.pathfinder.PathComputationType;
|
|
import net.minecraft.world.level.redstone.Orientation;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.world.phys.shapes.Shapes;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class FenceGateBlock extends HorizontalDirectionalBlock {
|
|
public static final MapCodec<FenceGateBlock> CODEC = RecordCodecBuilder.mapCodec(
|
|
instance -> instance.group(WoodType.CODEC.fieldOf("wood_type").forGetter(fenceGateBlock -> fenceGateBlock.type), propertiesCodec())
|
|
.apply(instance, FenceGateBlock::new)
|
|
);
|
|
public static final BooleanProperty OPEN = BlockStateProperties.OPEN;
|
|
public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
|
public static final BooleanProperty IN_WALL = BlockStateProperties.IN_WALL;
|
|
protected static final VoxelShape Z_SHAPE = Block.box(0.0, 0.0, 6.0, 16.0, 16.0, 10.0);
|
|
protected static final VoxelShape X_SHAPE = Block.box(6.0, 0.0, 0.0, 10.0, 16.0, 16.0);
|
|
protected static final VoxelShape Z_SHAPE_LOW = Block.box(0.0, 0.0, 6.0, 16.0, 13.0, 10.0);
|
|
protected static final VoxelShape X_SHAPE_LOW = Block.box(6.0, 0.0, 0.0, 10.0, 13.0, 16.0);
|
|
protected static final VoxelShape Z_COLLISION_SHAPE = Block.box(0.0, 0.0, 6.0, 16.0, 24.0, 10.0);
|
|
protected static final VoxelShape X_COLLISION_SHAPE = Block.box(6.0, 0.0, 0.0, 10.0, 24.0, 16.0);
|
|
protected static final VoxelShape Z_SUPPORT_SHAPE = Block.box(0.0, 5.0, 6.0, 16.0, 24.0, 10.0);
|
|
protected static final VoxelShape X_SUPPORT_SHAPE = Block.box(6.0, 5.0, 0.0, 10.0, 24.0, 16.0);
|
|
protected static final VoxelShape Z_OCCLUSION_SHAPE = Shapes.or(Block.box(0.0, 5.0, 7.0, 2.0, 16.0, 9.0), Block.box(14.0, 5.0, 7.0, 16.0, 16.0, 9.0));
|
|
protected static final VoxelShape X_OCCLUSION_SHAPE = Shapes.or(Block.box(7.0, 5.0, 0.0, 9.0, 16.0, 2.0), Block.box(7.0, 5.0, 14.0, 9.0, 16.0, 16.0));
|
|
protected static final VoxelShape Z_OCCLUSION_SHAPE_LOW = Shapes.or(Block.box(0.0, 2.0, 7.0, 2.0, 13.0, 9.0), Block.box(14.0, 2.0, 7.0, 16.0, 13.0, 9.0));
|
|
protected static final VoxelShape X_OCCLUSION_SHAPE_LOW = Shapes.or(Block.box(7.0, 2.0, 0.0, 9.0, 13.0, 2.0), Block.box(7.0, 2.0, 14.0, 9.0, 13.0, 16.0));
|
|
private final WoodType type;
|
|
|
|
@Override
|
|
public MapCodec<FenceGateBlock> codec() {
|
|
return CODEC;
|
|
}
|
|
|
|
public FenceGateBlock(WoodType type, BlockBehaviour.Properties properties) {
|
|
super(properties.sound(type.soundType()));
|
|
this.type = type;
|
|
this.registerDefaultState(this.stateDefinition.any().setValue(OPEN, false).setValue(POWERED, false).setValue(IN_WALL, false));
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
if ((Boolean)state.getValue(IN_WALL)) {
|
|
return ((Direction)state.getValue(FACING)).getAxis() == Direction.Axis.X ? X_SHAPE_LOW : Z_SHAPE_LOW;
|
|
} else {
|
|
return ((Direction)state.getValue(FACING)).getAxis() == Direction.Axis.X ? X_SHAPE : Z_SHAPE;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected BlockState updateShape(
|
|
BlockState blockState,
|
|
LevelReader levelReader,
|
|
ScheduledTickAccess scheduledTickAccess,
|
|
BlockPos blockPos,
|
|
Direction direction,
|
|
BlockPos blockPos2,
|
|
BlockState blockState2,
|
|
RandomSource randomSource
|
|
) {
|
|
Direction.Axis axis = direction.getAxis();
|
|
if (((Direction)blockState.getValue(FACING)).getClockWise().getAxis() != axis) {
|
|
return super.updateShape(blockState, levelReader, scheduledTickAccess, blockPos, direction, blockPos2, blockState2, randomSource);
|
|
} else {
|
|
boolean bl = this.isWall(blockState2) || this.isWall(levelReader.getBlockState(blockPos.relative(direction.getOpposite())));
|
|
return blockState.setValue(IN_WALL, bl);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getBlockSupportShape(BlockState state, BlockGetter level, BlockPos pos) {
|
|
if ((Boolean)state.getValue(OPEN)) {
|
|
return Shapes.empty();
|
|
} else {
|
|
return ((Direction)state.getValue(FACING)).getAxis() == Direction.Axis.Z ? Z_SUPPORT_SHAPE : X_SUPPORT_SHAPE;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
|
|
if ((Boolean)state.getValue(OPEN)) {
|
|
return Shapes.empty();
|
|
} else {
|
|
return ((Direction)state.getValue(FACING)).getAxis() == Direction.Axis.Z ? Z_COLLISION_SHAPE : X_COLLISION_SHAPE;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected VoxelShape getOcclusionShape(BlockState blockState) {
|
|
if ((Boolean)blockState.getValue(IN_WALL)) {
|
|
return ((Direction)blockState.getValue(FACING)).getAxis() == Direction.Axis.X ? X_OCCLUSION_SHAPE_LOW : Z_OCCLUSION_SHAPE_LOW;
|
|
} else {
|
|
return ((Direction)blockState.getValue(FACING)).getAxis() == Direction.Axis.X ? X_OCCLUSION_SHAPE : Z_OCCLUSION_SHAPE;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
|
|
switch (pathComputationType) {
|
|
case LAND:
|
|
return (Boolean)state.getValue(OPEN);
|
|
case WATER:
|
|
return false;
|
|
case AIR:
|
|
return (Boolean)state.getValue(OPEN);
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
|
Level level = context.getLevel();
|
|
BlockPos blockPos = context.getClickedPos();
|
|
boolean bl = level.hasNeighborSignal(blockPos);
|
|
Direction direction = context.getHorizontalDirection();
|
|
Direction.Axis axis = direction.getAxis();
|
|
boolean bl2 = axis == Direction.Axis.Z && (this.isWall(level.getBlockState(blockPos.west())) || this.isWall(level.getBlockState(blockPos.east())))
|
|
|| axis == Direction.Axis.X && (this.isWall(level.getBlockState(blockPos.north())) || this.isWall(level.getBlockState(blockPos.south())));
|
|
return this.defaultBlockState().setValue(FACING, direction).setValue(OPEN, bl).setValue(POWERED, bl).setValue(IN_WALL, bl2);
|
|
}
|
|
|
|
private boolean isWall(BlockState state) {
|
|
return state.is(BlockTags.WALLS);
|
|
}
|
|
|
|
@Override
|
|
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
|
|
if ((Boolean)state.getValue(OPEN)) {
|
|
state = state.setValue(OPEN, false);
|
|
level.setBlock(pos, state, 10);
|
|
} else {
|
|
Direction direction = player.getDirection();
|
|
if (state.getValue(FACING) == direction.getOpposite()) {
|
|
state = state.setValue(FACING, direction);
|
|
}
|
|
|
|
state = state.setValue(OPEN, true);
|
|
level.setBlock(pos, state, 10);
|
|
}
|
|
|
|
boolean bl = (Boolean)state.getValue(OPEN);
|
|
level.playSound(
|
|
player, pos, bl ? this.type.fenceGateOpen() : this.type.fenceGateClose(), SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F
|
|
);
|
|
level.gameEvent(player, bl ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, pos);
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
|
|
@Override
|
|
protected void onExplosionHit(
|
|
BlockState blockState, ServerLevel serverLevel, BlockPos blockPos, Explosion explosion, BiConsumer<ItemStack, BlockPos> biConsumer
|
|
) {
|
|
if (explosion.canTriggerBlocks() && !(Boolean)blockState.getValue(POWERED)) {
|
|
boolean bl = (Boolean)blockState.getValue(OPEN);
|
|
serverLevel.setBlockAndUpdate(blockPos, blockState.setValue(OPEN, !bl));
|
|
serverLevel.playSound(
|
|
null, blockPos, bl ? this.type.fenceGateClose() : this.type.fenceGateOpen(), SoundSource.BLOCKS, 1.0F, serverLevel.getRandom().nextFloat() * 0.1F + 0.9F
|
|
);
|
|
serverLevel.gameEvent(bl ? GameEvent.BLOCK_CLOSE : GameEvent.BLOCK_OPEN, blockPos, Context.of(blockState));
|
|
}
|
|
|
|
super.onExplosionHit(blockState, serverLevel, blockPos, explosion, biConsumer);
|
|
}
|
|
|
|
@Override
|
|
protected void neighborChanged(BlockState blockState, Level level, BlockPos blockPos, Block block, @Nullable Orientation orientation, boolean bl) {
|
|
if (!level.isClientSide) {
|
|
boolean bl2 = level.hasNeighborSignal(blockPos);
|
|
if ((Boolean)blockState.getValue(POWERED) != bl2) {
|
|
level.setBlock(blockPos, blockState.setValue(POWERED, bl2).setValue(OPEN, bl2), 2);
|
|
if ((Boolean)blockState.getValue(OPEN) != bl2) {
|
|
level.playSound(
|
|
null, blockPos, bl2 ? this.type.fenceGateOpen() : this.type.fenceGateClose(), SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F
|
|
);
|
|
level.gameEvent(null, bl2 ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, blockPos);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
|
builder.add(FACING, OPEN, POWERED, IN_WALL);
|
|
}
|
|
|
|
public static boolean connectsToDirection(BlockState state, Direction direction) {
|
|
return ((Direction)state.getValue(FACING)).getAxis() == direction.getClockWise().getAxis();
|
|
}
|
|
}
|