package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.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(((((AbstractStateHolder<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 bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<NoteBlockInstrument, NoteBlockInstrument>setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.byState(bcq.getLevel().getBlockState(bcq.getClickedPos().below())));
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp == Direction.DOWN) {
            return ((AbstractStateHolder<O, BlockState>)byj1).<NoteBlockInstrument, NoteBlockInstrument>setValue(NoteBlock.INSTRUMENT, NoteBlockInstrument.byState(byj3));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        final boolean boolean7 = bjw.hasNeighborSignal(fk3);
        if (boolean7 != byj.<Boolean>getValue((Property<Boolean>)NoteBlock.POWERED)) {
            if (boolean7) {
                this.playNote(bjw, fk3);
            }
            bjw.setBlock(fk3, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)NoteBlock.POWERED, boolean7), 3);
        }
    }
    
    private void playNote(final Level bjw, final BlockPos fk) {
        if (bjw.getBlockState(fk.above()).isAir()) {
            bjw.blockEvent(fk, this, 0, 0);
        }
    }
    
    @Override
    public InteractionResult use(BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (bjw.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        byj = ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)NoteBlock.NOTE);
        bjw.setBlock(fk, byj, 3);
        this.playNote(bjw, fk);
        ayj.awardStat(Stats.TUNE_NOTEBLOCK);
        return InteractionResult.SUCCESS;
    }
    
    @Override
    public void attack(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj) {
        if (bjw.isClientSide) {
            return;
        }
        this.playNote(bjw, fk);
        ayj.awardStat(Stats.PLAY_NOTEBLOCK);
    }
    
    @Override
    public boolean triggerEvent(final BlockState byj, final Level bjw, final BlockPos fk, final int integer4, final int integer5) {
        final int integer6 = byj.<Integer>getValue((Property<Integer>)NoteBlock.NOTE);
        final float float8 = (float)Math.pow(2.0, (integer6 - 12) / 12.0);
        bjw.playSound(null, fk, byj.<NoteBlockInstrument>getValue(NoteBlock.INSTRUMENT).getSoundEvent(), SoundSource.RECORDS, 3.0f, float8);
        bjw.addParticle(ParticleTypes.NOTE, fk.getX() + 0.5, fk.getY() + 1.2, fk.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;
    }
}
