Skip to content

Commit 2b5a001

Browse files
committed
Fixed custom instruments #26, finished #25
1 parent 9cd518e commit 2b5a001

File tree

6 files changed

+178
-47
lines changed

6 files changed

+178
-47
lines changed

src/main/java/com/xxmicloxx/NoteBlockAPI/CompatibilityUtils.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,61 @@ public static void playSound(Player player, Location location, Sound sound,
155155
/**
156156
* Gets instruments which were added post-1.12
157157
* @return ArrayList of instruments
158+
* @deprecated Use {@link #getVersionCustomInstruments(float)}
158159
*/
159160
public static ArrayList<CustomInstrument> get1_12Instruments(){
161+
return getVersionCustomInstruments(0.0112f);
162+
}
163+
164+
/**
165+
* Return list of instuments which were added in specified version
166+
* @param serverVersion 1.12 = 0.0112f, 1.14 = 0.0114f,...
167+
* @return list of custom instruments, if no instuments were added in specified version returns empty list
168+
*/
169+
public static ArrayList<CustomInstrument> getVersionCustomInstruments(float serverVersion){
170+
ArrayList<CustomInstrument> instruments = new ArrayList<>();
171+
if (serverVersion == 0.0112f){
172+
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
173+
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
174+
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
175+
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
176+
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
177+
return instruments;
178+
}
179+
180+
if (serverVersion == 0.0114f){
181+
instruments.add(new CustomInstrument((byte) 0, "Iron Xylophone", "iron_xylophone.ogg"));
182+
instruments.add(new CustomInstrument((byte) 0, "Cow Bell", "cow_bell.ogg"));
183+
instruments.add(new CustomInstrument((byte) 0, "Didgeridoo", "didgeridoo.ogg"));
184+
instruments.add(new CustomInstrument((byte) 0, "Bit", "bit.ogg"));
185+
instruments.add(new CustomInstrument((byte) 0, "Banjo", "banjo.ogg"));
186+
instruments.add(new CustomInstrument((byte) 0, "Pling", "pling.ogg"));
187+
return instruments;
188+
}
189+
return instruments;
190+
}
191+
192+
/**
193+
* Return list of custom instruments based on song first custom instrument index and server version
194+
* @param firstCustomInstrumentIndex
195+
* @return
196+
*/
197+
public static ArrayList<CustomInstrument> getVersionCustomInstrumentsForSong(int firstCustomInstrumentIndex){
160198
ArrayList<CustomInstrument> instruments = new ArrayList<>();
161-
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
162-
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
163-
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
164-
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
165-
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
199+
200+
if (com.xxmicloxx.NoteBlockAPI.utils.CompatibilityUtils.getServerVersion() < 0.0112f){
201+
if (firstCustomInstrumentIndex == 10) {
202+
instruments.addAll(getVersionCustomInstruments(0.0112f));
203+
} else if (firstCustomInstrumentIndex == 16){
204+
instruments.addAll(getVersionCustomInstruments(0.0112f));
205+
instruments.addAll(getVersionCustomInstruments(0.0114f));
206+
}
207+
} else if (com.xxmicloxx.NoteBlockAPI.utils.CompatibilityUtils.getServerVersion() < 0.0114f){
208+
if (firstCustomInstrumentIndex == 16){
209+
instruments.addAll(getVersionCustomInstruments(0.0114f));
210+
}
211+
}
212+
166213
return instruments;
167214
}
168215

src/main/java/com/xxmicloxx/NoteBlockAPI/NBSDecoder.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
@Deprecated
2323
public class NBSDecoder {
24-
24+
2525
/**
2626
* Parses a Song from a Note Block Studio project file (.nbs)
2727
* @see Song
@@ -60,6 +60,14 @@ private static Song parse(InputStream inputStream, File songFile) {
6060
try {
6161
DataInputStream dataInputStream = new DataInputStream(inputStream);
6262
short length = readShort(dataInputStream);
63+
int firstcustominstrument = 10; //Backward compatibility - most of songs with old structure are from 1.12
64+
int firstcustominstrumentdiff;
65+
int nbsversion = 0;
66+
if (length == 0){
67+
nbsversion = dataInputStream.readByte();
68+
firstcustominstrument = dataInputStream.readByte();
69+
}
70+
firstcustominstrumentdiff = InstrumentUtils.getCustomInstrumentFirstIndex() - firstcustominstrument;
6371
short songHeight = readShort(dataInputStream);
6472
String title = readString(dataInputStream);
6573
String author = readString(dataInputStream);
@@ -93,13 +101,19 @@ private static Song parse(InputStream inputStream, File songFile) {
93101
layer += jumpLayers;
94102
//System.out.println("Layer: " + layer);
95103
byte instrument = dataInputStream.readByte();
96-
if (instrument > biggestInstrumentIndex) {
97-
biggestInstrumentIndex = instrument;
104+
105+
if (firstcustominstrumentdiff > 0 && instrument >= firstcustominstrument){
106+
instrument += firstcustominstrumentdiff;
98107
}
99-
setNote(layer, tick, instrument /* instrument */,
108+
setNote(layer, tick, instrument /* instrument */,
100109
dataInputStream.readByte() /* note */, layerHashMap);
101110
}
102111
}
112+
113+
if (nbsversion > 0) {
114+
length = tick;
115+
}
116+
103117
for (int i = 0; i < songHeight; i++) {
104118
Layer layer = layerHashMap.get(i);
105119

@@ -115,18 +129,22 @@ private static Song parse(InputStream inputStream, File songFile) {
115129
CustomInstrument[] customInstrumentsArray = new CustomInstrument[customAmnt];
116130

117131
for (int index = 0; index < customAmnt; index++) {
118-
customInstrumentsArray[index] = new CustomInstrument((byte) index,
132+
customInstrumentsArray[index] = new CustomInstrument((byte) index,
119133
readString(dataInputStream), readString(dataInputStream));
134+
dataInputStream.readByte();//pitch
135+
dataInputStream.readByte();//key
120136
}
121137

122-
if (InstrumentUtils.isCustomInstrument((byte) (biggestInstrumentIndex - customAmnt))) {
123-
ArrayList<CustomInstrument> customInstruments = CompatibilityUtils.get1_12Instruments();
138+
if (firstcustominstrumentdiff < 0){
139+
ArrayList<CustomInstrument> customInstruments = CompatibilityUtils.getVersionCustomInstrumentsForSong(firstcustominstrument);
124140
customInstruments.addAll(Arrays.asList(customInstrumentsArray));
125141
customInstrumentsArray = customInstruments.toArray(customInstrumentsArray);
142+
} else {
143+
firstcustominstrument += firstcustominstrumentdiff;
126144
}
127145

128-
return new Song(speed, layerHashMap, songHeight, length, title,
129-
author, description, songFile, customInstrumentsArray);
146+
return new Song(speed, layerHashMap, songHeight, length, title,
147+
author, description, songFile, firstcustominstrument, customInstrumentsArray);
130148
} catch (FileNotFoundException e) {
131149
e.printStackTrace();
132150
} catch (EOFException e) {
@@ -149,8 +167,8 @@ private static Song parse(InputStream inputStream, File songFile) {
149167
* @param key
150168
* @param layerHashMap
151169
*/
152-
private static void setNote(int layerIndex, int ticks, byte instrument,
153-
byte key, HashMap<Integer, Layer> layerHashMap) {
170+
private static void setNote(int layerIndex, int ticks, byte instrument,
171+
byte key, HashMap<Integer, Layer> layerHashMap) {
154172
Layer layer = layerHashMap.get(layerIndex);
155173
if (layer == null) {
156174
layer = new Layer();
@@ -185,4 +203,5 @@ private static String readString(DataInputStream dataInputStream) throws IOExcep
185203
}
186204
return builder.toString();
187205
}
206+
188207
}

src/main/java/com/xxmicloxx/NoteBlockAPI/Song.java

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3+
import com.xxmicloxx.NoteBlockAPI.utils.InstrumentUtils;
4+
35
import java.io.File;
46
import java.util.HashMap;
57

@@ -19,22 +21,62 @@ public class Song implements Cloneable {
1921
private float speed;
2022
private float delay;
2123
private CustomInstrument[] customInstruments;
24+
private int firstCustomInstrumentIndex;
2225

26+
/**
27+
* Create Song instance by copying other Song parameters
28+
* @param other song
29+
*/
2330
public Song(Song other) {
24-
this(other.getSpeed(), other.getLayerHashMap(), other.getSongHeight(),
25-
other.getLength(), other.getTitle(), other.getAuthor(),
26-
other.getDescription(), other.getPath(), other.getCustomInstruments());
31+
this(other.getSpeed(), other.getLayerHashMap(), other.getSongHeight(),
32+
other.getLength(), other.getTitle(), other.getAuthor(),
33+
other.getDescription(), other.getPath(), other.getFirstCustomInstrumentIndex(), other.getCustomInstruments());
34+
}
35+
36+
/**
37+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int)}
38+
* @param speed
39+
* @param layerHashMap
40+
* @param songHeight
41+
* @param length
42+
* @param title
43+
* @param author
44+
* @param description
45+
* @param path
46+
*/
47+
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
48+
short songHeight, final short length, String title, String author,
49+
String description, File path) {
50+
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), new CustomInstrument[0]);
51+
}
52+
53+
/**
54+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int, CustomInstrument[])}
55+
* @param speed
56+
* @param layerHashMap
57+
* @param songHeight
58+
* @param length
59+
* @param title
60+
* @param author
61+
* @param description
62+
* @param path
63+
* @param customInstruments
64+
*/
65+
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
66+
short songHeight, final short length, String title, String author,
67+
String description, File path, CustomInstrument[] customInstruments) {
68+
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), customInstruments);
2769
}
2870

