NoteBlock .java

net.minecraft.world.level.block.NoteBlock (class): members: getPitchFromNote, NOTE_VOLUME, INSTRUMENT, NOTE, attack, triggerEvent, POWERED, useItemOn.

net.minecraft.world.level.blockclass177 linescommonextends Block6.8 KB

API Surface (9)

BlockState getStateForPlacement(final BlockPlaceContext context)
BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
InteractionResult useItemOn(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final BlockHitResult hitResult)
InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
void attack(final BlockState state, final Level level, final BlockPos pos, final Player player)
float getPitchFromNote(final int twoOctaveRangeNote)
boolean triggerEvent(final BlockState state, final Level level, final BlockPos pos, final int b0, final int b1)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (15)

INSTRUMENT
POWERED
NOTE
NOTE_VOLUME
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
orientation
itemStack
player
hand

Full Source

NoteBlock.java177 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.core.Holder;
6
import net.minecraft.core.particles.ParticleTypes;
7
import net.minecraft.resources.Identifier;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.sounds.SoundSource;
10
import net.minecraft.stats.Stats;
11
import net.minecraft.tags.ItemTags;
12
import net.minecraft.util.RandomSource;
13
import net.minecraft.world.InteractionHand;
14
import net.minecraft.world.InteractionResult;
15
import net.minecraft.world.entity.Entity;
16
import net.minecraft.world.entity.player.Player;
17
import net.minecraft.world.item.ItemStack;
18
import net.minecraft.world.item.context.BlockPlaceContext;
19
import net.minecraft.world.level.Level;
20
import net.minecraft.world.level.LevelReader;
21
import net.minecraft.world.level.ScheduledTickAccess;
22
import net.minecraft.world.level.block.entity.SkullBlockEntity;
23
import net.minecraft.world.level.block.state.BlockBehaviour;
24
import net.minecraft.world.level.block.state.BlockState;
25
import net.minecraft.world.level.block.state.StateDefinition;
26
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
27
import net.minecraft.world.level.block.state.properties.BooleanProperty;
28
import net.minecraft.world.level.block.state.properties.EnumProperty;
29
import net.minecraft.world.level.block.state.properties.IntegerProperty;
30
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
31
import net.minecraft.world.level.gameevent.GameEvent;
32
import net.minecraft.world.level.redstone.Orientation;
33
import net.minecraft.world.phys.BlockHitResult;
34
import org.jspecify.annotations.Nullable;
35
36
public class NoteBlock extends Block {
37
	public static final EnumProperty<NoteBlockInstrument> INSTRUMENT = BlockStateProperties.NOTEBLOCK_INSTRUMENT;
38
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
39
	public static final IntegerProperty NOTE = BlockStateProperties.NOTE;
40
	public static final int NOTE_VOLUME = 3;
41
42
	public NoteBlock(final BlockBehaviour.Properties properties) {
43
		super(properties);
44
		this.registerDefaultState(this.stateDefinition.any().setValue(INSTRUMENT, NoteBlockInstrument.HARP).setValue(NOTE, 0).setValue(POWERED, false));
45
	}
46
47
	private BlockState setInstrument(final LevelReader level, final BlockPos position, final BlockState state) {
48
		NoteBlockInstrument instrumentAbove = level.getBlockState(position.above()).instrument();
49
		if (instrumentAbove.worksAboveNoteBlock()) {
50
			return state.setValue(INSTRUMENT, instrumentAbove);
51
		}
52
53
		NoteBlockInstrument instrumentBelow = level.getBlockState(position.below()).instrument();
54
		NoteBlockInstrument newBelow = instrumentBelow.worksAboveNoteBlock() ? NoteBlockInstrument.HARP : instrumentBelow;
55
		return state.setValue(INSTRUMENT, newBelow);
56
	}
57
58
	@Override
59
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
60
		return this.setInstrument(context.getLevel(), context.getClickedPos(), this.defaultBlockState());
61
	}
62
63
	@Override
64
	protected BlockState updateShape(
65
		final BlockState state,
66
		final LevelReader level,
67
		final ScheduledTickAccess ticks,
68
		final BlockPos pos,
69
		final Direction directionToNeighbour,
70
		final BlockPos neighbourPos,
71
		final BlockState neighbourState,
72
		final RandomSource random
73
	) {
74
		boolean neighborDirectionSetsInstrument = directionToNeighbour.getAxis() == Direction.Axis.Y;
75
		return neighborDirectionSetsInstrument
76
			? this.setInstrument(level, pos, state)
77
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
78
	}
