package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.stats.Stats;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.phys.BlockHitResult;
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.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class TargetBlock extends Block {
    private static final IntegerProperty OUTPUT_POWER;
    
    public TargetBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)TargetBlock.OUTPUT_POWER, 0));
    }
    
    @Override
    public void onProjectileHit(final Level bqb, final BlockState cfj, final BlockHitResult deh, final Projectile bes) {
        final int integer6 = updateRedstoneOutput(bqb, cfj, deh, bes);
        final Entity aom7 = bes.getOwner();
        if (aom7 instanceof ServerPlayer) {
            final ServerPlayer ze8 = (ServerPlayer)aom7;
            ze8.awardStat(Stats.TARGET_HIT);
            CriteriaTriggers.TARGET_BLOCK_HIT.trigger(ze8, bes, deh.getLocation(), integer6);
        }
    }
    
    private static int updateRedstoneOutput(final LevelAccessor bqc, final BlockState cfj, final BlockHitResult deh, final Entity aom) {
        final int integer5 = getRedstoneStrength(deh, deh.getLocation());
        final int integer6 = (aom instanceof AbstractArrow) ? 20 : 8;
        if (!bqc.getBlockTicks().hasScheduledTick(deh.getBlockPos(), cfj.getBlock())) {
            setOutputPower(bqc, cfj, integer5, deh.getBlockPos(), integer6);
        }
        return integer5;
    }
    
    private static int getRedstoneStrength(final BlockHitResult deh, final Vec3 dem) {
        final Direction fz3 = deh.getDirection();
        final double double4 = Math.abs(Mth.frac(dem.x) - 0.5);
        final double double5 = Math.abs(Mth.frac(dem.y) - 0.5);
        final double double6 = Math.abs(Mth.frac(dem.z) - 0.5);
        final Direction.Axis a12 = fz3.getAxis();
        double double7;
        if (a12 == Direction.Axis.Y) {
            double7 = Math.max(double4, double6);
        }
        else if (a12 == Direction.Axis.Z) {
            double7 = Math.max(double4, double5);
        }
        else {
            double7 = Math.max(double5, double6);
        }
        return Math.max(1, Mth.ceil(15.0 * Mth.clamp((0.5 - double7) / 0.5, 0.0, 1.0)));
    }
    
    private static void setOutputPower(final LevelAccessor bqc, final BlockState cfj, final int integer3, final BlockPos fu, final int integer5) {
        bqc.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)TargetBlock.OUTPUT_POWER, integer3), 3);
        bqc.getBlockTicks().scheduleTick(fu, cfj.getBlock(), integer5);
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (cfj.<Integer>getValue((Property<Integer>)TargetBlock.OUTPUT_POWER) != 0) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)TargetBlock.OUTPUT_POWER, 0), 3);
        }
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return cfj.<Integer>getValue((Property<Integer>)TargetBlock.OUTPUT_POWER);
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(TargetBlock.OUTPUT_POWER);
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (bqb.isClientSide() || cfj1.is(cfj4.getBlock())) {
            return;
        }
        if (cfj1.<Integer>getValue((Property<Integer>)TargetBlock.OUTPUT_POWER) > 0 && !bqb.getBlockTicks().hasScheduledTick(fu, this)) {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj1).<Comparable, Integer>setValue((Property<Comparable>)TargetBlock.OUTPUT_POWER, 0), 18);
        }
    }
    
    static {
        OUTPUT_POWER = BlockStateProperties.POWER;
    }
}
