package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.BlockGetter;
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.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;

public class CarvedPumpkinBlock extends HorizontalDirectionalBlock {
    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(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Direction>setValue((Property<Comparable>)CarvedPumpkinBlock.FACING, Direction.NORTH));
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj4.getBlock() == byj1.getBlock()) {
            return;
        }
        this.trySpawnGolem(bjw, fk);
    }
    
    public boolean canSpawnGolem(final LevelReader bjz, final BlockPos fk) {
        return this.getOrCreateSnowGolemBase().find(bjz, fk) != null || this.getOrCreateIronGolemBase().find(bjz, fk) != null;
    }
    
    private void trySpawnGolem(final Level bjw, final BlockPos fk) {
        BlockPattern.BlockPatternMatch b4 = this.getOrCreateSnowGolemFull().find(bjw, fk);
        if (b4 != null) {
            for (int integer5 = 0; integer5 < this.getOrCreateSnowGolemFull().getHeight(); ++integer5) {
                final BlockInWorld byn6 = b4.getBlock(0, integer5, 0);
                bjw.setBlock(byn6.getPos(), Blocks.AIR.defaultBlockState(), 2);
                bjw.levelEvent(2001, byn6.getPos(), Block.getId(byn6.getState()));
            }
            final SnowGolem atx5 = EntityType.SNOW_GOLEM.create(bjw);
            final BlockPos fk2 = b4.getBlock(0, 2, 0).getPos();
            atx5.moveTo(fk2.getX() + 0.5, fk2.getY() + 0.05, fk2.getZ() + 0.5, 0.0f, 0.0f);
            bjw.addFreshEntity(atx5);
            for (final ServerPlayer xe8 : bjw.<ServerPlayer>getEntitiesOfClass(ServerPlayer.class, atx5.getBoundingBox().inflate(5.0))) {
                CriteriaTriggers.SUMMONED_ENTITY.trigger(xe8, atx5);
            }
            for (int integer6 = 0; integer6 < this.getOrCreateSnowGolemFull().getHeight(); ++integer6) {
                final BlockInWorld byn7 = b4.getBlock(0, integer6, 0);
                bjw.blockUpdated(byn7.getPos(), Blocks.AIR);
            }
        }
        else {
            b4 = this.getOrCreateIronGolemFull().find(bjw, fk);
            if (b4 != null) {
                for (int integer5 = 0; integer5 < this.getOrCreateIronGolemFull().getWidth(); ++integer5) {
                    for (int integer7 = 0; integer7 < this.getOrCreateIronGolemFull().getHeight(); ++integer7) {
                        final BlockInWorld byn8 = b4.getBlock(integer5, integer7, 0);
                        bjw.setBlock(byn8.getPos(), Blocks.AIR.defaultBlockState(), 2);
                        bjw.levelEvent(2001, byn8.getPos(), Block.getId(byn8.getState()));
                    }
                }
                final BlockPos fk3 = b4.getBlock(1, 2, 0).getPos();
                final IronGolem atl6 = EntityType.IRON_GOLEM.create(bjw);
                atl6.setPlayerCreated(true);
                atl6.moveTo(fk3.getX() + 0.5, fk3.getY() + 0.05, fk3.getZ() + 0.5, 0.0f, 0.0f);
                bjw.addFreshEntity(atl6);
                for (final ServerPlayer xe8 : bjw.<ServerPlayer>getEntitiesOfClass(ServerPlayer.class, atl6.getBoundingBox().inflate(5.0))) {
                    CriteriaTriggers.SUMMONED_ENTITY.trigger(xe8, atl6);
                }
                for (int integer6 = 0; integer6 < this.getOrCreateIronGolemFull().getWidth(); ++integer6) {
                    for (int integer8 = 0; integer8 < this.getOrCreateIronGolemFull().getHeight(); ++integer8) {
                        final BlockInWorld byn9 = b4.getBlock(integer6, integer8, 0);
                        bjw.blockUpdated(byn9.getPos(), Blocks.AIR);
                    }
                }
            }
        }
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)CarvedPumpkinBlock.FACING, bcq.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;
    }
    
    @Override
    public boolean isValidSpawn(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final EntityType<?> aku) {
        return true;
    }
    
    static {
        FACING = HorizontalDirectionalBlock.FACING;
        PUMPKINS_PREDICATE = (byj -> byj != null && (byj.getBlock() == Blocks.CARVED_PUMPKIN || byj.getBlock() == Blocks.JACK_O_LANTERN));
    }
}
