Skip to content

Commit 6cceb22

Browse files
committed
Rewrote MinecraftDefinitions#applyExtendedNotesResourcePack
The old method has been moved to MinecraftDefinitions#applyExtendedNotesResourcePackOld
1 parent 49fc360 commit 6cceb22

2 files changed

Lines changed: 103 additions & 4 deletions

File tree

src/main/java/net/raphimc/noteblocklib/format/minecraft/MinecraftDefinitions.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static void transposeNoteKey(final Note note) {
8181
* Transposes the key of the note to fall within minecraft octave range.<br>
8282
* Any key below 33 (NBS) will be transposed up by transposeAmount, and any key above 57 (NBS) will be transposed down by transposeAmount.
8383
*
84-
* @param note The note to transpose
84+
* @param note The note to transpose
8585
* @param transposeAmount The amount of keys to transpose by
8686
*/
8787
public static void transposeNoteKey(final Note note, final int transposeAmount) {
@@ -104,8 +104,8 @@ public static void transposeNoteKey(final Note note, final int transposeAmount)
104104
*/
105105
public static void instrumentShiftNote(final Note note) {
106106
Instrument instrument = note.getInstrument();
107-
if (!(instrument instanceof MinecraftInstrument)) { // Custom instrument
108-
return;
107+
if (!(instrument instanceof MinecraftInstrument)) {
108+
return; // Custom instrument
109109
}
110110

111111
final MinecraftInstrument[][] shifts = INSTRUMENT_SHIFTS.get(instrument);
@@ -130,15 +130,44 @@ public static void instrumentShiftNote(final Note note) {
130130
note.setMidiKey(key);
131131
}
132132

133+
/**
134+
* Modifies the note to be compatible with the Extended Octave Range Notes resource pack.<br>
135+
* This includes shifting the note key to be within the Minecraft octave range and changing its instrument to a {@link ShiftedMinecraftInstrument} with the correct octaves shift.<br>
136+
* The octaves shift value has to be appended to the sound name to play the note at the correct pitch. (For example: "block.note_block.harp_" + octavesShift)<br>
137+
* {@link ShiftedMinecraftInstrument#mcSoundName()} can be used to get the correct sound name with the suffix already appended.<br>
138+
* Link to the resource pack: <a href="https://github.com/RaphiMC/NoteBlockLib/raw/main/Extended%20Octave%20Range%20Notes%20Pack.zip">Extended Notes</a>
139+
*
140+
* @param note The note to modify
141+
*/
142+
public static void applyExtendedNotesResourcePack(final Note note) {
143+
if (!(note.getInstrument() instanceof MinecraftInstrument)) {
144+
return; // Custom instrument
145+
}
146+
int octavesShift = 0;
147+
while (note.getMidiKey() < LOWEST_MIDI_KEY) {
148+
note.setMidiKey(note.getMidiKey() + KEY_COUNT);
149+
octavesShift--;
150+
}
151+
while (note.getMidiKey() > HIGHEST_MIDI_KEY) {
152+
note.setMidiKey(note.getMidiKey() - KEY_COUNT);
153+
octavesShift++;
154+
}
155+
if (octavesShift != 0) {
156+
note.setInstrument(new ShiftedMinecraftInstrument((MinecraftInstrument) note.getInstrument(), octavesShift));
157+
}
158+
}
159+
133160
/**
134161
* Returns the octave delta to use as a suffix for the sound name and modifies the note key to be within the Minecraft octave range.<br>
135162
* The octave delta value has to be appended to the sound name to play the note at the correct pitch. (For example: "block.note_block.harp_" + octaveDelta)<br>
136163
* Link to the resource pack: <a href="https://github.com/RaphiMC/NoteBlockLib/raw/main/Extended%20Octave%20Range%20Notes%20Pack.zip">Extended Notes</a>
137164
*
138165
* @param note The note to modify
139166
* @return The octave delta to use as a suffix for the sound name
167+
* @deprecated Replaced by {@link #applyExtendedNotesResourcePack(Note)} which also sets the correct instrument automatically.
140168
*/
141-
public static int applyExtendedNotesResourcePack(final Note note) {
169+
@Deprecated
170+
public static int applyExtendedNotesResourcePackOld(final Note note) {
142171
int octavesDelta = 0;
143172
while (note.getMidiKey() < LOWEST_MIDI_KEY) {
144173
note.setMidiKey(note.getMidiKey() + KEY_COUNT);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This file is part of NoteBlockLib - https://github.com/RaphiMC/NoteBlockLib
3+
* Copyright (C) 2022-2025 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.format.minecraft;
19+
20+
import net.raphimc.noteblocklib.model.note.Instrument;
21+
22+
import java.util.Objects;
23+
24+
public class ShiftedMinecraftInstrument implements Instrument {
25+
26+
private final MinecraftInstrument instrument;
27+
private final int octavesShift;
28+
29+
public ShiftedMinecraftInstrument(final MinecraftInstrument instrument, final int octavesShift) {
30+
if (instrument == null) {
31+
throw new IllegalArgumentException("Instrument cannot be null");
32+
}
33+
this.instrument = instrument;
34+
this.octavesShift = octavesShift;
35+
}
36+
37+
public String mcSoundName() {
38+
if (this.octavesShift == 0) {
39+
return this.instrument.mcSoundName();
40+
} else {
41+
return this.instrument.mcSoundName() + '_' + this.octavesShift;
42+
}
43+
}
44+
45+
public MinecraftInstrument getInstrument() {
46+
return this.instrument;
47+
}
48+
49+
public int getOctavesShift() {
50+
return this.octavesShift;
51+
}
52+
53+
@Override
54+
public ShiftedMinecraftInstrument copy() {
55+
return new ShiftedMinecraftInstrument(this.instrument, this.octavesShift);
56+
}
57+
58+
@Override
59+
public boolean equals(Object o) {
60+
if (o == null || getClass() != o.getClass()) return false;
61+
ShiftedMinecraftInstrument that = (ShiftedMinecraftInstrument) o;
62+
return octavesShift == that.octavesShift && instrument == that.instrument;
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
return Objects.hash(instrument, octavesShift);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)