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.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.item.BlockPlaceContext;
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;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import net.minecraft.world.level.block.state.properties.EnumProperty;

public class NoteBlock extends Block {
    public static final EnumProperty<NoteBlockInstrument> INSTRUMENT;
    public static final BooleanProperty POWERED;
    public static final IntegerProperty NOTE;
    
    public NoteBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.HARP)).setValue((Property<Comparable>)NoteBlock.NOTE, 0)).<Comparable, Boolean>setValue((Property<Comparable>)NoteBlock.POWERED, false));
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<NoteBlockInstrument, NoteBlockInstrument>setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.byState(bin.getLevel().getBlockState(bin.getClickedPos().below())));
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (fz == Direction.DOWN) {
            return ((StateHolder<O, BlockState>)cfj1).<NoteBlockInstrument, NoteBlockInstrument>setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.byState(cfj3));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        final boolean boolean7 = bqb.hasNeighborSignal(fu3);
        if (boolean7 != cfj.<Boolean>getValue((Property<Boolean>)NoteBlock.POWERED)) {
            if (boolean7) {
                this.playNote(bqb, fu3);
            }
            bqb.setBlock(fu3, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)NoteBlock.POWERED, boolean7), 3);
        }
    }
    
    private void playNote(final Level bqb, final BlockPos fu) {
        if (bqb.getBlockState(fu.above()).isAir()) {
            bqb.blockEvent(fu, this, 0, 0);
        }
    }
    
    @Override
    public InteractionResult use(BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (bqb.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        cfj = ((StateHolder<O, BlockState>)cfj).<Comparable>cycle((Property<Comparable>)NoteBlock.NOTE);
        bqb.setBlock(fu, cfj, 3);
        this.playNote(bqb, fu);
        bec.awardStat(Stats.TUNE_NOTEBLOCK);
        return InteractionResult.CONSUME;
    }
    
    @Override
    public void attack(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec) {
        if (bqb.isClientSide) {
            return;
        }
        this.playNote(bqb, fu);
        bec.awardStat(Stats.PLAY_NOTEBLOCK);
    }
    
    @Override
    public boolean triggerEvent(final BlockState cfj, final Level bqb, final BlockPos fu, final int integer4, final int integer5) {
        final int integer6 = cfj.<Integer>getValue((Property<Integer>)NoteBlock.NOTE);
        final float float8 = (float)Math.pow(2.0, (integer6 - 12) / 12.0);
        bqb.playSound(null, fu, cfj.<NoteBlockInstrument>getValue(NoteBlock.INSTRUMENT).getSoundEvent(), SoundSource.RECORDS, 3.0f, float8);
        bqb.addParticle(ParticleTypes.NOTE, fu.getX() + 0.5, fu.getY() + 1.2, fu.getZ() + 0.5, integer6 / 24.0, 0.0, 0.0);
        return true;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(NoteBlock.INSTRUMENT, NoteBlock.POWERED, NoteBlock.NOTE);
    }
    
    static {
        INSTRUMENT = BlockStateProperties.NOTEBLOCK_INSTRUMENT;
        POWERED = BlockStateProperties.POWERED;
        NOTE = BlockStateProperties.NOTE;
    }
}
