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.BlockPlaceContext;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Containers;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.stats.Stats;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.level.block.entity.BarrelBlockEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;

public class BarrelBlock extends BaseEntityBlock {
    public static final DirectionProperty FACING;
    public static final BooleanProperty OPEN;
    
    public BarrelBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)BarrelBlock.FACING, Direction.NORTH)).<Comparable, Boolean>setValue((Property<Comparable>)BarrelBlock.OPEN, false));
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (bjw.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final BlockEntity bwl8 = bjw.getBlockEntity(fk);
        if (bwl8 instanceof BarrelBlockEntity) {
            ayj.openMenu((MenuProvider)bwl8);
            ayj.awardStat(Stats.OPEN_BARREL);
        }
        return InteractionResult.SUCCESS;
    }
    
    @Override
    public void onRemove(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj1.getBlock() == byj4.getBlock()) {
            return;
        }
        final BlockEntity bwl7 = bjw.getBlockEntity(fk);
        if (bwl7 instanceof Container) {
            Containers.dropContents(bjw, fk, (Container)bwl7);
            bjw.updateNeighbourForOutputSignal(fk, this);
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        final BlockEntity bwl6 = xd.getBlockEntity(fk);
        if (bwl6 instanceof BarrelBlockEntity) {
            ((BarrelBlockEntity)bwl6).recheckOpen();
        }
    }
    
    @Nullable
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new BarrelBlockEntity();
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, @Nullable final LivingEntity akz, final ItemStack ben) {
        if (ben.hasCustomHoverName()) {
            final BlockEntity bwl7 = bjw.getBlockEntity(fk);
            if (bwl7 instanceof BarrelBlockEntity) {
                ((BarrelBlockEntity)bwl7).setCustomName(ben.getHoverName());
            }
        }
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState byj, final Level bjw, final BlockPos fk) {
        return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(bjw.getBlockEntity(fk));
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)BarrelBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)BarrelBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<Direction>getValue((Property<Direction>)BarrelBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(BarrelBlock.FACING, BarrelBlock.OPEN);
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)BarrelBlock.FACING, bcq.getNearestLookingDirection().getOpposite());
    }
    
    static {
        FACING = BlockStateProperties.FACING;
        OPEN = BlockStateProperties.OPEN;
    }
}
