Skip to content

Commit 9d7a024

Browse files
committed
Cleaned up code, Added parameter validation, Corrected various data types
1 parent 6ec49e4 commit 9d7a024

31 files changed

Lines changed: 541 additions & 436 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ NoteBlockLib.writeSong(newSong, new File("output.nbs"));
115115
Song mySong = new GenericSong();
116116
mySong.setTitle("My song");
117117
mySong.getTempoEvents().set(0, 10F); // set the tempo to 10 ticks per second
118-
mySong.getNotes().add(0, new Note().setInstrument(MinecraftInstrument.HARP).setNbsKey((byte) 46));
119-
mySong.getNotes().add(5, new Note().setInstrument(MinecraftInstrument.BASS).setNbsKey((byte) 60));
120-
mySong.getNotes().add(8, new Note().setInstrument(MinecraftInstrument.BIT).setNbsKey((byte) 84));
118+
mySong.getNotes().add(0, new Note().setInstrument(MinecraftInstrument.HARP).setNbsKey(46));
119+
mySong.getNotes().add(5, new Note().setInstrument(MinecraftInstrument.BASS).setNbsKey(60));
120+
mySong.getNotes().add(8, new Note().setInstrument(MinecraftInstrument.BIT).setNbsKey(84));
121121
Song nbsSong = NoteBlockLib.convertSong(mySong, SongFormat.NBS);
122122
NoteBlockLib.writeSong(nbsSong, new File("output.nbs"));
123123
```

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ org.gradle.configuration-cache=true
55
java_version=8
66
maven_group=net.raphimc
77
maven_name=NoteBlockLib
8-
maven_version=3.1.2-SNAPSHOT
8+
maven_version=3.2.0-SNAPSHOT

src/main/java/net/raphimc/noteblocklib/NoteBlockLib.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import java.nio.file.Files;
4040
import java.nio.file.Path;
4141

42-
import static com.google.common.io.Files.getFileExtension;
43-
4442
public class NoteBlockLib {
4543

4644
public static Song readSong(final File file) throws Exception {
@@ -128,7 +126,7 @@ public static Song convertSong(final Song song, final SongFormat targetFormat) {
128126
}
129127

130128
public static SongFormat getFormat(final Path path) {
131-
return SongFormat.getByExtension(getFileExtension(path.getFileName().toString()));
129+
return SongFormat.getByExtension(com.google.common.io.Files.getFileExtension(path.getFileName().toString()));
132130
}
133131

134132
}

src/main/java/net/raphimc/noteblocklib/data/MinecraftDefinitions.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
*/
1818
package net.raphimc.noteblocklib.data;
1919

20+
import net.raphimc.noteblocklib.format.midi.MidiDefinitions;
2021
import net.raphimc.noteblocklib.model.Note;
2122
import net.raphimc.noteblocklib.model.instrument.Instrument;
23+
import net.raphimc.noteblocklib.util.MathUtil;
2224

2325
import java.util.EnumMap;
2426
import java.util.Map;
@@ -27,11 +29,11 @@
2729

2830
public class MinecraftDefinitions {
2931

30-
public static final int MC_LOWEST_MIDI_KEY = 54;
31-
public static final int MC_HIGHEST_MIDI_KEY = 78;
32-
public static final int MC_LOWEST_KEY = 0;
33-
public static final int MC_HIGHEST_KEY = 24;
34-
public static final int MC_KEYS = Constants.KEYS_PER_OCTAVE * 2;
32+
public static final int LOWEST_MIDI_KEY = 54;
33+
public static final int HIGHEST_MIDI_KEY = 78;
34+
public static final int LOWEST_KEY = 0;
35+
public static final int HIGHEST_KEY = 24;
36+
public static final int KEY_COUNT = MidiDefinitions.KEYS_PER_OCTAVE * 2;
3537

3638
// Instrument -> [lower shifts, upper shifts]
3739
private static final Map<MinecraftInstrument, MinecraftInstrument[][]> INSTRUMENT_SHIFTS = new EnumMap<>(MinecraftInstrument.class);
@@ -62,7 +64,7 @@ public class MinecraftDefinitions {
6264
* @param note The note to clamp
6365
*/
6466
public static void clampNoteKey(final Note note) {
65-
note.setMidiKey(Math.max(MC_LOWEST_MIDI_KEY, Math.min(MC_HIGHEST_MIDI_KEY, note.getMidiKey())));
67+
note.setMidiKey(MathUtil.clamp(note.getMidiKey(), LOWEST_MIDI_KEY, HIGHEST_MIDI_KEY));
6668
}
6769

6870
/**
@@ -72,7 +74,7 @@ public static void clampNoteKey(final Note note) {
7274
* @param note The note to transpose
7375
*/
7476
public static void transposeNoteKey(final Note note) {
75-
transposeNoteKey(note, Constants.KEYS_PER_OCTAVE);
77+
transposeNoteKey(note, MidiDefinitions.KEYS_PER_OCTAVE);
7678
}
7779

7880
/**
@@ -84,10 +86,10 @@ public static void transposeNoteKey(final Note note) {
8486
*/
8587
public static void transposeNoteKey(final Note note, final int transposeAmount) {
8688
float key = note.getMidiKey();
87-
while (key < MC_LOWEST_MIDI_KEY) {
89+
while (key < LOWEST_MIDI_KEY) {
8890
key += transposeAmount;
8991
}
90-
while (key > MC_HIGHEST_MIDI_KEY) {
92+
while (key > HIGHEST_MIDI_KEY) {
9193
key -= transposeAmount;
9294
}
9395
note.setMidiKey(key);
@@ -113,15 +115,15 @@ public static void instrumentShiftNote(final Note note) {
113115

114116
float key = note.getMidiKey();
115117
int downShifts = 0;
116-
while (key < MC_LOWEST_MIDI_KEY && downShifts < shifts[0].length) {
118+
while (key < LOWEST_MIDI_KEY && downShifts < shifts[0].length) {
117119
instrument = shifts[0][downShifts++];
118-
key += MinecraftDefinitions.MC_KEYS;
120+
key += KEY_COUNT;
119121
}
120122

121123
int upShifts = 0;
122-
while (key > MC_HIGHEST_MIDI_KEY && upShifts < shifts[1].length) {
124+
while (key > HIGHEST_MIDI_KEY && upShifts < shifts[1].length) {
123125
instrument = shifts[1][upShifts++];
124-
key -= MinecraftDefinitions.MC_KEYS;
126+
key -= KEY_COUNT;
125127
}
126128

127129
note.setInstrument(instrument);
@@ -138,12 +140,12 @@ public static void instrumentShiftNote(final Note note) {
138140
*/
139141
public static int applyExtendedNotesResourcePack(final Note note) {
140142
int octavesDelta = 0;
141-
while (note.getMidiKey() < MC_LOWEST_MIDI_KEY) {
142-
note.setMidiKey(note.getMidiKey() + MC_KEYS);
143+
while (note.getMidiKey() < LOWEST_MIDI_KEY) {
144+
note.setMidiKey(note.getMidiKey() + KEY_COUNT);
143145
octavesDelta--;
144146
}
145-
while (note.getMidiKey() > MC_HIGHEST_MIDI_KEY) {
146-
note.setMidiKey(note.getMidiKey() - MC_KEYS);
147+
while (note.getMidiKey() > HIGHEST_MIDI_KEY) {
148+
note.setMidiKey(note.getMidiKey() - KEY_COUNT);
147149
octavesDelta++;
148150
}
149151
return octavesDelta;

src/main/java/net/raphimc/noteblocklib/data/MinecraftInstrument.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ public enum MinecraftInstrument implements Instrument {
3838
BANJO(14, 14, "block.note_block.banjo"),
3939
PLING(15, 15, "block.note_block.pling");
4040

41-
private final byte nbsId;
42-
private final byte mcId;
41+
private final int nbsId;
42+
private final int mcId;
4343
private final String mcSoundName;
4444

4545
MinecraftInstrument(final int nbsId, final int mcId, final String mcSoundName) {
46-
this.nbsId = (byte) nbsId;
47-
this.mcId = (byte) mcId;
46+
this.nbsId = nbsId;
47+
this.mcId = mcId;
4848
this.mcSoundName = mcSoundName;
4949
}
5050

51-
public static MinecraftInstrument fromNbsId(final byte nbsId) {
51+
public static MinecraftInstrument fromNbsId(final int nbsId) {
5252
for (final MinecraftInstrument instrument : MinecraftInstrument.values()) {
5353
if (instrument.nbsId == nbsId) {
5454
return instrument;
@@ -57,7 +57,7 @@ public static MinecraftInstrument fromNbsId(final byte nbsId) {
5757
return null;
5858
}
5959

60-
public static MinecraftInstrument fromMcId(final byte mcId) {
60+
public static MinecraftInstrument fromMcId(final int mcId) {
6161
for (final MinecraftInstrument instrument : MinecraftInstrument.values()) {
6262
if (instrument.mcId == mcId) {
6363
return instrument;
@@ -75,11 +75,11 @@ public static MinecraftInstrument fromMcSoundName(final String mcSoundName) {
7575
return null;
7676
}
7777

78-
public byte nbsId() {
78+
public int nbsId() {
7979
return this.nbsId;
8080
}
8181

82-
public byte mcId() {
82+
public int mcId() {
8383
return this.mcId;
8484
}
8585

src/main/java/net/raphimc/noteblocklib/data/Constants.java renamed to src/main/java/net/raphimc/noteblocklib/format/futureclient/FutureClientDefinitions.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
package net.raphimc.noteblocklib.data;
18+
package net.raphimc.noteblocklib.format.futureclient;
1919

20-
public class Constants {
20+
public class FutureClientDefinitions {
2121

22-
public static final int F_SHARP_4_MIDI_KEY = 66;
23-
24-
public static final int KEYS_PER_OCTAVE = 12;
22+
public static final int TEMPO = 20;
2523

2624
}

src/main/java/net/raphimc/noteblocklib/format/futureclient/FutureClientIo.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class FutureClientIo {
3838
public static FutureClientSong readSong(final InputStream is, final String fileName) throws IOException {
3939
final LittleEndianDataInputStream dis = new LittleEndianDataInputStream(new BufferedInputStream(is, BUFFER_SIZE));
4040
final FutureClientSong song = new FutureClientSong(fileName);
41-
4241
final Map<Integer, List<FutureClientNote>> notes = song.getFutureClientNotes();
4342

4443
boolean use64 = false;
@@ -66,7 +65,7 @@ public static FutureClientSong readSong(final InputStream is, final String fileN
6665
}
6766

6867
{ // Fill generalized song structure with data
69-
song.getTempoEvents().set(0, 20);
68+
song.getTempoEvents().set(0, FutureClientDefinitions.TEMPO);
7069
for (Map.Entry<Integer, List<FutureClientNote>> entry : notes.entrySet()) {
7170
for (FutureClientNote futureClientNote : entry.getValue()) {
7271
final Note note = new Note();

src/main/java/net/raphimc/noteblocklib/format/futureclient/model/FutureClientNote.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public FutureClientNote setInstrument(final byte instrument) {
3333
return this;
3434
}
3535

36+
public FutureClientNote setInstrument(final int instrument) {
37+
if (instrument < Byte.MIN_VALUE || instrument > Byte.MAX_VALUE) {
38+
throw new IllegalArgumentException("Instrument must be between " + Byte.MIN_VALUE + " and " + Byte.MAX_VALUE);
39+
}
40+
return this.setInstrument((byte) instrument);
41+
}
42+
3643
public byte getKey() {
3744
return this.key;
3845
}
@@ -42,6 +49,13 @@ public FutureClientNote setKey(final byte key) {
4249
return this;
4350
}
4451

52+
public FutureClientNote setKey(final int key) {
53+
if (key < Byte.MIN_VALUE || key > Byte.MAX_VALUE) {
54+
throw new IllegalArgumentException("Key must be between " + Byte.MIN_VALUE + " and " + Byte.MAX_VALUE);
55+
}
56+
return this.setKey((byte) key);
57+
}
58+
4559
public FutureClientNote copy() {
4660
final FutureClientNote copyNote = new FutureClientNote();
4761
copyNote.setInstrument(this.getInstrument());

src/main/java/net/raphimc/noteblocklib/format/mcsp/McSpDefinitions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
public class McSpDefinitions {
2121

22+
public static final int TEMPO = 10;
2223
public static final int NOTE_COUNT = 7;
2324

2425
}

src/main/java/net/raphimc/noteblocklib/format/mcsp/McSpIo.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ public class McSpIo {
3535
public static McSpSong readSong(final InputStream is, final String fileName) {
3636
final Scanner scanner = new Scanner(new BufferedInputStream(is, BUFFER_SIZE), StandardCharsets.ISO_8859_1.name()).useDelimiter("\\|");
3737
final McSpSong song = new McSpSong(fileName);
38+
final Map<Integer, McSpNote[]> notes = song.getMcSpNotes();
3839

3940
scanner.nextInt(); // version? Is ignored by Minecraft Song Planner v2.5
4041

41-
final Map<Integer, McSpNote[]> notes = song.getMcSpNotes();
42-
4342
int tick = 0;
4443
while (scanner.hasNext()) {
4544
tick += scanner.nextInt();
@@ -54,18 +53,20 @@ public static McSpSong readSong(final InputStream is, final String fileName) {
5453
if (instrument == 0) continue;
5554

5655
final McSpNote note = new McSpNote();
57-
note.setInstrument((byte) (instrument - 1));
58-
note.setKey((byte) key);
56+
note.setInstrument(instrument - 1);
57+
note.setKey(key);
5958
noteArray[i] = note;
6059
}
6160
notes.put(tick, noteArray);
6261
}
6362

6463
{ // Fill generalized song structure with data
65-
song.getTempoEvents().set(0, 10);
64+
song.getTempoEvents().set(0, McSpDefinitions.TEMPO);
6665
for (Map.Entry<Integer, McSpNote[]> entry : notes.entrySet()) {
6766
for (McSpNote mcSpNote : entry.getValue()) {
68-
if (mcSpNote == null) continue;
67+
if (mcSpNote == null) {
68+
continue;
69+
}
6970

7071
final Note note = new Note();
7172
note.setInstrument(MinecraftInstrument.fromNbsId(mcSpNote.getInstrument()));

0 commit comments

Comments
 (0)