package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.level.block.entity.BannerBlockEntity;
import net.minecraft.world.item.BannerItem;
import net.minecraft.world.item.DyeableLeatherItem;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.stats.Stats;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
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.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
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.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class CauldronBlock extends Block {
    public static final IntegerProperty LEVEL;
    private static final VoxelShape INSIDE;
    protected static final VoxelShape SHAPE;
    
    public CauldronBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)CauldronBlock.LEVEL, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return CauldronBlock.SHAPE;
    }
    
    @Override
    public VoxelShape getInteractionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return CauldronBlock.INSIDE;
    }
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        final int integer6 = cfj.<Integer>getValue((Property<Integer>)CauldronBlock.LEVEL);
        final float float7 = fu.getY() + (6.0f + 3 * integer6) / 16.0f;
        if (!bqb.isClientSide && aom.isOnFire() && integer6 > 0 && aom.getY() <= float7) {
            aom.clearFire();
            this.setWaterLevel(bqb, fu, cfj, integer6 - 1);
        }
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        final ItemStack bki8 = bec.getItemInHand(anf);
        if (bki8.isEmpty()) {
            return InteractionResult.PASS;
        }
        final int integer9 = cfj.<Integer>getValue((Property<Integer>)CauldronBlock.LEVEL);
        final Item bke10 = bki8.getItem();
        if (bke10 == Items.WATER_BUCKET) {
            if (integer9 < 3 && !bqb.isClientSide) {
                if (!bec.abilities.instabuild) {
                    bec.setItemInHand(anf, new ItemStack(Items.BUCKET));
                }
                bec.awardStat(Stats.FILL_CAULDRON);
                this.setWaterLevel(bqb, fu, cfj, 3);
                bqb.playSound(null, fu, SoundEvents.BUCKET_EMPTY, SoundSource.BLOCKS, 1.0f, 1.0f);
            }
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        if (bke10 == Items.BUCKET) {
            if (integer9 == 3 && !bqb.isClientSide) {
                if (!bec.abilities.instabuild) {
                    bki8.shrink(1);
                    if (bki8.isEmpty()) {
                        bec.setItemInHand(anf, new ItemStack(Items.WATER_BUCKET));
                    }
                    else if (!bec.inventory.add(new ItemStack(Items.WATER_BUCKET))) {
                        bec.drop(new ItemStack(Items.WATER_BUCKET), false);
                    }
                }
                bec.awardStat(Stats.USE_CAULDRON);
                this.setWaterLevel(bqb, fu, cfj, 0);
                bqb.playSound(null, fu, SoundEvents.BUCKET_FILL, SoundSource.BLOCKS, 1.0f, 1.0f);
            }
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        if (bke10 == Items.GLASS_BOTTLE) {
            if (integer9 > 0 && !bqb.isClientSide) {
                if (!bec.abilities.instabuild) {
                    final ItemStack bki9 = PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER);
                    bec.awardStat(Stats.USE_CAULDRON);
                    bki8.shrink(1);
                    if (bki8.isEmpty()) {
                        bec.setItemInHand(anf, bki9);
                    }
                    else if (!bec.inventory.add(bki9)) {
                        bec.drop(bki9, false);
                    }
                    else if (bec instanceof ServerPlayer) {
                        ((ServerPlayer)bec).refreshContainer(bec.inventoryMenu);
                    }
                }
                bqb.playSound(null, fu, SoundEvents.BOTTLE_FILL, SoundSource.BLOCKS, 1.0f, 1.0f);
                this.setWaterLevel(bqb, fu, cfj, integer9 - 1);
            }
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        if (bke10 == Items.POTION && PotionUtils.getPotion(bki8) == Potions.WATER) {
            if (integer9 < 3 && !bqb.isClientSide) {
                if (!bec.abilities.instabuild) {
                    final ItemStack bki9 = new ItemStack(Items.GLASS_BOTTLE);
                    bec.awardStat(Stats.USE_CAULDRON);
                    bec.setItemInHand(anf, bki9);
                    if (bec instanceof ServerPlayer) {
                        ((ServerPlayer)bec).refreshContainer(bec.inventoryMenu);
                    }
                }
                bqb.playSound(null, fu, SoundEvents.BOTTLE_EMPTY, SoundSource.BLOCKS, 1.0f, 1.0f);
                this.setWaterLevel(bqb, fu, cfj, integer9 + 1);
            }
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        if (integer9 > 0 && bke10 instanceof DyeableLeatherItem) {
            final DyeableLeatherItem bji11 = (DyeableLeatherItem)bke10;
            if (bji11.hasCustomColor(bki8) && !bqb.isClientSide) {
                bji11.clearColor(bki8);
                this.setWaterLevel(bqb, fu, cfj, integer9 - 1);
                bec.awardStat(Stats.CLEAN_ARMOR);
                return InteractionResult.SUCCESS;
            }
        }
        if (integer9 > 0 && bke10 instanceof BannerItem) {
            if (BannerBlockEntity.getPatternCount(bki8) > 0 && !bqb.isClientSide) {
                final ItemStack bki9 = bki8.copy();
                bki9.setCount(1);
                BannerBlockEntity.removeLastPattern(bki9);
                bec.awardStat(Stats.CLEAN_BANNER);
                if (!bec.abilities.instabuild) {
                    bki8.shrink(1);
                    this.setWaterLevel(bqb, fu, cfj, integer9 - 1);
                }
                if (bki8.isEmpty()) {
                    bec.setItemInHand(anf, bki9);
                }
                else if (!bec.inventory.add(bki9)) {
                    bec.drop(bki9, false);
                }
                else if (bec instanceof ServerPlayer) {
                    ((ServerPlayer)bec).refreshContainer(bec.inventoryMenu);
                }
            }
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        if (integer9 <= 0 || !(bke10 instanceof BlockItem)) {
            return InteractionResult.PASS;
        }
        final Block bvr11 = ((BlockItem)bke10).getBlock();
        if (bvr11 instanceof ShulkerBoxBlock && !bqb.isClientSide()) {
            final ItemStack bki10 = new ItemStack(Blocks.SHULKER_BOX, 1);
            if (bki8.hasTag()) {
                bki10.setTag(bki8.getTag().copy());
            }
            bec.setItemInHand(anf, bki10);
            this.setWaterLevel(bqb, fu, cfj, integer9 - 1);
            bec.awardStat(Stats.CLEAN_SHULKER_BOX);
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.CONSUME;
    }
    
    public void setWaterLevel(final Level bqb, final BlockPos fu, final BlockState cfj, final int integer) {
        bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)CauldronBlock.LEVEL, Mth.clamp(integer, 0, 3)), 2);
        bqb.updateNeighbourForOutputSignal(fu, this);
    }
    
    @Override
    public void handleRain(final Level bqb, final BlockPos fu) {
        if (bqb.random.nextInt(20) != 1) {
            return;
        }
        final float float4 = bqb.getBiome(fu).getTemperature(fu);
        if (float4 < 0.15f) {
            return;
        }
        final BlockState cfj5 = bqb.getBlockState(fu);
        if (cfj5.<Integer>getValue((Property<Integer>)CauldronBlock.LEVEL) < 3) {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj5).<Comparable>cycle((Property<Comparable>)CauldronBlock.LEVEL), 2);
        }
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState cfj, final Level bqb, final BlockPos fu) {
        return cfj.<Integer>getValue((Property<Integer>)CauldronBlock.LEVEL);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(CauldronBlock.LEVEL);
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    static {
        LEVEL = BlockStateProperties.LEVEL_CAULDRON;
        INSIDE = Block.box(2.0, 4.0, 2.0, 14.0, 16.0, 14.0);
        SHAPE = Shapes.join(Shapes.block(), Shapes.or(Block.box(0.0, 0.0, 4.0, 16.0, 3.0, 12.0), Block.box(4.0, 0.0, 0.0, 12.0, 3.0, 16.0), Block.box(2.0, 0.0, 2.0, 14.0, 3.0, 14.0), CauldronBlock.INSIDE), BooleanOp.ONLY_FIRST);
    }
}
