package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.predicate.BlockMaterialPredicate;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.block.state.predicate.BlockStatePredicate;
import net.minecraft.world.level.block.state.pattern.BlockPatternBuilder;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.item.BlockPlaceContext;
import java.util.Iterator;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import net.minecraft.world.entity.animal.IronGolem;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.animal.SnowGolem;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.state.pattern.BlockPattern;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.item.Wearable;

public class CarvedPumpkinBlock extends HorizontalDirectionalBlock implements Wearable {
    public static final DirectionProperty FACING;
    @Nullable
    private BlockPattern snowGolemBase;
    @Nullable
    private BlockPattern snowGolemFull;
    @Nullable
    private BlockPattern ironGolemBase;
    @Nullable
    private BlockPattern ironGolemFull;
    private static final Predicate<BlockState> PUMPKINS_PREDICATE;
    
    protected CarvedPumpkinBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Direction>setValue((Property<Comparable>)CarvedPumpkinBlock.FACING, Direction.NORTH));
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj4.is(cfj1.getBlock())) {
            return;
        }
        this.trySpawnGolem(bqb, fu);
    }
    
    public boolean canSpawnGolem(final LevelReader bqd, final BlockPos fu) {
        return this.getOrCreateSnowGolemBase().find(bqd, fu) != null || this.getOrCreateIronGolemBase().find(bqd, fu) != null;
    }
    
    private void trySpawnGolem(final Level bqb, final BlockPos fu) {
        BlockPattern.BlockPatternMatch b4 = this.getOrCreateSnowGolemFull().find(bqb, fu);
        if (b4 != null) {
            for (int integer5 = 0; integer5 < this.getOrCreateSnowGolemFull().getHeight(); ++integer5) {
                final BlockInWorld cfn6 = b4.getBlock(0, integer5, 0);
                bqb.setBlock(cfn6.getPos(), Blocks.AIR.defaultBlockState(), 2);
                bqb.levelEvent(2001, cfn6.getPos(), Block.getId(cfn6.getState()));
            }
            final SnowGolem azf5 = EntityType.SNOW_GOLEM.create(bqb);
            final BlockPos fu2 = b4.getBlock(0, 2, 0).getPos();
            azf5.moveTo(fu2.getX() + 0.5, fu2.getY() + 0.05, fu2.getZ() + 0.5, 0.0f, 0.0f);
            bqb.addFreshEntity(azf5);
            for (final ServerPlayer ze8 : bqb.<ServerPlayer>getEntitiesOfClass(ServerPlayer.class, azf5.getBoundingBox().inflate(5.0))) {
                CriteriaTriggers.SUMMONED_ENTITY.trigger(ze8, azf5);
            }
            for (int integer6 = 0; integer6 < this.getOrCreateSnowGolemFull().getHeight(); ++integer6) {
                final BlockInWorld cfn7 = b4.getBlock(0, integer6, 0);
                bqb.blockUpdated(cfn7.getPos(), Blocks.AIR);
            }
        }
        else {
            b4 = this.getOrCreateIronGolemFull().find(bqb, fu);
            if (b4 != null) {
                for (int integer5 = 0; integer5 < this.getOrCreateIronGolemFull().getWidth(); ++integer5) {
                    for (int integer7 = 0; integer7 < this.getOrCreateIronGolemFull().getHeight(); ++integer7) {
                        final BlockInWorld cfn8 = b4.getBlock(integer5, integer7, 0);
                        bqb.setBlock(cfn8.getPos(), Blocks.AIR.defaultBlockState(), 2);
                        bqb.levelEvent(2001, cfn8.getPos(), Block.getId(cfn8.getState()));
                    }
                }
                final BlockPos fu3 = b4.getBlock(1, 2, 0).getPos();
                final IronGolem ayt6 = EntityType.IRON_GOLEM.create(bqb);
                ayt6.setPlayerCreated(true);
                ayt6.moveTo(fu3.getX() + 0.5, fu3.getY() + 0.05, fu3.getZ() + 0.5, 0.0f, 0.0f);
                bqb.addFreshEntity(ayt6);
                for (final ServerPlayer ze8 : bqb.<ServerPlayer>getEntitiesOfClass(ServerPlayer.class, ayt6.getBoundingBox().inflate(5.0))) {
                    CriteriaTriggers.SUMMONED_ENTITY.trigger(ze8, ayt6);
                }
                for (int integer6 = 0; integer6 < this.getOrCreateIronGolemFull().getWidth(); ++integer6) {
                    for (int integer8 = 0; integer8 < this.getOrCreateIronGolemFull().getHeight(); ++integer8) {
                        final BlockInWorld cfn9 = b4.getBlock(integer6, integer8, 0);
                        bqb.blockUpdated(cfn9.getPos(), Blocks.AIR);
                    }
                }
            }
        }
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)CarvedPumpkinBlock.FACING, bin.getHorizontalDirection().getOpposite());
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(CarvedPumpkinBlock.FACING);
    }
    
    private BlockPattern getOrCreateSnowGolemBase() {
        if (this.snowGolemBase == null) {
            this.snowGolemBase = BlockPatternBuilder.start().aisle(" ", "#", "#").where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.SNOW_BLOCK))).build();
        }
        return this.snowGolemBase;
    }
    
    private BlockPattern getOrCreateSnowGolemFull() {
        if (this.snowGolemFull == null) {
            this.snowGolemFull = BlockPatternBuilder.start().aisle("^", "#", "#").where('^', BlockInWorld.hasState(CarvedPumpkinBlock.PUMPKINS_PREDICATE)).where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.SNOW_BLOCK))).build();
        }
        return this.snowGolemFull;
    }
    
    private BlockPattern getOrCreateIronGolemBase() {
        if (this.ironGolemBase == null) {
            this.ironGolemBase = BlockPatternBuilder.start().aisle("~ ~", "###", "~#~").where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.IRON_BLOCK))).where('~', BlockInWorld.hasState(BlockMaterialPredicate.forMaterial(Material.AIR))).build();
        }
        return this.ironGolemBase;
    }
    
    private BlockPattern getOrCreateIronGolemFull() {
        if (this.ironGolemFull == null) {
            this.ironGolemFull = BlockPatternBuilder.start().aisle("~^~", "###", "~#~").where('^', BlockInWorld.hasState(CarvedPumpkinBlock.PUMPKINS_PREDICATE)).where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.IRON_BLOCK))).where('~', BlockInWorld.hasState(BlockMaterialPredicate.forMaterial(Material.AIR))).build();
        }
        return this.ironGolemFull;
    }
    
    static {
        FACING = HorizontalDirectionalBlock.FACING;
        PUMPKINS_PREDICATE = (cfj -> cfj != null && (cfj.is(Blocks.CARVED_PUMPKIN) || cfj.is(Blocks.JACK_O_LANTERN)));
    }
}
