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.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.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(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.LOWER));
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        final DoubleBlockHalf bzg8 = byj1.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF);
        if (fp.getAxis() == Direction.Axis.Y && bzg8 == DoubleBlockHalf.LOWER == (fp == Direction.UP) && (byj3.getBlock() != this || byj3.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == bzg8)) {
            return Blocks.AIR.defaultBlockState();
        }
        if (bzg8 == DoubleBlockHalf.LOWER && fp == Direction.DOWN && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockPos fk3 = bcq.getClickedPos();
        if (fk3.getY() < 255 && bcq.getLevel().getBlockState(fk3.above()).canBeReplaced(bcq)) {
            return super.getStateForPlacement(bcq);
        }
        return null;
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, final LivingEntity akz, final ItemStack ben) {
        bjw.setBlock(fk.above(), ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.UPPER), 3);
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        if (byj.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.UPPER) {
            final BlockState byj2 = bjz.getBlockState(fk.below());
            return byj2.getBlock() == this && byj2.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.LOWER;
        }
        return super.canSurvive(byj, bjz, fk);
    }
    
    public void placeAt(final LevelAccessor bjx, final BlockPos fk, final int integer) {
        bjx.setBlock(fk, ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.LOWER), integer);
        bjx.setBlock(fk.above(), ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoublePlantBlock.HALF, DoubleBlockHalf.UPPER), integer);
    }
    
    @Override
    public void playerDestroy(final Level bjw, final Player ayj, final BlockPos fk, final BlockState byj, @Nullable final BlockEntity bwl, final ItemStack ben) {
        super.playerDestroy(bjw, ayj, fk, Blocks.AIR.defaultBlockState(), bwl, ben);
    }
    
    @Override
    public void playerWillDestroy(final Level bjw, final BlockPos fk, final BlockState byj, final Player ayj) {
        final DoubleBlockHalf bzg6 = byj.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF);
        final BlockPos fk2 = (bzg6 == DoubleBlockHalf.LOWER) ? fk.above() : fk.below();
        final BlockState byj2 = bjw.getBlockState(fk2);
        if (byj2.getBlock() == this && byj2.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) != bzg6) {
            bjw.setBlock(fk2, Blocks.AIR.defaultBlockState(), 35);
            bjw.levelEvent(ayj, 2001, fk2, Block.getId(byj2));
            if (!bjw.isClientSide && !ayj.isCreative()) {
                Block.dropResources(byj, bjw, fk, null, ayj, ayj.getMainHandItem());
                Block.dropResources(byj2, bjw, fk2, null, ayj, ayj.getMainHandItem());
            }
        }
        super.playerWillDestroy(bjw, fk, byj, ayj);
    }
    
    @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 byj, final BlockPos fk) {
        return Mth.getSeed(fk.getX(), fk.below((int)((byj.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) != DoubleBlockHalf.LOWER) ? 1 : 0)).getY(), fk.getZ());
    }
    
    static {
        HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
    }
}
