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.level.GameRules;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ambient.Bat;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.animal.Turtle;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.monster.Zombie;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
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.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.VoxelShape;

public class TurtleEggBlock extends Block {
    private static final VoxelShape ONE_EGG_AABB;
    private static final VoxelShape MULTIPLE_EGGS_AABB;
    public static final IntegerProperty HATCH;
    public static final IntegerProperty EGGS;
    
    public TurtleEggBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)TurtleEggBlock.HATCH, 0)).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, 1));
    }
    
    @Override
    public void stepOn(final Level bqb, final BlockPos fu, final Entity aom) {
        this.destroyEgg(bqb, fu, aom, 100);
        super.stepOn(bqb, fu, aom);
    }
    
    @Override
    public void fallOn(final Level bqb, final BlockPos fu, final Entity aom, final float float4) {
        if (!(aom instanceof Zombie)) {
            this.destroyEgg(bqb, fu, aom, 3);
        }
        super.fallOn(bqb, fu, aom, float4);
    }
    
    private void destroyEgg(final Level bqb, final BlockPos fu, final Entity aom, final int integer) {
        if (!this.canDestroyEgg(bqb, aom)) {
            return;
        }
        if (!bqb.isClientSide && bqb.random.nextInt(integer) == 0) {
            final BlockState cfj6 = bqb.getBlockState(fu);
            if (cfj6.is(Blocks.TURTLE_EGG)) {
                this.decreaseEggs(bqb, fu, cfj6);
            }
        }
    }
    
    private void decreaseEggs(final Level bqb, final BlockPos fu, final BlockState cfj) {
        bqb.playSound(null, fu, SoundEvents.TURTLE_EGG_BREAK, SoundSource.BLOCKS, 0.7f, 0.9f + bqb.random.nextFloat() * 0.2f);
        final int integer5 = cfj.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS);
        if (integer5 <= 1) {
            bqb.destroyBlock(fu, false);
        }
        else {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, integer5 - 1), 2);
            bqb.levelEvent(2001, fu, Block.getId(cfj));
        }
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (this.shouldUpdateHatchLevel(zd) && onSand(zd, fu)) {
            final int integer6 = cfj.<Integer>getValue((Property<Integer>)TurtleEggBlock.HATCH);
            if (integer6 < 2) {
                zd.playSound(null, fu, SoundEvents.TURTLE_EGG_CRACK, SoundSource.BLOCKS, 0.7f, 0.9f + random.nextFloat() * 0.2f);
                zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.HATCH, integer6 + 1), 2);
            }
            else {
                zd.playSound(null, fu, SoundEvents.TURTLE_EGG_HATCH, SoundSource.BLOCKS, 0.7f, 0.9f + random.nextFloat() * 0.2f);
                zd.removeBlock(fu, false);
                for (int integer7 = 0; integer7 < cfj.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS); ++integer7) {
                    zd.levelEvent(2001, fu, Block.getId(cfj));
                    final Turtle azi8 = EntityType.TURTLE.create(zd);
                    azi8.setAge(-24000);
                    azi8.setHomePos(fu);
                    azi8.moveTo(fu.getX() + 0.3 + integer7 * 0.2, fu.getY(), fu.getZ() + 0.3, 0.0f, 0.0f);
                    zd.addFreshEntity(azi8);
                }
            }
        }
    }
    
    public static boolean onSand(final BlockGetter bpg, final BlockPos fu) {
        return isSand(bpg, fu.below());
    }
    
    public static boolean isSand(final BlockGetter bpg, final BlockPos fu) {
        return bpg.getBlockState(fu).is(BlockTags.SAND);
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (onSand(bqb, fu) && !bqb.isClientSide) {
            bqb.levelEvent(2005, fu, 0);
        }
    }
    
    private boolean shouldUpdateHatchLevel(final Level bqb) {
        final float float3 = bqb.getTimeOfDay(1.0f);
        return (float3 < 0.69 && float3 > 0.65) || bqb.random.nextInt(500) == 0;
    }
    
    @Override
    public void playerDestroy(final Level bqb, final Player bec, final BlockPos fu, final BlockState cfj, @Nullable final BlockEntity cdl, final ItemStack bki) {
        super.playerDestroy(bqb, bec, fu, cfj, cdl, bki);
        this.decreaseEggs(bqb, fu, cfj);
    }
    
    @Override
    public boolean canBeReplaced(final BlockState cfj, final BlockPlaceContext bin) {
        return (bin.getItemInHand().getItem() == this.asItem() && cfj.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) < 4) || super.canBeReplaced(cfj, bin);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final BlockState cfj3 = bin.getLevel().getBlockState(bin.getClickedPos());
        if (cfj3.is(this)) {
            return ((StateHolder<O, BlockState>)cfj3).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, Math.min(4, cfj3.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) + 1));
        }
        return super.getStateForPlacement(bin);
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        if (cfj.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) > 1) {
            return TurtleEggBlock.MULTIPLE_EGGS_AABB;
        }
        return TurtleEggBlock.ONE_EGG_AABB;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(TurtleEggBlock.HATCH, TurtleEggBlock.EGGS);
    }
    
    private boolean canDestroyEgg(final Level bqb, final Entity aom) {
        return !(aom instanceof Turtle) && !(aom instanceof Bat) && aom instanceof LivingEntity && (aom instanceof Player || bqb.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING));
    }
    
    static {
        ONE_EGG_AABB = Block.box(3.0, 0.0, 3.0, 12.0, 7.0, 12.0);
        MULTIPLE_EGGS_AABB = Block.box(1.0, 0.0, 1.0, 15.0, 7.0, 15.0);
        HATCH = BlockStateProperties.HATCH;
        EGGS = BlockStateProperties.EGGS;
    }
}
