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.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 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 bjw, final BlockPos fk, final BlockState byj, @Nullable final LivingEntity akz, final ItemStack ben) {
        super.setPlacedBy(bjw, fk, byj, akz, ben);
        final BlockEntity bwl7 = bjw.getBlockEntity(fk);
        if (bwl7 instanceof SkullBlockEntity) {
            checkSpawn(bjw, fk, (SkullBlockEntity)bwl7);
        }
    }
    
    public static void checkSpawn(final Level bjw, final BlockPos fk, final SkullBlockEntity bxi) {
        if (bjw.isClientSide) {
            return;
        }
        final Block bph4 = bxi.getBlockState().getBlock();
        final boolean boolean5 = bph4 == Blocks.WITHER_SKELETON_SKULL || bph4 == Blocks.WITHER_SKELETON_WALL_SKULL;
        if (!boolean5 || fk.getY() < 2 || bjw.getDifficulty() == Difficulty.PEACEFUL) {
            return;
        }
        final BlockPattern byo6 = getOrCreateWitherFull();
        final BlockPattern.BlockPatternMatch b7 = byo6.find(bjw, fk);
        if (b7 == null) {
            return;
        }
        for (int integer8 = 0; integer8 < byo6.getWidth(); ++integer8) {
            for (int integer9 = 0; integer9 < byo6.getHeight(); ++integer9) {
                final BlockInWorld byn10 = b7.getBlock(integer8, integer9, 0);
                bjw.setBlock(byn10.getPos(), Blocks.AIR.defaultBlockState(), 2);
                bjw.levelEvent(2001, byn10.getPos(), Block.getId(byn10.getState()));
            }
        }
        final WitherBoss avn8 = EntityType.WITHER.create(bjw);
        final BlockPos fk2 = b7.getBlock(1, 2, 0).getPos();
        avn8.moveTo(fk2.getX() + 0.5, fk2.getY() + 0.55, fk2.getZ() + 0.5, (b7.getForwards().getAxis() == Direction.Axis.X) ? 0.0f : 90.0f, 0.0f);
        avn8.yBodyRot = ((b7.getForwards().getAxis() == Direction.Axis.X) ? 0.0f : 90.0f);
        avn8.makeInvulnerable();
        for (final ServerPlayer xe11 : bjw.<ServerPlayer>getEntitiesOfClass(ServerPlayer.class, avn8.getBoundingBox().inflate(50.0))) {
            CriteriaTriggers.SUMMONED_ENTITY.trigger(xe11, avn8);
        }
        bjw.addFreshEntity(avn8);
        for (int integer10 = 0; integer10 < byo6.getWidth(); ++integer10) {
            for (int integer11 = 0; integer11 < byo6.getHeight(); ++integer11) {
                bjw.blockUpdated(b7.getBlock(integer10, integer11, 0).getPos(), Blocks.AIR);
            }
        }
    }
    
    public static boolean canSpawnMob(final Level bjw, final BlockPos fk, final ItemStack ben) {
        return ben.getItem() == Items.WITHER_SKELETON_SKULL && fk.getY() >= 2 && bjw.getDifficulty() != Difficulty.PEACEFUL && !bjw.isClientSide && getOrCreateWitherBase().find(bjw, fk) != null;
    }
    
    private static BlockPattern getOrCreateWitherFull() {
        if (WitherSkullBlock.witherPatternFull == null) {
            WitherSkullBlock.witherPatternFull = BlockPatternBuilder.start().aisle("^^^", "###", "~#~").where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.SOUL_SAND))).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('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.SOUL_SAND))).where('~', BlockInWorld.hasState(BlockMaterialPredicate.forMaterial(Material.AIR))).build();
        }
        return WitherSkullBlock.witherPatternBase;
    }
}
