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.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import java.util.Random;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.inventory.PlayerEnderChestContainer;
import net.minecraft.stats.Stats;
import net.minecraft.world.MenuProvider;
import net.minecraft.network.chat.Component;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.Container;
import net.minecraft.world.inventory.ChestMenu;
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.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
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.entity.BlockEntityType;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.entity.EnderChestBlockEntity;

public class EnderChestBlock extends AbstractChestBlock<EnderChestBlockEntity> implements SimpleWaterloggedBlock {
    public static final DirectionProperty FACING;
    public static final BooleanProperty WATERLOGGED;
    protected static final VoxelShape SHAPE;
    public static final TranslatableComponent CONTAINER_TITLE;
    
    protected EnderChestBlock(final Properties c) {
        super(c, () -> BlockEntityType.ENDER_CHEST);
        this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)EnderChestBlock.FACING, Direction.NORTH)).<Comparable, Boolean>setValue((Property<Comparable>)EnderChestBlock.WATERLOGGED, false));
    }
    
    @Override
    public DoubleBlockCombiner.NeighborCombineResult<? extends ChestBlockEntity> combine(final BlockState byj, final Level bjw, final BlockPos fk, final boolean boolean4) {
        return DoubleBlockCombiner.Combiner::acceptNone;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return EnderChestBlock.SHAPE;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final FluidState coj3 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        return (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)EnderChestBlock.FACING, bcq.getHorizontalDirection().getOpposite())).<Comparable, Boolean>setValue((Property<Comparable>)EnderChestBlock.WATERLOGGED, coj3.getType() == Fluids.WATER);
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        final PlayerEnderChestContainer bbs8 = ayj.getEnderChestInventory();
        final BlockEntity bwl9 = bjw.getBlockEntity(fk);
        if (bbs8 == null || !(bwl9 instanceof EnderChestBlockEntity)) {
            return InteractionResult.SUCCESS;
        }
        final BlockPos fk2 = fk.above();
        if (bjw.getBlockState(fk2).isRedstoneConductor(bjw, fk2)) {
            return InteractionResult.SUCCESS;
        }
        if (bjw.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final EnderChestBlockEntity bwx11 = (EnderChestBlockEntity)bwl9;
        bbs8.setActiveChest(bwx11);
        ayj.openMenu(new SimpleMenuProvider((integer, ayi, ayj) -> ChestMenu.threeRows(integer, ayi, bbs8), EnderChestBlock.CONTAINER_TITLE));
        ayj.awardStat(Stats.OPEN_ENDERCHEST);
        return InteractionResult.SUCCESS;
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new EnderChestBlockEntity();
    }
    
    @Override
    public void animateTick(final BlockState byj, final Level bjw, final BlockPos fk, final Random random) {
        for (int integer6 = 0; integer6 < 3; ++integer6) {
            final int integer7 = random.nextInt(2) * 2 - 1;
            final int integer8 = random.nextInt(2) * 2 - 1;
            final double double9 = fk.getX() + 0.5 + 0.25 * integer7;
            final double double10 = fk.getY() + random.nextFloat();
            final double double11 = fk.getZ() + 0.5 + 0.25 * integer8;
            final double double12 = random.nextFloat() * integer7;
            final double double13 = (random.nextFloat() - 0.5) * 0.125;
            final double double14 = random.nextFloat() * integer8;
            bjw.addParticle(ParticleTypes.PORTAL, double9, double10, double11, double12, double13, double14);
        }
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)EnderChestBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)EnderChestBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<Direction>getValue((Property<Direction>)EnderChestBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(EnderChestBlock.FACING, EnderChestBlock.WATERLOGGED);
    }
    
    @Override
    public FluidState getFluidState(final BlockState byj) {
        if (byj.<Boolean>getValue((Property<Boolean>)EnderChestBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(byj);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (byj1.<Boolean>getValue((Property<Boolean>)EnderChestBlock.WATERLOGGED)) {
            bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean isPathfindable(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        return false;
    }
    
    static {
        FACING = HorizontalDirectionalBlock.FACING;
        WATERLOGGED = BlockStateProperties.WATERLOGGED;
        SHAPE = Block.box(1.0, 0.0, 1.0, 15.0, 14.0, 15.0);
        CONTAINER_TITLE = new TranslatableComponent("container.enderchest", new Object[0]);
    }
}