2971
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
30-
short songHeight, final short length, String title, String author,
31-
String description, File path) {
32-
this(speed, layerHashMap, songHeight, length, title, author, description, path, new CustomInstrument[0]);
72+
short songHeight, final short length, String title, String author,
73+
String description, File path, int firstCustomInstrumentIndex) {
74+
this(speed, layerHashMap, songHeight, length, title, author, description, path, firstCustomInstrumentIndex, new CustomInstrument[0]);
3375
}
3476

35-
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
36-
short songHeight, final short length, String title, String author,
37-
String description, File path, CustomInstrument[] customInstruments) {
77+
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
78+
short songHeight, final short length, String title, String author,
79+
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments) {
3880
this.speed = speed;
3981
delay = 20 / speed;
4082
this.layerHashMap = layerHashMap;
@@ -44,6 +86,7 @@ public Song(float speed, HashMap<Integer, Layer> layerHashMap,
4486
this.author = author;
4587
this.description = description;
4688
this.path = path;
89+
this.firstCustomInstrumentIndex = firstCustomInstrumentIndex;
4790
this.customInstruments = customInstruments;
4891
}
4992

@@ -133,4 +176,8 @@ public Song clone() {
133176
return new Song(this);
134177
}
135178

179+
public int getFirstCustomInstrumentIndex() {
180+
return firstCustomInstrumentIndex;
181+
}
182+
136183
}

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/SongPlayer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public SongPlayer(Playlist playlist, SoundCategory soundCategory, boolean random
131131
fadeOut = new Fade(FadeType.NONE, 60);
132132
fadeOut.setFadeStart(volume);
133133
fadeOut.setFadeTarget((byte) 0);
134+
135+
plugin = NoteBlockAPI.getAPI();
134136
}
135137

