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.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.world.entity.monster.piglin.PiglinAi;
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.world.level.block.state.BlockBehaviour;
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((((StateHolder<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 cfj, final Level bqb, final BlockPos fu, final boolean boolean4) {
        return DoubleBlockCombiner.Combiner::acceptNone;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return EnderChestBlock.SHAPE;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final FluidState cxa3 = bin.getLevel().getFluidState(bin.getClickedPos());
        return (((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)EnderChestBlock.FACING, bin.getHorizontalDirection().getOpposite())).<Comparable, Boolean>setValue((Property<Comparable>)EnderChestBlock.WATERLOGGED, cxa3.getType() == Fluids.WATER);
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        final PlayerEnderChestContainer bho8 = bec.getEnderChestInventory();
        final BlockEntity cdl9 = bqb.getBlockEntity(fu);
        if (bho8 == null || !(cdl9 instanceof EnderChestBlockEntity)) {
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        final BlockPos fu2 = fu.above();
        if (bqb.getBlockState(fu2).isRedstoneConductor(bqb, fu2)) {
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        if (bqb.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final EnderChestBlockEntity cdx11 = (EnderChestBlockEntity)cdl9;
        bho8.setActiveChest(cdx11);
        bec.openMenu(new SimpleMenuProvider((integer, beb, bec) -> ChestMenu.threeRows(integer, beb, bho8), EnderChestBlock.CONTAINER_TITLE));
        bec.awardStat(Stats.OPEN_ENDERCHEST);
        PiglinAi.angerNearbyPiglins(bec, true);
        return InteractionResult.CONSUME;
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new EnderChestBlockEntity();
    }
    
    @Override
    public void animateTick(final BlockState cfj, final Level bqb, final BlockPos fu, 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 = fu.getX() + 0.5 + 0.25 * integer7;
            final double double10 = fu.getY() + random.nextFloat();
            final double double11 = fu.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;
            bqb.addParticle(ParticleTypes.PORTAL, double9, double10, double11, double12, double13, double14);
        }
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)EnderChestBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)EnderChestBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<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 cfj) {
        if (cfj.<Boolean>getValue((Property<Boolean>)EnderChestBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(cfj);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (cfj1.<Boolean>getValue((Property<Boolean>)EnderChestBlock.WATERLOGGED)) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        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");
    }
}
