package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.predicate.BlockMaterialPredicate;
import net.minecraft.world.level.material.Material;
import java.util.function.Predicate;
import net.minecraft.world.level.block.state.predicate.BlockStatePredicate;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.state.pattern.BlockPatternBuilder;
import net.minecraft.world.item.Items;
import java.util.Iterator;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import net.minecraft.world.entity.Entity;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.boss.wither.WitherBoss;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.Difficulty;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SkullBlockEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockBehaviour;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.state.pattern.BlockPattern;

public class WitherSkullBlock extends SkullBlock {
    @Nullable
    private static BlockPattern witherPatternFull;
    @Nullable
    private static BlockPattern witherPatternBase;
    
    protected WitherSkullBlock(final Properties c) {
        super(Types.WITHER_SKELETON, c);
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, @Nullable final LivingEntity aoy, final ItemStack bki) {
        super.setPlacedBy(bqb, fu, cfj, aoy, bki);
        final BlockEntity cdl7 = bqb.getBlockEntity(fu);
        if (cdl7 instanceof SkullBlockEntity) {
            checkSpawn(bqb, fu, (SkullBlockEntity)cdl7);
        }
    }
    
    public static void checkSpawn(final Level bqb, final BlockPos fu, final SkullBlockEntity cei) {
        if (bqb.isClientSide) {
            return;
        }
        final BlockState cfj4 = cei.getBlockState();
        final boolean boolean5 = cfj4.is(Blocks.WITHER_SKELETON_SKULL) || cfj4.is(Blocks.WITHER_SKELETON_WALL_SKULL);
        if (!boolean5 || fu.getY() < 0 || bqb.getDifficulty() == Difficulty.PEACEFUL) {
            return;
        }
        final BlockPattern cfo6 = getOrCreateWitherFull();
        final BlockPattern.BlockPatternMatch b7 = cfo6.find(bqb, fu);
        if (b7 == null) {
            return;
        }
        for (int integer8 = 0; integer8 < cfo6.getWidth(); ++integer8) {
            for (int integer9 = 0; integer9 < cfo6.getHeight(); ++integer9) {
                final BlockInWorld cfn10 = b7.getBlock(integer8, integer9, 0);
                bqb.setBlock(cfn10.getPos(), Blocks.AIR.defaultBlockState(), 2);
                bqb.levelEvent(2001, cfn10.getPos(), Block.getId(cfn10.getState()));
            }
        }
        final WitherBoss baw8 = EntityType.WITHER.create(bqb);
        final BlockPos fu2 = b7.getBlock(1, 2, 0).getPos();
        baw8.moveTo(fu2.getX() + 0.5, fu2.getY() + 0.55, fu2.getZ() + 0.5, (b7.getForwards().getAxis() == Direction.Axis.X) ? 0.0f : 90.0f, 0.0f);
        baw8.yBodyRot = ((b7.getForwards().getAxis() == Direction.Axis.X) ? 0.0f : 90.0f);
        baw8.makeInvulnerable();
        for (final ServerPlayer ze11 : bqb.<ServerPlayer>getEntitiesOfClass(ServerPlayer.class, baw8.getBoundingBox().inflate(50.0))) {
            CriteriaTriggers.SUMMONED_ENTITY.trigger(ze11, baw8);
        }
        bqb.addFreshEntity(baw8);
        for (int integer10 = 0; integer10 < cfo6.getWidth(); ++integer10) {
            for (int integer11 = 0; integer11 < cfo6.getHeight(); ++integer11) {
                bqb.blockUpdated(b7.getBlock(integer10, integer11, 0).getPos(), Blocks.AIR);
            }
        }
    }
    
    public static boolean canSpawnMob(final Level bqb, final BlockPos fu, final ItemStack bki) {
        return bki.getItem() == Items.WITHER_SKELETON_SKULL && fu.getY() >= 2 && bqb.getDifficulty() != Difficulty.PEACEFUL && !bqb.isClientSide && getOrCreateWitherBase().find(bqb, fu) != null;
    }
    
    private static BlockPattern getOrCreateWitherFull() {
        if (WitherSkullBlock.witherPatternFull == null) {
            WitherSkullBlock.witherPatternFull = BlockPatternBuilder.start().aisle("^^^", "###", "~#~").where('#', cfn -> cfn.getState().is(BlockTags.WITHER_SUMMON_BASE_BLOCKS)).where('^', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.WITHER_SKELETON_SKULL).or(BlockStatePredicate.forBlock(Blocks.WITHER_SKELETON_WALL_SKULL)))).where('~', BlockInWorld.hasState(BlockMaterialPredicate.forMaterial(Material.AIR))).build();
        }
        return WitherSkullBlock.witherPatternFull;
    }
    
    private static BlockPattern getOrCreateWitherBase() {
        if (WitherSkullBlock.witherPatternBase == null) {
            WitherSkullBlock.witherPatternBase = BlockPatternBuilder.start().aisle("   ", "###", "~#~").where('#', cfn -> cfn.getState().is(BlockTags.WITHER_SUMMON_BASE_BLOCKS)).where('~', BlockInWorld.hasState(BlockMaterialPredicate.forMaterial(Material.AIR))).build();
        }
        return WitherSkullBlock.witherPatternBase;
    }
}
