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.level.GameRules;
import net.minecraft.world.entity.LivingEntity;
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.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.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((((AbstractStateHolder<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 bjw, final BlockPos fk, final Entity akq) {
        this.destroyEgg(bjw, fk, akq, 100);
        super.stepOn(bjw, fk, akq);
    }
    
    @Override
    public void fallOn(final Level bjw, final BlockPos fk, final Entity akq, final float float4) {
        if (!(akq instanceof Zombie)) {
            this.destroyEgg(bjw, fk, akq, 3);
        }
        super.fallOn(bjw, fk, akq, float4);
    }
    
    private void destroyEgg(final Level bjw, final BlockPos fk, final Entity akq, final int integer) {
        if (!this.canDestroyEgg(bjw, akq)) {
            super.stepOn(bjw, fk, akq);
            return;
        }
        if (!bjw.isClientSide && bjw.random.nextInt(integer) == 0) {
            this.decreaseEggs(bjw, fk, bjw.getBlockState(fk));
        }
    }
    
    private void decreaseEggs(final Level bjw, final BlockPos fk, final BlockState byj) {
        bjw.playSound(null, fk, SoundEvents.TURTLE_EGG_BREAK, SoundSource.BLOCKS, 0.7f, 0.9f + bjw.random.nextFloat() * 0.2f);
        final int integer5 = byj.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS);
        if (integer5 <= 1) {
            bjw.destroyBlock(fk, false);
        }
        else {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, integer5 - 1), 2);
            bjw.levelEvent(2001, fk, Block.getId(byj));
        }
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (this.shouldUpdateHatchLevel(xd) && this.onSand(xd, fk)) {
            final int integer6 = byj.<Integer>getValue((Property<Integer>)TurtleEggBlock.HATCH);
            if (integer6 < 2) {
                xd.playSound(null, fk, SoundEvents.TURTLE_EGG_CRACK, SoundSource.BLOCKS, 0.7f, 0.9f + random.nextFloat() * 0.2f);
                xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.HATCH, integer6 + 1), 2);
            }
            else {
                xd.playSound(null, fk, SoundEvents.TURTLE_EGG_HATCH, SoundSource.BLOCKS, 0.7f, 0.9f + random.nextFloat() * 0.2f);
                xd.removeBlock(fk, false);
                for (int integer7 = 0; integer7 < byj.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS); ++integer7) {
                    xd.levelEvent(2001, fk, Block.getId(byj));
                    final Turtle aua8 = EntityType.TURTLE.create(xd);
                    aua8.setAge(-24000);
                    aua8.setHomePos(fk);
                    aua8.moveTo(fk.getX() + 0.3 + integer7 * 0.2, fk.getY(), fk.getZ() + 0.3, 0.0f, 0.0f);
                    xd.addFreshEntity(aua8);
                }
            }
        }
    }
    
    private boolean onSand(final BlockGetter bjg, final BlockPos fk) {
        return bjg.getBlockState(fk.below()).getBlock() == Blocks.SAND;
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (this.onSand(bjw, fk) && !bjw.isClientSide) {
            bjw.levelEvent(2005, fk, 0);
        }
    }
    
    private boolean shouldUpdateHatchLevel(final Level bjw) {
        final float float3 = bjw.getTimeOfDay(1.0f);
        return (float3 < 0.69 && float3 > 0.65) || bjw.random.nextInt(500) == 0;
    }
    
    @Override
    public void playerDestroy(final Level bjw, final Player ayj, final BlockPos fk, final BlockState byj, @Nullable final BlockEntity bwl, final ItemStack ben) {
        super.playerDestroy(bjw, ayj, fk, byj, bwl, ben);
        this.decreaseEggs(bjw, fk, byj);
    }
    
    @Override
    public boolean canBeReplaced(final BlockState byj, final BlockPlaceContext bcq) {
        return (bcq.getItemInHand().getItem() == this.asItem() && byj.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) < 4) || super.canBeReplaced(byj, bcq);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockState byj3 = bcq.getLevel().getBlockState(bcq.getClickedPos());
        if (byj3.getBlock() == this) {
            return ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Integer>setValue((Property<Comparable>)TurtleEggBlock.EGGS, Math.min(4, byj3.<Integer>getValue((Property<Integer>)TurtleEggBlock.EGGS) + 1));
        }
        return super.getStateForPlacement(bcq);
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        if (byj.<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 bjw, final Entity akq) {
        return !(akq instanceof Turtle) && (!(akq instanceof LivingEntity) || akq instanceof Player || bjw.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;
    }
}