136138
void update(String key, Object value){

src/main/java/com/xxmicloxx/NoteBlockAPI/utils/CompatibilityUtils.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
/**
1616
* Fields/methods for reflection &amp; version checking
17-
*
1817
*/
1918
public class CompatibilityUtils {
2019

@@ -204,53 +203,68 @@ public static void playSound(Player player, Location location, Sound sound,
204203
/**
205204
* Gets instruments which were added post-1.12
206205
* @return ArrayList of instruments
207-
* @deprecated Alternative not ready yet
206+
* @deprecated Use {@link #getVersionCustomInstruments(float)}
208207
*/
209208
public static ArrayList<CustomInstrument> get1_12Instruments(){
210-
ArrayList<CustomInstrument> instruments = new ArrayList<>();
211-
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
212-
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
213-
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
214-
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
215-
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
216-
return instruments;
209+
return getVersionCustomInstruments(0.0112f);
217210
}
218211

219-
/* NOT READY TO USE YET
212+
/**
213+
* Return list of instuments which were added in specified version
214+
* @param serverVersion 1.12 = 0.0112f, 1.14 = 0.0114f,...
215+
* @return list of custom instruments, if no instuments were added in specified version returns empty list
216+
*/
220217
public static ArrayList<CustomInstrument> getVersionCustomInstruments(float serverVersion){
221218
ArrayList<CustomInstrument> instruments = new ArrayList<>();
222-
if (serverVersion < 0.0112f){
219+
if (serverVersion == 0.0112f){
223220
instruments.add(new CustomInstrument((byte) 0, "Guitar", "guitar.ogg"));
224221
instruments.add(new CustomInstrument((byte) 0, "Flute", "flute.ogg"));
225222
instruments.add(new CustomInstrument((byte) 0, "Bell", "bell.ogg"));
226223
instruments.add(new CustomInstrument((byte) 0, "Chime", "icechime.ogg"));
227224
instruments.add(new CustomInstrument((byte) 0, "Xylophone", "xylobone.ogg"));
225+
return instruments;
228226
}
229227

230-
if (serverVersion < 0.0114f){
228+
if (serverVersion == 0.0114f){
231229
instruments.add(new CustomInstrument((byte) 0, "Iron Xylophone", "iron_xylophone.ogg"));
232230
instruments.add(new CustomInstrument((byte) 0, "Cow Bell", "cow_bell.ogg"));
233231
instruments.add(new CustomInstrument((byte) 0, "Didgeridoo", "didgeridoo.ogg"));
234232
instruments.add(new CustomInstrument((byte) 0, "Bit", "bit.ogg"));
235233
instruments.add(new CustomInstrument((byte) 0, "Banjo", "banjo.ogg"));
236234
instruments.add(new CustomInstrument((byte) 0, "Pling", "pling.ogg"));
235+
return instruments;
237236
}
238237
return instruments;
239238
}
240239

240+
/**
241+
* Return list of custom instruments based on song first custom instrument index and server version
242+
* @param firstCustomInstrumentIndex
243+
* @return
244+
*/
241245
public static ArrayList<CustomInstrument> getVersionCustomInstrumentsForSong(int firstCustomInstrumentIndex){
242246
ArrayList<CustomInstrument> instruments = new ArrayList<>();
243247

244-
if (getServerVersion() < 0.0112f && firstCustomInstrumentIndex < 10){
245-
instruments.addAll(getVersionCustomInstruments(0.0111f));
246-
} else if (getServerVersion() < 0.0114f && firstCustomInstrumentIndex < 16){
247-
instruments.addAll(getVersionCustomInstruments(0.0113f));
248+
if (getServerVersion() < 0.0112f){
249+
if (firstCustomInstrumentIndex == 10) {
250+
instruments.addAll(getVersionCustomInstruments(0.0112f));
251+
} else if (firstCustomInstrumentIndex == 16){
252+
instruments.addAll(getVersionCustomInstruments(0.0112f));
253+
instruments.addAll(getVersionCustomInstruments(0.0114f));
254+
}
255+
} else if (getServerVersion() < 0.0114f){
256+
if (firstCustomInstrumentIndex == 16){
257+
instruments.addAll(getVersionCustomInstruments(0.0114f));
258+
}
248259
}
249260

250261
return instruments;
251-
}*/
252-
262+
}
253263

264+
/**
265+
* Returns server version as float less than 1 with two digits for each version part
266+
* @return e.g. 0.011401f for 1.14.1
267+
*/
254268
public static float getServerVersion(){
255269
if (serverVersion != -1){
256270
return serverVersion;

src/main/java/com/xxmicloxx/NoteBlockAPI/utils/NBSDecoder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ private static Song parse(InputStream inputStream, File songFile) {
6363
try {
6464
DataInputStream dataInputStream = new DataInputStream(inputStream);
6565
short length = readShort(dataInputStream);
66-
int firstcustominstrument = InstrumentUtils.getCustomInstrumentFirstIndex();
67-
int firstcustominstrumentdiff = 0;
66+
int firstcustominstrument = 10; //Backward compatibility - most of songs with old structure are from 1.12
67+
int firstcustominstrumentdiff;
6868
int nbsversion = 0;
6969
if (length == 0){
7070
nbsversion = dataInputStream.readByte();
7171
firstcustominstrument = dataInputStream.readByte();
72-
firstcustominstrumentdiff = InstrumentUtils.getCustomInstrumentFirstIndex() - firstcustominstrument;
7372
}
73+
firstcustominstrumentdiff = InstrumentUtils.getCustomInstrumentFirstIndex() - firstcustominstrument;
7474
short songHeight = readShort(dataInputStream);
7575
String title = readString(dataInputStream);
7676
String author = readString(dataInputStream);
@@ -139,9 +139,11 @@ private static Song parse(InputStream inputStream, File songFile) {
139139
}
140140

141141
if (firstcustominstrumentdiff < 0){
142-
ArrayList<CustomInstrument> customInstruments = CompatibilityUtils.get1_12Instruments();//CompatibilityUtils.getVersionCustomInstrumentsForSong(firstcustominstrument);
142+
ArrayList<CustomInstrument> customInstruments = CompatibilityUtils.getVersionCustomInstrumentsForSong(firstcustominstrument);
143143
customInstruments.addAll(Arrays.asList(customInstrumentsArray));
144144
customInstrumentsArray = customInstruments.toArray(customInstrumentsArray);
145+
} else {
146+
firstcustominstrument += firstcustominstrumentdiff;
145147
}
146148

147149
return new Song(speed, layerHashMap, songHeight, length, title,

0 commit comments

Comments
 (0)