Skip to content

Commit 7e15ee3

Browse files
committed
Added FullNoteConsumer interface
1 parent a5c7acf commit 7e15ee3

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* This file is part of NoteBlockLib - https://github.com/RaphiMC/NoteBlockLib
3+
* Copyright (C) 2022-2024 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.noteblocklib.player;
19+
20+
import net.raphimc.noteblocklib.format.nbs.NbsDefinitions;
21+
import net.raphimc.noteblocklib.format.nbs.model.NbsCustomInstrument;
22+
import net.raphimc.noteblocklib.format.nbs.model.NbsNote;
23+
import net.raphimc.noteblocklib.model.Note;
24+
import net.raphimc.noteblocklib.model.NoteWithPanning;
25+
import net.raphimc.noteblocklib.model.NoteWithVolume;
26+
import net.raphimc.noteblocklib.util.Instrument;
27+
import net.raphimc.noteblocklib.util.MinecraftDefinitions;
28+
29+
public interface FullNoteConsumer extends NoteConsumer {
30+
31+
@Override
32+
default void playNote(final Note note) {
33+
final float volume;
34+
if (note instanceof NoteWithVolume) {
35+
final NoteWithVolume noteWithVolume = (NoteWithVolume) note;
36+
volume = noteWithVolume.getVolume();
37+
} else {
38+
volume = 100F;
39+
}
40+
if (volume <= 0) return;
41+
42+
final float panning;
43+
if (note instanceof NoteWithPanning) {
44+
final NoteWithPanning noteWithPanning = (NoteWithPanning) note;
45+
panning = noteWithPanning.getPanning();
46+
} else {
47+
panning = 0F;
48+
}
49+
50+
final float pitch;
51+
if (note instanceof NbsNote) {
52+
NbsNote nbsNote = (NbsNote) note;
53+
if (nbsNote.getCustomInstrument() != null) {
54+
nbsNote = nbsNote.clone();
55+
nbsNote.setKey((byte) (nbsNote.getKey() + nbsNote.getCustomInstrument().getPitch() - 45));
56+
}
57+
pitch = MinecraftDefinitions.nbsPitchToMcPitch(NbsDefinitions.getPitch(nbsNote));
58+
} else {
59+
pitch = MinecraftDefinitions.mcKeyToMcPitch(MinecraftDefinitions.nbsKeyToMcKey(note.getKey()));
60+
}
61+
62+
final float playerVolume = volume / 100F;
63+
final float playerPanning = panning / 100F;
64+
if (note.getInstrument() != null) {
65+
this.playNote(note.getInstrument(), pitch, playerVolume, playerPanning);
66+
} else if (note instanceof NbsNote && ((NbsNote) note).getCustomInstrument() != null) {
67+
this.playCustomNote(((NbsNote) note).getCustomInstrument(), pitch, playerVolume, playerPanning);
68+
}
69+
}
70+
71+
/**
72+
* Plays a note with the given instrument, volume, pitch and panning.
73+
*
74+
* @param instrument The instrument of the note
75+
* @param pitch The pitch of the note
76+
* @param volume The volume of the note (0.0 to 1.0)
77+
* @param panning The panning of the note (-1.0 to 1.0)
78+
*/
79+
void playNote(final Instrument instrument, final float pitch, final float volume, final float panning);
80+
81+
/**
82+
* Plays a note with the given custom instrument, volume, pitch and panning. The pitch offset of the custom instrument is already applied to the note.
83+
*
84+
* @param customInstrument The custom instrument of the note
85+
* @param pitch The pitch of the note
86+
* @param volume The volume of the note (0.0 to 1.0)
87+
* @param panning The panning of the note (-1.0 to 1.0)
88+
*/
89+
default void playCustomNote(final NbsCustomInstrument customInstrument, final float pitch, final float volume, final float panning) {
90+
}
91+
92+
}

0 commit comments

Comments
 (0)