79
80
	@Override
81
	protected void neighborChanged(
82
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
83
	) {
84
		boolean signal = level.hasNeighborSignal(pos);
85
		if (signal != state.getValue(POWERED)) {
86
			if (signal) {
87
				this.playNote(null, state, level, pos);
88
			}
89
90
			level.setBlockAndUpdate(pos, state.setValue(POWERED, signal));
91
		}
92
	}
93
94
	private void playNote(final @Nullable Entity source, final BlockState state, final Level level, final BlockPos pos) {
95
		if (state.getValue(INSTRUMENT).worksAboveNoteBlock() || level.getBlockState(pos.above()).isAir()) {
96
			level.blockEvent(pos, this, 0, 0);
97
			level.gameEvent(source, GameEvent.NOTE_BLOCK_PLAY, pos);
98
		}
99
	}
100
101
	@Override
102
	protected InteractionResult useItemOn(
103
		final ItemStack itemStack,
104
		final BlockState state,
105
		final Level level,
106
		final BlockPos pos,
107
		final Player player,
108
		final InteractionHand hand,
109
		final BlockHitResult hitResult
110
	) {
111
		return itemStack.is(ItemTags.NOTE_BLOCK_TOP_INSTRUMENTS) && hitResult.getDirection() == Direction.UP
112
			? InteractionResult.PASS
113
			: super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
114
	}
115
116
	@Override
117
	protected InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
118
		if (!level.isClientSide()) {
119
			state = state.cycle(NOTE);
120
			level.setBlockAndUpdate(pos, state);
121
			this.playNote(player, state, level, pos);
122
			player.awardStat(Stats.TUNE_NOTEBLOCK);
123
		}
124
125
		return InteractionResult.SUCCESS;
126
	}
127
128
	@Override
129
	protected void attack(final BlockState state, final Level level, final BlockPos pos, final Player player) {
130
		if (!level.isClientSide()) {
131
			this.playNote(player, state, level, pos);
132
			player.awardStat(Stats.PLAY_NOTEBLOCK);
133
		}
134
	}
135
136
	public static float getPitchFromNote(final int twoOctaveRangeNote) {
137
		return (float)Math.pow(2.0, (twoOctaveRangeNote - 12) / 12.0);
138
	}
139
140
	@Override
141
	protected boolean triggerEvent(final BlockState state, final Level level, final BlockPos pos, final int b0, final int b1) {
142
		NoteBlockInstrument instrument = state.getValue(INSTRUMENT);
143
		float pitch;
144
		if (instrument.isTunable()) {
145
			int note = state.getValue(NOTE);
146
			pitch = getPitchFromNote(note);
147
			level.addParticle(ParticleTypes.NOTE, pos.getX() + 0.5, pos.getY() + 1.2, pos.getZ() + 0.5, note / 24.0, 0.0, 0.0);
148
		} else {
149
			pitch = 1.0F;
150
		}
151
152
		Holder<SoundEvent> soundEvent;
153
		if (instrument.hasCustomSound()) {
154
			Identifier soundId = this.getCustomSoundId(level, pos);
155
			if (soundId == null) {
156
				return false;
157
			}
158
159
			soundEvent = Holder.direct(SoundEvent.createVariableRangeEvent(soundId));
160
		} else {
161
			soundEvent = instrument.getSoundEvent();
162
		}
163
164
		level.playSeededSound(null, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, soundEvent, SoundSource.RECORDS, 3.0F, pitch, level.getRandom().nextLong());
165
		return true;
166
	}
167
168
	private @Nullable Identifier getCustomSoundId(final Level level, final BlockPos pos) {
169
		return level.getBlockEntity(pos.above()) instanceof SkullBlockEntity head ? head.getNoteBlockSound() : null;
170
	}
171
172
	@Override
173
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
174
		builder.add(INSTRUMENT, POWERED, NOTE);
175
	}
176
}
177

Decompiled Minecraft 26.3 source of NoteBlock (net/minecraft/world/level/block/NoteBlock.java) with Mojang official mappings, stored verbatim on the site.