package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
import net.minecraft.world.level.block.state.properties.EnumProperty;

public class DoublePlantBlock extends BushBlock {
    public static final EnumProperty<DoubleBlockHalf> HALF;
    
    public DoublePlantBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.LOWER));
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        final DoubleBlockHalf cgf8 = cfj1.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF);
        if (fz.getAxis() == Direction.Axis.Y && cgf8 == DoubleBlockHalf.LOWER == (fz == Direction.UP) && (!cfj3.is(this) || cfj3.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == cgf8)) {
            return Blocks.AIR.defaultBlockState();
        }
        if (cgf8 == DoubleBlockHalf.LOWER && fz == Direction.DOWN && !cfj1.canSurvive(bqc, fu5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final BlockPos fu3 = bin.getClickedPos();
        if (fu3.getY() < 255 && bin.getLevel().getBlockState(fu3.above()).canBeReplaced(bin)) {
            return super.getStateForPlacement(bin);
        }
        return null;
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, final LivingEntity aoy, final ItemStack bki) {
        bqb.setBlock(fu.above(), ((StateHolder<O, BlockState>)this.defaultBlockState()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.UPPER), 3);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        if (cfj.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.UPPER) {
            final BlockState cfj2 = bqd.getBlockState(fu.below());
            return cfj2.is(this) && cfj2.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.LOWER;
        }
        return super.canSurvive(cfj, bqd, fu);
    }
    
    public void placeAt(final LevelAccessor bqc, final BlockPos fu, final int integer) {
        bqc.setBlock(fu, ((StateHolder<O, BlockState>)this.defaultBlockState()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.LOWER), integer);
        bqc.setBlock(fu.above(), ((StateHolder<O, BlockState>)this.defaultBlockState()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.UPPER), integer);
    }
    
    @Override
    public void playerWillDestroy(final Level bqb, final BlockPos fu, final BlockState cfj, final Player bec) {
        if (!bqb.isClientSide) {
            if (bec.isCreative()) {
                preventCreativeDropFromBottomPart(bqb, fu, cfj, bec);
            }
            else {
                Block.dropResources(cfj, bqb, fu, null, bec, bec.getMainHandItem());
            }
        }
        super.playerWillDestroy(bqb, fu, cfj, bec);
    }
    
    @Override
    public void playerDestroy(final Level bqb, final Player bec, final BlockPos fu, final BlockState cfj, @Nullable final BlockEntity cdl, final ItemStack bki) {
        super.playerDestroy(bqb, bec, fu, Blocks.AIR.defaultBlockState(), cdl, bki);
    }
    
    protected static void preventCreativeDropFromBottomPart(final Level bqb, final BlockPos fu, final BlockState cfj, final Player bec) {
        final DoubleBlockHalf cgf5 = cfj.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF);
        if (cgf5 == DoubleBlockHalf.UPPER) {
            final BlockPos fu2 = fu.below();
            final BlockState cfj2 = bqb.getBlockState(fu2);
            if (cfj2.getBlock() == cfj.getBlock() && cfj2.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.LOWER) {
                bqb.setBlock(fu2, Blocks.AIR.defaultBlockState(), 35);
                bqb.levelEvent(bec, 2001, fu2, Block.getId(cfj2));
            }
        }
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(DoublePlantBlock.HALF);
    }
    
    @Override
    public OffsetType getOffsetType() {
        return OffsetType.XZ;
    }
    
    @Override
    public long getSeed(final BlockState cfj, final BlockPos fu) {
        return Mth.getSeed(fu.getX(), fu.below((int)((cfj.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) != DoubleBlockHalf.LOWER) ? 1 : 0)).getY(), fu.getZ());
    }
    
    static {
        HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
    }
}
