package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.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(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)CauldronBlock.LEVEL, 0));
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return CauldronBlock.SHAPE;
    }
    
    @Override
    public VoxelShape getInteractionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return CauldronBlock.INSIDE;
    }
    
    @Override
    public void entityInside(final BlockState byj, final Level bjw, final BlockPos fk, final Entity akq) {
        final int integer6 = byj.<Integer>getValue((Property<Integer>)CauldronBlock.LEVEL);
        final float float7 = fk.getY() + (6.0f + 3 * integer6) / 16.0f;
        if (!bjw.isClientSide && akq.isOnFire() && integer6 > 0 && akq.getY() <= float7) {
            akq.clearFire();
            this.setWaterLevel(bjw, fk, byj, integer6 - 1);
        }
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        final ItemStack ben8 = ayj.getItemInHand(ajk);
        if (ben8.isEmpty()) {
            return InteractionResult.PASS;
        }
        final int integer9 = byj.<Integer>getValue((Property<Integer>)CauldronBlock.LEVEL);
        final Item bei10 = ben8.getItem();
        if (bei10 == Items.WATER_BUCKET) {
            if (integer9 < 3 && !bjw.isClientSide) {
                if (!ayj.abilities.instabuild) {
                    ayj.setItemInHand(ajk, new ItemStack(Items.BUCKET));
                }
                ayj.awardStat(Stats.FILL_CAULDRON);
                this.setWaterLevel(bjw, fk, byj, 3);
                bjw.playSound(null, fk, SoundEvents.BUCKET_EMPTY, SoundSource.BLOCKS, 1.0f, 1.0f);
            }
            return InteractionResult.SUCCESS;
        }
        if (bei10 == Items.BUCKET) {
            if (integer9 == 3 && !bjw.isClientSide) {
                if (!ayj.abilities.instabuild) {
                    ben8.shrink(1);
                    if (ben8.isEmpty()) {
                        ayj.setItemInHand(ajk, new ItemStack(Items.WATER_BUCKET));
                    }
                    else if (!ayj.inventory.add(new ItemStack(Items.WATER_BUCKET))) {
                        ayj.drop(new ItemStack(Items.WATER_BUCKET), false);
                    }
                }
                ayj.awardStat(Stats.USE_CAULDRON);
                this.setWaterLevel(bjw, fk, byj, 0);
                bjw.playSound(null, fk, SoundEvents.BUCKET_FILL, SoundSource.BLOCKS, 1.0f, 1.0f);
            }
            return InteractionResult.SUCCESS;
        }
        if (bei10 == Items.GLASS_BOTTLE) {
            if (integer9 > 0 && !bjw.isClientSide) {
                if (!ayj.abilities.instabuild) {
                    final ItemStack ben9 = PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER);
                    ayj.awardStat(Stats.USE_CAULDRON);
                    ben8.shrink(1);
                    if (ben8.isEmpty()) {
                        ayj.setItemInHand(ajk, ben9);
                    }
                    else if (!ayj.inventory.add(ben9)) {
                        ayj.drop(ben9, false);
                    }
                    else if (ayj instanceof ServerPlayer) {
                        ((ServerPlayer)ayj).refreshContainer(ayj.inventoryMenu);
                    }
                }
                bjw.playSound(null, fk, SoundEvents.BOTTLE_FILL, SoundSource.BLOCKS, 1.0f, 1.0f);
                this.setWaterLevel(bjw, fk, byj, integer9 - 1);
            }
            return InteractionResult.SUCCESS;
        }
        if (bei10 == Items.POTION && PotionUtils.getPotion(ben8) == Potions.WATER) {
            if (integer9 < 3 && !bjw.isClientSide) {
                if (!ayj.abilities.instabuild) {
                    final ItemStack ben9 = new ItemStack(Items.GLASS_BOTTLE);
                    ayj.awardStat(Stats.USE_CAULDRON);
                    ayj.setItemInHand(ajk, ben9);
                    if (ayj instanceof ServerPlayer) {
                        ((ServerPlayer)ayj).refreshContainer(ayj.inventoryMenu);
                    }
                }
                bjw.playSound(null, fk, SoundEvents.BOTTLE_EMPTY, SoundSource.BLOCKS, 1.0f, 1.0f);
                this.setWaterLevel(bjw, fk, byj, integer9 + 1);
            }
            return InteractionResult.SUCCESS;
        }
        if (integer9 > 0 && bei10 instanceof DyeableLeatherItem) {
            final DyeableLeatherItem bdn11 = (DyeableLeatherItem)bei10;
            if (bdn11.hasCustomColor(ben8) && !bjw.isClientSide) {
                bdn11.clearColor(ben8);
                this.setWaterLevel(bjw, fk, byj, integer9 - 1);
                ayj.awardStat(Stats.CLEAN_ARMOR);
                return InteractionResult.SUCCESS;
            }
        }
        if (integer9 > 0 && bei10 instanceof BannerItem) {
            if (BannerBlockEntity.getPatternCount(ben8) > 0 && !bjw.isClientSide) {
                final ItemStack ben9 = ben8.copy();
                ben9.setCount(1);
                BannerBlockEntity.removeLastPattern(ben9);
                ayj.awardStat(Stats.CLEAN_BANNER);
                if (!ayj.abilities.instabuild) {
                    ben8.shrink(1);
                    this.setWaterLevel(bjw, fk, byj, integer9 - 1);
                }
                if (ben8.isEmpty()) {
                    ayj.setItemInHand(ajk, ben9);
                }
                else if (!ayj.inventory.add(ben9)) {
                    ayj.drop(ben9, false);
                }
                else if (ayj instanceof ServerPlayer) {
                    ((ServerPlayer)ayj).refreshContainer(ayj.inventoryMenu);
                }
            }
            return InteractionResult.SUCCESS;
        }
        if (integer9 <= 0 || !(bei10 instanceof BlockItem)) {
            return InteractionResult.PASS;
        }
        final Block bph11 = ((BlockItem)bei10).getBlock();
        if (bph11 instanceof ShulkerBoxBlock && !bjw.isClientSide()) {
            final ItemStack ben10 = new ItemStack(Blocks.SHULKER_BOX, 1);
            if (ben8.hasTag()) {
                ben10.setTag(ben8.getTag().copy());
            }
            ayj.setItemInHand(ajk, ben10);
            this.setWaterLevel(bjw, fk, byj, integer9 - 1);
            ayj.awardStat(Stats.CLEAN_SHULKER_BOX);
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.CONSUME;
    }
    
    public void setWaterLevel(final Level bjw, final BlockPos fk, final BlockState byj, final int integer) {
        bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)CauldronBlock.LEVEL, Mth.clamp(integer, 0, 3)), 2);
        bjw.updateNeighbourForOutputSignal(fk, this);
    }
    
    @Override
    public void handleRain(final Level bjw, final BlockPos fk) {
        if (bjw.random.nextInt(20) != 1) {
            return;
        }
        final float float4 = bjw.getBiome(fk).getTemperature(fk);
        if (float4 < 0.15f) {
            return;
        }
        final BlockState byj5 = bjw.getBlockState(fk);
        if (byj5.<Integer>getValue((Property<Integer>)CauldronBlock.LEVEL) < 3) {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj5).<Comparable>cycle((Property<Comparable>)CauldronBlock.LEVEL), 2);
        }
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState byj, final Level bjw, final BlockPos fk) {
        return byj.<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 byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        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);
    }
}
