211 lines
8.8 KiB
Java
211 lines
8.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.BlockStateProperties;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.entity.LivingEntity;
|
|
import net.minecraft.world.item.BlockPlaceContext;
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.world.level.TickPriority;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.state.properties.Property;
|
|
import java.util.Random;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.world.level.LevelReader;
|
|
import net.minecraft.world.phys.shapes.CollisionContext;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.level.BlockGetter;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
|
import net.minecraft.world.phys.shapes.VoxelShape;
|
|
|
|
public abstract class DiodeBlock extends HorizontalDirectionalBlock {
|
|
protected static final VoxelShape SHAPE;
|
|
public static final BooleanProperty POWERED;
|
|
|
|
protected DiodeBlock(final Properties c) {
|
|
super(c);
|
|
}
|
|
|
|
@Override
|
|
public VoxelShape getShape(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final CollisionContext cvn) {
|
|
return DiodeBlock.SHAPE;
|
|
}
|
|
|
|
@Override
|
|
public boolean canSurvive(final BlockState byg, final LevelReader bjw, final BlockPos fk) {
|
|
return Block.canSupportRigidBlock(bjw, fk.below());
|
|
}
|
|
|
|
@Override
|
|
public void tick(final BlockState byg, final ServerLevel xd, final BlockPos fk, final Random random) {
|
|
if (this.isLocked(xd, fk, byg)) {
|
|
return;
|
|
}
|
|
final boolean boolean6 = byg.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED);
|
|
final boolean boolean7 = this.shouldTurnOn(xd, fk, byg);
|
|
if (boolean6 && !boolean7) {
|
|
xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)DiodeBlock.POWERED, false), 2);
|
|
}
|
|
else if (!boolean6) {
|
|
xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byg).<Comparable, Boolean>setValue((Property<Comparable>)DiodeBlock.POWERED, true), 2);
|
|
if (!boolean7) {
|
|
xd.getBlockTicks().scheduleTick(fk, this, this.getDelay(byg), TickPriority.VERY_HIGH);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getDirectSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
return byg.getSignal(bjd, fk, fp);
|
|
}
|
|
|
|
@Override
|
|
public int getSignal(final BlockState byg, final BlockGetter bjd, final BlockPos fk, final Direction fp) {
|
|
if (!byg.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED)) {
|
|
return 0;
|
|
}
|
|
if (byg.<Comparable>getValue((Property<Comparable>)DiodeBlock.FACING) == fp) {
|
|
return this.getOutputSignal(bjd, fk, byg);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void neighborChanged(final BlockState byg, final Level bjt, final BlockPos fk3, final Block bpe, final BlockPos fk5, final boolean boolean6) {
|
|
if (byg.canSurvive(bjt, fk3)) {
|
|
this.checkTickOnNeighbor(bjt, fk3, byg);
|
|
return;
|
|
}
|
|
final BlockEntity bwi8 = this.isEntityBlock() ? bjt.getBlockEntity(fk3) : null;
|
|
Block.dropResources(byg, bjt, fk3, bwi8);
|
|
bjt.removeBlock(fk3, false);
|
|
for (final Direction fp12 : Direction.values()) {
|
|
bjt.updateNeighborsAt(fk3.relative(fp12), this);
|
|
}
|
|
}
|
|
|
|
protected void checkTickOnNeighbor(final Level bjt, final BlockPos fk, final BlockState byg) {
|
|
if (this.isLocked(bjt, fk, byg)) {
|
|
return;
|
|
}
|
|
final boolean boolean5 = byg.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED);
|
|
final boolean boolean6 = this.shouldTurnOn(bjt, fk, byg);
|
|
if (boolean5 != boolean6 && !bjt.getBlockTicks().willTickThisTick(fk, this)) {
|
|
TickPriority bkk7 = TickPriority.HIGH;
|
|
if (this.shouldPrioritize(bjt, fk, byg)) {
|
|
bkk7 = TickPriority.EXTREMELY_HIGH;
|
|
}
|
|
else if (boolean5) {
|
|
bkk7 = TickPriority.VERY_HIGH;
|
|
}
|
|
bjt.getBlockTicks().scheduleTick(fk, this, this.getDelay(byg), bkk7);
|
|
}
|
|
}
|
|
|
|
public boolean isLocked(final LevelReader bjw, final BlockPos fk, final BlockState byg) {
|
|
return false;
|
|
}
|
|
|
|
protected boolean shouldTurnOn(final Level bjt, final BlockPos fk, final BlockState byg) {
|
|
return this.getInputSignal(bjt, fk, byg) > 0;
|
|
}
|
|
|
|
protected int getInputSignal(final Level bjt, final BlockPos fk, final BlockState byg) {
|
|
final Direction fp5 = byg.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
|
|
final BlockPos fk2 = fk.relative(fp5);
|
|
final int integer7 = bjt.getSignal(fk2, fp5);
|
|
if (integer7 >= 15) {
|
|
return integer7;
|
|
}
|
|
final BlockState byg2 = bjt.getBlockState(fk2);
|
|
return Math.max(integer7, (byg2.getBlock() == Blocks.REDSTONE_WIRE) ? ((int)byg2.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER)) : 0);
|
|
}
|
|
|
|
protected int getAlternateSignal(final LevelReader bjw, final BlockPos fk, final BlockState byg) {
|
|
final Direction fp5 = byg.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
|
|
final Direction fp6 = fp5.getClockWise();
|
|
final Direction fp7 = fp5.getCounterClockWise();
|
|
return Math.max(this.getAlternateSignalAt(bjw, fk.relative(fp6), fp6), this.getAlternateSignalAt(bjw, fk.relative(fp7), fp7));
|
|
}
|
|
|
|
protected int getAlternateSignalAt(final LevelReader bjw, final BlockPos fk, final Direction fp) {
|
|
final BlockState byg5 = bjw.getBlockState(fk);
|
|
final Block bpe6 = byg5.getBlock();
|
|
if (!this.isAlternateInput(byg5)) {
|
|
return 0;
|
|
}
|
|
if (bpe6 == Blocks.REDSTONE_BLOCK) {
|
|
return 15;
|
|
}
|
|
if (bpe6 == Blocks.REDSTONE_WIRE) {
|
|
return byg5.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER);
|
|
}
|
|
return bjw.getDirectSignal(fk, fp);
|
|
}
|
|
|
|
@Override
|
|
public boolean isSignalSource(final BlockState byg) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public BlockState getStateForPlacement(final BlockPlaceContext bcn) {
|
|
return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)DiodeBlock.FACING, bcn.getHorizontalDirection().getOpposite());
|
|
}
|
|
|
|
@Override
|
|
public void setPlacedBy(final Level bjt, final BlockPos fk, final BlockState byg, final LivingEntity akw, final ItemStack bek) {
|
|
if (this.shouldTurnOn(bjt, fk, byg)) {
|
|
bjt.getBlockTicks().scheduleTick(fk, this, 1);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onPlace(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
|
|
this.updateNeighborsInFront(bjt, fk, byg1);
|
|
}
|
|
|
|
@Override
|
|
public void onRemove(final BlockState byg1, final Level bjt, final BlockPos fk, final BlockState byg4, final boolean boolean5) {
|
|
if (boolean5 || byg1.getBlock() == byg4.getBlock()) {
|
|
return;
|
|
}
|
|
super.onRemove(byg1, bjt, fk, byg4, boolean5);
|
|
this.updateNeighborsInFront(bjt, fk, byg1);
|
|
}
|
|
|
|
protected void updateNeighborsInFront(final Level bjt, final BlockPos fk, final BlockState byg) {
|
|
final Direction fp5 = byg.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
|
|
final BlockPos fk2 = fk.relative(fp5.getOpposite());
|
|
bjt.neighborChanged(fk2, this, fk);
|
|
bjt.updateNeighborsAtExceptFromFacing(fk2, this, fp5);
|
|
}
|
|
|
|
protected boolean isAlternateInput(final BlockState byg) {
|
|
return byg.isSignalSource();
|
|
}
|
|
|
|
protected int getOutputSignal(final BlockGetter bjd, final BlockPos fk, final BlockState byg) {
|
|
return 15;
|
|
}
|
|
|
|
public static boolean isDiode(final BlockState byg) {
|
|
return byg.getBlock() instanceof DiodeBlock;
|
|
}
|
|
|
|
public boolean shouldPrioritize(final BlockGetter bjd, final BlockPos fk, final BlockState byg) {
|
|
final Direction fp5 = byg.<Direction>getValue((Property<Direction>)DiodeBlock.FACING).getOpposite();
|
|
final BlockState byg2 = bjd.getBlockState(fk.relative(fp5));
|
|
return isDiode(byg2) && byg2.<Comparable>getValue((Property<Comparable>)DiodeBlock.FACING) != fp5;
|
|
}
|
|
|
|
protected abstract int getDelay(final BlockState byg);
|
|
|
|
static {
|
|
SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
|
|
POWERED = BlockStateProperties.POWERED;
|
|
}
|
|
}
|