minecraft-source/src/net/minecraft/world/level/block/BaseRailBlock.java

155 lines
5.8 KiB
Java

package net.minecraft.world.level.block;
import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.properties.RailShape;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.VoxelShape;
public abstract class BaseRailBlock extends Block {
protected static final VoxelShape FLAT_AABB;
protected static final VoxelShape HALF_BLOCK_AABB;
private final boolean isStraight;
public static boolean isRail(final Level bjt, final BlockPos fk) {
return isRail(bjt.getBlockState(fk));
}
public static boolean isRail(final BlockState byg) {
return byg.is(BlockTags.RAILS);
}
protected BaseRailBlock(final boolean boolean1, final Properties c) {
super(c);
this.isStraight = boolean1;
}
public boolean isStraight() {
return this.isStraight;
}
@Override
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
final RailShape bzk6 = (byg.getBlock() == this) ? byg.<RailShape>getValue(this.getShapeProperty()) : null;
if (bzk6 != null && bzk6.isAscending()) {
return BaseRailBlock.HALF_BLOCK_AABB;
}
return BaseRailBlock.FLAT_AABB;
}
@Override
public boolean canSurvive(final BlockState byg, final LevelReader bjw, final BlockPos fk) {
return Block.canSupportRigidBlock(bjw, fk.below());
}
@Override
public void onPlace(BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
if (byg4.getBlock() == byg1.getBlock()) {
return;
}
byg1 = this.updateDir(bjt, fk, byg1, true);
if (this.isStraight) {
byg1.neighborChanged(bjt, fk, this, fk, boolean5);
}
}
@Override
public void neighborChanged(final BlockState byg, final Level bjt, final BlockPos fk3, final Block bpe, final BlockPos fk5, final boolean boolean6) {
if (bjt.isClientSide) {
return;
}
final RailShape bzk8 = byg.<RailShape>getValue(this.getShapeProperty());
boolean boolean7 = false;
final BlockPos fk6 = fk3.below();
if (!Block.canSupportRigidBlock(bjt, fk6)) {
boolean7 = true;
}
final BlockPos fk7 = fk3.east();
if (bzk8 == RailShape.ASCENDING_EAST && !Block.canSupportRigidBlock(bjt, fk7)) {
boolean7 = true;
}
else {
final BlockPos fk8 = fk3.west();
if (bzk8 == RailShape.ASCENDING_WEST && !Block.canSupportRigidBlock(bjt, fk8)) {
boolean7 = true;
}
else {
final BlockPos fk9 = fk3.north();
if (bzk8 == RailShape.ASCENDING_NORTH && !Block.canSupportRigidBlock(bjt, fk9)) {
boolean7 = true;
}
else {
final BlockPos fk10 = fk3.south();
if (bzk8 == RailShape.ASCENDING_SOUTH && !Block.canSupportRigidBlock(bjt, fk10)) {
boolean7 = true;
}
}
}
}
if (boolean7 && !bjt.isEmptyBlock(fk3)) {
if (!boolean6) {
Block.dropResources(byg, bjt, fk3);
}
bjt.removeBlock(fk3, boolean6);
}
else {
this.updateState(byg, bjt, fk3, bpe);
}
}
protected void updateState(final BlockState byg, final Level bjt, final BlockPos fk, final Block bpe) {
}
protected BlockState updateDir(final Level bjt, final BlockPos fk, final BlockState byg, final boolean boolean4) {
if (bjt.isClientSide) {
return byg;
}
final RailShape bzk6 = byg.<RailShape>getValue(this.getShapeProperty());
return new RailState(bjt, fk, byg).place(bjt.hasNeighborSignal(fk), boolean4, bzk6).getState();
}
@Override
public PushReaction getPistonPushReaction(final BlockState byg) {
return PushReaction.NORMAL;
}
@Override
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
if (boolean5) {
return;
}
super.onRemove(byg1, bjt, fk, byg4, boolean5);
if (byg1.<RailShape>getValue(this.getShapeProperty()).isAscending()) {
bjt.updateNeighborsAt(fk.above(), this);
}
if (this.isStraight) {
bjt.updateNeighborsAt(fk, this);
bjt.updateNeighborsAt(fk.below(), this);
}
}
@Override
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
final BlockState byg3 = super.defaultBlockState();
final Direction fp4 = bcn.getHorizontalDirection();
final boolean boolean5 = fp4 == Direction.EAST || fp4 == Direction.WEST;
return ((AbstractStateHolder<O, BlockState>)byg3).<RailShape, RailShape>setValue(this.getShapeProperty(), boolean5 ? RailShape.EAST_WEST : RailShape.NORTH_SOUTH);
}
public abstract Property<RailShape> getShapeProperty();
static {
FLAT_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
HALF_BLOCK_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0);
}
}